Problem setsid function relative to ppid

0

**** When you run the font below regarding the creation of a deamon with the setsid function, it was verified that after the ps -fu root command, ppid is not 1, or the same as the operating system init. The source was taken from the book Programming in C for Linux, Unix and Windows, with didactic purpose. I've tried all kinds of modifications in the source and I can not get the process leader to have the same init ppid. Any help is welcome and thank you in advance. My operating system is buntu 14.04. ****

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int daemon_init (void)
{
pid_t iPid/*,sid*/;
long iMaxFd;
int i;

if ((iPid = fork()) < 0)
return -1;//exit(EXIT_FAILURE);
if (iPid != 0)
exit(0);//exit(EXIT_SUCCESS);
setsid();//sid=setsid()
chdir ("/");
umask (0);
iMaxFd = sysconf (_SC_OPEN_MAX);
for (i=0; i < iMaxFd; i++)
close (i);
return 0;
}
void main (int argc, char *argv[])
{
int iFd;
char szBuffer[100];
int i;
if (daemon_init () < 0)
{
perror (argv[0]);
exit (errno);
}
sprintf (szBuffer, "/tmp/daemon%d.arq", getpid());//cria buffer .arq
iFd = open (szBuffer, O_CREAT | O_WRONLY, 0700);//cria arquivo
i = 1;
while (1)
{
sleep(3);
sprintf(szBuffer, "Esta eh a linha de numero %04d\n", i++);
write(iFd, szBuffer, strlen(szBuffer));
}
exit (0);
}
    
asked by anonymous 26.09.2016 / 03:32

1 answer

0

You are running your program on a graphical desktop (GNOME, KDE, etc.). I tested it in GNOME and it really does happen as you said.

If you run in console (CTRL + ALT + Fn) the parent pid of the daemon will be 1 (process init).

    
26.09.2016 / 04:45