blob: 721888f6dbf26c585e455f5ec2d055d9c77744d5 [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
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000013#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000014
15#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000016
17
Denis Vlasenkofbd29182007-04-07 01:05:47 +000018const uint8_t MAC_BCAST_ADDR[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
19
Mike Frysinger7031f622006-05-08 03:20:50 +000020long uptime(void)
21{
22 struct sysinfo info;
23 sysinfo(&info);
24 return info.uptime;
25}
26
Denis Vlasenko10457b92007-03-27 22:01:31 +000027#if ENABLE_FEATURE_PIDFILE
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000028static const char *saved_pidfile;
29
30static void pidfile_delete(void)
31{
32 if (saved_pidfile)
Denis Vlasenko10457b92007-03-27 22:01:31 +000033 remove_pidfile(saved_pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +000034}
Denis Vlasenko10457b92007-03-27 22:01:31 +000035#endif
Mike Frysinger7031f622006-05-08 03:20:50 +000036
Denis Vlasenko10457b92007-03-27 22:01:31 +000037static void create_pidfile(const char *pidfile)
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000038{
Denis Vlasenko10457b92007-03-27 22:01:31 +000039 if (!pidfile)
40 return;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000041
Denis Vlasenko10457b92007-03-27 22:01:31 +000042 if (!write_pidfile(pidfile)) {
43 bb_perror_msg("cannot create pidfile %s", pidfile);
44 return;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000045 }
Denis Vlasenko10457b92007-03-27 22:01:31 +000046#if ENABLE_FEATURE_PIDFILE
47 /* lockf(pid_fd, F_LOCK, 0); */
48 if (!saved_pidfile)
49 atexit(pidfile_delete);
50 saved_pidfile = pidfile;
51#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000052}
53
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000054void udhcp_make_pidfile(const char *pidfile)
Mike Frysinger7031f622006-05-08 03:20:50 +000055{
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000056 /* Make sure fd 0,1,2 are open */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000057 bb_sanitize_stdio();
Mike Frysinger7031f622006-05-08 03:20:50 +000058
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000059 /* Equivalent of doing a fflush after every \n */
Mike Frysinger7031f622006-05-08 03:20:50 +000060 setlinebuf(stdout);
61
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000062 /* Create pidfile */
Denis Vlasenko10457b92007-03-27 22:01:31 +000063 create_pidfile(pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +000064
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000065 bb_info_msg("%s (v%s) started", applet_name, BB_VER);
Mike Frysinger7031f622006-05-08 03:20:50 +000066}