Implement basic pkill plumbing, and fix FORCE_FLAGS on flag context switch.

This commit is contained in:
Rob Landley 2016-01-18 15:45:56 -06:00
parent ba86864699
commit 36b7cb7c3b
2 changed files with 40 additions and 7 deletions

View File

@ -116,8 +116,9 @@ int main(int argc, char *argv[])
// See "intentionally crappy", above. // See "intentionally crappy", above.
if (!(out = outbuf)) return 1; if (!(out = outbuf)) return 1;
printf("#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n#define FORCED_FLAGLL 1LL\n" printf("#undef FORCED_FLAG\n#undef FORCED_FLAGLL\n"
"#else\n#define FORCED_FLAG 0\n#define FORCED_FLAGLL 0\n#endif\n\n"); "#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n#define FORCED_FLAGLL 1LL\n"
"#else\n#define FORCED_FLAG 0\n#define FORCED_FLAGLL 0\n#endif\n\n");
for (;;) { for (;;) {
struct flag *flist, *aflist, *offlist; struct flag *flist, *aflist, *offlist;

View File

@ -40,7 +40,7 @@ USE_PS(NEWTOY(ps, "k(sort)*P(ppid)*aAdeflno*p(pid)*s*t*u*U*g*G*wZ[!ol][+Ae]", TO
USE_TTOP(NEWTOY(ttop, ">0d#=3n#<1mb", TOYFLAG_USR|TOYFLAG_BIN)) USE_TTOP(NEWTOY(ttop, ">0d#=3n#<1mb", TOYFLAG_USR|TOYFLAG_BIN))
USE_IOTOP(NEWTOY(iotop, "Aabkoqp*u*d#n#", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT|TOYFLAG_LOCALE)) USE_IOTOP(NEWTOY(iotop, "Aabkoqp*u*d#n#", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT|TOYFLAG_LOCALE))
USE_PGREP(NEWTOY(pgrep, "?cld:u*U*t*s*P*g*G*fnovxL:", TOYFLAG_USR|TOYFLAG_BIN)) USE_PGREP(NEWTOY(pgrep, "?cld:u*U*t*s*P*g*G*fnovxL:", TOYFLAG_USR|TOYFLAG_BIN))
USE_PKILL(NEWTOY(pkill, "u*U*t*s*P*g*G*fnovxl:", TOYFLAG_USR|TOYFLAG_BIN)) USE_PKILL(NEWTOY(pkill, "Vu*U*t*s*P*g*G*fnovxl:", TOYFLAG_USR|TOYFLAG_BIN))
config PS config PS
bool "ps" bool "ps"
@ -206,11 +206,12 @@ config PGKILL_COMMON
config PKILL config PKILL
bool "pkill" bool "pkill"
default n default y
help help
usage: pkill [-l SIGNAL] [PATTERN] usage: pkill [-l SIGNAL] [PATTERN]
-l SIGNAL to send -l SIGNAL to send
-V verbose
*/ */
#define FOR_ps #define FOR_ps
@ -253,6 +254,7 @@ GLOBALS(
void *regexes; void *regexes;
int signal; int signal;
pid_t self;
} pgrep; } pgrep;
}; };
@ -1256,6 +1258,10 @@ void iotop_main(void)
if (!(toys.optflags&FLAG_b)) tty_reset(); if (!(toys.optflags&FLAG_b)) tty_reset();
} }
// pkill's plumbing wrap's pgrep's and thus mostly takes place in pgrep's flag
// context, so force pgrep's flags on even when building pkill standalone.
// (All the pgrep/pkill functions drop out when building ps standalone.)
#define FORCE_FLAGS
#define CLEANUP_iotop #define CLEANUP_iotop
#define FOR_pgrep #define FOR_pgrep
#include "generated/flags.h" #include "generated/flags.h"
@ -1271,6 +1277,9 @@ static void show_pgrep(struct carveup *tb)
struct regex_list *reg; struct regex_list *reg;
char *name = tb->str; char *name = tb->str;
// Never match ourselves.
if (TT.pgrep.self == *tb->slot) return;
if (toys.optflags&FLAG_f) name += tb->offset[4]; if (toys.optflags&FLAG_f) name += tb->offset[4];
if (TT.pgrep.regexes) { if (TT.pgrep.regexes) {
@ -1283,7 +1292,17 @@ static void show_pgrep(struct carveup *tb)
if (!reg) return; if (!reg) return;
} }
printf("%lld%s", *tb->slot, TT.pgrep.d ? TT.pgrep.d : "\n"); if (TT.pgrep.signal) {
if (kill(*tb->slot, TT.pgrep.signal)) {
char *s = num_to_sig(TT.pgrep.signal);
if (!s) sprintf(s = toybuf, "%d", TT.pgrep.signal);
perror_msg("%s->%lld", s, *tb->slot);
}
}
if (!TT.pgrep.signal || TT.tty)
printf("%lld%s", *tb->slot, TT.pgrep.d ? TT.pgrep.d : "\n");
} }
void pgrep_main(void) void pgrep_main(void)
@ -1291,6 +1310,12 @@ void pgrep_main(void)
char **arg; char **arg;
struct regex_list *reg; struct regex_list *reg;
TT.pgrep.self = getpid();
// No signal names start with "L", so no need for "L: " parsing.
if (TT.pgrep.L && 1>(TT.pgrep.signal = sig_to_num(TT.pgrep.L)))
error_exit("bad -L '%s'", TT.pgrep.L);
comma_args(TT.pgrep.G, &TT.GG, "bad -G", parse_rest); comma_args(TT.pgrep.G, &TT.GG, "bad -G", parse_rest);
comma_args(TT.pgrep.g, &TT.gg, "bad -g", parse_rest); comma_args(TT.pgrep.g, &TT.gg, "bad -g", parse_rest);
comma_args(TT.pgrep.P, &TT.PP, "bad -P", parse_rest); comma_args(TT.pgrep.P, &TT.PP, "bad -P", parse_rest);
@ -1303,8 +1328,10 @@ void pgrep_main(void)
!(toys.optflags&(FLAG_G|FLAG_g|FLAG_P|FLAG_s|FLAG_t|FLAG_U|FLAG_u))) !(toys.optflags&(FLAG_G|FLAG_g|FLAG_P|FLAG_s|FLAG_t|FLAG_U|FLAG_u)))
if (!toys.optc) help_exit("No PATTERN"); if (!toys.optc) help_exit("No PATTERN");
if (toys.optflags&FLAG_f) TT.bits |= _PS_CMDLINE; printf("f=%llx in %llx\n", (long long)FLAG_v, (long long)toys.optflags);
if (toys.optflags&FLAG_f) {TT.bits |= _PS_CMDLINE;
printf("got f\n");
}
for (arg = toys.optargs; *arg; arg++) { for (arg = toys.optargs; *arg; arg++) {
reg = xmalloc(sizeof(struct regex_list)); reg = xmalloc(sizeof(struct regex_list));
xregcomp(&reg->reg, *arg, REG_EXTENDED); xregcomp(&reg->reg, *arg, REG_EXTENDED);
@ -1318,8 +1345,13 @@ void pgrep_main(void)
if (TT.pgrep.d) xputc('\n'); if (TT.pgrep.d) xputc('\n');
} }
#define CLEANUP_pgrep
#define FOR_pkill
#include "generated/flags.h"
void pkill_main(void) void pkill_main(void)
{ {
if (!TT.pgrep.L) TT.pgrep.signal = SIGTERM; if (!TT.pgrep.L) TT.pgrep.signal = SIGTERM;
if (toys.optflags & FLAG_V) TT.tty = 1;
pgrep_main(); pgrep_main();
} }