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
This commit is contained in:
parent
9725daa03a
commit
08ea11ab07
@ -373,6 +373,9 @@ void sig_unblock(int sig) FAST_FUNC;
|
|||||||
int sigaction_set(int sig, const struct sigaction *act) FAST_FUNC;
|
int sigaction_set(int sig, const struct sigaction *act) FAST_FUNC;
|
||||||
/* SIG_BLOCK/SIG_UNBLOCK all signals: */
|
/* SIG_BLOCK/SIG_UNBLOCK all signals: */
|
||||||
int sigprocmask_allsigs(int how) FAST_FUNC;
|
int sigprocmask_allsigs(int how) FAST_FUNC;
|
||||||
|
/* Standard handler which just records signo */
|
||||||
|
extern smallint bb_got_signal;
|
||||||
|
void record_signo(int signo); /* not FAST_FUNC! */
|
||||||
|
|
||||||
|
|
||||||
void xsetgid(gid_t gid) FAST_FUNC;
|
void xsetgid(gid_t gid) FAST_FUNC;
|
||||||
|
@ -11,6 +11,14 @@
|
|||||||
|
|
||||||
#include "libbb.h"
|
#include "libbb.h"
|
||||||
|
|
||||||
|
/* All known arches use small ints for signals */
|
||||||
|
smallint bb_got_signal;
|
||||||
|
|
||||||
|
void record_signo(int signo)
|
||||||
|
{
|
||||||
|
bb_got_signal = signo;
|
||||||
|
}
|
||||||
|
|
||||||
/* Saves 2 bytes on x86! Oh my... */
|
/* Saves 2 bytes on x86! Oh my... */
|
||||||
int FAST_FUNC sigaction_set(int signum, const struct sigaction *act)
|
int FAST_FUNC sigaction_set(int signum, const struct sigaction *act)
|
||||||
{
|
{
|
||||||
|
@ -30,13 +30,6 @@
|
|||||||
#include "libbb.h"
|
#include "libbb.h"
|
||||||
#include <linux/inotify.h>
|
#include <linux/inotify.h>
|
||||||
|
|
||||||
static volatile smallint signalled;
|
|
||||||
|
|
||||||
static void signal_handler(int signo)
|
|
||||||
{
|
|
||||||
signalled = signo;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char mask_names[] ALIGN1 =
|
static const char mask_names[] ALIGN1 =
|
||||||
"a" // 0x00000001 File was accessed
|
"a" // 0x00000001 File was accessed
|
||||||
"c" // 0x00000002 File was modified
|
"c" // 0x00000002 File was modified
|
||||||
@ -104,14 +97,14 @@ int inotifyd_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
+ (1 << SIGINT)
|
+ (1 << SIGINT)
|
||||||
+ (1 << SIGTERM)
|
+ (1 << SIGTERM)
|
||||||
+ (1 << SIGPIPE)
|
+ (1 << SIGPIPE)
|
||||||
, signal_handler);
|
, record_signo);
|
||||||
|
|
||||||
// do watch
|
// do watch
|
||||||
|
|
||||||
// pfd.fd = fd;
|
// pfd.fd = fd;
|
||||||
pfd.events = POLLIN;
|
pfd.events = POLLIN;
|
||||||
|
|
||||||
while (!signalled && poll(&pfd, 1, -1) > 0) {
|
while (!bb_got_signal && poll(&pfd, 1, -1) > 0) {
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
void *buf;
|
void *buf;
|
||||||
struct inotify_event *ie;
|
struct inotify_event *ie;
|
||||||
|
@ -9,14 +9,6 @@
|
|||||||
*/
|
*/
|
||||||
#include "libbb.h"
|
#include "libbb.h"
|
||||||
|
|
||||||
/* All known arches use small ints for signals */
|
|
||||||
static volatile smallint signalled;
|
|
||||||
|
|
||||||
static void signal_handler(int signo)
|
|
||||||
{
|
|
||||||
signalled = signo;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set raw tty mode
|
// set raw tty mode
|
||||||
static void xget1(int fd, struct termios *t, struct termios *oldt)
|
static void xget1(int fd, struct termios *t, struct termios *oldt)
|
||||||
{
|
{
|
||||||
@ -91,10 +83,10 @@ int microcom_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
+ (1 << SIGINT)
|
+ (1 << SIGINT)
|
||||||
+ (1 << SIGTERM)
|
+ (1 << SIGTERM)
|
||||||
+ (1 << SIGPIPE)
|
+ (1 << SIGPIPE)
|
||||||
, signal_handler);
|
, record_signo);
|
||||||
|
|
||||||
// error exit code if we fail to open the device
|
// error exit code if we fail to open the device
|
||||||
signalled = 1;
|
bb_got_signal = 1;
|
||||||
|
|
||||||
// open device
|
// open device
|
||||||
sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
|
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)
|
|||||||
pfd[1].fd = STDIN_FILENO;
|
pfd[1].fd = STDIN_FILENO;
|
||||||
pfd[1].events = POLLIN;
|
pfd[1].events = POLLIN;
|
||||||
|
|
||||||
signalled = 0;
|
bb_got_signal = 0;
|
||||||
nfd = 2;
|
nfd = 2;
|
||||||
while (!signalled && safe_poll(pfd, nfd, timeout) > 0) {
|
while (!bb_got_signal && safe_poll(pfd, nfd, timeout) > 0) {
|
||||||
if (nfd > 1 && pfd[1].revents) {
|
if (nfd > 1 && pfd[1].revents) {
|
||||||
char c;
|
char c;
|
||||||
// read from stdin -> write to device
|
// read from stdin -> write to device
|
||||||
@ -159,7 +151,7 @@ skip_write: ;
|
|||||||
full_write(STDOUT_FILENO, iobuf, len);
|
full_write(STDOUT_FILENO, iobuf, len);
|
||||||
else {
|
else {
|
||||||
// EOF/error -> bail out
|
// EOF/error -> bail out
|
||||||
signalled = SIGHUP;
|
bb_got_signal = SIGHUP;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -175,5 +167,5 @@ done:
|
|||||||
if (device_lock_file)
|
if (device_lock_file)
|
||||||
unlink(device_lock_file);
|
unlink(device_lock_file);
|
||||||
|
|
||||||
return signalled;
|
return bb_got_signal;
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,6 @@ struct globals {
|
|||||||
byte telwish; /* DO, DONT, WILL, WONT */
|
byte telwish; /* DO, DONT, WILL, WONT */
|
||||||
byte charmode;
|
byte charmode;
|
||||||
byte telflags;
|
byte telflags;
|
||||||
byte gotsig;
|
|
||||||
byte do_termios;
|
byte do_termios;
|
||||||
#if ENABLE_FEATURE_TELNET_TTYPE
|
#if ENABLE_FEATURE_TELNET_TTYPE
|
||||||
char *ttype;
|
char *ttype;
|
||||||
@ -113,7 +112,7 @@ static void conescape(void)
|
|||||||
{
|
{
|
||||||
char b;
|
char b;
|
||||||
|
|
||||||
if (G.gotsig) /* came from line mode... go raw */
|
if (bb_got_signal) /* came from line mode... go raw */
|
||||||
rawmode();
|
rawmode();
|
||||||
|
|
||||||
write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
|
write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
|
||||||
@ -127,13 +126,13 @@ static void conescape(void)
|
|||||||
|
|
||||||
switch (b) {
|
switch (b) {
|
||||||
case 'l':
|
case 'l':
|
||||||
if (!G.gotsig) {
|
if (!bb_got_signal) {
|
||||||
do_linemode();
|
do_linemode();
|
||||||
goto rrturn;
|
goto rrturn;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
if (G.gotsig) {
|
if (bb_got_signal) {
|
||||||
will_charmode();
|
will_charmode();
|
||||||
goto rrturn;
|
goto rrturn;
|
||||||
}
|
}
|
||||||
@ -149,11 +148,11 @@ static void conescape(void)
|
|||||||
|
|
||||||
write_str(1, "continuing...\r\n");
|
write_str(1, "continuing...\r\n");
|
||||||
|
|
||||||
if (G.gotsig)
|
if (bb_got_signal)
|
||||||
cookmode();
|
cookmode();
|
||||||
|
|
||||||
rrturn:
|
rrturn:
|
||||||
G.gotsig = 0;
|
bb_got_signal = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -524,12 +523,6 @@ static int subneg(byte c)
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fgotsig(int sig)
|
|
||||||
{
|
|
||||||
G.gotsig = sig;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void rawmode(void)
|
static void rawmode(void)
|
||||||
{
|
{
|
||||||
if (G.do_termios)
|
if (G.do_termios)
|
||||||
@ -592,7 +585,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
|
|
||||||
setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
|
setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
|
||||||
|
|
||||||
signal(SIGINT, fgotsig);
|
signal(SIGINT, record_signo);
|
||||||
|
|
||||||
#ifdef USE_POLL
|
#ifdef USE_POLL
|
||||||
ufds[0].fd = 0; ufds[1].fd = netfd;
|
ufds[0].fd = 0; ufds[1].fd = netfd;
|
||||||
@ -617,7 +610,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
/* timeout */
|
/* timeout */
|
||||||
case -1:
|
case -1:
|
||||||
/* error, ignore and/or log something, bay go to loop */
|
/* error, ignore and/or log something, bay go to loop */
|
||||||
if (G.gotsig)
|
if (bb_got_signal)
|
||||||
conescape();
|
conescape();
|
||||||
else
|
else
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
@ -52,7 +52,6 @@ struct globals {
|
|||||||
struct pollfd pfd[1];
|
struct pollfd pfd[1];
|
||||||
unsigned stamplog;
|
unsigned stamplog;
|
||||||
smallint check; /* = 1; */
|
smallint check; /* = 1; */
|
||||||
smallint exitsoon;
|
|
||||||
smallint set_pgrp;
|
smallint set_pgrp;
|
||||||
};
|
};
|
||||||
#define G (*(struct globals*)&bb_common_bufsiz1)
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
||||||
@ -65,7 +64,6 @@ struct globals {
|
|||||||
#define pfd (G.pfd )
|
#define pfd (G.pfd )
|
||||||
#define stamplog (G.stamplog )
|
#define stamplog (G.stamplog )
|
||||||
#define check (G.check )
|
#define check (G.check )
|
||||||
#define exitsoon (G.exitsoon )
|
|
||||||
#define set_pgrp (G.set_pgrp )
|
#define set_pgrp (G.set_pgrp )
|
||||||
#define INIT_G() do { \
|
#define INIT_G() do { \
|
||||||
check = 1; \
|
check = 1; \
|
||||||
@ -89,15 +87,6 @@ static void warnx(const char *m1)
|
|||||||
warn3x(m1, "", "");
|
warn3x(m1, "", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void s_term(int sig_no UNUSED_PARAM)
|
|
||||||
{
|
|
||||||
exitsoon = 1;
|
|
||||||
}
|
|
||||||
static void s_hangup(int sig_no UNUSED_PARAM)
|
|
||||||
{
|
|
||||||
exitsoon = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void runsv(int no, const char *name)
|
static void runsv(int no, const char *name)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
@ -246,20 +235,11 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
|
|
||||||
INIT_G();
|
INIT_G();
|
||||||
|
|
||||||
argv++;
|
opt_complementary = "-1";
|
||||||
if (!*argv)
|
set_pgrp = getopt32(argv, "P");
|
||||||
bb_show_usage();
|
argv += optind;
|
||||||
if (argv[0][0] == '-') {
|
|
||||||
switch (argv[0][1]) {
|
|
||||||
case 'P': set_pgrp = 1;
|
|
||||||
case '-': ++argv;
|
|
||||||
}
|
|
||||||
if (!*argv)
|
|
||||||
bb_show_usage();
|
|
||||||
}
|
|
||||||
|
|
||||||
bb_signals_recursive(1 << SIGTERM, s_term);
|
bb_signals_recursive((1 << SIGTERM) | (1 << SIGHUP), record_signo);
|
||||||
bb_signals_recursive(1 << SIGHUP, s_hangup);
|
|
||||||
svdir = *argv++;
|
svdir = *argv++;
|
||||||
if (argv && *argv) {
|
if (argv && *argv) {
|
||||||
rplog = *argv;
|
rplog = *argv;
|
||||||
@ -346,14 +326,14 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (exitsoon) {
|
switch (bb_got_signal) {
|
||||||
case 1:
|
case SIGHUP:
|
||||||
_exit(EXIT_SUCCESS);
|
|
||||||
case 2:
|
|
||||||
for (i = 0; i < svnum; i++)
|
for (i = 0; i < svnum; i++)
|
||||||
if (sv[i].pid)
|
if (sv[i].pid)
|
||||||
kill(sv[i].pid, SIGTERM);
|
kill(sv[i].pid, SIGTERM);
|
||||||
_exit(111);
|
// N.B. fall through
|
||||||
|
case SIGTERM:
|
||||||
|
_exit((SIGHUP == bb_got_signal) ? 111 : EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* not reached */
|
/* not reached */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user