Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <ctype.h> |
| 24 | #include <string.h> |
| 25 | #include <dirent.h> |
| 26 | #include <stdlib.h> |
| 27 | #include "libbb.h" |
| 28 | |
Eric Andersen | 8a6dbf6 | 2001-03-17 00:05:42 +0000 | [diff] [blame] | 29 | #define READ_BUF_SIZE 50 |
| 30 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | |
| 32 | /* For Erik's nifty devps device driver */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 33 | #ifdef CONFIG_FEATURE_USE_DEVPS_PATCH |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | #include <linux/devps.h> |
| 35 | |
| 36 | /* find_pid_by_name() |
| 37 | * |
| 38 | * This finds the pid of the specified process, |
| 39 | * by using the /dev/ps device driver. |
| 40 | * |
| 41 | * Returns a list of all matching PIDs |
| 42 | */ |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 43 | extern long* find_pid_by_name( char* pidName) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 44 | { |
| 45 | int fd, i, j; |
| 46 | char device[] = "/dev/ps"; |
| 47 | pid_t num_pids; |
| 48 | pid_t* pid_array = NULL; |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 49 | long* pidList=NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 50 | |
| 51 | /* open device */ |
| 52 | fd = open(device, O_RDONLY); |
| 53 | if (fd < 0) |
| 54 | perror_msg_and_die("open failed for `%s'", device); |
| 55 | |
| 56 | /* Find out how many processes there are */ |
| 57 | if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) |
| 58 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); |
| 59 | |
| 60 | /* Allocate some memory -- grab a few extras just in case |
| 61 | * some new processes start up while we wait. The kernel will |
| 62 | * just ignore any extras if we give it too many, and will trunc. |
| 63 | * the list if we give it too few. */ |
| 64 | pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t)); |
| 65 | pid_array[0] = num_pids+10; |
| 66 | |
| 67 | /* Now grab the pid list */ |
| 68 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) |
| 69 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); |
| 70 | |
| 71 | /* Now search for a match */ |
| 72 | for (i=1, j=0; i<pid_array[0] ; i++) { |
| 73 | char* p; |
| 74 | struct pid_info info; |
| 75 | |
| 76 | info.pid = pid_array[i]; |
| 77 | if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0) |
| 78 | perror_msg_and_die("\nDEVPS_GET_PID_INFO"); |
| 79 | |
| 80 | /* Make sure we only match on the process name */ |
| 81 | p=info.command_line+1; |
| 82 | while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) { |
| 83 | (p)++; |
| 84 | } |
| 85 | if (isspace(*(p))) |
| 86 | *p='\0'; |
| 87 | |
| 88 | if ((strstr(info.command_line, pidName) != NULL) |
| 89 | && (strlen(pidName) == strlen(info.command_line))) { |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 90 | pidList=xrealloc( pidList, sizeof(long) * (j+2)); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 91 | pidList[j++]=info.pid; |
| 92 | } |
| 93 | } |
Eric Andersen | d50a619 | 2001-07-05 15:56:36 +0000 | [diff] [blame] | 94 | if (pidList) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 95 | pidList[j]=0; |
Eric Andersen | d50a619 | 2001-07-05 15:56:36 +0000 | [diff] [blame] | 96 | } else { |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 97 | pidList=xrealloc( pidList, sizeof(long)); |
Eric Andersen | d50a619 | 2001-07-05 15:56:36 +0000 | [diff] [blame] | 98 | pidList[0]=-1; |
| 99 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 100 | |
| 101 | /* Free memory */ |
| 102 | free( pid_array); |
| 103 | |
| 104 | /* close device */ |
| 105 | if (close (fd) != 0) |
| 106 | perror_msg_and_die("close failed for `%s'", device); |
| 107 | |
| 108 | return pidList; |
| 109 | } |
| 110 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 111 | #else /* CONFIG_FEATURE_USE_DEVPS_PATCH */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 112 | |
| 113 | /* find_pid_by_name() |
| 114 | * |
| 115 | * This finds the pid of the specified process. |
| 116 | * Currently, it's implemented by rummaging through |
| 117 | * the proc filesystem. |
| 118 | * |
| 119 | * Returns a list of all matching PIDs |
| 120 | */ |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 121 | extern long* find_pid_by_name( char* pidName) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 122 | { |
| 123 | DIR *dir; |
| 124 | struct dirent *next; |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 125 | long* pidList=NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 126 | int i=0; |
| 127 | |
| 128 | dir = opendir("/proc"); |
| 129 | if (!dir) |
| 130 | perror_msg_and_die("Cannot open /proc"); |
| 131 | |
| 132 | while ((next = readdir(dir)) != NULL) { |
| 133 | FILE *status; |
Eric Andersen | 8a6dbf6 | 2001-03-17 00:05:42 +0000 | [diff] [blame] | 134 | char filename[READ_BUF_SIZE]; |
| 135 | char buffer[READ_BUF_SIZE]; |
| 136 | char name[READ_BUF_SIZE]; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 137 | |
Eric Andersen | 91a6318 | 2001-06-26 22:44:09 +0000 | [diff] [blame] | 138 | /* Must skip ".." since that is outside /proc */ |
| 139 | if (strcmp(next->d_name, "..") == 0) |
| 140 | continue; |
| 141 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 142 | /* If it isn't a number, we don't want it */ |
| 143 | if (!isdigit(*next->d_name)) |
| 144 | continue; |
| 145 | |
Eric Andersen | 8a6dbf6 | 2001-03-17 00:05:42 +0000 | [diff] [blame] | 146 | sprintf(filename, "/proc/%s/status", next->d_name); |
| 147 | if (! (status = fopen(filename, "r")) ) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 148 | continue; |
| 149 | } |
Eric Andersen | 8a6dbf6 | 2001-03-17 00:05:42 +0000 | [diff] [blame] | 150 | if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) { |
| 151 | fclose(status); |
| 152 | continue; |
| 153 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 154 | fclose(status); |
| 155 | |
Eric Andersen | 8a6dbf6 | 2001-03-17 00:05:42 +0000 | [diff] [blame] | 156 | /* Buffer should contain a string like "Name: binary_name" */ |
| 157 | sscanf(buffer, "%*s %s", name); |
| 158 | if (strcmp(name, pidName) == 0) { |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 159 | pidList=xrealloc( pidList, sizeof(long) * (i+2)); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 160 | pidList[i++]=strtol(next->d_name, NULL, 0); |
| 161 | } |
| 162 | } |
| 163 | |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 164 | if (pidList) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 165 | pidList[i]=0; |
Eric Andersen | d50a619 | 2001-07-05 15:56:36 +0000 | [diff] [blame] | 166 | } else { |
Eric Andersen | b24d656 | 2001-12-06 14:52:32 +0000 | [diff] [blame] | 167 | pidList=xrealloc( pidList, sizeof(long)); |
Eric Andersen | d50a619 | 2001-07-05 15:56:36 +0000 | [diff] [blame] | 168 | pidList[0]=-1; |
Eric Andersen | 91a6318 | 2001-06-26 22:44:09 +0000 | [diff] [blame] | 169 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 170 | return pidList; |
| 171 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 172 | #endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 173 | |
| 174 | /* END CODE */ |
| 175 | /* |
| 176 | Local Variables: |
| 177 | c-file-style: "linux" |
| 178 | c-basic-offset: 4 |
| 179 | tab-width: 4 |
| 180 | End: |
| 181 | */ |