Error setsid function relative to ppid

Asked

Viewed 43 times

0

****When running the source below relative to the creation of a Deamon with the setsid function, it was verified that after the ps -fu root command, ppid is not 1, that is the same as the init of the operating system. The source was taken from the book Programming in C for Linux, Unix and Windows, for educational purposes. I’ve tried all kinds of font modification and I can’t get the process leader to have the same init ppid. Any help is welcome and I thank you. 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);
}

1 answer

0


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

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

  • I don’t understand your comment. I am running the program directly on the terminal with the command. /exec, in this case, exec is my executable file for the function that contains Deamon.

  • Are you running the terminal in some graphical environment, like GNOME or KDE ? If you are, this is what my answer refers to. If I haven’t been then I can’t explain. I reproduced the behavior you described running the program in gnome-terminal. The native Linux console worked as expected.

  • Now I understand. The detail and how to run on the native buntu console. In this case it would have to be through a linux system without graphical interface??

  • To run on the native console I already put in my answer, you use the keys CTRL+ALT+F1, or CTRL+ALT+F2, etc...to open a console, If I am not mistaken the graphical desktop runs on CTRL+ALT+F6 or CTRL+ALT+F7, then starts on CTRL+ALT+F1 and goes testing. To go back to the graphical desktop you use CTRL+ALT+F6 or CTRL+ALT+F7. I’m not sure why I no longer use Ubuntu, use Fedora,

  • Thanks for the tip. I’ll try and get back to you.

  • My dear you are completely, totally, entirely right. I just don’t understand why it only works on the native console.

  • What can be inferred is that one of the graphical desktop processes is the ancestor of all the processes created on the desktop, so a process that would normally be inherited by process 1 (init) is inherited by some graphical desktop control process.

  • If this answer doi useful for you mark as accepted, please.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.