I understand the “daemon()” call as the means of making a process immune
to SIGHUPs.
Since there is no official implementation from QSSL one has to choose from
several available ports.
This one is from OpenBSD:
int
daemon(int nochdir, int noclose)
{
int fd;
switch (fork()) {
case -1:
return (-1);
case 0:
break;
default:
_exit(0);
}
if (setsid() == -1)
return (-1);
if (!nochdir)
(void)chdir("/");
if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
(void)dup2(fd, STDIN_FILENO);
(void)dup2(fd, STDOUT_FILENO);
(void)dup2(fd, STDERR_FILENO);
if (fd > 2)
(void)close (fd);
}
return (0);
}
Note it executes “setsid()” get rid of the controlling terminal and to
escape into a separate session.
This code is found in the ssh-1.2.26 port to QNX4 by J.C.Michot:
daemon(nochdir, noclose)
int nochdir, noclose;
{
int cpid;
if ((cpid = fork()) == -1)
return (-1);
if (cpid)
exit(0);
(void) setpgid(0, 0);
if (!nochdir)
(void) chdir("/");
if (!noclose) {
int devnull = open( “/dev/null”, O_RDWR, 0);
if (devnull != -1) {
(void) dup2(devnull, STDIN_FILENO);
(void) dup2(devnull, STDOUT_FILENO);
(void) dup2(devnull, STDERR_FILENO);
if (devnull > 2)
(void) close(devnull);
}
}
setpgrp(); /tcsetpgrp(0, setpgid(0, 0));/
}
Note it DOES NOT execute “setsid()” but instead it is busy with some
shamanry around “setpgid()” and “setpgrp()”. This (I believe) does exactly
the contrary to the idea of daemonizing.
Please comment on this.
What is the proper way to daemonize in QNX v4.25G with Watcom C
v10.6B+Security patch?
Tony.