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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
* Daemonization & pidfile handling.
*
* Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <usual/daemon.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <usual/logging.h>
#include <usual/signal.h>
/*
* pidfile management.
*/
static char *g_pidfile;
static void remove_pidfile(void)
{
if (!g_pidfile)
return;
unlink(g_pidfile);
free(g_pidfile);
g_pidfile = NULL;
}
/*
* Reads pid from pidfile and sends a signal to it.
*
* true - signaling was successful.
* false - ENOENT / ESRCH
*
* fatal() otherwise.
*/
bool signal_pidfile(const char *pidfile, int sig)
{
char buf[128 + 1];
struct stat st;
pid_t pid = 0;
int fd, res;
if (!pidfile || !pidfile[0])
return false;
intr_loop:
/* check if pidfile exists */
if (stat(pidfile, &st) < 0)
goto fail;
/* read old pid */
fd = open(pidfile, O_RDONLY);
if (fd < 0)
goto fail;
res = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (res <= 0)
goto fail;
/* parse pid */
buf[res] = 0;
errno = 0;
pid = strtoul(buf, NULL, 10);
if (errno) {
/* should we panic, or say no such process exists? */
if (0)
errno = ESRCH;
goto fail;
}
/* send the signal */
res = kill(pid, sig);
if (res == 0)
return true;
fail:
/* decide error seriousness */
if (errno == EINTR)
goto intr_loop;
if (errno == ENOENT || errno == ESRCH)
return false;
fatal_perror("signal_pidfile: unexpected error");
}
static void check_pidfile(const char *pidfile)
{
if (signal_pidfile(pidfile, 0))
die("pidfile exists, another instance running?");
if (errno == ESRCH) {
log_info("stale pidfile, removing");
unlink(pidfile);
}
}
static void write_pidfile(const char *pidfile, bool first_write)
{
char buf[64];
pid_t pid;
int res, fd, len;
static int atexit_hook = 0;
int flags = O_WRONLY | O_CREAT;
if (!pidfile || !pidfile[0])
return;
free(g_pidfile);
g_pidfile = strdup(pidfile);
if (!g_pidfile)
die("out of memory");
pid = getpid();
snprintf(buf, sizeof(buf), "%u\n", (unsigned)pid);
/* don't allow overwrite on first write */
if (first_write)
flags |= O_EXCL;
fd = open(pidfile, flags, 0644);
if (fd < 0)
die("could not open pidfile '%s': %s", pidfile, strerror(errno));
len = strlen(buf);
loop:
res = write(fd, buf, len);
if (res < 0) {
if (errno == EINTR)
goto loop;
die("write to pidfile '%s' failed: %s", pidfile, strerror(errno));
} else if (res < len) {
len -= res;
goto loop;
}
close(fd);
if (!atexit_hook) {
/* only remove when we have it actually written */
atexit(remove_pidfile);
atexit_hook = 1;
}
}
/*
* Function: daemonize
*
* Handle pidfile and daemonization.
*
* If pidfile is given, check if old process is running.
*
* If going background is required, require non-empty pidfile
* and logfile. Then fork to background and write pidfile.
*/
void daemonize(const char *pidfile, bool go_background)
{
int pid, fd;
if (pidfile && pidfile[0]) {
check_pidfile(pidfile);
/* write pidfile twice, to be able to show problems to user */
write_pidfile(pidfile, true);
} else if (go_background)
fatal("daemon needs pidfile configured");
if (!go_background)
return;
if ((!cf_logfile || !cf_logfile[0]) && !cf_syslog)
fatal("daemon needs logging configured");
/* send stdin, stdout, stderr to /dev/null */
fd = open("/dev/null", O_RDWR);
if (fd < 0)
die("could not open /dev/null: %s", strerror(errno));
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2)
close(fd);
/* fork new process */
pid = fork();
if (pid < 0)
die("fork failed: %s", strerror(errno));
if (pid > 0)
_exit(0);
/* create new session */
pid = setsid();
if (pid < 0)
die("setsid: %s", strerror(errno));
/* fork again to avoid being session leader */
pid = fork();
if (pid < 0)
die("fork failed; %s", strerror(errno));
if (pid > 0)
_exit(0);
write_pidfile(pidfile, false);
}
|