aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/udhcp/common.c')
-rw-r--r--networking/udhcp/common.c71
1 files changed, 14 insertions, 57 deletions
diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c
index c2025e588..1ae65f750 100644
--- a/networking/udhcp/common.c
+++ b/networking/udhcp/common.c
@@ -19,6 +19,7 @@
19#include <paths.h> 19#include <paths.h>
20#include <sys/socket.h> 20#include <sys/socket.h>
21#include <stdarg.h> 21#include <stdarg.h>
22#include <syslog.h>
22 23
23#include "common.h" 24#include "common.h"
24#include "pidfile.h" 25#include "pidfile.h"
@@ -33,7 +34,6 @@ long uptime(void)
33 return info.uptime; 34 return info.uptime;
34} 35}
35 36
36
37/* 37/*
38 * This function makes sure our first socket calls 38 * This function makes sure our first socket calls
39 * aren't going to fd 1 (printf badness...) and are 39 * aren't going to fd 1 (printf badness...) and are
@@ -41,77 +41,32 @@ long uptime(void)
41 */ 41 */
42static inline void sanitize_fds(void) 42static inline void sanitize_fds(void)
43{ 43{
44 int zero; 44 int fd = open(bb_dev_null, O_RDWR, 0);
45 if ((zero = open(bb_dev_null, O_RDWR, 0)) < 0) 45 if (fd < 0)
46 return; 46 return;
47 while (zero < 3) 47 while (fd < 3)
48 zero = dup(zero); 48 fd = dup(fd);
49 close(zero); 49 close(fd);
50} 50}
51 51
52 52
53void udhcp_background(const char *pidfile) 53void udhcp_background(const char *pidfile)
54{ 54{
55#ifdef __uClinux__ 55#ifdef __uClinux__
56 LOG(LOG_ERR, "Cannot background in uclinux (yet)"); 56 bb_error_msg("Cannot background in uclinux (yet)");
57#else /* __uClinux__ */ 57#else /* __uClinux__ */
58 int pid_fd; 58 int pid_fd;
59 59
60 /* hold lock during fork. */ 60 /* hold lock during fork. */
61 pid_fd = pidfile_acquire(pidfile); 61 pid_fd = pidfile_acquire(pidfile);
62 setsid();
62 xdaemon(0, 0); 63 xdaemon(0, 0);
63 daemonized++; 64 daemonized++;
65 logmode &= ~LOGMODE_STDIO;
64 pidfile_write_release(pid_fd); 66 pidfile_write_release(pid_fd);
65#endif /* __uClinux__ */ 67#endif /* __uClinux__ */
66} 68}
67 69
68
69#ifdef CONFIG_FEATURE_UDHCP_SYSLOG
70
71void udhcp_logging(int level, const char *fmt, ...)
72{
73 va_list p;
74 va_list p2;
75
76 va_start(p, fmt);
77 __va_copy(p2, p);
78 if (!daemonized) {
79 vprintf(fmt, p);
80 putchar('\n');
81 }
82 vsyslog(level, fmt, p2);
83 va_end(p);
84}
85
86#else
87
88
89static char *syslog_level_msg[] = {
90 [LOG_EMERG] = "EMERGENCY!",
91 [LOG_ALERT] = "ALERT!",
92 [LOG_CRIT] = "critical!",
93 [LOG_WARNING] = "warning",
94 [LOG_ERR] = "error",
95 [LOG_INFO] = "info",
96 [LOG_DEBUG] = "debug"
97};
98
99
100void udhcp_logging(int level, const char *fmt, ...)
101{
102 va_list p;
103
104 va_start(p, fmt);
105 if (!daemonized) {
106 printf("%s, ", syslog_level_msg[level]);
107 vprintf(fmt, p);
108 putchar('\n');
109 }
110 va_end(p);
111}
112#endif
113
114
115void udhcp_start_log_and_pid(const char *client_server, const char *pidfile) 70void udhcp_start_log_and_pid(const char *client_server, const char *pidfile)
116{ 71{
117 int pid_fd; 72 int pid_fd;
@@ -126,8 +81,10 @@ void udhcp_start_log_and_pid(const char *client_server, const char *pidfile)
126 /* equivelent of doing a fflush after every \n */ 81 /* equivelent of doing a fflush after every \n */
127 setlinebuf(stdout); 82 setlinebuf(stdout);
128 83
129 if (ENABLE_FEATURE_UDHCP_SYSLOG) 84 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
130 openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0); 85 openlog(client_server, LOG_PID, LOG_LOCAL0);
86 logmode |= LOGMODE_SYSLOG;
87 }
131 88
132 udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, BB_VER); 89 bb_info_msg("%s (v%s) started", client_server, BB_VER);
133} 90}