diff options
| author | Eric Andersen <andersen@codepoet.org> | 2001-12-06 14:52:32 +0000 |
|---|---|---|
| committer | Eric Andersen <andersen@codepoet.org> | 2001-12-06 14:52:32 +0000 |
| commit | b24d65659f193cd7497dfdae4d8aa1bc91dbf343 (patch) | |
| tree | 430b543e3b98a9028f5c0435b1363b2256d26a24 /libbb | |
| parent | 06656f363de87846086f03c5e12541a66637858e (diff) | |
| download | busybox-w32-b24d65659f193cd7497dfdae4d8aa1bc91dbf343.tar.gz busybox-w32-b24d65659f193cd7497dfdae4d8aa1bc91dbf343.tar.bz2 busybox-w32-b24d65659f193cd7497dfdae4d8aa1bc91dbf343.zip | |
If find_pid_by_name() had an error, it was returning -1, but storing
that into a pid_t, which is unsigned on a number archs. Furthermore,
find_pid_by_name() would _never_ return an error if the intended proces
was "init", but instead would return 1, meaning we would fail to work
on 2.4.x kernels running an initrd...
-Erik
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/find_pid_by_name.c | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c index f183cc0bd..4eaee03e3 100644 --- a/libbb/find_pid_by_name.c +++ b/libbb/find_pid_by_name.c | |||
| @@ -40,13 +40,13 @@ | |||
| 40 | * | 40 | * |
| 41 | * Returns a list of all matching PIDs | 41 | * Returns a list of all matching PIDs |
| 42 | */ | 42 | */ |
| 43 | extern pid_t* find_pid_by_name( char* pidName) | 43 | extern long* find_pid_by_name( char* pidName) |
| 44 | { | 44 | { |
| 45 | int fd, i, j; | 45 | int fd, i, j; |
| 46 | char device[] = "/dev/ps"; | 46 | char device[] = "/dev/ps"; |
| 47 | pid_t num_pids; | 47 | pid_t num_pids; |
| 48 | pid_t* pid_array = NULL; | 48 | pid_t* pid_array = NULL; |
| 49 | pid_t* pidList=NULL; | 49 | long* pidList=NULL; |
| 50 | 50 | ||
| 51 | /* open device */ | 51 | /* open device */ |
| 52 | fd = open(device, O_RDONLY); | 52 | fd = open(device, O_RDONLY); |
| @@ -87,20 +87,14 @@ extern pid_t* find_pid_by_name( char* pidName) | |||
| 87 | 87 | ||
| 88 | if ((strstr(info.command_line, pidName) != NULL) | 88 | if ((strstr(info.command_line, pidName) != NULL) |
| 89 | && (strlen(pidName) == strlen(info.command_line))) { | 89 | && (strlen(pidName) == strlen(info.command_line))) { |
| 90 | pidList=xrealloc( pidList, sizeof(pid_t) * (j+2)); | 90 | pidList=xrealloc( pidList, sizeof(long) * (j+2)); |
| 91 | pidList[j++]=info.pid; | 91 | pidList[j++]=info.pid; |
| 92 | } | 92 | } |
| 93 | } | 93 | } |
| 94 | if (pidList) { | 94 | if (pidList) { |
| 95 | pidList[j]=0; | 95 | pidList[j]=0; |
| 96 | } else if ( strcmp(pidName, "init")==0) { | ||
| 97 | /* If we found nothing and they were trying to kill "init", | ||
| 98 | * guess PID 1 and call it good... Perhaps we should simply | ||
| 99 | * exit if /proc isn't mounted, but this will do for now. */ | ||
| 100 | pidList=xrealloc( pidList, sizeof(pid_t)); | ||
| 101 | pidList[0]=1; | ||
| 102 | } else { | 96 | } else { |
| 103 | pidList=xrealloc( pidList, sizeof(pid_t)); | 97 | pidList=xrealloc( pidList, sizeof(long)); |
| 104 | pidList[0]=-1; | 98 | pidList[0]=-1; |
| 105 | } | 99 | } |
| 106 | 100 | ||
| @@ -124,11 +118,11 @@ extern pid_t* find_pid_by_name( char* pidName) | |||
| 124 | * | 118 | * |
| 125 | * Returns a list of all matching PIDs | 119 | * Returns a list of all matching PIDs |
| 126 | */ | 120 | */ |
| 127 | extern pid_t* find_pid_by_name( char* pidName) | 121 | extern long* find_pid_by_name( char* pidName) |
| 128 | { | 122 | { |
| 129 | DIR *dir; | 123 | DIR *dir; |
| 130 | struct dirent *next; | 124 | struct dirent *next; |
| 131 | pid_t* pidList=NULL; | 125 | long* pidList=NULL; |
| 132 | int i=0; | 126 | int i=0; |
| 133 | 127 | ||
| 134 | dir = opendir("/proc"); | 128 | dir = opendir("/proc"); |
| @@ -162,21 +156,15 @@ extern pid_t* find_pid_by_name( char* pidName) | |||
| 162 | /* Buffer should contain a string like "Name: binary_name" */ | 156 | /* Buffer should contain a string like "Name: binary_name" */ |
| 163 | sscanf(buffer, "%*s %s", name); | 157 | sscanf(buffer, "%*s %s", name); |
| 164 | if (strcmp(name, pidName) == 0) { | 158 | if (strcmp(name, pidName) == 0) { |
| 165 | pidList=xrealloc( pidList, sizeof(pid_t) * (i+2)); | 159 | pidList=xrealloc( pidList, sizeof(long) * (i+2)); |
| 166 | pidList[i++]=strtol(next->d_name, NULL, 0); | 160 | pidList[i++]=strtol(next->d_name, NULL, 0); |
| 167 | } | 161 | } |
| 168 | } | 162 | } |
| 169 | 163 | ||
| 170 | if (pidList) | 164 | if (pidList) { |
| 171 | pidList[i]=0; | 165 | pidList[i]=0; |
| 172 | else if ( strcmp(pidName, "init")==0) { | ||
| 173 | /* If we found nothing and they were trying to kill "init", | ||
| 174 | * guess PID 1 and call it good... Perhaps we should simply | ||
| 175 | * exit if /proc isn't mounted, but this will do for now. */ | ||
| 176 | pidList=xrealloc( pidList, sizeof(pid_t)); | ||
| 177 | pidList[0]=1; | ||
| 178 | } else { | 166 | } else { |
| 179 | pidList=xrealloc( pidList, sizeof(pid_t)); | 167 | pidList=xrealloc( pidList, sizeof(long)); |
| 180 | pidList[0]=-1; | 168 | pidList[0]=-1; |
| 181 | } | 169 | } |
| 182 | return pidList; | 170 | return pidList; |
