Re: CGI starting and stopping daemons.
"James Lehman" <james[remove]@akrobiz.com> wrote in message
news:zzKwc.1299$Jp.59508@ord-read.news.verio.net...
> I'm writing a CGI app that will provide a user interface to real hardware
> devices that are part of the server, namely one or more SCSI tape drives.
I
> have a perfectly functional console app that controls the SCSI device the
> way I want. It writes its pid to a file when it starts and sets it back to
> zero when it terminates. No other instance of this process can be started
if
> one is already running. The process also writes it current state, every
time
> it changes, to a status file. The process needs to run as a daemon. It
needs
> to be started as a background process. It never returns, unless its pid is
> sent a kill signal. It will, most likely be started in /etc/rc.d/rc.local.
>
> I want to write a CGI app in C/C++. When the CGI app starts up, it reads
the
C/C++ is not a language, choose one or the other and stick to it.
> pid and status files. If the process is not running, it offers a "start"
> button. If the process is running, it offers a "stop" button. It calls
back
> to the same CGI app with an argument to the app of start or stop that
calls
> commands at the prompt using popen(). Like this:
>
> command = popen("process_name device_name &", "r"); // start
> command = popen((string("kill -HUP ") + pid).c_str(), "r"); // stop
>
You probably don't want to use popen and you almost certainly do not want to
use the & operator to "backgroundify" what you're running.
> I think I need to stick a fork in it somewhere. the CGI shell has to
> terminate and that would kill the background process, right?
Yes, you do, maybe even two. If you fork one process from the CGI script,
the parent will be required to wait() on its child, otherwise you will get
zombies and that's a bad thing. You can either ignore SIGCHILD (man 3
signal) or fork two children. You then almost certainly want to be using one
of the exec() family of functions. Try something like this:
int forkpid;
forkpid = fork();
if (forkpid ==0) {
/* we're the child */
if (fork() == 0) {
/* we're the child of the child */
execl("program")
}
exit(0);
} else {
while (wait(NULL) != forkpid) {}
}
> There are also some issues as to who should own the SCSI device and the
pid
> and status files.
>
> I did a chmod u+s as root to the console app that the CGI app calls. When
I
> call the code above to start the process, it does return. A call to ps
axf,
> from the console as root, shows that the console tape app is running, but
> the pid file still shows zero, the status file says "down" and the program
> doesn't work. Obviously, I can't kill a pid of zero.
what does ps aux tell you is the pid of the application that's running?
Sounds like a zombie to me..
>
> Any thoughts on this situation would be most appreciated!
You might try comp.unix.programmer in future as this isn't really an apache
specific question.
>
> Thanks.
> Have a great day! James. :o)
>
>
>
|