1997-03-23 18:51:21 +00:00
|
|
|
/*
|
|
|
|
* Top users/processes display for Unix
|
2018-06-22 09:21:01 +00:00
|
|
|
* Version 3
|
1997-03-23 18:51:21 +00:00
|
|
|
*
|
|
|
|
* This program may be freely redistributed,
|
|
|
|
* but this entire comment MUST remain intact.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
|
|
|
|
* Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
|
2002-01-24 17:52:26 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
1997-03-23 18:51:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file contains the routines that implement some of the interactive
|
|
|
|
* mode commands. Note that some of the commands are implemented in-line
|
|
|
|
* in "main". This is necessary because they change the global state of
|
|
|
|
* "top" (i.e.: changing the number of processes to display).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/resource.h>
|
2018-06-04 04:59:32 +00:00
|
|
|
#include <sys/signal.h>
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2016-05-22 04:17:00 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
2018-06-02 03:25:15 +00:00
|
|
|
#include <stdbool.h>
|
2018-05-19 23:19:24 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2016-05-22 04:17:00 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "commands.h"
|
1998-07-06 20:40:57 +00:00
|
|
|
#include "top.h"
|
2016-05-22 04:17:00 +00:00
|
|
|
#include "machine.h"
|
1997-03-23 18:51:21 +00:00
|
|
|
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
static int err_compar(const void *p1, const void *p2);
|
|
|
|
|
|
|
|
struct errs /* structure for a system-call error */
|
|
|
|
{
|
|
|
|
int errnum; /* value of errno (that is, the actual error) */
|
|
|
|
char *arg; /* argument that caused the error */
|
|
|
|
};
|
|
|
|
|
2018-05-21 04:32:14 +00:00
|
|
|
static char *err_string(void);
|
2016-05-22 04:17:00 +00:00
|
|
|
static int str_adderr(char *str, int len, int err);
|
2018-06-03 02:58:53 +00:00
|
|
|
static int str_addarg(char *str, int len, char *arg, bool first);
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* show_help() - display the help screen; invoked in response to
|
|
|
|
* either 'h' or '?'.
|
|
|
|
*/
|
|
|
|
|
2018-06-12 06:53:31 +00:00
|
|
|
const struct command all_commands[] =
|
2018-06-10 08:59:57 +00:00
|
|
|
{
|
2018-06-12 06:53:31 +00:00
|
|
|
{'C', "toggle the displaying of weighted CPU percentage", false, CMD_wcputog},
|
|
|
|
{'d', "change number of displays to show", false, CMD_displays},
|
|
|
|
{'e', "list errors generated by last \"kill\" or \"renice\" command", false, CMD_errors},
|
|
|
|
{'H', "toggle the displaying of threads", false, CMD_thrtog},
|
|
|
|
{'h', "show this help text", true, CMD_help},
|
2018-06-13 08:52:06 +00:00
|
|
|
{'?', NULL, true, CMD_help},
|
2018-06-12 06:53:31 +00:00
|
|
|
{'i', "toggle the displaying of idle processes", false, CMD_idletog},
|
2018-06-13 08:52:06 +00:00
|
|
|
{'I', NULL, false, CMD_idletog},
|
2018-06-12 06:53:31 +00:00
|
|
|
{'j', "toggle the displaying of jail ID", false, CMD_jidtog},
|
|
|
|
{'J', "display processes for only one jail (+ selects all jails)", false, CMD_jail},
|
|
|
|
{'k', "kill processes; send a signal to a list of processes", false, CMD_kill},
|
|
|
|
{'q', "quit" , true, CMD_quit},
|
|
|
|
{'m', "toggle the display between 'cpu' and 'io' modes", false, CMD_viewtog},
|
|
|
|
{'n', "change number of processes to display", false, CMD_number},
|
2018-06-13 08:52:06 +00:00
|
|
|
{'#', NULL, false, CMD_number},
|
2018-06-12 06:53:31 +00:00
|
|
|
{'o', "specify the sort order", false, CMD_order},
|
|
|
|
{'p', "display one process (+ selects all processes)", false, CMD_pid},
|
|
|
|
{'P', "toggle the displaying of per-CPU statistics", false, CMD_pcputog},
|
|
|
|
{'r', "renice a process", false, CMD_renice},
|
|
|
|
{'s', "change number of seconds to delay between updates", false, CMD_delay},
|
|
|
|
{'S', "toggle the displaying of system processes", false, CMD_viewsys},
|
|
|
|
{'a', "toggle the displaying of process titles", false, CMD_showargs},
|
|
|
|
{'T', "toggle the displaying of thread IDs", false, CMD_toggletid},
|
|
|
|
{'t', "toggle the display of this process", false, CMD_selftog},
|
|
|
|
{'u', "display processes for only one user (+ selects all users)", false, CMD_user},
|
|
|
|
{'w', "toggle the display of swap use for each process", false, CMD_swaptog},
|
|
|
|
{'z', "toggle the displaying of the system idle process", false, CMD_kidletog},
|
|
|
|
{' ', "update the display", false, CMD_update},
|
|
|
|
{0, NULL, true, CMD_NONE}
|
2018-06-10 08:59:57 +00:00
|
|
|
};
|
|
|
|
|
2016-05-22 04:17:00 +00:00
|
|
|
void
|
2018-06-02 00:02:15 +00:00
|
|
|
show_help(void)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-06-13 08:52:06 +00:00
|
|
|
const struct command *curcmd, *nextcmd;
|
|
|
|
char keys[8] = "";
|
|
|
|
_Static_assert(sizeof(keys) >= sizeof("a or b"), "keys right size");
|
2018-06-10 08:59:57 +00:00
|
|
|
|
2018-05-19 23:04:42 +00:00
|
|
|
printf("Top version FreeBSD, %s\n", copyright);
|
2018-06-10 08:59:57 +00:00
|
|
|
curcmd = all_commands;
|
|
|
|
while (curcmd->c != 0) {
|
|
|
|
if (overstrike && !curcmd->available_to_dumb) {
|
|
|
|
++curcmd;
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-13 08:52:06 +00:00
|
|
|
if (curcmd->desc == NULL) {
|
|
|
|
/* we already printed this */
|
|
|
|
++curcmd;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nextcmd = curcmd + 1;
|
|
|
|
if (nextcmd->desc == NULL && nextcmd->c != '\0') {
|
|
|
|
sprintf(keys, "%c or %c", curcmd->c, nextcmd->c);
|
|
|
|
} else if (curcmd->c == ' '){
|
|
|
|
/* special case space rather than introducing a "display string" to
|
|
|
|
* the struct */
|
|
|
|
sprintf(keys, "SPC");
|
|
|
|
} else {
|
|
|
|
sprintf(keys, "%c", curcmd->c);
|
|
|
|
}
|
|
|
|
printf("%s\t- %s\n", keys, curcmd->desc);
|
2018-06-10 08:59:57 +00:00
|
|
|
++curcmd;
|
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
if (overstrike)
|
|
|
|
{
|
2004-07-08 16:45:55 +00:00
|
|
|
fputs("\
|
2018-06-10 08:59:57 +00:00
|
|
|
Other commands are also available, but this terminal is not\n\
|
|
|
|
sophisticated enough to handle those commands gracefully.\n", stdout);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Utility routines that help with some of the commands.
|
|
|
|
*/
|
|
|
|
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
static char *
|
|
|
|
next_field(char *str)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
if ((str = strchr(str, ' ')) == NULL)
|
|
|
|
{
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
*str = '\0';
|
|
|
|
while (*++str == ' ') /* loop */;
|
|
|
|
|
|
|
|
/* if there is nothing left of the string, return NULL */
|
|
|
|
/* This fix is dedicated to Greg Earle */
|
|
|
|
return(*str == '\0' ? NULL : str);
|
|
|
|
}
|
|
|
|
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
static int
|
2018-05-22 02:13:04 +00:00
|
|
|
scanint(char *str, int *intp)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
int val = 0;
|
|
|
|
char ch;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* if there is nothing left of the string, flag it as an error */
|
|
|
|
/* This fix is dedicated to Greg Earle */
|
|
|
|
if (*str == '\0')
|
|
|
|
{
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ch = *str++) != '\0')
|
|
|
|
{
|
|
|
|
if (isdigit(ch))
|
|
|
|
{
|
|
|
|
val = val * 10 + (ch - '0');
|
|
|
|
}
|
|
|
|
else if (isspace(ch))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*intp = val;
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some of the commands make system calls that could generate errors.
|
|
|
|
* These errors are collected up in an array of structures for later
|
|
|
|
* contemplation and display. Such routines return a string containing an
|
|
|
|
* error message, or NULL if no errors occurred. The next few routines are
|
|
|
|
* for manipulating and displaying these errors. We need an upper limit on
|
|
|
|
* the number of errors, so we arbitrarily choose 20.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ERRMAX 20
|
|
|
|
|
|
|
|
static struct errs errs[ERRMAX];
|
|
|
|
static int errcnt;
|
2018-05-21 09:32:52 +00:00
|
|
|
static char err_toomany[] = " too many errors occurred";
|
2018-06-12 07:31:59 +00:00
|
|
|
static char err_listem[] =
|
1997-03-23 18:51:21 +00:00
|
|
|
" Many errors occurred. Press `e' to display the list of errors.";
|
|
|
|
|
|
|
|
/* These macros get used to reset and log the errors */
|
|
|
|
#define ERR_RESET errcnt = 0
|
|
|
|
#define ERROR(p, e) if (errcnt >= ERRMAX) \
|
|
|
|
{ \
|
|
|
|
return(err_toomany); \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
errs[errcnt].arg = (p); \
|
2002-01-24 17:52:26 +00:00
|
|
|
errs[errcnt++].errnum = (e); \
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* err_string() - return an appropriate error string. This is what the
|
|
|
|
* command will return for displaying. If no errors were logged, then
|
|
|
|
* return NULL. The maximum length of the error string is defined by
|
|
|
|
* "STRMAX".
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define STRMAX 80
|
|
|
|
|
2018-06-12 07:14:12 +00:00
|
|
|
char *
|
|
|
|
err_string(void)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
struct errs *errp;
|
2018-06-03 02:58:53 +00:00
|
|
|
int cnt = 0;
|
|
|
|
bool first = true;
|
|
|
|
int currerr = -1;
|
1997-03-23 18:51:21 +00:00
|
|
|
int stringlen; /* characters still available in "string" */
|
|
|
|
static char string[STRMAX];
|
|
|
|
|
|
|
|
/* if there are no errors, return NULL */
|
|
|
|
if (errcnt == 0)
|
|
|
|
{
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort the errors */
|
|
|
|
qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
|
|
|
|
|
|
|
|
/* need a space at the front of the error string */
|
|
|
|
string[0] = ' ';
|
|
|
|
string[1] = '\0';
|
|
|
|
stringlen = STRMAX - 2;
|
|
|
|
|
|
|
|
/* loop thru the sorted list, building an error string */
|
|
|
|
while (cnt < errcnt)
|
|
|
|
{
|
|
|
|
errp = &(errs[cnt++]);
|
2002-01-24 17:52:26 +00:00
|
|
|
if (errp->errnum != currerr)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-06-19 18:32:06 +00:00
|
|
|
if (currerr >= 0)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
|
|
|
|
{
|
|
|
|
return(err_listem);
|
|
|
|
}
|
2018-05-21 04:32:14 +00:00
|
|
|
strcat(string, "; "); /* we know there's more */
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
2002-01-24 17:52:26 +00:00
|
|
|
currerr = errp->errnum;
|
2018-06-02 03:25:15 +00:00
|
|
|
first = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
|
|
|
|
{
|
|
|
|
return(err_listem);
|
|
|
|
}
|
2018-06-02 03:25:15 +00:00
|
|
|
first = false;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add final message */
|
|
|
|
stringlen = str_adderr(string, stringlen, currerr);
|
|
|
|
|
|
|
|
/* return the error string */
|
|
|
|
return(stringlen == 0 ? err_listem : string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* str_adderr(str, len, err) - add an explanation of error "err" to
|
|
|
|
* the string "str".
|
|
|
|
*/
|
|
|
|
|
2016-05-22 04:17:00 +00:00
|
|
|
static int
|
2018-05-21 04:32:14 +00:00
|
|
|
str_adderr(char *str, int len, int err)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-21 04:32:14 +00:00
|
|
|
const char *msg;
|
|
|
|
int msglen;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
msg = err == 0 ? "Not a number" : strerror(err);
|
1997-03-23 18:51:21 +00:00
|
|
|
msglen = strlen(msg) + 2;
|
|
|
|
if (len <= msglen)
|
|
|
|
{
|
|
|
|
return(0);
|
|
|
|
}
|
2018-05-21 04:32:14 +00:00
|
|
|
strcat(str, ": ");
|
|
|
|
strcat(str, msg);
|
1997-03-23 18:51:21 +00:00
|
|
|
return(len - msglen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* str_addarg(str, len, arg, first) - add the string argument "arg" to
|
|
|
|
* the string "str". This is the first in the group when "first"
|
|
|
|
* is set (indicating that a comma should NOT be added to the front).
|
|
|
|
*/
|
|
|
|
|
2016-05-22 04:17:00 +00:00
|
|
|
static int
|
2018-06-03 02:58:53 +00:00
|
|
|
str_addarg(char str[], int len, char arg[], bool first)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
int arglen;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
arglen = strlen(arg);
|
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
arglen += 2;
|
|
|
|
}
|
|
|
|
if (len <= arglen)
|
|
|
|
{
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
if (!first)
|
|
|
|
{
|
2018-05-21 04:32:14 +00:00
|
|
|
strcat(str, ", ");
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
2018-05-21 04:32:14 +00:00
|
|
|
strcat(str, arg);
|
1997-03-23 18:51:21 +00:00
|
|
|
return(len - arglen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* err_compar(p1, p2) - comparison routine used by "qsort"
|
|
|
|
* for sorting errors.
|
|
|
|
*/
|
|
|
|
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
static int
|
|
|
|
err_compar(const void *p1, const void *p2)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
int result;
|
2018-05-21 03:36:16 +00:00
|
|
|
const struct errs * const g1 = (const struct errs * const)p1;
|
|
|
|
const struct errs * const g2 = (const struct errs * const)p2;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
if ((result = g1->errnum - g2->errnum) == 0)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
return(strcmp(g1->arg, g2->arg));
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* error_count() - return the number of errors currently logged.
|
|
|
|
*/
|
|
|
|
|
2016-05-22 04:17:00 +00:00
|
|
|
int
|
2018-06-02 00:02:15 +00:00
|
|
|
error_count(void)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
return(errcnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* show_errors() - display on stdout the current log of errors.
|
|
|
|
*/
|
|
|
|
|
2016-05-22 04:17:00 +00:00
|
|
|
void
|
2018-06-02 00:02:15 +00:00
|
|
|
show_errors(void)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
int cnt = 0;
|
|
|
|
struct errs *errp = errs;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
|
|
|
|
while (cnt++ < errcnt)
|
|
|
|
{
|
|
|
|
printf("%5s: %s\n", errp->arg,
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
errp->errnum == 0 ? "Not a number" : strerror(errp->errnum));
|
1997-03-23 18:51:21 +00:00
|
|
|
errp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 08:59:57 +00:00
|
|
|
static const char no_proc_specified[] = " no processes specified";
|
|
|
|
static const char invalid_signal_number[] = " invalid_signal_number";
|
|
|
|
static const char bad_signal_name[] = " bad signal name";
|
|
|
|
static const char bad_pri_value[] = " bad priority value";
|
2018-05-21 09:32:52 +00:00
|
|
|
|
2018-06-10 09:00:01 +00:00
|
|
|
static int
|
|
|
|
signame_to_signum(const char * sig)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (strncasecmp(sig, "SIG", 3) == 0)
|
|
|
|
sig += 3;
|
|
|
|
for (n = 1; n < sys_nsig; n++) {
|
|
|
|
if (!strcasecmp(sys_signame[n], sig))
|
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
/*
|
|
|
|
* kill_procs(str) - send signals to processes, much like the "kill"
|
|
|
|
* command does; invoked in response to 'k'.
|
|
|
|
*/
|
|
|
|
|
2018-06-10 08:59:57 +00:00
|
|
|
const char *
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
kill_procs(char *str)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
char *nptr;
|
1997-03-23 18:51:21 +00:00
|
|
|
int signum = SIGTERM; /* default */
|
|
|
|
int procnum;
|
|
|
|
|
|
|
|
/* reset error array */
|
|
|
|
ERR_RESET;
|
|
|
|
|
|
|
|
/* skip over leading white space */
|
|
|
|
while (isspace(*str)) str++;
|
|
|
|
|
|
|
|
if (str[0] == '-')
|
|
|
|
{
|
|
|
|
/* explicit signal specified */
|
|
|
|
if ((nptr = next_field(str)) == NULL)
|
|
|
|
{
|
2018-05-21 09:32:52 +00:00
|
|
|
return(no_proc_specified);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isdigit(str[1]))
|
|
|
|
{
|
2018-05-21 04:32:14 +00:00
|
|
|
scanint(str + 1, &signum);
|
1997-03-23 18:51:21 +00:00
|
|
|
if (signum <= 0 || signum >= NSIG)
|
|
|
|
{
|
2018-05-21 09:32:52 +00:00
|
|
|
return(invalid_signal_number);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-12 07:31:59 +00:00
|
|
|
else
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-06-10 09:00:01 +00:00
|
|
|
signum = signame_to_signum(str + 1);
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* was it ever found */
|
2018-06-10 09:00:01 +00:00
|
|
|
if (signum == -1 )
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-06-10 06:21:45 +00:00
|
|
|
return(bad_signal_name);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* put the new pointer in place */
|
|
|
|
str = nptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loop thru the string, killing processes */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (scanint(str, &procnum) == -1)
|
|
|
|
{
|
|
|
|
ERROR(str, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* go in for the kill */
|
2018-06-10 09:15:13 +00:00
|
|
|
if (kill(procnum, signum) == -1)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
/* chalk up an error */
|
|
|
|
ERROR(str, errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ((str = next_field(str)) != NULL);
|
|
|
|
|
|
|
|
/* return appropriate error string */
|
|
|
|
return(err_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* renice_procs(str) - change the "nice" of processes, much like the
|
|
|
|
* "renice" command does; invoked in response to 'r'.
|
|
|
|
*/
|
|
|
|
|
2018-06-10 08:59:57 +00:00
|
|
|
const char *
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
renice_procs(char *str)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
char negate;
|
1997-03-23 18:51:21 +00:00
|
|
|
int prio;
|
|
|
|
int procnum;
|
|
|
|
|
|
|
|
ERR_RESET;
|
|
|
|
|
|
|
|
/* allow for negative priority values */
|
|
|
|
if ((negate = (*str == '-')) != 0)
|
|
|
|
{
|
|
|
|
/* move past the minus sign */
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use procnum as a temporary holding place and get the number */
|
|
|
|
procnum = scanint(str, &prio);
|
|
|
|
|
|
|
|
/* negate if necessary */
|
|
|
|
if (negate)
|
|
|
|
{
|
|
|
|
prio = -prio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for validity */
|
|
|
|
if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
|
|
|
|
{
|
2018-05-21 09:32:52 +00:00
|
|
|
return(bad_pri_value);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* move to the first process number */
|
|
|
|
if ((str = next_field(str)) == NULL)
|
|
|
|
{
|
2018-05-21 09:32:52 +00:00
|
|
|
return(no_proc_specified);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* loop thru the process numbers, renicing each one */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (scanint(str, &procnum) == -1)
|
|
|
|
{
|
|
|
|
ERROR(str, 0);
|
|
|
|
}
|
|
|
|
|
2018-06-10 09:15:13 +00:00
|
|
|
if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
ERROR(str, errno);
|
|
|
|
}
|
|
|
|
} while ((str = next_field(str)) != NULL);
|
|
|
|
|
|
|
|
/* return appropriate error string */
|
|
|
|
return(err_string());
|
|
|
|
}
|
|
|
|
|