How to daemonize a process properly on QNX4?

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.

The following works for me in QNX4…

if (signal(SIGINT, SIG_IGN) != SIG_IGN || signal(SIGQUIT, SIG_IGN) !=
SIG_IGN)
{
if (!(!fork() && !fork()))
_exit(0);
}

Geoff Roberts

Tony wrote:

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.

On Wed, 05 Jan 2005 20:28:28 +1100, Geoff Roberts <ger_two@rttsdotcom.au>
wrote:

The following works for me in QNX4…

if (signal(SIGINT, SIG_IGN) != SIG_IGN || signal(SIGQUIT, SIG_IGN) !=
SIG_IGN)
{
if (!(!fork() && !fork()))
_exit(0);
}
Cool stuff!

I’ll try it.
What happens to STDIN, STDOUT and STDERR file descriptors when this
forking is executed?
Is this code equivalent to a regular UNIX’s “daemon(1, 0)”?

Tony.