| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
Ü |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <usual/daemon.h> |
| 21 |
|
| 22 |
#include <sys/stat.h> |
| 23 |
#include <fcntl.h> |
| 24 |
#include <string.h> |
| 25 |
#include <stdio.h> |
| 26 |
|
| 27 |
#include <usual/logging.h> |
| 28 |
#include <usual/signal.h> |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
static char *g_pidfile; |
| 35 |
|
| 36 |
static void remove_pidfile(void) |
| 37 |
{ |
| 38 |
if (!g_pidfile) |
| 39 |
return; |
| 40 |
unlink(g_pidfile); |
| 41 |
free(g_pidfile); |
| 42 |
g_pidfile = NULL; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
bool signal_pidfile(const char *pidfile, int sig) |
| 54 |
{ |
| 55 |
char buf[128 + 1]; |
| 56 |
struct stat st; |
| 57 |
pid_t pid = 0; |
| 58 |
int fd, res; |
| 59 |
|
| 60 |
if (!pidfile || !pidfile[0]) |
| 61 |
return false; |
| 62 |
|
| 63 |
intr_loop: |
| 64 |
|
| 65 |
if (stat(pidfile, &st) < 0) |
| 66 |
goto fail; |
| 67 |
|
| 68 |
|
| 69 |
fd = open(pidfile, O_RDONLY); |
| 70 |
if (fd < 0) |
| 71 |
goto fail; |
| 72 |
res = read(fd, buf, sizeof(buf) - 1); |
| 73 |
close(fd); |
| 74 |
if (res <= 0) |
| 75 |
goto fail; |
| 76 |
|
| 77 |
|
| 78 |
buf[res] = 0; |
| 79 |
errno = 0; |
| 80 |
pid = strtoul(buf, NULL, 10); |
| 81 |
if (errno) { |
| 82 |
|
| 83 |
if (0) |
| 84 |
errno = ESRCH; |
| 85 |
goto fail; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
res = kill(pid, sig); |
| 90 |
if (res == 0) |
| 91 |
return true; |
| 92 |
fail: |
| 93 |
|
| 94 |
if (errno == EINTR) |
| 95 |
goto intr_loop; |
| 96 |
if (errno == ENOENT || errno == ESRCH) |
| 97 |
return false; |
| 98 |
fatal_perror("signal_pidfile: unexpected error"); |
| 99 |
} |
| 100 |
|
| 101 |
static void check_pidfile(const char *pidfile) |
| 102 |
{ |
| 103 |
if (signal_pidfile(pidfile, 0)) |
| 104 |
die("pidfile exists, another instance running?"); |
| 105 |
if (errno == ESRCH) { |
| 106 |
log_info("stale pidfile, removing"); |
| 107 |
unlink(pidfile); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
static void write_pidfile(const char *pidfile, bool first_write) |
| 112 |
{ |
| 113 |
char buf[64]; |
| 114 |
pid_t pid; |
| 115 |
int res, fd, len; |
| 116 |
static int atexit_hook = 0; |
| 117 |
int flags = O_WRONLY | O_CREAT; |
| 118 |
|
| 119 |
if (!pidfile || !pidfile[0]) |
| 120 |
return; |
| 121 |
|
| 122 |
free(g_pidfile); |
| 123 |
g_pidfile = strdup(pidfile); |
| 124 |
if (!g_pidfile) |
| 125 |
die("out of memory"); |
| 126 |
|
| 127 |
pid = getpid(); |
| 128 |
snprintf(buf, sizeof(buf), "%u\n", (unsigned)pid); |
| 129 |
|
| 130 |
|
| 131 |
if (first_write) |
| 132 |
flags |= O_EXCL; |
| 133 |
|
| 134 |
fd = open(pidfile, flags, 0644); |
| 135 |
if (fd < 0) |
| 136 |
die("could not open pidfile '%s': %s", pidfile, strerror(errno)); |
| 137 |
len = strlen(buf); |
| 138 |
loop: |
| 139 |
res = write(fd, buf, len); |
| 140 |
if (res < 0) { |
| 141 |
if (errno == EINTR) |
| 142 |
goto loop; |
| 143 |
die("write to pidfile '%s' failed: %s", pidfile, strerror(errno)); |
| 144 |
} else if (res < len) { |
| 145 |
len -= res; |
| 146 |
goto loop; |
| 147 |
} |
| 148 |
close(fd); |
| 149 |
|
| 150 |
if (!atexit_hook) { |
| 151 |
|
| 152 |
atexit(remove_pidfile); |
| 153 |
atexit_hook = 1; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
void daemonize(const char *pidfile, bool go_background) |
| 168 |
{ |
| 169 |
int pid, fd; |
| 170 |
|
| 171 |
if (pidfile && pidfile[0]) { |
| 172 |
check_pidfile(pidfile); |
| 173 |
|
| 174 |
write_pidfile(pidfile, true); |
| 175 |
} else if (go_background) |
| 176 |
fatal("daemon needs pidfile configured"); |
| 177 |
|
| 178 |
if (!go_background) |
| 179 |
return; |
| 180 |
|
| 181 |
if ((!cf_logfile || !cf_logfile[0]) && !cf_syslog) |
| 182 |
fatal("daemon needs logging configured"); |
| 183 |
|
| 184 |
|
| 185 |
fd = open("/dev/null", O_RDWR); |
| 186 |
if (fd < 0) |
| 187 |
die("could not open /dev/null: %s", strerror(errno)); |
| 188 |
dup2(fd, 0); |
| 189 |
dup2(fd, 1); |
| 190 |
dup2(fd, 2); |
| 191 |
if (fd > 2) |
| 192 |
close(fd); |
| 193 |
|
| 194 |
|
| 195 |
pid = fork(); |
| 196 |
if (pid < 0) |
| 197 |
die("fork failed: %s", strerror(errno)); |
| 198 |
if (pid > 0) |
| 199 |
_exit(0); |
| 200 |
|
| 201 |
|
| 202 |
pid = setsid(); |
| 203 |
if (pid < 0) |
| 204 |
die("setsid: %s", strerror(errno)); |
| 205 |
|
| 206 |
|
| 207 |
pid = fork(); |
| 208 |
if (pid < 0) |
| 209 |
die("fork failed; %s", strerror(errno)); |
| 210 |
if (pid > 0) |
| 211 |
_exit(0); |
| 212 |
|
| 213 |
write_pidfile(pidfile, false); |
| 214 |
} |