make pidfile writing configurable.
[platform/upstream/busybox.git] / networking / udhcp / common.c
1 /* 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 <syslog.h>
14
15 #include "common.h"
16
17
18 long uptime(void)
19 {
20         struct sysinfo info;
21         sysinfo(&info);
22         return info.uptime;
23 }
24
25 #if ENABLE_FEATURE_PIDFILE
26 static const char *saved_pidfile;
27
28 static void pidfile_delete(void)
29 {
30         if (saved_pidfile)
31                 remove_pidfile(saved_pidfile);
32 }
33 #endif
34
35 static void create_pidfile(const char *pidfile)
36 {
37         if (!pidfile)
38                 return;
39
40         if (!write_pidfile(pidfile)) {
41                 bb_perror_msg("cannot create pidfile %s", pidfile);
42                 return;
43         }
44 #if ENABLE_FEATURE_PIDFILE
45         /* lockf(pid_fd, F_LOCK, 0); */
46         if (!saved_pidfile)
47                 atexit(pidfile_delete);
48         saved_pidfile = pidfile;
49 #endif
50 }
51
52 void udhcp_make_pidfile(const char *pidfile)
53 {
54         /* Make sure fd 0,1,2 are open */
55         bb_sanitize_stdio();
56
57         /* Equivalent of doing a fflush after every \n */
58         setlinebuf(stdout);
59
60         /* Create pidfile */
61         create_pidfile(pidfile);
62
63         bb_info_msg("%s (v%s) started", applet_name, BB_VER);
64 }