diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-27 22:01:31 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-27 22:01:31 +0000 |
commit | 10457b90db925369739a900445b640364eda5e4c (patch) | |
tree | 6d6f18564291257738360d97712724868175167e | |
parent | f4d40c87d3a18fccb8c0946fc09f1d8f24a2bcf3 (diff) | |
download | busybox-w32-10457b90db925369739a900445b640364eda5e4c.tar.gz busybox-w32-10457b90db925369739a900445b640364eda5e4c.tar.bz2 busybox-w32-10457b90db925369739a900445b640364eda5e4c.zip |
make pidfile writing configurable.
[ui]toa_to_buf: change API. No users yet.
-rw-r--r-- | Config.in | 7 | ||||
-rw-r--r-- | include/libbb.h | 13 | ||||
-rw-r--r-- | libbb/Kbuild | 1 | ||||
-rw-r--r-- | libbb/xfuncs.c | 12 | ||||
-rw-r--r-- | miscutils/crond.c | 1 | ||||
-rw-r--r-- | networking/inetd.c | 10 | ||||
-rw-r--r-- | networking/udhcp/common.c | 45 | ||||
-rw-r--r-- | shell/msh.c | 10 | ||||
-rw-r--r-- | sysklogd/syslogd.c | 2 |
9 files changed, 51 insertions, 50 deletions
@@ -141,6 +141,13 @@ config FEATURE_CLEAN_UP | |||
141 | Don't enable this unless you have a really good reason to clean | 141 | Don't enable this unless you have a really good reason to clean |
142 | things up manually. | 142 | things up manually. |
143 | 143 | ||
144 | config FEATURE_PIDFILE | ||
145 | bool "Support writing pidfiles" | ||
146 | default n | ||
147 | help | ||
148 | This option makes some applets (crond, syslogd and inetd) write | ||
149 | a pidfile in /var/run. Some applications rely on them | ||
150 | |||
144 | config FEATURE_SUID | 151 | config FEATURE_SUID |
145 | bool "Support for SUID/SGID handling" | 152 | bool "Support for SUID/SGID handling" |
146 | default n | 153 | default n |
diff --git a/include/libbb.h b/include/libbb.h index 67fd2af21..d734abed6 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -408,10 +408,11 @@ extern FILE *fopen_or_warn(const char *filename, const char *mode); | |||
408 | extern FILE *fopen_or_warn_stdin(const char *filename); | 408 | extern FILE *fopen_or_warn_stdin(const char *filename); |
409 | 409 | ||
410 | 410 | ||
411 | extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen); | ||
412 | extern char *utoa(unsigned n); | 411 | extern char *utoa(unsigned n); |
413 | extern void itoa_to_buf(int n, char *buf, unsigned buflen); | ||
414 | extern char *itoa(int n); | 412 | extern char *itoa(int n); |
413 | /* Returns a pointer past the formatted number, does NOT null-terminate */ | ||
414 | extern char *utoa_to_buf(unsigned n, char *buf, unsigned buflen); | ||
415 | extern char *itoa_to_buf(int n, char *buf, unsigned buflen); | ||
415 | extern void smart_ulltoa5(unsigned long long ul, char buf[5]); | 416 | extern void smart_ulltoa5(unsigned long long ul, char buf[5]); |
416 | /* Put a string of hex bytes (ala "1b"), return advanced pointer */ | 417 | /* Put a string of hex bytes (ala "1b"), return advanced pointer */ |
417 | extern char *bin2hex(char *buf, const char *cp, int count); | 418 | extern char *bin2hex(char *buf, const char *cp, int count); |
@@ -542,6 +543,14 @@ extern void llist_unlink(llist_t **head, llist_t *elm); | |||
542 | extern void llist_free(llist_t *elm, void (*freeit)(void *data)); | 543 | extern void llist_free(llist_t *elm, void (*freeit)(void *data)); |
543 | extern llist_t* llist_rev(llist_t *list); | 544 | extern llist_t* llist_rev(llist_t *list); |
544 | 545 | ||
546 | #if ENABLE_FEATURE_PIDFILE | ||
547 | int write_pidfile(const char *path); | ||
548 | #define remove_pidfile(f) ((void)unlink(f)) | ||
549 | #else | ||
550 | #define write_pidfile(f) 1 | ||
551 | #define remove_pidfile(f) ((void)0) | ||
552 | #endif | ||
553 | |||
545 | enum { | 554 | enum { |
546 | LOGMODE_NONE = 0, | 555 | LOGMODE_NONE = 0, |
547 | LOGMODE_STDIO = 1<<0, | 556 | LOGMODE_STDIO = 1<<0, |
diff --git a/libbb/Kbuild b/libbb/Kbuild index ffded6a68..a3b78ef28 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild | |||
@@ -61,6 +61,7 @@ lib-y += perror_msg.o | |||
61 | lib-y += perror_msg_and_die.o | 61 | lib-y += perror_msg_and_die.o |
62 | lib-y += perror_nomsg.o | 62 | lib-y += perror_nomsg.o |
63 | lib-y += perror_nomsg_and_die.o | 63 | lib-y += perror_nomsg_and_die.o |
64 | lib-y += pidfile.o | ||
64 | lib-y += process_escape_sequence.o | 65 | lib-y += process_escape_sequence.o |
65 | lib-y += procps.o | 66 | lib-y += procps.o |
66 | lib-y += read.o | 67 | lib-y += read.o |
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 7f870ac8b..e1632a4b6 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -257,7 +257,7 @@ void smart_ulltoa5(unsigned long long ul, char buf[5]) | |||
257 | // truncated result is always null terminated (unless buflen is 0), and | 257 | // truncated result is always null terminated (unless buflen is 0), and |
258 | // contains the first few digits of the result ala strncpy. | 258 | // contains the first few digits of the result ala strncpy. |
259 | void BUG_sizeof_unsigned_not_4(void); | 259 | void BUG_sizeof_unsigned_not_4(void); |
260 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) | 260 | char *utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
261 | { | 261 | { |
262 | unsigned i, out, res; | 262 | unsigned i, out, res; |
263 | if (sizeof(unsigned) != 4) | 263 | if (sizeof(unsigned) != 4) |
@@ -273,19 +273,19 @@ void utoa_to_buf(unsigned n, char *buf, unsigned buflen) | |||
273 | *buf++ = '0' + res; | 273 | *buf++ = '0' + res; |
274 | } | 274 | } |
275 | } | 275 | } |
276 | *buf = '\0'; | ||
277 | } | 276 | } |
277 | return buf; | ||
278 | } | 278 | } |
279 | 279 | ||
280 | // Convert signed integer to ascii, like utoa_to_buf() | 280 | // Convert signed integer to ascii, like utoa_to_buf() |
281 | void itoa_to_buf(int n, char *buf, unsigned buflen) | 281 | char *itoa_to_buf(int n, char *buf, unsigned buflen) |
282 | { | 282 | { |
283 | if (buflen && n<0) { | 283 | if (buflen && n<0) { |
284 | n = -n; | 284 | n = -n; |
285 | *buf++ = '-'; | 285 | *buf++ = '-'; |
286 | buflen--; | 286 | buflen--; |
287 | } | 287 | } |
288 | utoa_to_buf((unsigned)n, buf, buflen); | 288 | return utoa_to_buf((unsigned)n, buf, buflen); |
289 | } | 289 | } |
290 | 290 | ||
291 | // The following two functions use a static buffer, so calling either one a | 291 | // The following two functions use a static buffer, so calling either one a |
@@ -300,7 +300,7 @@ static char local_buf[12]; | |||
300 | // Convert unsigned integer to ascii using a static buffer (returned). | 300 | // Convert unsigned integer to ascii using a static buffer (returned). |
301 | char *utoa(unsigned n) | 301 | char *utoa(unsigned n) |
302 | { | 302 | { |
303 | utoa_to_buf(n, local_buf, sizeof(local_buf)); | 303 | *(utoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0'; |
304 | 304 | ||
305 | return local_buf; | 305 | return local_buf; |
306 | } | 306 | } |
@@ -308,7 +308,7 @@ char *utoa(unsigned n) | |||
308 | // Convert signed integer to ascii using a static buffer (returned). | 308 | // Convert signed integer to ascii using a static buffer (returned). |
309 | char *itoa(int n) | 309 | char *itoa(int n) |
310 | { | 310 | { |
311 | itoa_to_buf(n, local_buf, sizeof(local_buf)); | 311 | *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0'; |
312 | 312 | ||
313 | return local_buf; | 313 | return local_buf; |
314 | } | 314 | } |
diff --git a/miscutils/crond.c b/miscutils/crond.c index 9d511f960..24aa3dcc3 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c | |||
@@ -185,6 +185,7 @@ int crond_main(int ac, char **av) | |||
185 | int rescan = 60; | 185 | int rescan = 60; |
186 | short sleep_time = 60; | 186 | short sleep_time = 60; |
187 | 187 | ||
188 | write_pidfile("/var/run/crond.pid"); | ||
188 | for (;;) { | 189 | for (;;) { |
189 | sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time)); | 190 | sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time)); |
190 | 191 | ||
diff --git a/networking/inetd.c b/networking/inetd.c index 4faa4203a..fd72aa726 100644 --- a/networking/inetd.c +++ b/networking/inetd.c | |||
@@ -1212,7 +1212,7 @@ static void goaway(int sig ATTRIBUTE_UNUSED) | |||
1212 | } | 1212 | } |
1213 | (void) close(sep->se_fd); | 1213 | (void) close(sep->se_fd); |
1214 | } | 1214 | } |
1215 | (void) unlink(_PATH_INETDPID); | 1215 | remove_pidfile(_PATH_INETDPID); |
1216 | exit(0); | 1216 | exit(0); |
1217 | } | 1217 | } |
1218 | 1218 | ||
@@ -1301,13 +1301,7 @@ int inetd_main(int argc, char *argv[]) | |||
1301 | setgroups(1, &gid); | 1301 | setgroups(1, &gid); |
1302 | } | 1302 | } |
1303 | 1303 | ||
1304 | { | 1304 | write_pidfile(_PATH_INETDPID); |
1305 | FILE *fp = fopen(_PATH_INETDPID, "w"); | ||
1306 | if (fp != NULL) { | ||
1307 | fprintf(fp, "%u\n", getpid()); | ||
1308 | fclose(fp); | ||
1309 | } | ||
1310 | } | ||
1311 | 1305 | ||
1312 | if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) { | 1306 | if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) { |
1313 | bb_perror_msg("getrlimit"); | 1307 | bb_perror_msg("getrlimit"); |
diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index 7b2e19c42..46cc0348f 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c | |||
@@ -22,47 +22,35 @@ long uptime(void) | |||
22 | return info.uptime; | 22 | return info.uptime; |
23 | } | 23 | } |
24 | 24 | ||
25 | 25 | #if ENABLE_FEATURE_PIDFILE | |
26 | static const char *saved_pidfile; | 26 | static const char *saved_pidfile; |
27 | 27 | ||
28 | static void pidfile_delete(void) | 28 | static void pidfile_delete(void) |
29 | { | 29 | { |
30 | if (saved_pidfile) | 30 | if (saved_pidfile) |
31 | unlink(saved_pidfile); | 31 | remove_pidfile(saved_pidfile); |
32 | } | 32 | } |
33 | #endif | ||
33 | 34 | ||
34 | static int pidfile_acquire(const char *pidfile) | 35 | static void create_pidfile(const char *pidfile) |
35 | { | 36 | { |
36 | int pid_fd; | 37 | if (!pidfile) |
37 | if (!pidfile) return -1; | 38 | return; |
38 | 39 | ||
39 | pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644); | 40 | if (!write_pidfile(pidfile)) { |
40 | if (pid_fd < 0) { | 41 | bb_perror_msg("cannot create pidfile %s", pidfile); |
41 | bb_perror_msg("cannot open pidfile %s", pidfile); | 42 | return; |
42 | } else { | ||
43 | /* lockf(pid_fd, F_LOCK, 0); */ | ||
44 | if (!saved_pidfile) | ||
45 | atexit(pidfile_delete); | ||
46 | saved_pidfile = pidfile; | ||
47 | } | 43 | } |
48 | 44 | #if ENABLE_FEATURE_PIDFILE | |
49 | return pid_fd; | 45 | /* lockf(pid_fd, F_LOCK, 0); */ |
50 | } | 46 | if (!saved_pidfile) |
51 | 47 | atexit(pidfile_delete); | |
52 | static void pidfile_write_release(int pid_fd) | 48 | saved_pidfile = pidfile; |
53 | { | 49 | #endif |
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 | } | 50 | } |
60 | 51 | ||
61 | |||
62 | void udhcp_make_pidfile(const char *pidfile) | 52 | void udhcp_make_pidfile(const char *pidfile) |
63 | { | 53 | { |
64 | int pid_fd; | ||
65 | |||
66 | /* Make sure fd 0,1,2 are open */ | 54 | /* Make sure fd 0,1,2 are open */ |
67 | bb_sanitize_stdio(); | 55 | bb_sanitize_stdio(); |
68 | 56 | ||
@@ -70,8 +58,7 @@ void udhcp_make_pidfile(const char *pidfile) | |||
70 | setlinebuf(stdout); | 58 | setlinebuf(stdout); |
71 | 59 | ||
72 | /* Create pidfile */ | 60 | /* Create pidfile */ |
73 | pid_fd = pidfile_acquire(pidfile); | 61 | create_pidfile(pidfile); |
74 | pidfile_write_release(pid_fd); | ||
75 | 62 | ||
76 | bb_info_msg("%s (v%s) started", applet_name, BB_VER); | 63 | bb_info_msg("%s (v%s) started", applet_name, BB_VER); |
77 | } | 64 | } |
diff --git a/shell/msh.c b/shell/msh.c index 66b10f346..50ec90b0b 100644 --- a/shell/msh.c +++ b/shell/msh.c | |||
@@ -49,7 +49,7 @@ static char *find_applet_by_name(const char *applet) | |||
49 | { | 49 | { |
50 | return NULL; | 50 | return NULL; |
51 | } | 51 | } |
52 | static void utoa_to_buf(unsigned n, char *buf, unsigned buflen) | 52 | static char *utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
53 | { | 53 | { |
54 | unsigned i, out, res; | 54 | unsigned i, out, res; |
55 | assert(sizeof(unsigned) == 4); | 55 | assert(sizeof(unsigned) == 4); |
@@ -64,22 +64,22 @@ static void utoa_to_buf(unsigned n, char *buf, unsigned buflen) | |||
64 | *buf++ = '0' + res; | 64 | *buf++ = '0' + res; |
65 | } | 65 | } |
66 | } | 66 | } |
67 | *buf = '\0'; | ||
68 | } | 67 | } |
68 | return buf; | ||
69 | } | 69 | } |
70 | static void itoa_to_buf(int n, char *buf, unsigned buflen) | 70 | static char *itoa_to_buf(int n, char *buf, unsigned buflen) |
71 | { | 71 | { |
72 | if (buflen && n < 0) { | 72 | if (buflen && n < 0) { |
73 | n = -n; | 73 | n = -n; |
74 | *buf++ = '-'; | 74 | *buf++ = '-'; |
75 | buflen--; | 75 | buflen--; |
76 | } | 76 | } |
77 | utoa_to_buf((unsigned)n, buf, buflen); | 77 | return utoa_to_buf((unsigned)n, buf, buflen); |
78 | } | 78 | } |
79 | static char local_buf[12]; | 79 | static char local_buf[12]; |
80 | static char *itoa(int n) | 80 | static char *itoa(int n) |
81 | { | 81 | { |
82 | itoa_to_buf(n, local_buf, sizeof(local_buf)); | 82 | *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0'; |
83 | return local_buf; | 83 | return local_buf; |
84 | } | 84 | } |
85 | #else | 85 | #else |
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 51627bd82..76a446b6a 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c | |||
@@ -519,6 +519,7 @@ static void do_syslogd(void) | |||
519 | signal(SIGALRM, do_mark); | 519 | signal(SIGALRM, do_mark); |
520 | alarm(G.markInterval); | 520 | alarm(G.markInterval); |
521 | #endif | 521 | #endif |
522 | remove_pidfile("/var/run/syslogd.pid"); | ||
522 | 523 | ||
523 | memset(&sunx, 0, sizeof(sunx)); | 524 | memset(&sunx, 0, sizeof(sunx)); |
524 | sunx.sun_family = AF_UNIX; | 525 | sunx.sun_family = AF_UNIX; |
@@ -645,6 +646,7 @@ int syslogd_main(int argc, char **argv) | |||
645 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); | 646 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); |
646 | } | 647 | } |
647 | umask(0); | 648 | umask(0); |
649 | write_pidfile("/var/run/syslogd.pid"); | ||
648 | do_syslogd(); | 650 | do_syslogd(); |
649 | /* return EXIT_SUCCESS; */ | 651 | /* return EXIT_SUCCESS; */ |
650 | } | 652 | } |