aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-01 09:16:49 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-01 09:16:49 +0000
commit9929fc484855149072edb217da1b801f00c0fe15 (patch)
treea97deb26bca43e394a603840039846cd9d89cae9
parent7a695ef456cfdfcd5d460681a8016ad9a1cf0833 (diff)
downloadbusybox-w32-9929fc484855149072edb217da1b801f00c0fe15.tar.gz
busybox-w32-9929fc484855149072edb217da1b801f00c0fe15.tar.bz2
busybox-w32-9929fc484855149072edb217da1b801f00c0fe15.zip
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. git-svn-id: svn://busybox.net/trunk/busybox@16485 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--include/libbb.h7
-rw-r--r--init/halt.c27
-rw-r--r--libbb/find_pid_by_name.c25
-rw-r--r--libbb/procps.c3
-rw-r--r--procps/kill.c30
-rw-r--r--procps/pidof.c36
-rw-r--r--procps/ps.c8
-rw-r--r--procps/top.c9
8 files changed, 76 insertions, 69 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 770c1ecc1..343a93290 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -501,11 +501,10 @@ void reset_ino_dev_hashtable(void);
501#endif 501#endif
502#endif 502#endif
503typedef struct { 503typedef struct {
504 int pid; 504 pid_t pid, ppid;
505 char user[9]; 505 char user[9];
506 char state[4]; 506 char state[4];
507 unsigned long rss; 507 unsigned long rss;
508 int ppid;
509#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 508#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
510 unsigned pcpu; 509 unsigned pcpu;
511 unsigned pscpu; 510 unsigned pscpu;
@@ -518,8 +517,8 @@ typedef struct {
518 char short_cmd[COMM_LEN]; 517 char short_cmd[COMM_LEN];
519} procps_status_t; 518} procps_status_t;
520procps_status_t* procps_scan(int save_user_arg0); 519procps_status_t* procps_scan(int save_user_arg0);
521long *find_pid_by_name( const char* pidName); 520pid_t *find_pid_by_name(const char* procName);
522long *pidlist_reverse(long *pidList); 521pid_t *pidlist_reverse(pid_t *pidList);
523 522
524 523
525extern const char bb_uuenc_tbl_base64[]; 524extern const char bb_uuenc_tbl_base64[];
diff --git a/init/halt.c b/init/halt.c
index 3ab41f1f2..a6cf48bbe 100644
--- a/init/halt.c
+++ b/init/halt.c
@@ -25,29 +25,34 @@ RB_POWERDOWN,
25#endif 25#endif
26RB_AUTOBOOT 26RB_AUTOBOOT
27 }; 27 };
28 static const int signals[] = {SIGUSR1, SIGUSR2, SIGTERM}; 28 static const int signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
29 29
30 char *delay = "hpr"; 30 char *delay;
31 int which, flags, rc = 1; 31 int which, flags, rc = 1;
32 32
33 /* Figure out which applet we're running */ 33 /* Figure out which applet we're running */
34 for(which=0;delay[which]!=*applet_name;which++); 34 for (which = 0; "hpr"[which] != *applet_name; which++);
35 35
36 /* Parse and handle arguments */ 36 /* Parse and handle arguments */
37 flags = getopt32(argc, argv, "d:nf", &delay); 37 flags = getopt32(argc, argv, "d:nf", &delay);
38 if (flags&1) sleep(xatou(delay)); 38 if (flags & 1) sleep(xatou(delay));
39 if (!(flags&2)) sync(); 39 if (!(flags & 2)) sync();
40 40
41 /* Perform action. */ 41 /* Perform action. */
42 if (ENABLE_INIT && !(flags & 4)) { 42 if (ENABLE_INIT && !(flags & 4)) {
43 if (ENABLE_FEATURE_INITRD) { 43 if (ENABLE_FEATURE_INITRD) {
44 long *pidlist=find_pid_by_name("linuxrc"); 44 pid_t *pidlist = find_pid_by_name("linuxrc");
45 if (*pidlist>0) rc = kill(*pidlist,signals[which]); 45 if (pidlist[0] > 0)
46 if (ENABLE_FEATURE_CLEAN_UP) free(pidlist); 46 rc = kill(pidlist[0], signals[which]);
47 if (ENABLE_FEATURE_CLEAN_UP)
48 free(pidlist);
47 } 49 }
48 if (rc) rc = kill(1,signals[which]); 50 if (rc)
49 } else rc = reboot(magic[which]); 51 rc = kill(1, signals[which]);
52 } else
53 rc = reboot(magic[which]);
50 54
51 if (rc) bb_error_msg("no"); 55 if (rc)
56 bb_error_msg("no");
52 return rc; 57 return rc;
53} 58}
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
index 247d79f9f..05f7f968f 100644
--- a/libbb/find_pid_by_name.c
+++ b/libbb/find_pid_by_name.c
@@ -7,10 +7,6 @@
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. 7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8 */ 8 */
9 9
10#include <stdio.h>
11#include <ctype.h>
12#include <string.h>
13#include <stdlib.h>
14#include "libbb.h" 10#include "libbb.h"
15 11
16/* find_pid_by_name() 12/* find_pid_by_name()
@@ -23,30 +19,31 @@
23 * Returns a list of all matching PIDs 19 * Returns a list of all matching PIDs
24 * It is the caller's duty to free the returned pidlist. 20 * It is the caller's duty to free the returned pidlist.
25 */ 21 */
26long* find_pid_by_name(const char* pidName) 22pid_t* find_pid_by_name(const char* procName)
27{ 23{
28 long* pidList; 24 pid_t* pidList;
29 int i = 0; 25 int i = 0;
30 procps_status_t* p; 26 procps_status_t* p;
31 27
32 pidList = xmalloc(sizeof(long)); 28 pidList = xmalloc(sizeof(*pidList));
33 while ((p = procps_scan(0)) != 0) { 29 while ((p = procps_scan(0)) != 0) {
34 if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) { 30 if (strncmp(p->short_cmd, procName, COMM_LEN-1) == 0) {
35 pidList = xrealloc( pidList, sizeof(long) * (i+2)); 31 pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
36 pidList[i++] = p->pid; 32 pidList[i++] = p->pid;
37 } 33 }
38 } 34 }
39 35
40 pidList[i] = i==0 ? -1 : 0; 36 pidList[i] = 0;
41 return pidList; 37 return pidList;
42} 38}
43 39
44long *pidlist_reverse(long *pidList) 40pid_t *pidlist_reverse(pid_t *pidList)
45{ 41{
46 int i = 0; 42 int i = 0;
47 while (pidList[i] > 0 && ++i); 43 while (pidList[i])
48 if (i-- > 0) { 44 i++;
49 long k; 45 if (--i >= 0) {
46 pid_t k;
50 int j; 47 int j;
51 for (j = 0; i > j; i--, j++) { 48 for (j = 0; i > j; i--, j++) {
52 k = pidList[i]; 49 k = pidList[i];
diff --git a/libbb/procps.c b/libbb/procps.c
index eba90705c..15a1cf74b 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -32,8 +32,9 @@ static int read_to_buf(const char *filename, void *buf)
32procps_status_t * procps_scan(int save_user_arg0) 32procps_status_t * procps_scan(int save_user_arg0)
33{ 33{
34 static DIR *dir; 34 static DIR *dir;
35 struct dirent *entry;
36 static procps_status_t ret_status; 35 static procps_status_t ret_status;
36
37 struct dirent *entry;
37 char *name; 38 char *name;
38 int n; 39 int n;
39 char status[32]; 40 char status[32];
diff --git a/procps/kill.c b/procps/kill.c
index b29f61b58..f22bdbe46 100644
--- a/procps/kill.c
+++ b/procps/kill.c
@@ -103,31 +103,31 @@ do_it_now:
103 } 103 }
104 104
105 /* Pid or name required for kill/killall */ 105 /* Pid or name required for kill/killall */
106 if (argc<1) 106 if (argc < 1)
107 bb_show_usage(); 107 bb_show_usage();
108 108
109 if (killall) { 109 if (killall) {
110 /* Looks like they want to do a killall. Do that */ 110 /* Looks like they want to do a killall. Do that */
111 pid = getpid(); 111 pid = getpid();
112 while (arg) { 112 while (arg) {
113 long* pidList; 113 pid_t* pidList;
114 114
115 pidList = find_pid_by_name(arg); 115 pidList = find_pid_by_name(arg);
116 if (!pidList || *pidList<=0) { 116 if (*pidList == 0) {
117 errors++; 117 errors++;
118 if (!quiet) 118 if (!quiet)
119 bb_error_msg("%s: no process killed", arg); 119 bb_error_msg("%s: no process killed", arg);
120 } else { 120 } else {
121 long *pl; 121 pid_t *pl;
122 122
123 for (pl = pidList; *pl!=0; pl++) { 123 for (pl = pidList; *pl; pl++) {
124 if (*pl==pid) 124 if (*pl == pid)
125 continue; 125 continue;
126 if (kill(*pl, signo)!=0) { 126 if (kill(*pl, signo) == 0)
127 errors++; 127 continue;
128 if (!quiet) 128 errors++;
129 bb_perror_msg("cannot kill pid %ld", *pl); 129 if (!quiet)
130 } 130 bb_perror_msg("cannot kill pid %u", (unsigned)*pl);
131 } 131 }
132 } 132 }
133 free(pidList); 133 free(pidList);
@@ -138,12 +138,14 @@ do_it_now:
138 138
139 /* Looks like they want to do a kill. Do that */ 139 /* Looks like they want to do a kill. Do that */
140 while (arg) { 140 while (arg) {
141 if (!isdigit(arg[0]) && arg[0]!='-') 141 /* Huh?
142 if (!isdigit(arg[0]) && arg[0] != '-')
142 bb_error_msg_and_die("bad pid '%s'", arg); 143 bb_error_msg_and_die("bad pid '%s'", arg);
144 */
143 pid = xatou(arg); 145 pid = xatou(arg);
144 /* FIXME: better overflow check? */ 146 /* FIXME: better overflow check? */
145 if (kill(pid, signo)!=0) { 147 if (kill(pid, signo) != 0) {
146 bb_perror_msg("cannot kill pid %ld", (long)pid); 148 bb_perror_msg("cannot kill pid %u", (unsigned)pid);
147 errors++; 149 errors++;
148 } 150 }
149 arg = *++argv; 151 arg = *++argv;
diff --git a/procps/pidof.c b/procps/pidof.c
index 62c590fd8..28c5c04e2 100644
--- a/procps/pidof.c
+++ b/procps/pidof.c
@@ -18,18 +18,18 @@
18#endif 18#endif
19 19
20#if ENABLE_FEATURE_PIDOF_OMIT 20#if ENABLE_FEATURE_PIDOF_OMIT
21#define _OMIT_COMPL(a) a 21# define _OMIT_COMPL(a) a
22#define _OMIT(a) ,a 22# define _OMIT(a) ,a
23#if ENABLE_FEATURE_PIDOF_SINGLE 23# if ENABLE_FEATURE_PIDOF_SINGLE
24#define OMIT (1<<1) 24# define OMIT (1<<1)
25#else 25# else
26#define OMIT (1<<0) 26# define OMIT (1<<0)
27#endif 27# endif
28#else 28#else
29#define _OMIT_COMPL(a) "" 29# define _OMIT_COMPL(a) ""
30#define _OMIT(a) 30# define _OMIT(a)
31#define OMIT (0) 31# define OMIT (0)
32#define omitted (0) 32# define omitted (0)
33#endif 33#endif
34 34
35int pidof_main(int argc, char **argv) 35int pidof_main(int argc, char **argv)
@@ -65,21 +65,23 @@ int pidof_main(int argc, char **argv)
65#endif 65#endif
66 /* Looks like everything is set to go. */ 66 /* Looks like everything is set to go. */
67 while (optind < argc) { 67 while (optind < argc) {
68 long *pidList; 68 pid_t *pidList;
69 long *pl; 69 pid_t *pl;
70 70
71 /* reverse the pidlist like GNU pidof does. */ 71 /* reverse the pidlist like GNU pidof does. */
72 pidList = pidlist_reverse(find_pid_by_name(argv[optind])); 72 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
73 for (pl = pidList; *pl > 0; pl++) { 73 for (pl = pidList; *pl; pl++) {
74#if ENABLE_FEATURE_PIDOF_OMIT 74#if ENABLE_FEATURE_PIDOF_OMIT
75 unsigned omitted = 0; 75 unsigned omitted = 0;
76 if (opt & OMIT) { 76 if (opt & OMIT) {
77 llist_t *omits_p = omits; 77 llist_t *omits_p = omits;
78 while (omits_p) 78 while (omits_p) {
79 if (xatoul(omits_p->data) == *pl) { 79 if (xatoul(omits_p->data) == *pl) {
80 omitted = 1; break; 80 omitted = 1;
81 break;
81 } else 82 } else
82 omits_p = omits_p->link; 83 omits_p = omits_p->link;
84 }
83 } 85 }
84#endif 86#endif
85 if (!omitted) { 87 if (!omitted) {
@@ -88,7 +90,7 @@ int pidof_main(int argc, char **argv)
88 } else { 90 } else {
89 n = 1; 91 n = 1;
90 } 92 }
91 printf("%ld", *pl); 93 printf("%u", (unsigned)*pl);
92 } 94 }
93 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); 95 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
94 96
diff --git a/procps/ps.c b/procps/ps.c
index 97e239b07..df4dcc4fc 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -58,7 +58,7 @@ int ps_main(int argc, char **argv)
58 len = sizeof(sbuf); 58 len = sizeof(sbuf);
59 59
60 if (is_selinux_enabled()) { 60 if (is_selinux_enabled()) {
61 if (getpidcon(p->pid,&sid) < 0) 61 if (getpidcon(p->pid, &sid) < 0)
62 sid = NULL; 62 sid = NULL;
63 } 63 }
64 64
@@ -71,14 +71,14 @@ int ps_main(int argc, char **argv)
71 } else { 71 } else {
72 safe_strncpy(sbuf, "unknown", 7); 72 safe_strncpy(sbuf, "unknown", 7);
73 } 73 }
74 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state); 74 len = printf("%5u %-32s %s ", (unsigned)p->pid, sbuf, p->state);
75 } 75 }
76 else 76 else
77#endif 77#endif
78 if (p->rss == 0) 78 if (p->rss == 0)
79 len = printf("%5d %-8s %s ", p->pid, p->user, p->state); 79 len = printf("%5u %-8s %s ", (unsigned)p->pid, p->user, p->state);
80 else 80 else
81 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state); 81 len = printf("%5u %-8s %6ld %s ", (unsigned)p->pid, p->user, p->rss, p->state);
82 82
83 i = terminal_width-len; 83 i = terminal_width-len;
84 84
diff --git a/procps/top.c b/procps/top.c
index d58fdda97..3ff61dfa7 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -78,7 +78,7 @@ static int mult_lvl_cmp(void* a, void* b) {
78 the next. Mostly used for sorting. */ 78 the next. Mostly used for sorting. */
79struct save_hist { 79struct save_hist {
80 int ticks; 80 int ticks;
81 int pid; 81 pid_t pid;
82}; 82};
83 83
84/* 84/*
@@ -119,7 +119,8 @@ static void get_jiffy_counts(void)
119static void do_stats(void) 119static void do_stats(void)
120{ 120{
121 procps_status_t *cur; 121 procps_status_t *cur;
122 int pid, total_time, i, last_i, n; 122 pid_t pid;
123 int total_time, i, last_i, n;
123 struct save_hist *new_hist; 124 struct save_hist *new_hist;
124 125
125 get_jiffy_counts(); 126 get_jiffy_counts();
@@ -328,9 +329,9 @@ static void display_status(int count, int scr_width)
328 else 329 else
329 sprintf(rss_str_buf, "%7ld", s->rss); 330 sprintf(rss_str_buf, "%7ld", s->rss);
330 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);) 331 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);)
331 col -= printf("\n%5d %-8s %s %s%6d%3u.%c" \ 332 col -= printf("\n%5u %-8s %s %s%6u%3u.%c" \
332 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ", 333 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ",
333 s->pid, s->user, s->state, rss_str_buf, s->ppid, 334 (unsigned)s->pid, s->user, s->state, rss_str_buf, (unsigned)s->ppid,
334 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,) 335 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
335 pmem.quot, '0'+pmem.rem); 336 pmem.quot, '0'+pmem.rem);
336 if (col > 0) 337 if (col > 0)