Skip to content

Commit

Permalink
Build fixes for ubuntu 8.10/64.
Browse files Browse the repository at this point in the history
gcc seems to have been wise to all of the syscalls being cast to void
to avoid doing real result checking in unlikely errors.
  • Loading branch information
dustin committed Jan 20, 2009
1 parent ad00048 commit 2c196ed
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#endif

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

Expand All @@ -56,15 +57,33 @@ int daemonize(int nochdir, int noclose)
if (setsid() == -1)
return (-1);

if (nochdir == 0)
(void)chdir("/");
if (nochdir == 0) {
if(chdir("/") != 0) {
perror("chdir");
return (-1);
}
}

if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
(void)dup2(fd, STDIN_FILENO);
(void)dup2(fd, STDOUT_FILENO);
(void)dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
(void)close(fd);
if(dup2(fd, STDIN_FILENO) < 0) {
perror("dup2 stdin");
return (-1);
}
if(dup2(fd, STDOUT_FILENO) < 0) {
perror("dup2 stdout");
return (-1);
}
if(dup2(fd, STDERR_FILENO) < 0) {
perror("dup2 stderr");
return (-1);
}

if (fd > STDERR_FILENO) {
if(close(fd) < 0) {
perror("close");
return (-1);
}
}
}
return (0);
}

0 comments on commit 2c196ed

Please sign in to comment.