WvStreams
wvprociter.cc
1 /*
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * Process iterator. Iterates through all the processes.
6  *
7  */
8 
9 #include "wvprociter.h"
10 #include "wvfile.h"
11 #include "wvfileutils.h"
12 #include <sys/types.h>
13 #include <signal.h>
14 
15 WvProcIter::WvProcIter() :
16  dir_iter("/proc", false, true)
17 {
18  if (!dir_iter.isok())
19  fprintf(stderr, "WARNING: Can't open /proc: is it mounted?\n");
20  if (access("/proc/1/.", F_OK) != 0)
21  fprintf(stderr, "WARNING: Can't find /proc/1: is /proc mounted?\n");
22 }
23 
24 WvProcIter::~WvProcIter()
25 {
26 }
27 
28 bool WvProcIter::isok() const
29 {
30  return dir_iter.isok();
31 }
32 
33 void WvProcIter::rewind()
34 {
35  dir_iter.rewind();
36 }
37 
38 bool WvProcIter::next()
39 {
40  for (;;)
41  {
42  if (!dir_iter.next())
43  return false;
44  if (!wvstring_to_num(dir_iter->name, proc_ent.pid))
45  continue;
46 
47  proc_ent.exe = wvreadlink(WvString("%s/exe", dir_iter->fullname));
48 
49  proc_ent.cmdline.zap();
50  WvFile cmdline_file(WvString("%s/cmdline", dir_iter->fullname), O_RDONLY);
51  while (cmdline_file.isok())
52  {
53  const char *line = cmdline_file.getline(0, '\0');
54  if (line == NULL)
55  break;
56  WvString line_str(line);
57  line_str.unique();
58  proc_ent.cmdline.append(line_str);
59  }
60  cmdline_file.close();
61 
62  break;
63  }
64  return true;
65 }
66 
67 bool wvkillall(WvStringParm name, int sig)
68 {
69  bool found = false;
70  WvProcIter i;
71  for (i.rewind(); i.next(); )
72  {
73  if (!i->cmdline.isempty()
74  && !!*i->cmdline.first()
75  && getfilename(*i->cmdline.first()) == name
76  && i->pid > 0)
77  {
78  ::kill(i->pid, sig);
79  found = true;
80  }
81  }
82  return found;
83 }
WvProcIter
Definition: wvprociter.h:22
getfilename
WvString getfilename(WvStringParm fullname)
Take a full path/file name and splits it up into respective pathname and filename.
Definition: strutils.cc:506
WvFile
WvFile implements a stream connected to a file or Unix device.
Definition: wvfile.h:28
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329