Saner handling if config.mk doesn't exist: use a default config.defaults.mk.
[wvstreams.git] / include / wvsubproc.h
blob9488e9e54d8b889f82846363230a9c1aaba69e96
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * A class for reliably starting/stopping subprocesses.
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.
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").
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().)
20 #ifndef __WVSUBPROC_H
21 #define __WVSUBPROC_H
23 #include "wvstringlist.h"
25 #include <stdarg.h>
26 #include <signal.h>
27 #include <time.h>
29 class WvSubProc
31 public:
32 DeclareWvList(pid_t);
33 pid_tList old_pids;
35 pid_t pid;
36 bool running;
37 int estatus;
38 WvString pidfile, last_cmd, app;
39 WvStringList last_args, env;
41 WvSubProc()
42 { init(); }
44 WvSubProc(const char cmd[], const char * const *argv)
45 { init(); startv(cmd, argv); }
47 virtual ~WvSubProc();
49 private:
50 void init();
51 int _startv(const char cmd[], const char * const *argv);
53 int memlimit;
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 &);
61 // launch a subprocess, which will be owned by this object.
62 int start(const char cmd[], ...);
64 int startv(const char cmd[], const char * const *argv);
65 virtual int start_again();
67 virtual int fork(int *waitfd);
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);
73 // wait for the subprocess (and all its children) to die.
74 virtual void wait(time_t msec_delay, bool wait_children = true);
76 // figure out the pid from the /var/run pidfile
77 pid_t pidfile_pid();
79 /// Sets a limit on the number of megabytes of memory the subprocess will
80 // use
81 void setMemLimit(int megs) { memlimit = megs; }
83 // send a signal to the subprocess and all its children.
84 void kill(int sig);
86 // send a signal only to the main subprocess.
87 void kill_primary(int sig);
89 // suspend the process temporarily, or resume it.
90 virtual void suspend()
91 { kill(SIGSTOP); }
92 virtual void resume()
93 { kill(SIGCONT); }
96 DeclareWvList(WvSubProc);
98 #endif // __WVSUBPROC_H