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
typedef struct {
int pid;
pid_t pid, ppid;
char user[9];
char state[4];
unsigned long rss;
int ppid;
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
unsigned pcpu;
unsigned pscpu;
@ -518,8 +517,8 @@ typedef struct {
char short_cmd[COMM_LEN];
} procps_status_t;
procps_status_t* procps_scan(int save_user_arg0);
long *find_pid_by_name( const char* pidName);
long *pidlist_reverse(long *pidList);
pid_t *find_pid_by_name(const char* procName);
pid_t *pidlist_reverse(pid_t *pidList);
extern const char bb_uuenc_tbl_base64[];

View File

@ -25,29 +25,34 @@ RB_POWERDOWN,
#endif
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;
/* 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 */
flags = getopt32(argc, argv, "d:nf", &delay);
if (flags&1) sleep(xatou(delay));
if (!(flags&2)) sync();
if (flags & 1) sleep(xatou(delay));
if (!(flags & 2)) sync();
/* Perform action. */
if (ENABLE_INIT && !(flags & 4)) {
if (ENABLE_FEATURE_INITRD) {
long *pidlist=find_pid_by_name("linuxrc");
if (*pidlist>0) rc = kill(*pidlist,signals[which]);
if (ENABLE_FEATURE_CLEAN_UP) free(pidlist);
pid_t *pidlist = find_pid_by_name("linuxrc");
if (pidlist[0] > 0)
rc = kill(pidlist[0], signals[which]);
if (ENABLE_FEATURE_CLEAN_UP)
free(pidlist);
}
if (rc) rc = kill(1,signals[which]);
} else rc = reboot(magic[which]);
if (rc)
rc = kill(1, signals[which]);
} else
rc = reboot(magic[which]);
if (rc) bb_error_msg("no");
if (rc)
bb_error_msg("no");
return rc;
}

View File

@ -7,10 +7,6 @@
* 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"
/* find_pid_by_name()
@ -23,30 +19,31 @@
* Returns a list of all matching PIDs
* 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;
procps_status_t* p;
pidList = xmalloc(sizeof(long));
pidList = xmalloc(sizeof(*pidList));
while ((p = procps_scan(0)) != 0) {
if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) {
pidList = xrealloc( pidList, sizeof(long) * (i+2));
if (strncmp(p->short_cmd, procName, COMM_LEN-1) == 0) {
pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
pidList[i++] = p->pid;
}
}
pidList[i] = i==0 ? -1 : 0;
pidList[i] = 0;
return pidList;
}
long *pidlist_reverse(long *pidList)
pid_t *pidlist_reverse(pid_t *pidList)
{
int i = 0;
while (pidList[i] > 0 && ++i);
if (i-- > 0) {
long k;
while (pidList[i])
i++;
if (--i >= 0) {
pid_t k;
int j;
for (j = 0; i > j; i--, j++) {
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)
{
static DIR *dir;
struct dirent *entry;
static procps_status_t ret_status;
struct dirent *entry;
char *name;
int n;
char status[32];

View File

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

View File

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

View File

@ -58,7 +58,7 @@ int ps_main(int argc, char **argv)
len = sizeof(sbuf);
if (is_selinux_enabled()) {
if (getpidcon(p->pid,&sid) < 0)
if (getpidcon(p->pid, &sid) < 0)
sid = NULL;
}
@ -71,14 +71,14 @@ int ps_main(int argc, char **argv)
} else {
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
#endif
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
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;

View File

@ -78,7 +78,7 @@ static int mult_lvl_cmp(void* a, void* b) {
the next. Mostly used for sorting. */
struct save_hist {
int ticks;
int pid;
pid_t pid;
};
/*
@ -119,7 +119,8 @@ static void get_jiffy_counts(void)
static void do_stats(void)
{
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;
get_jiffy_counts();
@ -328,9 +329,9 @@ static void display_status(int count, int scr_width)
else
sprintf(rss_str_buf, "%7ld", s->rss);
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") " ",
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,)
pmem.quot, '0'+pmem.rem);
if (col > 0)