~ubuntu-branches/ubuntu/trusty/musl/trusty-proposed

« back to all changes in this revision

Viewing changes to src/legacy/daemon.c

  • Committer: Package Import Robot
  • Author(s): Kevin Bortis
  • Date: 2013-09-20 20:54:14 UTC
  • Revision ID: package-import@ubuntu.com-20130920205414-5b61trtmma18w58o
Tags: upstream-0.9.13
ImportĀ upstreamĀ versionĀ 0.9.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define _GNU_SOURCE
 
2
#include <fcntl.h>
 
3
#include <unistd.h>
 
4
 
 
5
int daemon(int nochdir, int noclose)
 
6
{
 
7
        if (!nochdir && chdir("/"))
 
8
                return -1;
 
9
        if (!noclose) {
 
10
                int fd, failed = 0;
 
11
                if ((fd = open("/dev/null", O_RDWR)) < 0) return -1;
 
12
                if (dup2(fd, 0) < 0 || dup2(fd, 1) < 0 || dup2(fd, 2) < 0)
 
13
                        failed++;
 
14
                if (fd > 2) close(fd);
 
15
                if (failed) return -1;
 
16
        }
 
17
 
 
18
        switch(fork()) {
 
19
        case 0: break;
 
20
        case -1: return -1;
 
21
        default: _exit(0);
 
22
        }
 
23
 
 
24
        if (setsid() < 0) return -1;
 
25
 
 
26
        switch(fork()) {
 
27
        case 0: break;
 
28
        case -1: return -1;
 
29
        default: _exit(0);
 
30
        }
 
31
 
 
32
        return 0;
 
33
}