29 #include <sys/types.h>
39 #include <sys/prctl.h>
47 static void write_pidfile(
int pid_fd)
53 if (ftruncate(pid_fd, 0))
58 if (write(pid_fd, buf, strlen(buf)) != (
int) strlen(buf))
67 int child_got_signal_from_us = 0;
78 static void log_reopen_handler(
int num)
85 static void sigusr2_handler(
int num)
87 child_got_signal_from_us = 1;
90 static pid_t keepalive_pid = 0;
92 static void keepalive(
void (*work)(
void *data),
void *data)
100 void (*old_sigterm)(int);
101 void (*old_sigusr1)(int);
102 struct sigaction sa2, sa1;
104 keepalive_pid = getpid();
111 sigemptyset(&sa2.sa_mask);
112 sa2.sa_handler = sigusr2_handler;
114 sigaction(SIGUSR2, &sa2, &sa1);
116 while (cont && !child_got_signal_from_us)
121 if (p == (pid_t) (-1))
129 signal(SIGTERM, old_sigterm);
130 signal(SIGUSR1, old_sigusr1);
131 sigaction(SIGUSR2, &sa1, NULL);
140 p1 = waitpid(p, &status, 0);
145 if (p1 == (pid_t)(-1))
159 if (WIFSIGNALED(status))
162 switch (WTERMSIG(status))
191 WTERMSIG(status), (
long) p);
195 else if (WIFEXITED(status))
198 if (WEXITSTATUS(status) != 0)
201 WEXITSTATUS(status), (
long) p);
224 kill(keepalive_pid, SIGUSR2);
231 void (*work)(
void *data),
void *data,
232 const char *pidfile,
const char *uid)
240 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
251 write_pidfile(pid_fd);
260 struct passwd *pw = getpwnam(uid);
271 if (fchown(fileno(f), pw->pw_uid, -1))
275 if (setuid(pw->pw_uid) < 0)
283 #ifdef PR_SET_DUMPABLE
284 prctl(PR_SET_DUMPABLE, 1, 0, 0);
310 int res = read(hand[0], dummy, 1);
311 if (res < 0 && errno != EINTR)
330 open(
"/dev/null", O_RDWR);
338 write_pidfile(pid_fd);
342 signal(SIGHUP, log_reopen_handler);
346 keepalive(work, data);
void yaz_daemon_stop(void)
stop daemon - stop parent process
int yaz_daemon(const char *progname, unsigned int flags, void(*work)(void *data), void *data, const char *pidfile, const char *uid)
daemon utility.
#define YAZ_DAEMON_KEEPALIVE
#define YAZ_DAEMON_LOG_REOPEN
Header for errno utilities.
FILE * yaz_log_file(void)
returns FILE handle for log or NULL if no file is in use
void yaz_log_reopen()
reopen current log file (unless disabled or stderr)
void yaz_log(int level, const char *fmt,...)
Writes log message.
#define YLOG_WARN
log level: warning
#define YLOG_FATAL
log level: fatal
#define YLOG_ERRNO
log level: append system error message
#define YLOG_LOG
log level: log (regular)
void yaz_snprintf(char *buf, size_t size, const char *fmt,...)
Header for config file reading utilities.
static void normal_stop_handler(int num)
Header with fundamental macros.