WvStreams
wvsubproc.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * A class for reliably starting/stopping subprocesses.
6  *
7  * We want to avoid calling system(), since it uses the shell (and
8  * thus has strange parsing weirdness, environment variable changes,
9  * and so on). Plus calling the shell when we need to is just slow.
10  *
11  * On the other hand, we want handy features like the ability to wait
12  * for our child process to die, and the ability to kill it if it
13  * doesn't (without having to use "killall").
14  *
15  * By using setsid(), we also deal with strange situations like
16  * scripts which launch other programs. stop() and kill() will kill
17  * them all. (If you don't want that, use stop_primary() and
18  * kill_primary().)
19  */
20 #ifndef __WVSUBPROC_H
21 #define __WVSUBPROC_H
22 
23 #include "wvstringlist.h"
24 
25 #include <stdarg.h>
26 #include <signal.h>
27 #include <time.h>
28 
29 class WvSubProc
30 {
31 public:
32  DeclareWvList(pid_t);
33  pid_tList old_pids;
34 
35  pid_t pid;
36  bool running;
37  int estatus;
38  WvString pidfile, last_cmd, app;
39  WvStringList last_args, env;
40 
41  WvSubProc()
42  { init(); }
43 
44  WvSubProc(const char cmd[], const char * const *argv)
45  { init(); startv(cmd, argv); }
46 
47  virtual ~WvSubProc();
48 
49 private:
50  void init();
51  int _startv(const char cmd[], const char * const *argv);
52 
53  int memlimit;
54 
55 public:
56  void prepare(const char cmd[], ...);
57  void preparev(const char cmd[], va_list ap);
58  void preparev(const char cmd[], const char * const *argv);
59  void preparev(const char cmd[], WvStringList &);
60 
61  // launch a subprocess, which will be owned by this object.
62  int start(const char cmd[], ...);
63 
64  int startv(const char cmd[], const char * const *argv);
65  virtual int start_again();
66 
67  virtual int fork(int *waitfd);
68 
69  // stop (kill -TERM or -KILL as necessary) the subprocess and
70  // all its children.
71  virtual void stop(time_t msec_delay, bool kill_children = true);
72 
73  // wait for the subprocess (and all its children) to die.
74  virtual void wait(time_t msec_delay, bool wait_children = true);
75 
76  // figure out the pid from the /var/run pidfile
77  pid_t pidfile_pid();
78 
80  // use
81  void setMemLimit(int megs) { memlimit = megs; }
82 
83  // send a signal to the subprocess and all its children.
84  void kill(int sig);
85 
86  // send a signal only to the main subprocess.
87  void kill_primary(int sig);
88 
89  // suspend the process temporarily, or resume it.
90  virtual void suspend()
91  { kill(SIGSTOP); }
92  virtual void resume()
93  { kill(SIGCONT); }
94 };
95 
96 DeclareWvList(WvSubProc);
97 
98 #endif // __WVSUBPROC_H
WvSubProc
Definition: wvsubproc.h:29
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvSubProc::setMemLimit
void setMemLimit(int megs)
Sets a limit on the number of megabytes of memory the subprocess will.
Definition: wvsubproc.h:81
WvStringList
This is a WvList of WvStrings, and is a really handy way to parse strings.
Definition: wvstringlist.h:27