PID should be stored in pid_t, not int or long.

find_pid_by_name() was returning 0 or -1 in last array element,
but -1 was never checked. We can use just 0 intead.
This commit is contained in:
Denis Vlasenko 2006-11-01 09:16:49 +00:00
parent d3ada32285
commit 35fb512728
8 changed files with 76 additions and 69 deletions

View File

@ -501,11 +501,10 @@ void reset_ino_dev_hashtable(void);
#endif #endif
#endif #endif
typedef struct { typedef struct {
int pid; pid_t pid, ppid;
char user[9]; char user[9];
char state[4]; char state[4];
unsigned long rss; unsigned long rss;
int ppid;
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
unsigned pcpu; unsigned pcpu;
unsigned pscpu; unsigned pscpu;
@ -518,8 +517,8 @@ typedef struct {
char short_cmd[COMM_LEN]; char short_cmd[COMM_LEN];
} procps_status_t; } procps_status_t;
procps_status_t* procps_scan(int save_user_arg0); procps_status_t* procps_scan(int save_user_arg0);
long *find_pid_by_name( const char* pidName); pid_t *find_pid_by_name(const char* procName);
long *pidlist_reverse(long *pidList); pid_t *pidlist_reverse(pid_t *pidList);
extern const char bb_uuenc_tbl_base64[]; extern const char bb_uuenc_tbl_base64[];

View File

@ -25,29 +25,34 @@ RB_POWERDOWN,
#endif #endif
RB_AUTOBOOT RB_AUTOBOOT
}; };
static const int signals[] = {SIGUSR1, SIGUSR2, SIGTERM}; static const int signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
char *delay = "hpr"; char *delay;
int which, flags, rc = 1; int which, flags, rc = 1;
/* Figure out which applet we're running */ /* Figure out which applet we're running */
for(which=0;delay[which]!=*applet_name;which++); for (which = 0; "hpr"[which] != *applet_name; which++);
/* Parse and handle arguments */ /* Parse and handle arguments */
flags = getopt32(argc, argv, "d:nf", &delay); flags = getopt32(argc, argv, "d:nf", &delay);
if (flags&1) sleep(xatou(delay)); if (flags & 1) sleep(xatou(delay));
if (!(flags&2)) sync(); if (!(flags & 2)) sync();
/* Perform action. */ /* Perform action. */
if (ENABLE_INIT && !(flags & 4)) { if (ENABLE_INIT && !(flags & 4)) {
if (ENABLE_FEATURE_INITRD) { if (ENABLE_FEATURE_INITRD) {
long *pidlist=find_pid_by_name("linuxrc"); pid_t *pidlist = find_pid_by_name("linuxrc");
if (*pidlist>0) rc = kill(*pidlist,signals[which]); if (pidlist[0] > 0)
if (ENABLE_FEATURE_CLEAN_UP) free(pidlist); rc = kill(pidlist[0], signals[which]);
if (ENABLE_FEATURE_CLEAN_UP)
free(pidlist);
} }
if (rc) rc = kill(1,signals[which]); if (rc)
} else rc = reboot(magic[which]); rc = kill(1, signals[which]);
} else
rc = reboot(magic[which]);
if (rc) bb_error_msg("no"); if (rc)
bb_error_msg("no");
return rc; return rc;
} }

View File

@ -7,10 +7,6 @@
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*/ */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "libbb.h" #include "libbb.h"
/* find_pid_by_name() /* find_pid_by_name()
@ -23,30 +19,31 @@
* Returns a list of all matching PIDs * Returns a list of all matching PIDs
* It is the caller's duty to free the returned pidlist. * It is the caller's duty to free the returned pidlist.
*/ */
long* find_pid_by_name(const char* pidName) pid_t* find_pid_by_name(const char* procName)
{ {
long* pidList; pid_t* pidList;
int i = 0; int i = 0;
procps_status_t* p; procps_status_t* p;
pidList = xmalloc(sizeof(long)); pidList = xmalloc(sizeof(*pidList));
while ((p = procps_scan(0)) != 0) { while ((p = procps_scan(0)) != 0) {
if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) { if (strncmp(p->short_cmd, procName, COMM_LEN-1) == 0) {
pidList = xrealloc( pidList, sizeof(long) * (i+2)); pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
pidList[i++] = p->pid; pidList[i++] = p->pid;
} }
} }
pidList[i] = i==0 ? -1 : 0; pidList[i] = 0;
return pidList; return pidList;
} }
long *pidlist_reverse(long *pidList) pid_t *pidlist_reverse(pid_t *pidList)
{ {
int i = 0; int i = 0;
while (pidList[i] > 0 && ++i); while (pidList[i])
if (i-- > 0) { i++;
long k; if (--i >= 0) {
pid_t k;
int j; int j;
for (j = 0; i > j; i--, j++) { for (j = 0; i > j; i--, j++) {
k = pidList[i]; k = pidList[i];

View File

@ -32,8 +32,9 @@ static int read_to_buf(const char *filename, void *buf)
procps_status_t * procps_scan(int save_user_arg0) procps_status_t * procps_scan(int save_user_arg0)
{ {
static DIR *dir; static DIR *dir;
struct dirent *entry;
static procps_status_t ret_status; static procps_status_t ret_status;
struct dirent *entry;
char *name; char *name;
int n; int n;
char status[32]; char status[32];

View File

@ -103,31 +103,31 @@ do_it_now:
} }
/* Pid or name required for kill/killall */ /* Pid or name required for kill/killall */
if (argc<1) if (argc < 1)
bb_show_usage(); bb_show_usage();
if (killall) { if (killall) {
/* Looks like they want to do a killall. Do that */ /* Looks like they want to do a killall. Do that */
pid = getpid(); pid = getpid();
while (arg) { while (arg) {
long* pidList; pid_t* pidList;
pidList = find_pid_by_name(arg); pidList = find_pid_by_name(arg);
if (!pidList || *pidList<=0) { if (*pidList == 0) {
errors++; errors++;
if (!quiet) if (!quiet)
bb_error_msg("%s: no process killed", arg); bb_error_msg("%s: no process killed", arg);
} else { } else {
long *pl; pid_t *pl;
for (pl = pidList; *pl!=0; pl++) { for (pl = pidList; *pl; pl++) {
if (*pl==pid) if (*pl == pid)
continue; continue;
if (kill(*pl, signo)!=0) { if (kill(*pl, signo) == 0)
errors++; continue;
if (!quiet) errors++;
bb_perror_msg("cannot kill pid %ld", *pl); if (!quiet)
} bb_perror_msg("cannot kill pid %u", (unsigned)*pl);
} }
} }
free(pidList); free(pidList);
@ -138,12 +138,14 @@ do_it_now:
/* Looks like they want to do a kill. Do that */ /* Looks like they want to do a kill. Do that */
while (arg) { while (arg) {
if (!isdigit(arg[0]) && arg[0]!='-') /* Huh?
if (!isdigit(arg[0]) && arg[0] != '-')
bb_error_msg_and_die("bad pid '%s'", arg); bb_error_msg_and_die("bad pid '%s'", arg);
*/
pid = xatou(arg); pid = xatou(arg);
/* FIXME: better overflow check? */ /* FIXME: better overflow check? */
if (kill(pid, signo)!=0) { if (kill(pid, signo) != 0) {
bb_perror_msg("cannot kill pid %ld", (long)pid); bb_perror_msg("cannot kill pid %u", (unsigned)pid);
errors++; errors++;
} }
arg = *++argv; arg = *++argv;

View File

@ -18,18 +18,18 @@
#endif #endif
#if ENABLE_FEATURE_PIDOF_OMIT #if ENABLE_FEATURE_PIDOF_OMIT
#define _OMIT_COMPL(a) a # define _OMIT_COMPL(a) a
#define _OMIT(a) ,a # define _OMIT(a) ,a
#if ENABLE_FEATURE_PIDOF_SINGLE # if ENABLE_FEATURE_PIDOF_SINGLE
#define OMIT (1<<1) # define OMIT (1<<1)
# else
# define OMIT (1<<0)
# endif
#else #else
#define OMIT (1<<0) # define _OMIT_COMPL(a) ""
#endif # define _OMIT(a)
#else # define OMIT (0)
#define _OMIT_COMPL(a) "" # define omitted (0)
#define _OMIT(a)
#define OMIT (0)
#define omitted (0)
#endif #endif
int pidof_main(int argc, char **argv) int pidof_main(int argc, char **argv)
@ -65,21 +65,23 @@ int pidof_main(int argc, char **argv)
#endif #endif
/* Looks like everything is set to go. */ /* Looks like everything is set to go. */
while (optind < argc) { while (optind < argc) {
long *pidList; pid_t *pidList;
long *pl; pid_t *pl;
/* reverse the pidlist like GNU pidof does. */ /* reverse the pidlist like GNU pidof does. */
pidList = pidlist_reverse(find_pid_by_name(argv[optind])); pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
for (pl = pidList; *pl > 0; pl++) { for (pl = pidList; *pl; pl++) {
#if ENABLE_FEATURE_PIDOF_OMIT #if ENABLE_FEATURE_PIDOF_OMIT
unsigned omitted = 0; unsigned omitted = 0;
if (opt & OMIT) { if (opt & OMIT) {
llist_t *omits_p = omits; llist_t *omits_p = omits;
while (omits_p) while (omits_p) {
if (xatoul(omits_p->data) == *pl) { if (xatoul(omits_p->data) == *pl) {
omitted = 1; break; omitted = 1;
break;
} else } else
omits_p = omits_p->link; omits_p = omits_p->link;
}
} }
#endif #endif
if (!omitted) { if (!omitted) {
@ -88,7 +90,7 @@ int pidof_main(int argc, char **argv)
} else { } else {
n = 1; n = 1;
} }
printf("%ld", *pl); printf("%u", (unsigned)*pl);
} }
fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);

View File

@ -58,7 +58,7 @@ int ps_main(int argc, char **argv)
len = sizeof(sbuf); len = sizeof(sbuf);
if (is_selinux_enabled()) { if (is_selinux_enabled()) {
if (getpidcon(p->pid,&sid) < 0) if (getpidcon(p->pid, &sid) < 0)
sid = NULL; sid = NULL;
} }
@ -71,14 +71,14 @@ int ps_main(int argc, char **argv)
} else { } else {
safe_strncpy(sbuf, "unknown", 7); safe_strncpy(sbuf, "unknown", 7);
} }
len = printf("%5d %-32s %s ", p->pid, sbuf, p->state); len = printf("%5u %-32s %s ", (unsigned)p->pid, sbuf, p->state);
} }
else else
#endif #endif
if (p->rss == 0) if (p->rss == 0)
len = printf("%5d %-8s %s ", p->pid, p->user, p->state); len = printf("%5u %-8s %s ", (unsigned)p->pid, p->user, p->state);
else else
len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state); len = printf("%5u %-8s %6ld %s ", (unsigned)p->pid, p->user, p->rss, p->state);
i = terminal_width-len; i = terminal_width-len;

View File

@ -78,7 +78,7 @@ static int mult_lvl_cmp(void* a, void* b) {
the next. Mostly used for sorting. */ the next. Mostly used for sorting. */
struct save_hist { struct save_hist {
int ticks; int ticks;
int pid; pid_t pid;
}; };
/* /*
@ -119,7 +119,8 @@ static void get_jiffy_counts(void)
static void do_stats(void) static void do_stats(void)
{ {
procps_status_t *cur; procps_status_t *cur;
int pid, total_time, i, last_i, n; pid_t pid;
int total_time, i, last_i, n;
struct save_hist *new_hist; struct save_hist *new_hist;
get_jiffy_counts(); get_jiffy_counts();
@ -328,9 +329,9 @@ static void display_status(int count, int scr_width)
else else
sprintf(rss_str_buf, "%7ld", s->rss); sprintf(rss_str_buf, "%7ld", s->rss);
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);) USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);)
col -= printf("\n%5d %-8s %s %s%6d%3u.%c" \ col -= printf("\n%5u %-8s %s %s%6u%3u.%c" \
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ", USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ",
s->pid, s->user, s->state, rss_str_buf, s->ppid, (unsigned)s->pid, s->user, s->state, rss_str_buf, (unsigned)s->ppid,
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,) USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
pmem.quot, '0'+pmem.rem); pmem.quot, '0'+pmem.rem);
if (col > 0) if (col > 0)