3 * Functions to assist in the writing and removing of pidfiles.
5 * Russ Dill <Russ.Dill@asu.edu> Soptember 2001
6 * Rewrited by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/socket.h>
34 static int daemonized;
36 #ifdef CONFIG_FEATURE_UDHCP_SYSLOG
38 void udhcp_logging(int level, const char *fmt, ...)
51 vsyslog(level, fmt, p2);
55 void start_log(const char *client_server)
57 openlog(bb_applet_name, LOG_PID | LOG_CONS, LOG_LOCAL0);
58 udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
63 static char *syslog_level_msg[] = {
64 [LOG_EMERG] = "EMERGENCY!",
65 [LOG_ALERT] = "ALERT!",
66 [LOG_CRIT] = "critical!",
67 [LOG_WARNING] = "warning",
73 void udhcp_logging(int level, const char *fmt, ...)
80 printf("%s, ", syslog_level_msg[level]);
88 void start_log(const char *client_server)
90 udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
94 static const char *saved_pidfile;
96 static void exit_fun(void)
98 if (saved_pidfile) unlink(saved_pidfile);
101 void background(const char *pidfile)
106 pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
108 LOG(LOG_ERR, "Unable to open pidfile %s: %m", pidfile);
110 lockf(pid_fd, F_LOCK, 0);
112 atexit(exit_fun); /* set atexit one only */
113 saved_pidfile = pidfile; /* but may be rewrite */
116 while (pid_fd >= 0 && pid_fd < 3) pid_fd = dup(pid_fd); /* don't let daemon close it */
117 if (daemon(0, 0) == -1) {
125 if ((out = fdopen(pid_fd, "w")) != NULL) {
126 fprintf(out, "%d\n", getpid());
129 lockf(pid_fd, F_UNLCK, 0);
135 int udhcp_signal_pipe[2];
136 static void signal_handler(int sig)
138 if (send(udhcp_signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) {
139 LOG(LOG_ERR, "Could not send signal: %m");
143 void udhcp_set_signal_pipe(int sig_add)
145 socketpair(AF_UNIX, SOCK_STREAM, 0, udhcp_signal_pipe);
146 signal(SIGUSR1, signal_handler);
147 signal(SIGTERM, signal_handler);
149 signal(sig_add, signal_handler);