diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-11 19:51:11 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-11 19:51:11 +0000 |
commit | 08ea11ab0749a7977e6d47cd0fa7b3c9cc10af32 (patch) | |
tree | 84049bf86fbcbe95ad0443816efd07f8c02c1288 | |
parent | 9725daa03a7806b1c9c5a3c511dfe2ff9f97dd26 (diff) | |
download | busybox-w32-08ea11ab0749a7977e6d47cd0fa7b3c9cc10af32.tar.gz busybox-w32-08ea11ab0749a7977e6d47cd0fa7b3c9cc10af32.tar.bz2 busybox-w32-08ea11ab0749a7977e6d47cd0fa7b3c9cc10af32.zip |
runsvd: shrink by Vladimir
*: use unified trivial signal handler
function old new delta
record_signo - 10 +10
process_stdin 433 443 +10
bbunpack 383 391 +8
wc_main 598 605 +7
conescape 293 296 +3
nmeter_main 670 672 +2
fallbackSort 1717 1719 +2
bb_got_signal - 1 +1
microcom_main 713 712 -1
signalled 2 - -2
pack_gzip 1661 1659 -2
evalvar 1376 1374 -2
compare_keys 737 735 -2
parse_command 1460 1456 -4
expand 1748 1744 -4
s_term 37 29 -8
s_hangup 8 - -8
fgotsig 10 - -10
find_pair 187 169 -18
signal_handler 190 170 -20
runsvdir_main 1701 1583 -118
------------------------------------------------------------------------------
(add/remove: 2/3 grow/shrink: 6/10 up/down: 43/-199) Total: -156 bytes
-rw-r--r-- | include/libbb.h | 3 | ||||
-rw-r--r-- | libbb/signals.c | 8 | ||||
-rw-r--r-- | miscutils/inotifyd.c | 11 | ||||
-rw-r--r-- | miscutils/microcom.c | 20 | ||||
-rw-r--r-- | networking/telnet.c | 21 | ||||
-rw-r--r-- | runit/runsvdir.c | 38 |
6 files changed, 35 insertions, 66 deletions
diff --git a/include/libbb.h b/include/libbb.h index 388e2f9ef..34c7e417b 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -373,6 +373,9 @@ void sig_unblock(int sig) FAST_FUNC; | |||
373 | int sigaction_set(int sig, const struct sigaction *act) FAST_FUNC; | 373 | int sigaction_set(int sig, const struct sigaction *act) FAST_FUNC; |
374 | /* SIG_BLOCK/SIG_UNBLOCK all signals: */ | 374 | /* SIG_BLOCK/SIG_UNBLOCK all signals: */ |
375 | int sigprocmask_allsigs(int how) FAST_FUNC; | 375 | int sigprocmask_allsigs(int how) FAST_FUNC; |
376 | /* Standard handler which just records signo */ | ||
377 | extern smallint bb_got_signal; | ||
378 | void record_signo(int signo); /* not FAST_FUNC! */ | ||
376 | 379 | ||
377 | 380 | ||
378 | void xsetgid(gid_t gid) FAST_FUNC; | 381 | void xsetgid(gid_t gid) FAST_FUNC; |
diff --git a/libbb/signals.c b/libbb/signals.c index 959114679..f56ce65a8 100644 --- a/libbb/signals.c +++ b/libbb/signals.c | |||
@@ -11,6 +11,14 @@ | |||
11 | 11 | ||
12 | #include "libbb.h" | 12 | #include "libbb.h" |
13 | 13 | ||
14 | /* All known arches use small ints for signals */ | ||
15 | smallint bb_got_signal; | ||
16 | |||
17 | void record_signo(int signo) | ||
18 | { | ||
19 | bb_got_signal = signo; | ||
20 | } | ||
21 | |||
14 | /* Saves 2 bytes on x86! Oh my... */ | 22 | /* Saves 2 bytes on x86! Oh my... */ |
15 | int FAST_FUNC sigaction_set(int signum, const struct sigaction *act) | 23 | int FAST_FUNC sigaction_set(int signum, const struct sigaction *act) |
16 | { | 24 | { |
diff --git a/miscutils/inotifyd.c b/miscutils/inotifyd.c index 2a1a15348..3b11dc271 100644 --- a/miscutils/inotifyd.c +++ b/miscutils/inotifyd.c | |||
@@ -30,13 +30,6 @@ | |||
30 | #include "libbb.h" | 30 | #include "libbb.h" |
31 | #include <linux/inotify.h> | 31 | #include <linux/inotify.h> |
32 | 32 | ||
33 | static volatile smallint signalled; | ||
34 | |||
35 | static void signal_handler(int signo) | ||
36 | { | ||
37 | signalled = signo; | ||
38 | } | ||
39 | |||
40 | static const char mask_names[] ALIGN1 = | 33 | static const char mask_names[] ALIGN1 = |
41 | "a" // 0x00000001 File was accessed | 34 | "a" // 0x00000001 File was accessed |
42 | "c" // 0x00000002 File was modified | 35 | "c" // 0x00000002 File was modified |
@@ -104,14 +97,14 @@ int inotifyd_main(int argc UNUSED_PARAM, char **argv) | |||
104 | + (1 << SIGINT) | 97 | + (1 << SIGINT) |
105 | + (1 << SIGTERM) | 98 | + (1 << SIGTERM) |
106 | + (1 << SIGPIPE) | 99 | + (1 << SIGPIPE) |
107 | , signal_handler); | 100 | , record_signo); |
108 | 101 | ||
109 | // do watch | 102 | // do watch |
110 | 103 | ||
111 | // pfd.fd = fd; | 104 | // pfd.fd = fd; |
112 | pfd.events = POLLIN; | 105 | pfd.events = POLLIN; |
113 | 106 | ||
114 | while (!signalled && poll(&pfd, 1, -1) > 0) { | 107 | while (!bb_got_signal && poll(&pfd, 1, -1) > 0) { |
115 | ssize_t len; | 108 | ssize_t len; |
116 | void *buf; | 109 | void *buf; |
117 | struct inotify_event *ie; | 110 | struct inotify_event *ie; |
diff --git a/miscutils/microcom.c b/miscutils/microcom.c index ac3e5514f..a322197b8 100644 --- a/miscutils/microcom.c +++ b/miscutils/microcom.c | |||
@@ -9,14 +9,6 @@ | |||
9 | */ | 9 | */ |
10 | #include "libbb.h" | 10 | #include "libbb.h" |
11 | 11 | ||
12 | /* All known arches use small ints for signals */ | ||
13 | static volatile smallint signalled; | ||
14 | |||
15 | static void signal_handler(int signo) | ||
16 | { | ||
17 | signalled = signo; | ||
18 | } | ||
19 | |||
20 | // set raw tty mode | 12 | // set raw tty mode |
21 | static void xget1(int fd, struct termios *t, struct termios *oldt) | 13 | static void xget1(int fd, struct termios *t, struct termios *oldt) |
22 | { | 14 | { |
@@ -91,10 +83,10 @@ int microcom_main(int argc UNUSED_PARAM, char **argv) | |||
91 | + (1 << SIGINT) | 83 | + (1 << SIGINT) |
92 | + (1 << SIGTERM) | 84 | + (1 << SIGTERM) |
93 | + (1 << SIGPIPE) | 85 | + (1 << SIGPIPE) |
94 | , signal_handler); | 86 | , record_signo); |
95 | 87 | ||
96 | // error exit code if we fail to open the device | 88 | // error exit code if we fail to open the device |
97 | signalled = 1; | 89 | bb_got_signal = 1; |
98 | 90 | ||
99 | // open device | 91 | // open device |
100 | sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK); | 92 | sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK); |
@@ -123,9 +115,9 @@ int microcom_main(int argc UNUSED_PARAM, char **argv) | |||
123 | pfd[1].fd = STDIN_FILENO; | 115 | pfd[1].fd = STDIN_FILENO; |
124 | pfd[1].events = POLLIN; | 116 | pfd[1].events = POLLIN; |
125 | 117 | ||
126 | signalled = 0; | 118 | bb_got_signal = 0; |
127 | nfd = 2; | 119 | nfd = 2; |
128 | while (!signalled && safe_poll(pfd, nfd, timeout) > 0) { | 120 | while (!bb_got_signal && safe_poll(pfd, nfd, timeout) > 0) { |
129 | if (nfd > 1 && pfd[1].revents) { | 121 | if (nfd > 1 && pfd[1].revents) { |
130 | char c; | 122 | char c; |
131 | // read from stdin -> write to device | 123 | // read from stdin -> write to device |
@@ -159,7 +151,7 @@ skip_write: ; | |||
159 | full_write(STDOUT_FILENO, iobuf, len); | 151 | full_write(STDOUT_FILENO, iobuf, len); |
160 | else { | 152 | else { |
161 | // EOF/error -> bail out | 153 | // EOF/error -> bail out |
162 | signalled = SIGHUP; | 154 | bb_got_signal = SIGHUP; |
163 | break; | 155 | break; |
164 | } | 156 | } |
165 | } | 157 | } |
@@ -175,5 +167,5 @@ done: | |||
175 | if (device_lock_file) | 167 | if (device_lock_file) |
176 | unlink(device_lock_file); | 168 | unlink(device_lock_file); |
177 | 169 | ||
178 | return signalled; | 170 | return bb_got_signal; |
179 | } | 171 | } |
diff --git a/networking/telnet.c b/networking/telnet.c index 63ee38088..5d7ecef3b 100644 --- a/networking/telnet.c +++ b/networking/telnet.c | |||
@@ -60,7 +60,6 @@ struct globals { | |||
60 | byte telwish; /* DO, DONT, WILL, WONT */ | 60 | byte telwish; /* DO, DONT, WILL, WONT */ |
61 | byte charmode; | 61 | byte charmode; |
62 | byte telflags; | 62 | byte telflags; |
63 | byte gotsig; | ||
64 | byte do_termios; | 63 | byte do_termios; |
65 | #if ENABLE_FEATURE_TELNET_TTYPE | 64 | #if ENABLE_FEATURE_TELNET_TTYPE |
66 | char *ttype; | 65 | char *ttype; |
@@ -113,7 +112,7 @@ static void conescape(void) | |||
113 | { | 112 | { |
114 | char b; | 113 | char b; |
115 | 114 | ||
116 | if (G.gotsig) /* came from line mode... go raw */ | 115 | if (bb_got_signal) /* came from line mode... go raw */ |
117 | rawmode(); | 116 | rawmode(); |
118 | 117 | ||
119 | write_str(1, "\r\nConsole escape. Commands are:\r\n\n" | 118 | write_str(1, "\r\nConsole escape. Commands are:\r\n\n" |
@@ -127,13 +126,13 @@ static void conescape(void) | |||
127 | 126 | ||
128 | switch (b) { | 127 | switch (b) { |
129 | case 'l': | 128 | case 'l': |
130 | if (!G.gotsig) { | 129 | if (!bb_got_signal) { |
131 | do_linemode(); | 130 | do_linemode(); |
132 | goto rrturn; | 131 | goto rrturn; |
133 | } | 132 | } |
134 | break; | 133 | break; |
135 | case 'c': | 134 | case 'c': |
136 | if (G.gotsig) { | 135 | if (bb_got_signal) { |
137 | will_charmode(); | 136 | will_charmode(); |
138 | goto rrturn; | 137 | goto rrturn; |
139 | } | 138 | } |
@@ -149,11 +148,11 @@ static void conescape(void) | |||
149 | 148 | ||
150 | write_str(1, "continuing...\r\n"); | 149 | write_str(1, "continuing...\r\n"); |
151 | 150 | ||
152 | if (G.gotsig) | 151 | if (bb_got_signal) |
153 | cookmode(); | 152 | cookmode(); |
154 | 153 | ||
155 | rrturn: | 154 | rrturn: |
156 | G.gotsig = 0; | 155 | bb_got_signal = 0; |
157 | 156 | ||
158 | } | 157 | } |
159 | 158 | ||
@@ -524,12 +523,6 @@ static int subneg(byte c) | |||
524 | return FALSE; | 523 | return FALSE; |
525 | } | 524 | } |
526 | 525 | ||
527 | static void fgotsig(int sig) | ||
528 | { | ||
529 | G.gotsig = sig; | ||
530 | } | ||
531 | |||
532 | |||
533 | static void rawmode(void) | 526 | static void rawmode(void) |
534 | { | 527 | { |
535 | if (G.do_termios) | 528 | if (G.do_termios) |
@@ -592,7 +585,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv) | |||
592 | 585 | ||
593 | setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); | 586 | setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); |
594 | 587 | ||
595 | signal(SIGINT, fgotsig); | 588 | signal(SIGINT, record_signo); |
596 | 589 | ||
597 | #ifdef USE_POLL | 590 | #ifdef USE_POLL |
598 | ufds[0].fd = 0; ufds[1].fd = netfd; | 591 | ufds[0].fd = 0; ufds[1].fd = netfd; |
@@ -617,7 +610,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv) | |||
617 | /* timeout */ | 610 | /* timeout */ |
618 | case -1: | 611 | case -1: |
619 | /* error, ignore and/or log something, bay go to loop */ | 612 | /* error, ignore and/or log something, bay go to loop */ |
620 | if (G.gotsig) | 613 | if (bb_got_signal) |
621 | conescape(); | 614 | conescape(); |
622 | else | 615 | else |
623 | sleep(1); | 616 | sleep(1); |
diff --git a/runit/runsvdir.c b/runit/runsvdir.c index aaa4c470c..0dedd5c77 100644 --- a/runit/runsvdir.c +++ b/runit/runsvdir.c | |||
@@ -52,7 +52,6 @@ struct globals { | |||
52 | struct pollfd pfd[1]; | 52 | struct pollfd pfd[1]; |
53 | unsigned stamplog; | 53 | unsigned stamplog; |
54 | smallint check; /* = 1; */ | 54 | smallint check; /* = 1; */ |
55 | smallint exitsoon; | ||
56 | smallint set_pgrp; | 55 | smallint set_pgrp; |
57 | }; | 56 | }; |
58 | #define G (*(struct globals*)&bb_common_bufsiz1) | 57 | #define G (*(struct globals*)&bb_common_bufsiz1) |
@@ -65,7 +64,6 @@ struct globals { | |||
65 | #define pfd (G.pfd ) | 64 | #define pfd (G.pfd ) |
66 | #define stamplog (G.stamplog ) | 65 | #define stamplog (G.stamplog ) |
67 | #define check (G.check ) | 66 | #define check (G.check ) |
68 | #define exitsoon (G.exitsoon ) | ||
69 | #define set_pgrp (G.set_pgrp ) | 67 | #define set_pgrp (G.set_pgrp ) |
70 | #define INIT_G() do { \ | 68 | #define INIT_G() do { \ |
71 | check = 1; \ | 69 | check = 1; \ |
@@ -89,15 +87,6 @@ static void warnx(const char *m1) | |||
89 | warn3x(m1, "", ""); | 87 | warn3x(m1, "", ""); |
90 | } | 88 | } |
91 | 89 | ||
92 | static void s_term(int sig_no UNUSED_PARAM) | ||
93 | { | ||
94 | exitsoon = 1; | ||
95 | } | ||
96 | static void s_hangup(int sig_no UNUSED_PARAM) | ||
97 | { | ||
98 | exitsoon = 2; | ||
99 | } | ||
100 | |||
101 | static void runsv(int no, const char *name) | 90 | static void runsv(int no, const char *name) |
102 | { | 91 | { |
103 | pid_t pid; | 92 | pid_t pid; |
@@ -246,20 +235,11 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv) | |||
246 | 235 | ||
247 | INIT_G(); | 236 | INIT_G(); |
248 | 237 | ||
249 | argv++; | 238 | opt_complementary = "-1"; |
250 | if (!*argv) | 239 | set_pgrp = getopt32(argv, "P"); |
251 | bb_show_usage(); | 240 | argv += optind; |
252 | if (argv[0][0] == '-') { | ||
253 | switch (argv[0][1]) { | ||
254 | case 'P': set_pgrp = 1; | ||
255 | case '-': ++argv; | ||
256 | } | ||
257 | if (!*argv) | ||
258 | bb_show_usage(); | ||
259 | } | ||
260 | 241 | ||
261 | bb_signals_recursive(1 << SIGTERM, s_term); | 242 | bb_signals_recursive((1 << SIGTERM) | (1 << SIGHUP), record_signo); |
262 | bb_signals_recursive(1 << SIGHUP, s_hangup); | ||
263 | svdir = *argv++; | 243 | svdir = *argv++; |
264 | if (argv && *argv) { | 244 | if (argv && *argv) { |
265 | rplog = *argv; | 245 | rplog = *argv; |
@@ -346,14 +326,14 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv) | |||
346 | } | 326 | } |
347 | } | 327 | } |
348 | 328 | ||
349 | switch (exitsoon) { | 329 | switch (bb_got_signal) { |
350 | case 1: | 330 | case SIGHUP: |
351 | _exit(EXIT_SUCCESS); | ||
352 | case 2: | ||
353 | for (i = 0; i < svnum; i++) | 331 | for (i = 0; i < svnum; i++) |
354 | if (sv[i].pid) | 332 | if (sv[i].pid) |
355 | kill(sv[i].pid, SIGTERM); | 333 | kill(sv[i].pid, SIGTERM); |
356 | _exit(111); | 334 | // N.B. fall through |
335 | case SIGTERM: | ||
336 | _exit((SIGHUP == bb_got_signal) ? 111 : EXIT_SUCCESS); | ||
357 | } | 337 | } |
358 | } | 338 | } |
359 | /* not reached */ | 339 | /* not reached */ |