Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

wvsubproc.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * A class for reliably starting/stopping subprocesses.
00006  *
00007  * We want to avoid calling system(), since it uses the shell (and
00008  * thus has strange parsing weirdness, environment variable changes,
00009  * and so on).  Plus calling the shell when we need to is just slow.
00010  * 
00011  * On the other hand, we want handy features like the ability to wait
00012  * for our child process to die, and the ability to kill it if it
00013  * doesn't (without having to use "killall").
00014  * 
00015  * By using setsid(), we also deal with strange situations like
00016  * scripts which launch other programs.  stop() and kill() will kill
00017  * them all. (If you don't want that, use stop_primary() and
00018  * kill_primary().)
00019  */
00020 #ifndef __WVSUBPROC_H
00021 #define __WVSUBPROC_H
00022 
00023 #include "wvstringlist.h"
00024 
00025 #include <stdarg.h>
00026 #include <signal.h>
00027 #include <time.h>
00028 
00029 class WvSubProc
00030 {
00031 public:
00032     DeclareWvList(pid_t);
00033     pid_tList old_pids;
00034     
00035     pid_t pid;
00036     bool running;
00037     int estatus;
00038     WvString pidfile, last_cmd, app;
00039     WvStringList last_args, env;
00040     
00041     WvSubProc();
00042 
00043     WvSubProc(const char cmd[], const char * const *argv)
00044         { startv(cmd, argv); }
00045 
00046     virtual ~WvSubProc();
00047     
00048 private:
00049     int _startv(const char cmd[], const char * const *argv);
00050 
00051     int memlimit;
00052     
00053 public:
00054     void prepare(const char cmd[], ...);
00055     void preparev(const char cmd[], va_list ap);
00056     void preparev(const char cmd[], const char * const *argv);
00057     void preparev(const char cmd[], WvStringList &);
00058     
00059     // launch a subprocess, which will be owned by this object.
00060     int start(const char cmd[], ...);
00061     
00062     int startv(const char cmd[], const char * const *argv);
00063     virtual int start_again();
00064     
00065     virtual int fork(int *waitfd);
00066 
00067     // stop (kill -TERM or -KILL as necessary) the subprocess and
00068     // all its children.
00069     virtual void stop(time_t msec_delay, bool kill_children = true);
00070     
00071     // wait for the subprocess (and all its children) to die.
00072     virtual void wait(time_t msec_delay, bool wait_children = true);
00073 
00074     // figure out the pid from the /var/run pidfile
00075     pid_t pidfile_pid();
00076 
00077     /// Sets a limit on the number of megabytes of memory the subprocess will
00078     // use
00079     void setMemLimit(int megs) { memlimit = megs; }
00080     
00081     // send a signal to the subprocess and all its children.
00082     void kill(int sig);
00083     
00084     // send a signal only to the main subprocess.
00085     void kill_primary(int sig);
00086     
00087     // suspend the process temporarily, or resume it.
00088     virtual void suspend()
00089         { kill(SIGSTOP); }
00090     virtual void resume()
00091         { kill(SIGCONT); }
00092 };
00093 
00094 DeclareWvList(WvSubProc);
00095 
00096 #endif // __WVSUBPROC_H

Generated on Sat Mar 13 14:55:58 2004 for WvStreams by doxygen 1.3.6-20040222