blob: 1ae65f75085c9dee64b103a59fa238811f75b225 [file] [log] [blame]
Mike Frysinger7031f622006-05-08 03:20:50 +00001/* vi: set sw=4 ts=4: */
2/* common.c
3 *
4 * Functions for debugging and logging as well as some other
5 * simple helper functions.
6 *
7 * Russ Dill <Russ.Dill@asu.edu> 2001-2003
8 * Rewritten by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003
9 *
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 */
12
13#include <fcntl.h>
14#include <unistd.h>
15#include <errno.h>
16#include <string.h>
17#include <stdlib.h>
18#include <signal.h>
19#include <paths.h>
20#include <sys/socket.h>
21#include <stdarg.h>
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000022#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000023
24#include "common.h"
25#include "pidfile.h"
26
27
28static int daemonized;
29
30long uptime(void)
31{
32 struct sysinfo info;
33 sysinfo(&info);
34 return info.uptime;
35}
36
Mike Frysinger7031f622006-05-08 03:20:50 +000037/*
38 * This function makes sure our first socket calls
39 * aren't going to fd 1 (printf badness...) and are
40 * not later closed by daemon()
41 */
42static inline void sanitize_fds(void)
43{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000044 int fd = open(bb_dev_null, O_RDWR, 0);
45 if (fd < 0)
Bernhard Reutner-Fischer0a8812b2006-05-19 13:12:21 +000046 return;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000047 while (fd < 3)
48 fd = dup(fd);
49 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +000050}
51
52
Rob Landley3f785612006-05-28 01:06:36 +000053void udhcp_background(const char *pidfile)
Mike Frysinger7031f622006-05-08 03:20:50 +000054{
55#ifdef __uClinux__
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000056 bb_error_msg("Cannot background in uclinux (yet)");
Mike Frysinger7031f622006-05-08 03:20:50 +000057#else /* __uClinux__ */
58 int pid_fd;
59
60 /* hold lock during fork. */
61 pid_fd = pidfile_acquire(pidfile);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000062 setsid();
Denis Vlasenko27af5a02006-09-03 12:21:59 +000063 xdaemon(0, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +000064 daemonized++;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000065 logmode &= ~LOGMODE_STDIO;
Mike Frysinger7031f622006-05-08 03:20:50 +000066 pidfile_write_release(pid_fd);
67#endif /* __uClinux__ */
68}
69
Rob Landley3f785612006-05-28 01:06:36 +000070void udhcp_start_log_and_pid(const char *client_server, const char *pidfile)
Mike Frysinger7031f622006-05-08 03:20:50 +000071{
72 int pid_fd;
73
74 /* Make sure our syslog fd isn't overwritten */
75 sanitize_fds();
76
77 /* do some other misc startup stuff while we are here to save bytes */
78 pid_fd = pidfile_acquire(pidfile);
79 pidfile_write_release(pid_fd);
80
81 /* equivelent of doing a fflush after every \n */
82 setlinebuf(stdout);
83
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000084 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
85 openlog(client_server, LOG_PID, LOG_LOCAL0);
86 logmode |= LOGMODE_SYSLOG;
87 }
Mike Frysinger7031f622006-05-08 03:20:50 +000088
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000089 bb_info_msg("%s (v%s) started", client_server, BB_VER);
Mike Frysinger7031f622006-05-08 03:20:50 +000090}