47static void write_pidfile(
int pid_fd)
53 if (ftruncate(pid_fd, 0))
58 if (write(pid_fd, buf, strlen(buf)) != (
int) strlen(buf))
67int child_got_signal_from_us = 0;
78static void log_reopen_handler(
int num)
86static void sigusr2_handler(
int num)
88 child_got_signal_from_us = 1;
91static pid_t keepalive_pid = 0;
93static void keepalive(
void (*work)(
void *data),
void *data)
101 void (*old_sigterm)(int);
102 void (*old_sigusr1)(int);
103 struct sigaction sa2, sa1;
105 keepalive_pid = getpid();
112 sigemptyset(&sa2.sa_mask);
113 sa2.sa_handler = sigusr2_handler;
115 sigaction(SIGUSR2, &sa2, &sa1);
117 while (cont && !child_got_signal_from_us)
122 if (p == (pid_t) (-1))
130 signal(SIGTERM, old_sigterm);
131 signal(SIGUSR1, old_sigusr1);
132 sigaction(SIGUSR2, &sa1, NULL);
142 while ((p1 = waitpid(p, &status, 0) == (pid_t ) (-1)) && errno == EINTR)
145 if (p1 == (pid_t) (-1))
159 if (WIFSIGNALED(status))
162 switch (WTERMSIG(status))
187 WTERMSIG(status), (
long) p);
191 else if (WIFEXITED(status))
194 if (WEXITSTATUS(status) != 0)
197 WEXITSTATUS(status), (
long) p);
220 kill(keepalive_pid, SIGUSR2);
227 void (*work)(
void *data),
void *data,
228 const char *pidfile,
const char *uid)
236 pid_fd = open(pidfile, O_CREAT|O_RDWR, 0666);
247 write_pidfile(pid_fd);
256 struct passwd *pw = getpwnam(uid);
267 if (fchown(fileno(f), pw->pw_uid, -1))
271 if (setuid(pw->pw_uid) < 0)
279#ifdef PR_SET_DUMPABLE
280 prctl(PR_SET_DUMPABLE, 1, 0, 0);
306 int res = read(hand[0], dummy, 1);
307 if (res < 0 && errno != EINTR)
326 open(
"/dev/null", O_RDWR);
334 write_pidfile(pid_fd);
339 sigemptyset(&sa.sa_mask);
340 sa.sa_handler = log_reopen_handler;
342 sigaction(SIGHUP, &sa, 0);
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.
void yaz_log_reopen()
reopen current log file (unless disabled or stderr)
void yaz_log(int level, const char *fmt,...)
Writes log message.
FILE * yaz_log_file(void)
returns FILE handle for log or NULL if no file is in use
#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.