Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 13 | #include <syslog.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 14 | |
| 15 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 16 | |
| 17 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 18 | const uint8_t MAC_BCAST_ADDR[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 19 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 20 | long uptime(void) |
| 21 | { |
| 22 | struct sysinfo info; |
| 23 | sysinfo(&info); |
| 24 | return info.uptime; |
| 25 | } |
| 26 | |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 27 | #if ENABLE_FEATURE_PIDFILE |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 28 | static const char *saved_pidfile; |
| 29 | |
| 30 | static void pidfile_delete(void) |
| 31 | { |
| 32 | if (saved_pidfile) |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 33 | remove_pidfile(saved_pidfile); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 34 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 35 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 37 | static void create_pidfile(const char *pidfile) |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 38 | { |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 39 | if (!pidfile) |
| 40 | return; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 42 | if (!write_pidfile(pidfile)) { |
| 43 | bb_perror_msg("cannot create pidfile %s", pidfile); |
| 44 | return; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 45 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 46 | #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 Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 54 | void udhcp_make_pidfile(const char *pidfile) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 55 | { |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 56 | /* Make sure fd 0,1,2 are open */ |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 57 | bb_sanitize_stdio(); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 59 | /* Equivalent of doing a fflush after every \n */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 60 | setlinebuf(stdout); |
| 61 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 62 | /* Create pidfile */ |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 63 | create_pidfile(pidfile); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 65 | bb_info_msg("%s (v%s) started", applet_name, BB_VER); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 66 | } |