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 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 18 | long uptime(void) |
| 19 | { |
| 20 | struct sysinfo info; |
| 21 | sysinfo(&info); |
| 22 | return info.uptime; |
| 23 | } |
| 24 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame^] | 26 | static const char *saved_pidfile; |
| 27 | |
| 28 | static void pidfile_delete(void) |
| 29 | { |
| 30 | if (saved_pidfile) |
| 31 | unlink(saved_pidfile); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame^] | 34 | static int pidfile_acquire(const char *pidfile) |
| 35 | { |
| 36 | int pid_fd; |
| 37 | if (!pidfile) return -1; |
| 38 | |
| 39 | pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644); |
| 40 | if (pid_fd < 0) { |
| 41 | bb_perror_msg("cannot open pidfile %s", pidfile); |
| 42 | } else { |
| 43 | /* lockf(pid_fd, F_LOCK, 0); */ |
| 44 | if (!saved_pidfile) |
| 45 | atexit(pidfile_delete); |
| 46 | saved_pidfile = pidfile; |
| 47 | } |
| 48 | |
| 49 | return pid_fd; |
| 50 | } |
| 51 | |
| 52 | static void pidfile_write_release(int pid_fd) |
| 53 | { |
| 54 | if (pid_fd < 0) return; |
| 55 | |
| 56 | fdprintf(pid_fd, "%d\n", getpid()); |
| 57 | /* lockf(pid_fd, F_UNLCK, 0); */ |
| 58 | close(pid_fd); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | void udhcp_make_pidfile(const char *pidfile) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 63 | { |
| 64 | int pid_fd; |
| 65 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame^] | 66 | /* Make sure fd 0,1,2 are open */ |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 67 | bb_sanitize_stdio(); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame^] | 69 | /* Equivalent of doing a fflush after every \n */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 70 | setlinebuf(stdout); |
| 71 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame^] | 72 | /* Create pidfile */ |
| 73 | pid_fd = pidfile_acquire(pidfile); |
| 74 | pidfile_write_release(pid_fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 76 | bb_info_msg("%s (v%s) started", applet_name, BB_VER); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 77 | } |