Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions ch10/my-daemon-v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ int main(void)
the code the child process */
setsid(); /* create a new session to lose the controlling
terminal */


umask(022); /* reset the umask to something sensible */
chdir("/"); /* change working directory to / */

/* fork again, creating a grandchild, the actual daemon */
if ( (pid = fork()) == -1 )
{
Expand All @@ -52,14 +55,12 @@ int main(void)
{
perror("Can't open file for writing");
return 1;
}
}
fprintf(fp, "%d\n", pid); /* write pid to file */
fclose(fp); /* close the file pointer */
exit(0);
}

umask(022); /* reset the umask to something sensible */
chdir("/"); /* change working directory to / */
/* open the "daemonfile" for writing */
if ( (fp = fopen(daemonfile, "w")) == NULL )
{
Expand Down Expand Up @@ -102,8 +103,8 @@ int main(void)
void sigHandler(int sig)
{
int status = 0;
if ( sig == SIGTERM || sig == SIGINT
|| sig == SIGQUIT
if ( sig == SIGTERM || sig == SIGINT
|| sig == SIGQUIT
|| sig == SIGABRT )
{
/* remove the pid-file */
Expand Down