skill: reindent the file
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
3e90357803
commit
57f290dcb4
362
skill.c
362
skill.c
@ -55,15 +55,14 @@ static int program = -1;
|
|||||||
/*#define PROG_NICE 3*/ /* easy, but the old one isn't broken */
|
/*#define PROG_NICE 3*/ /* easy, but the old one isn't broken */
|
||||||
#define PROG_SNICE 4
|
#define PROG_SNICE 4
|
||||||
|
|
||||||
|
static void display_kill_version(void)
|
||||||
/********************************************************************/
|
{
|
||||||
|
|
||||||
static void display_kill_version(void){
|
|
||||||
fprintf(stdout, PROCPS_NG_VERSION);
|
fprintf(stdout, PROCPS_NG_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
/***** kill or nice a process */
|
/* kill or nice a process */
|
||||||
static void hurt_proc(int tty, int uid, int pid, const char *restrict const cmd){
|
static void hurt_proc(int tty, int uid, int pid, const char *restrict const cmd)
|
||||||
|
{
|
||||||
int failed;
|
int failed;
|
||||||
int saved_errno;
|
int saved_errno;
|
||||||
char dn_buf[1000];
|
char dn_buf[1000];
|
||||||
@ -71,31 +70,32 @@ static void hurt_proc(int tty, int uid, int pid, const char *restrict const cmd)
|
|||||||
if (i_flag) {
|
if (i_flag) {
|
||||||
char buf[8];
|
char buf[8];
|
||||||
fprintf(stderr, "%-8s %-8s %5d %-16.16s ? ",
|
fprintf(stderr, "%-8s %-8s %5d %-16.16s ? ",
|
||||||
(char*)dn_buf,user_from_uid(uid),pid,cmd
|
(char *)dn_buf, user_from_uid(uid), pid, cmd);
|
||||||
);
|
|
||||||
if (!fgets(buf, 7, stdin)) {
|
if (!fgets(buf, 7, stdin)) {
|
||||||
printf("\n");
|
printf("\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
if(*buf!='y' && *buf!='Y') return;
|
if (*buf != 'y' && *buf != 'Y')
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
/* do the actual work */
|
/* do the actual work */
|
||||||
if(program==PROG_SKILL) failed=kill(pid,sig_or_pri);
|
if (program == PROG_SKILL)
|
||||||
else failed=setpriority(PRIO_PROCESS,pid,sig_or_pri);
|
failed = kill(pid, sig_or_pri);
|
||||||
|
else
|
||||||
|
failed = setpriority(PRIO_PROCESS, pid, sig_or_pri);
|
||||||
saved_errno = errno;
|
saved_errno = errno;
|
||||||
if (w_flag && failed) {
|
if (w_flag && failed) {
|
||||||
fprintf(stderr, "%-8s %-8s %5d %-16.16s ",
|
fprintf(stderr, "%-8s %-8s %5d %-16.16s ",
|
||||||
(char*)dn_buf,user_from_uid(uid),pid,cmd
|
(char *)dn_buf, user_from_uid(uid), pid, cmd);
|
||||||
);
|
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
perror("");
|
perror("");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(i_flag) return;
|
if (i_flag)
|
||||||
|
return;
|
||||||
if (v_flag) {
|
if (v_flag) {
|
||||||
printf("%-8s %-8s %5d %-16.16s\n",
|
printf("%-8s %-8s %5d %-16.16s\n",
|
||||||
(char*)dn_buf,user_from_uid(uid),pid,cmd
|
(char *)dn_buf, user_from_uid(uid), pid, cmd);
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (n_flag) {
|
if (n_flag) {
|
||||||
@ -104,99 +104,127 @@ static void hurt_proc(int tty, int uid, int pid, const char *restrict const cmd)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* check one process */
|
||||||
/***** check one process */
|
static void check_proc(int pid)
|
||||||
static void check_proc(int pid){
|
{
|
||||||
char buf[128];
|
char buf[128];
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
int tty;
|
int tty;
|
||||||
int fd;
|
int fd;
|
||||||
int i;
|
int i;
|
||||||
if(pid==my_pid) return;
|
if (pid == my_pid)
|
||||||
sprintf(buf, "/proc/%d/stat", pid); /* pid (cmd) state ppid pgrp session tty */
|
return;
|
||||||
|
/* pid (cmd) state ppid pgrp session tty */
|
||||||
|
sprintf(buf, "/proc/%d/stat", pid);
|
||||||
fd = open(buf, O_RDONLY);
|
fd = open(buf, O_RDONLY);
|
||||||
if(fd==-1){ /* process exited maybe */
|
if (fd == -1) {
|
||||||
if(pids && w_flag) printf(_("WARNING: process %d could not be found.\n"),pid);
|
/* process exited maybe */
|
||||||
|
if (pids && w_flag)
|
||||||
|
printf(_("WARNING: process %d could not be found.\n"),
|
||||||
|
pid);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fstat(fd, &statbuf);
|
fstat(fd, &statbuf);
|
||||||
if(uids){ /* check the EUID */
|
if (uids) {
|
||||||
|
/* check the EUID */
|
||||||
i = uid_count;
|
i = uid_count;
|
||||||
while(i--) if(uids[i]==statbuf.st_uid) break;
|
while (i--)
|
||||||
if(i==-1) goto closure;
|
if (uids[i] == statbuf.st_uid)
|
||||||
|
break;
|
||||||
|
if (i == -1)
|
||||||
|
goto closure;
|
||||||
}
|
}
|
||||||
read(fd, buf, 128);
|
read(fd, buf, 128);
|
||||||
buf[127] = '\0';
|
buf[127] = '\0';
|
||||||
tmp = strrchr(buf, ')');
|
tmp = strrchr(buf, ')');
|
||||||
*tmp++ = '\0';
|
*tmp++ = '\0';
|
||||||
i = 5; while(i--) while(*tmp++!=' '); /* scan to find tty */
|
i = 5;
|
||||||
|
while (i--)
|
||||||
|
while (*tmp++ != ' ')
|
||||||
|
/* scan to find tty */ ;
|
||||||
tty = atoi(tmp);
|
tty = atoi(tmp);
|
||||||
if (ttys) {
|
if (ttys) {
|
||||||
i = tty_count;
|
i = tty_count;
|
||||||
while(i--) if(ttys[i]==tty) break;
|
while (i--)
|
||||||
if(i==-1) goto closure;
|
if (ttys[i] == tty)
|
||||||
|
break;
|
||||||
|
if (i == -1)
|
||||||
|
goto closure;
|
||||||
}
|
}
|
||||||
tmp = strchr(buf, '(') + 1;
|
tmp = strchr(buf, '(') + 1;
|
||||||
if (cmds) {
|
if (cmds) {
|
||||||
i = cmd_count;
|
i = cmd_count;
|
||||||
/* fast comparison trick -- useful? */
|
/* fast comparison trick -- useful? */
|
||||||
while(i--) if(cmds[i][0]==*tmp && !strcmp(cmds[i],tmp)) break;
|
while (i--)
|
||||||
if(i==-1) goto closure;
|
if (cmds[i][0] == *tmp && !strcmp(cmds[i], tmp))
|
||||||
|
break;
|
||||||
|
if (i == -1)
|
||||||
|
goto closure;
|
||||||
}
|
}
|
||||||
/* This is where we kill/nice something. */
|
/* This is where we kill/nice something. */
|
||||||
/* fprintf(stderr, "PID %d, UID %d, TTY %d,%d, COMM %s\n",
|
/* for debugging purposes?
|
||||||
pid, statbuf.st_uid, tty>>8, tty&0xf, tmp
|
fprintf(stderr, "PID %d, UID %d, TTY %d,%d, COMM %s\n",
|
||||||
);
|
pid, statbuf.st_uid, tty >> 8, tty & 0xf, tmp);
|
||||||
*/
|
*/
|
||||||
hurt_proc(tty, statbuf.st_uid, pid, tmp);
|
hurt_proc(tty, statbuf.st_uid, pid, tmp);
|
||||||
closure:
|
closure:
|
||||||
close(fd); /* kill/nice _first_ to avoid PID reuse */
|
/* kill/nice _first_ to avoid PID reuse */
|
||||||
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* debug function */
|
||||||
/***** debug function */
|
|
||||||
#if 0
|
#if 0
|
||||||
static void show_lists(void){
|
static void show_lists(void)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
fprintf(stderr, _("%d TTY: "), tty_count);
|
fprintf(stderr, _("%d TTY: "), tty_count);
|
||||||
if (ttys) {
|
if (ttys) {
|
||||||
i = tty_count;
|
i = tty_count;
|
||||||
while (i--) {
|
while (i--) {
|
||||||
fprintf(stderr, "%d,%d%c", (ttys[i]>>8)&0xff, ttys[i]&0xff, i?' ':'\n');
|
fprintf(stderr, "%d,%d%c", (ttys[i] >> 8) & 0xff,
|
||||||
|
ttys[i] & 0xff, i ? ' ' : '\n');
|
||||||
}
|
}
|
||||||
}else fprintf(stderr, "\n");
|
} else
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
|
||||||
fprintf(stderr, _("%d UID: "), uid_count);
|
fprintf(stderr, _("%d UID: "), uid_count);
|
||||||
if (uids) {
|
if (uids) {
|
||||||
i = uid_count;
|
i = uid_count;
|
||||||
while(i--) fprintf(stderr, "%d%c", uids[i], i?' ':'\n');
|
while (i--)
|
||||||
}else fprintf(stderr, "\n");
|
fprintf(stderr, "%d%c", uids[i], i ? ' ' : '\n');
|
||||||
|
} else
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
|
||||||
fprintf(stderr, _("%d PID: "), pid_count);
|
fprintf(stderr, _("%d PID: "), pid_count);
|
||||||
if (pids) {
|
if (pids) {
|
||||||
i = pid_count;
|
i = pid_count;
|
||||||
while(i--) fprintf(stderr, "%d%c", pids[i], i?' ':'\n');
|
while (i--)
|
||||||
}else fprintf(stderr, "\n");
|
fprintf(stderr, "%d%c", pids[i], i ? ' ' : '\n');
|
||||||
|
} else
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
|
||||||
fprintf(stderr, _("%d CMD: "), cmd_count);
|
fprintf(stderr, _("%d CMD: "), cmd_count);
|
||||||
if (cmds) {
|
if (cmds) {
|
||||||
i = cmd_count;
|
i = cmd_count;
|
||||||
while(i--) fprintf(stderr, "%s%c", cmds[i], i?' ':'\n');
|
while (i--)
|
||||||
}else fprintf(stderr, "\n");
|
fprintf(stderr, "%s%c", cmds[i], i ? ' ' : '\n');
|
||||||
|
} else
|
||||||
|
fprintf(stderr, "\n");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* iterate over all PIDs */
|
||||||
/***** iterate over all PIDs */
|
static void iterate(void)
|
||||||
static void iterate(void){
|
{
|
||||||
int pid;
|
int pid;
|
||||||
DIR *d;
|
DIR *d;
|
||||||
struct dirent *de;
|
struct dirent *de;
|
||||||
if (pids) {
|
if (pids) {
|
||||||
pid = pid_count;
|
pid = pid_count;
|
||||||
while(pid--) check_proc(pids[pid]);
|
while (pid--)
|
||||||
|
check_proc(pids[pid]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#if 0
|
#if 0
|
||||||
@ -210,15 +238,18 @@ static void iterate(void){
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
while ((de = readdir(d))) {
|
while ((de = readdir(d))) {
|
||||||
if(de->d_name[0] > '9') continue;
|
if (de->d_name[0] > '9')
|
||||||
if(de->d_name[0] < '1') continue;
|
continue;
|
||||||
|
if (de->d_name[0] < '1')
|
||||||
|
continue;
|
||||||
pid = atoi(de->d_name);
|
pid = atoi(de->d_name);
|
||||||
if(pid) check_proc(pid);
|
if (pid)
|
||||||
|
check_proc(pid);
|
||||||
}
|
}
|
||||||
closedir(d);
|
closedir(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
/***** kill help */
|
/* kill help */
|
||||||
static void __attribute__ ((__noreturn__)) kill_usage(void)
|
static void __attribute__ ((__noreturn__)) kill_usage(void)
|
||||||
{
|
{
|
||||||
fputs(USAGE_HEADER, stderr);
|
fputs(USAGE_HEADER, stderr);
|
||||||
@ -236,13 +267,16 @@ static void __attribute__ ((__noreturn__)) kill_usage(void)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/***** kill */
|
/* kill */
|
||||||
static void kill_main(int argc, const char *restrict const *restrict argv) NORETURN;
|
static void kill_main(int argc,
|
||||||
static void kill_main(int argc, const char *restrict const *restrict argv){
|
const char *restrict const *restrict argv) NORETURN;
|
||||||
|
static void kill_main(int argc, const char *restrict const *restrict argv)
|
||||||
|
{
|
||||||
const char *sigptr;
|
const char *sigptr;
|
||||||
int signo = SIGTERM;
|
int signo = SIGTERM;
|
||||||
int exitvalue = 0;
|
int exitvalue = 0;
|
||||||
if(argc<2) kill_usage();
|
if (argc < 2)
|
||||||
|
kill_usage();
|
||||||
if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
|
if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
|
||||||
display_kill_version();
|
display_kill_version();
|
||||||
exit(0);
|
exit(0);
|
||||||
@ -260,7 +294,8 @@ static void kill_main(int argc, const char *restrict const *restrict argv){
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
/* at this point, argc must be 3 or more */
|
/* at this point, argc must be 3 or more */
|
||||||
if(argc>128 || argv[2][0] == '-') kill_usage();
|
if (argc > 128 || argv[2][0] == '-')
|
||||||
|
kill_usage();
|
||||||
exit(print_given_signals(argc - 2, argv + 2, 80));
|
exit(print_given_signals(argc - 2, argv + 2, 80));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,7 +312,8 @@ static void kill_main(int argc, const char *restrict const *restrict argv){
|
|||||||
argc -= 2;
|
argc -= 2;
|
||||||
goto no_more_args;
|
goto no_more_args;
|
||||||
}
|
}
|
||||||
if(argv[1][1]=='-') kill_usage(); /* likely --help */
|
if (argv[1][1] == '-')
|
||||||
|
kill_usage(); /* likely --help */
|
||||||
// FIXME: "kill -sWINCH $$" not handled
|
// FIXME: "kill -sWINCH $$" not handled
|
||||||
if (argv[1][2] == '\0' && (argv[1][1] == 's' || argv[1][1] == 'n')) {
|
if (argv[1][2] == '\0' && (argv[1][1] == 's' || argv[1][1] == 'n')) {
|
||||||
sigptr = argv[2];
|
sigptr = argv[2];
|
||||||
@ -290,36 +326,45 @@ static void kill_main(int argc, const char *restrict const *restrict argv){
|
|||||||
}
|
}
|
||||||
signo = signal_name_to_number(sigptr);
|
signo = signal_name_to_number(sigptr);
|
||||||
if (signo < 0) {
|
if (signo < 0) {
|
||||||
fprintf(stderr, _("ERROR: unknown signal name \"%s\".\n"), sigptr);
|
fprintf(stderr, _("ERROR: unknown signal name \"%s\".\n"),
|
||||||
|
sigptr);
|
||||||
kill_usage();
|
kill_usage();
|
||||||
}
|
}
|
||||||
no_more_args:
|
no_more_args:
|
||||||
if(!argc) kill_usage(); /* nothing to kill? */
|
if (!argc)
|
||||||
|
kill_usage(); /* nothing to kill? */
|
||||||
while (argc--) {
|
while (argc--) {
|
||||||
long pid;
|
long pid;
|
||||||
char *endp;
|
char *endp;
|
||||||
pid = strtol(argv[argc], &endp, 10);
|
pid = strtol(argv[argc], &endp, 10);
|
||||||
if (!*endp && (endp != argv[argc])) {
|
if (!*endp && (endp != argv[argc])) {
|
||||||
if(!kill((pid_t)pid,signo)) continue;
|
if (!kill((pid_t) pid, signo))
|
||||||
// The UNIX standard contradicts itself. If at least one process
|
continue;
|
||||||
// is matched for each PID (as if processes could share PID!) and
|
/*
|
||||||
// "the specified signal was successfully processed" (the systcall
|
* The UNIX standard contradicts itself. If at least
|
||||||
// returned zero?) for at least one of those processes, then we must
|
* one process is matched for each PID (as if
|
||||||
// exit with zero. Note that an error might have also occured.
|
* processes could share PID!) and "the specified
|
||||||
// The standard says we return non-zero if an error occurs. Thus if
|
* signal was successfully processed" (the systcall
|
||||||
// killing two processes gives 0 for one and EPERM for the other,
|
* returned zero?) for at least one of those
|
||||||
// we are required to return both zero and non-zero. Quantum kill???
|
* processes, then we must exit with zero. Note that
|
||||||
|
* an error might have also occured. The standard
|
||||||
|
* says we return non-zero if an error occurs. Thus
|
||||||
|
* if killing two processes gives 0 for one and EPERM
|
||||||
|
* for the other, we are required to return both zero
|
||||||
|
* and non-zero. Quantum kill???
|
||||||
|
*/
|
||||||
perror("kill");
|
perror("kill");
|
||||||
exitvalue = 1;
|
exitvalue = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fprintf(stderr, _("ERROR: garbage process ID \"%s\".\n"), argv[argc]);
|
fprintf(stderr, _("ERROR: garbage process ID \"%s\".\n"),
|
||||||
|
argv[argc]);
|
||||||
kill_usage();
|
kill_usage();
|
||||||
}
|
}
|
||||||
exit(exitvalue);
|
exit(exitvalue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/***** skill/snice help */
|
/* skill/snice help */
|
||||||
static void __attribute__ ((__noreturn__)) skillsnice_usage(void)
|
static void __attribute__ ((__noreturn__)) skillsnice_usage(void)
|
||||||
{
|
{
|
||||||
fputs(USAGE_HEADER, stderr);
|
fputs(USAGE_HEADER, stderr);
|
||||||
@ -336,14 +381,17 @@ static void __attribute__ ((__noreturn__)) skillsnice_usage(void)
|
|||||||
fputs(_(" -f fast mode (not implemented)\n"), stderr);
|
fputs(_(" -f fast mode (not implemented)\n"), stderr);
|
||||||
fputs(_(" -i interactive\n"), stderr);
|
fputs(_(" -i interactive\n"), stderr);
|
||||||
fputs(_(" -l list all signal names\n"), stderr);
|
fputs(_(" -l list all signal names\n"), stderr);
|
||||||
fputs(_(" -L list all signal names in a nice table\n"), stderr);
|
fputs(_(" -L list all signal names in a nice table\n"),
|
||||||
|
stderr);
|
||||||
fputs(_(" -n no action\n"), stderr);
|
fputs(_(" -n no action\n"), stderr);
|
||||||
fputs(_(" -v explain what is being done\n"), stderr);
|
fputs(_(" -v explain what is being done\n"), stderr);
|
||||||
fputs(_(" -w enable warnings (not implemented)\n"), stderr);
|
fputs(_(" -w enable warnings (not implemented)\n"), stderr);
|
||||||
fputs(USAGE_VERSION, stderr);
|
fputs(USAGE_VERSION, stderr);
|
||||||
fputs(_("\n"), stderr);
|
fputs(_("\n"), stderr);
|
||||||
fputs(_("Expression can be: terminal, user, pid, command.\n"), stderr);
|
fputs(_("Expression can be: terminal, user, pid, command.\n"), stderr);
|
||||||
fputs(_("The options below may be used to ensure correct interpretation.\n"), stderr);
|
fputs(_
|
||||||
|
("The options below may be used to ensure correct interpretation.\n"),
|
||||||
|
stderr);
|
||||||
fputs(_(" -c <command> expression is a command name\n"), stderr);
|
fputs(_(" -c <command> expression is a command name\n"), stderr);
|
||||||
fputs(_(" -p <pid> expression is a process id number\n"), stderr);
|
fputs(_(" -p <pid> expression is a process id number\n"), stderr);
|
||||||
fputs(_(" -t <tty> expression is a terminal\n"), stderr);
|
fputs(_(" -t <tty> expression is a terminal\n"), stderr);
|
||||||
@ -367,24 +415,29 @@ static void __attribute__ ((__noreturn__)) skillsnice_usage(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
static void _skillsnice_usage(int line){
|
static void _skillsnice_usage(int line)
|
||||||
|
{
|
||||||
fprintf(stderr, _("Something at line %d.\n"), line);
|
fprintf(stderr, _("Something at line %d.\n"), line);
|
||||||
skillsnice_usage();
|
skillsnice_usage();
|
||||||
}
|
}
|
||||||
|
|
||||||
#define skillsnice_usage() _skillsnice_usage(__LINE__)
|
#define skillsnice_usage() _skillsnice_usage(__LINE__)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define NEXTARG (argc?( argc--, ((argptr=*++argv)) ):NULL)
|
#define NEXTARG (argc?( argc--, ((argptr=*++argv)) ):NULL)
|
||||||
|
|
||||||
/***** common skill/snice argument parsing code */
|
/* common skill/snice argument parsing code */
|
||||||
#define NO_PRI_VAL ((int)0xdeafbeef)
|
#define NO_PRI_VAL ((int)0xdeafbeef)
|
||||||
static void skillsnice_parse(int argc, const char *restrict const *restrict argv){
|
static void skillsnice_parse(int argc,
|
||||||
|
const char *restrict const *restrict argv)
|
||||||
|
{
|
||||||
int signo = -1;
|
int signo = -1;
|
||||||
int prino = NO_PRI_VAL;
|
int prino = NO_PRI_VAL;
|
||||||
int force = 0;
|
int force = 0;
|
||||||
int num_found = 0;
|
int num_found = 0;
|
||||||
const char *restrict argptr;
|
const char *restrict argptr;
|
||||||
if(argc<2) skillsnice_usage();
|
if (argc < 2)
|
||||||
|
skillsnice_usage();
|
||||||
if (argc == 2 && argv[1][0] == '-') {
|
if (argc == 2 && argv[1][0] == '-') {
|
||||||
if (!strcmp(argv[1], "-L")) {
|
if (!strcmp(argv[1], "-L")) {
|
||||||
pretty_print_signals();
|
pretty_print_signals();
|
||||||
@ -403,15 +456,19 @@ static void skillsnice_parse(int argc, const char *restrict const *restrict argv
|
|||||||
NEXTARG;
|
NEXTARG;
|
||||||
/* Time for serious parsing. What does "skill -int 123 456" mean? */
|
/* Time for serious parsing. What does "skill -int 123 456" mean? */
|
||||||
while (argc) {
|
while (argc) {
|
||||||
if(force && !num_found){ /* if forced, _must_ find something */
|
if (force && !num_found) {
|
||||||
fprintf(stderr,_("ERROR: -%c used with bad data.\n"), force);
|
/* if forced, _must_ find something */
|
||||||
|
fprintf(stderr, _("ERROR: -%c used with bad data.\n"),
|
||||||
|
force);
|
||||||
skillsnice_usage();
|
skillsnice_usage();
|
||||||
}
|
}
|
||||||
force = 0;
|
force = 0;
|
||||||
if (program == PROG_SKILL && signo < 0 && *argptr == '-') {
|
if (program == PROG_SKILL && signo < 0 && *argptr == '-') {
|
||||||
signo = signal_name_to_number(argptr + 1);
|
signo = signal_name_to_number(argptr + 1);
|
||||||
if(signo>=0){ /* found a signal */
|
if (signo >= 0) {
|
||||||
if(!NEXTARG) break;
|
/* found a signal */
|
||||||
|
if (!NEXTARG)
|
||||||
|
break;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -422,7 +479,8 @@ static void skillsnice_parse(int argc, const char *restrict const *restrict argv
|
|||||||
val = strtol(argptr, &endp, 10);
|
val = strtol(argptr, &endp, 10);
|
||||||
if (!*endp && val <= 999 && val >= -999) {
|
if (!*endp && val <= 999 && val >= -999) {
|
||||||
prino = val;
|
prino = val;
|
||||||
if(!NEXTARG) break;
|
if (!NEXTARG)
|
||||||
|
break;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -431,62 +489,90 @@ static void skillsnice_parse(int argc, const char *restrict const *restrict argv
|
|||||||
argptr++;
|
argptr++;
|
||||||
do {
|
do {
|
||||||
switch ((force = *argptr++)) {
|
switch ((force = *argptr++)) {
|
||||||
default: skillsnice_usage();
|
default:
|
||||||
|
skillsnice_usage();
|
||||||
case 't':
|
case 't':
|
||||||
case 'u':
|
case 'u':
|
||||||
case 'p':
|
case 'p':
|
||||||
case 'c':
|
case 'c':
|
||||||
if(!*argptr){ /* nothing left here, *argptr is '\0' */
|
if (!*argptr) {
|
||||||
|
/* nothing left here, *argptr is '\0' */
|
||||||
if (!NEXTARG) {
|
if (!NEXTARG) {
|
||||||
fprintf(stderr,_("ERROR: -%c with nothing after it.\n"), force);
|
fprintf(stderr,
|
||||||
|
_
|
||||||
|
("ERROR: -%c with nothing after it.\n"),
|
||||||
|
force);
|
||||||
skillsnice_usage();
|
skillsnice_usage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
goto selection_collection;
|
goto selection_collection;
|
||||||
case 'f': f_flag++; break;
|
case 'f':
|
||||||
case 'i': i_flag++; break;
|
f_flag++;
|
||||||
case 'v': v_flag++; break;
|
break;
|
||||||
case 'w': w_flag++; break;
|
case 'i':
|
||||||
case 'n': n_flag++; break;
|
i_flag++;
|
||||||
|
break;
|
||||||
|
case 'v':
|
||||||
|
v_flag++;
|
||||||
|
break;
|
||||||
|
case 'w':
|
||||||
|
w_flag++;
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
n_flag++;
|
||||||
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
NEXTARG;
|
NEXTARG;
|
||||||
/*
|
/*
|
||||||
* If no more arguments, all the "if(argc)..." tests will fail
|
* If no more arguments, all the
|
||||||
* and the big loop will exit.
|
* "if(argc)..." tests will fail and
|
||||||
|
* the big loop will exit.
|
||||||
*/
|
*/
|
||||||
} /* END OF SWITCH */
|
} /* END OF SWITCH */
|
||||||
} while (force);
|
} while (force);
|
||||||
} /* END OF IF */
|
} /* END OF IF */
|
||||||
selection_collection:
|
selection_collection:
|
||||||
num_found = 0; /* we should find at least one thing */
|
num_found = 0; /* we should find at least one thing */
|
||||||
switch(force){ /* fall through each data type */
|
switch (force) {
|
||||||
default: skillsnice_usage();
|
/* fall through each data type */
|
||||||
|
default:
|
||||||
|
skillsnice_usage();
|
||||||
case 0: /* not forced */
|
case 0: /* not forced */
|
||||||
if (argptr && argptr[0] == '-') /* its the next argument not a parameter */
|
if (argptr && argptr[0] == '-')
|
||||||
|
/* its the next argument not a parameter */
|
||||||
continue;
|
continue;
|
||||||
case 't':
|
case 't':
|
||||||
if (argc) {
|
if (argc) {
|
||||||
struct stat sbuf;
|
struct stat sbuf;
|
||||||
char path[32];
|
char path[32];
|
||||||
if(!argptr) skillsnice_usage(); /* Huh? Maybe "skill -t ''". */
|
if (!argptr)
|
||||||
|
/* Huh? Maybe "skill -t ''". */
|
||||||
|
skillsnice_usage();
|
||||||
snprintf(path, 32, "/dev/%s", argptr);
|
snprintf(path, 32, "/dev/%s", argptr);
|
||||||
if(stat(path, &sbuf)>=0 && S_ISCHR(sbuf.st_mode)){
|
if (stat(path, &sbuf) >= 0
|
||||||
|
&& S_ISCHR(sbuf.st_mode)) {
|
||||||
num_found++;
|
num_found++;
|
||||||
ENLIST(tty, sbuf.st_rdev);
|
ENLIST(tty, sbuf.st_rdev);
|
||||||
if(!NEXTARG) break;
|
if (!NEXTARG)
|
||||||
}else if(!(argptr[1])){ /* if only 1 character */
|
break;
|
||||||
|
} else if (!(argptr[1])) {
|
||||||
|
/* if only 1 character */
|
||||||
switch (*argptr) {
|
switch (*argptr) {
|
||||||
default:
|
default:
|
||||||
if(stat(argptr,&sbuf)<0) break; /* the shell eats '?' */
|
if (stat(argptr, &sbuf) < 0)
|
||||||
|
/* the shell eats '?' */
|
||||||
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
case '?':
|
case '?':
|
||||||
num_found++;
|
num_found++;
|
||||||
ENLIST(tty, 0);
|
ENLIST(tty, 0);
|
||||||
if(!NEXTARG) break;
|
if (!NEXTARG)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(force) continue;
|
if (force)
|
||||||
|
continue;
|
||||||
case 'u':
|
case 'u':
|
||||||
if (argc) {
|
if (argc) {
|
||||||
struct passwd *passwd_data;
|
struct passwd *passwd_data;
|
||||||
@ -494,10 +580,12 @@ selection_collection:
|
|||||||
if (passwd_data) {
|
if (passwd_data) {
|
||||||
num_found++;
|
num_found++;
|
||||||
ENLIST(uid, passwd_data->pw_uid);
|
ENLIST(uid, passwd_data->pw_uid);
|
||||||
if(!NEXTARG) break;
|
if (!NEXTARG)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(force) continue;
|
if (force)
|
||||||
|
continue;
|
||||||
case 'p':
|
case 'p':
|
||||||
if (argc && *argptr >= '0' && *argptr <= '9') {
|
if (argc && *argptr >= '0' && *argptr <= '9') {
|
||||||
char *endp;
|
char *endp;
|
||||||
@ -506,16 +594,21 @@ selection_collection:
|
|||||||
if (*endp == '\0') {
|
if (*endp == '\0') {
|
||||||
num_found++;
|
num_found++;
|
||||||
ENLIST(pid, num);
|
ENLIST(pid, num);
|
||||||
if(!NEXTARG) break;
|
if (!NEXTARG)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(force) continue;
|
if (force)
|
||||||
if(num_found) continue; /* could still be an option */
|
continue;
|
||||||
|
if (num_found)
|
||||||
|
/* could still be an option */
|
||||||
|
continue;
|
||||||
case 'c':
|
case 'c':
|
||||||
if (argc) {
|
if (argc) {
|
||||||
num_found++;
|
num_found++;
|
||||||
ENLIST(cmd, argptr);
|
ENLIST(cmd, argptr);
|
||||||
if(!NEXTARG) break;
|
if (!NEXTARG)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} /* END OF SWITCH */
|
} /* END OF SWITCH */
|
||||||
} /* END OF WHILE */
|
} /* END OF WHILE */
|
||||||
@ -525,30 +618,39 @@ selection_collection:
|
|||||||
skillsnice_usage();
|
skillsnice_usage();
|
||||||
}
|
}
|
||||||
if ((f_flag | i_flag | v_flag | w_flag | n_flag) & ~1) {
|
if ((f_flag | i_flag | v_flag | w_flag | n_flag) & ~1) {
|
||||||
fprintf(stderr,_("ERROR: general flags may not be repeated.\n"));
|
fprintf(stderr,
|
||||||
|
_("ERROR: general flags may not be repeated.\n"));
|
||||||
skillsnice_usage();
|
skillsnice_usage();
|
||||||
}
|
}
|
||||||
if (i_flag && (v_flag | f_flag | n_flag)) {
|
if (i_flag && (v_flag | f_flag | n_flag)) {
|
||||||
fprintf(stderr,_("ERROR: -i makes no sense with -v, -f, and -n.\n"));
|
fprintf(stderr,
|
||||||
|
_("ERROR: -i makes no sense with -v, -f, and -n.\n"));
|
||||||
skillsnice_usage();
|
skillsnice_usage();
|
||||||
}
|
}
|
||||||
if (v_flag && (i_flag | f_flag)) {
|
if (v_flag && (i_flag | f_flag)) {
|
||||||
fprintf(stderr,_("ERROR: -v makes no sense with -i and -f.\n"));
|
fprintf(stderr,
|
||||||
|
_("ERROR: -v makes no sense with -i and -f.\n"));
|
||||||
skillsnice_usage();
|
skillsnice_usage();
|
||||||
}
|
}
|
||||||
/* OK, set up defaults */
|
/* OK, set up defaults */
|
||||||
if(prino==NO_PRI_VAL) prino=4;
|
if (prino == NO_PRI_VAL)
|
||||||
if(signo<0) signo=SIGTERM;
|
prino = 4;
|
||||||
|
if (signo < 0)
|
||||||
|
signo = SIGTERM;
|
||||||
if (n_flag) {
|
if (n_flag) {
|
||||||
program = PROG_SKILL;
|
program = PROG_SKILL;
|
||||||
signo=0; /* harmless */
|
/* harmless */
|
||||||
|
signo = 0;
|
||||||
}
|
}
|
||||||
if(program==PROG_SKILL) sig_or_pri = signo;
|
if (program == PROG_SKILL)
|
||||||
else sig_or_pri = prino;
|
sig_or_pri = signo;
|
||||||
|
else
|
||||||
|
sig_or_pri = prino;
|
||||||
}
|
}
|
||||||
|
|
||||||
/***** main body */
|
/* main body */
|
||||||
int main(int argc, const char *argv[]){
|
int main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
const char *tmpstr;
|
const char *tmpstr;
|
||||||
my_pid = getpid();
|
my_pid = getpid();
|
||||||
saved_argc = argc;
|
saved_argc = argc;
|
||||||
@ -557,18 +659,24 @@ int main(int argc, const char *argv[]){
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
tmpstr = strrchr(*argv, '/');
|
tmpstr = strrchr(*argv, '/');
|
||||||
if(tmpstr) tmpstr++;
|
if (tmpstr)
|
||||||
if(!tmpstr) tmpstr=*argv;
|
tmpstr++;
|
||||||
if(strstr(tmpstr,"kill")) program = PROG_KILL;
|
if (!tmpstr)
|
||||||
if(strstr(tmpstr,"skill")) program = PROG_SKILL;
|
tmpstr = *argv;
|
||||||
if(strstr(tmpstr,"snice")) program = PROG_SNICE;
|
if (strstr(tmpstr, "kill"))
|
||||||
|
program = PROG_KILL;
|
||||||
|
if (strstr(tmpstr, "skill"))
|
||||||
|
program = PROG_SKILL;
|
||||||
|
if (strstr(tmpstr, "snice"))
|
||||||
|
program = PROG_SNICE;
|
||||||
switch (program) {
|
switch (program) {
|
||||||
case PROG_SNICE:
|
case PROG_SNICE:
|
||||||
case PROG_SKILL:
|
case PROG_SKILL:
|
||||||
setpriority(PRIO_PROCESS, my_pid, -20);
|
setpriority(PRIO_PROCESS, my_pid, -20);
|
||||||
skillsnice_parse(argc, argv);
|
skillsnice_parse(argc, argv);
|
||||||
/* show_lists(); */
|
/* show_lists(); */
|
||||||
iterate(); /* this is it, go get them */
|
/* this is it, go get them */
|
||||||
|
iterate();
|
||||||
break;
|
break;
|
||||||
case PROG_KILL:
|
case PROG_KILL:
|
||||||
kill_main(argc, argv);
|
kill_main(argc, argv);
|
||||||
@ -578,5 +686,3 @@ int main(int argc, const char *argv[]){
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user