File: daemon.c

package info (click to toggle)
ndtpd 3.1.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,524 kB
  • ctags: 690
  • sloc: ansic: 8,565; sh: 7,275; perl: 655; makefile: 175
file content (142 lines) | stat: -rw-r--r-- 3,027 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (c) 1997, 98, 2000, 01  
 *    Motoyuki Kasahara
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/*
 * This program requires the following Autoconf macros:
 *   AC_TYPE_PID_T
 *   AC_CHECK_HEADERS(fcntl.h, unistd.h)
 *   AC_CHECK_FUNCS(setsid)
 *   AC_FUNC_SETPGRP
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <sys/types.h>
#include <syslog.h>
#include <errno.h>

#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
#endif /* not STDC_HEADERS and not HAVE_STRING_H */

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#else
#include <sys/file.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifndef HAVE_SETSID
#include <sys/ioctl.h>
#endif

#ifdef USE_FAKELOG
#include "fakelog.h"
#endif


/*
 * Run the current process as daemon.
 */
int
daemonize()
{
    pid_t pid;
#ifndef HAVE_SETSID
    int fd = -1;
#endif

    /*
     * Fork, and then the parent exits.
     */
    pid = fork();
    if (pid < 0) {
	syslog(LOG_ERR, "fork() failed, %s", strerror(errno));
	goto failed;
    } else if (0 < pid)
	exit(0);

    /*
     * Close a tty.
     */
#ifdef HAVE_SETSID
    if ((int)setsid() < 0) {
	syslog(LOG_ERR, "setsid() failed, %s", strerror(errno));
	goto failed;
    }
#else
#ifdef SETPGRP_VOID
    setpgrp();
#else /* not SETPGRP_VOID */
    if (setpgrp(0, getpid()) < 0) {
	syslog(LOG_ERR, "setpgrp() failed, %s", strerror(errno));
	goto failed;
    }
#endif /* not SETPGRP_VOID */
#ifdef TIOCNOTTY
    fd = open("/dev/tty" , O_RDWR);
    if (0 <= fd) {
        if (ioctl(fd, TIOCNOTTY, NULL) < 0) {
	    syslog(LOG_ERR, "ioctl(TIOCNOTTY) failed", strerror(errno));
	    goto failed;
	}
	if (close(fd) < 0) {
	    syslog(LOG_ERR, "failed to close the file, %s: /dev/tty",
		strerror(errno));
	    fd = -1;
	    goto failed;
	}
    }
#endif /* TIOCNOTTY */
#endif /* HAVE_SETSID */

    /*
     * Change the current directory to root (`/').
     */
    if (chdir("/") < 0) {
	syslog(LOG_ERR, "failed to change directory to root, %s",
	    strerror(errno));
	goto failed;
    }

    syslog(LOG_DEBUG, "debug: run as daemon");
    return 0;

    /*
     * An error occurs...
     */
  failed:
#ifndef HAVE_SETSID
    if (0 <= fd && close(fd) < 0) {
	syslog(LOG_ERR, "failed to close the file, %s: /dev/tty",
	    strerror(errno));
    }
#endif

    syslog(LOG_ERR, "failed to run as daemon");
    return -1;
}