blob: a648137e14e19376493a28a3172fd06e1c39b8cb [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
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 Andersenaad1a882001-03-16 22:47:14 +000020 */
21
22#include <stdio.h>
23#include <ctype.h>
24#include <string.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000025#include <stdlib.h>
26#include "libbb.h"
27
Eric Andersen8a6dbf62001-03-17 00:05:42 +000028#define READ_BUF_SIZE 50
29
Eric Andersenaad1a882001-03-16 22:47:14 +000030
31/* For Erik's nifty devps device driver */
Eric Andersenbdfd0d72001-10-24 05:00:29 +000032#ifdef CONFIG_FEATURE_USE_DEVPS_PATCH
Eric Andersenaad1a882001-03-16 22:47:14 +000033#include <linux/devps.h>
34
35/* find_pid_by_name()
36 *
37 * This finds the pid of the specified process,
38 * by using the /dev/ps device driver.
39 *
40 * Returns a list of all matching PIDs
41 */
Eric Andersen44608e92002-10-22 12:21:15 +000042extern long* find_pid_by_name( const char* pidName)
Eric Andersenaad1a882001-03-16 22:47:14 +000043{
44 int fd, i, j;
45 char device[] = "/dev/ps";
46 pid_t num_pids;
47 pid_t* pid_array = NULL;
Eric Andersenb24d6562001-12-06 14:52:32 +000048 long* pidList=NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000049
50 /* open device */
51 fd = open(device, O_RDONLY);
52 if (fd < 0)
53 perror_msg_and_die("open failed for `%s'", device);
54
55 /* Find out how many processes there are */
56 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
57 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
58
59 /* Allocate some memory -- grab a few extras just in case
60 * some new processes start up while we wait. The kernel will
61 * just ignore any extras if we give it too many, and will trunc.
62 * the list if we give it too few. */
63 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
64 pid_array[0] = num_pids+10;
65
66 /* Now grab the pid list */
67 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
68 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
69
70 /* Now search for a match */
71 for (i=1, j=0; i<pid_array[0] ; i++) {
72 char* p;
73 struct pid_info info;
74
75 info.pid = pid_array[i];
76 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
77 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
78
79 /* Make sure we only match on the process name */
80 p=info.command_line+1;
81 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
82 (p)++;
83 }
84 if (isspace(*(p)))
85 *p='\0';
86
87 if ((strstr(info.command_line, pidName) != NULL)
88 && (strlen(pidName) == strlen(info.command_line))) {
Eric Andersenb24d6562001-12-06 14:52:32 +000089 pidList=xrealloc( pidList, sizeof(long) * (j+2));
Eric Andersenaad1a882001-03-16 22:47:14 +000090 pidList[j++]=info.pid;
91 }
92 }
Eric Andersend50a6192001-07-05 15:56:36 +000093 if (pidList) {
Eric Andersenaad1a882001-03-16 22:47:14 +000094 pidList[j]=0;
Eric Andersend50a6192001-07-05 15:56:36 +000095 } else {
Eric Andersenb24d6562001-12-06 14:52:32 +000096 pidList=xrealloc( pidList, sizeof(long));
Eric Andersend50a6192001-07-05 15:56:36 +000097 pidList[0]=-1;
98 }
Eric Andersenaad1a882001-03-16 22:47:14 +000099
100 /* Free memory */
101 free( pid_array);
102
103 /* close device */
104 if (close (fd) != 0)
105 perror_msg_and_die("close failed for `%s'", device);
106
107 return pidList;
108}
109
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000110#else /* CONFIG_FEATURE_USE_DEVPS_PATCH */
Eric Andersenaad1a882001-03-16 22:47:14 +0000111
112/* find_pid_by_name()
113 *
Eric Andersen44608e92002-10-22 12:21:15 +0000114 * Modified by Vladimir Oleynik for use with libbb/procps.c
Eric Andersenaad1a882001-03-16 22:47:14 +0000115 * 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 Andersen44608e92002-10-22 12:21:15 +0000121extern long* find_pid_by_name( const char* pidName)
Eric Andersenaad1a882001-03-16 22:47:14 +0000122{
Eric Andersen44608e92002-10-22 12:21:15 +0000123 long* pidList;
Eric Andersenaad1a882001-03-16 22:47:14 +0000124 int i=0;
Eric Andersen44608e92002-10-22 12:21:15 +0000125 procps_status_t * p;
Eric Andersenaad1a882001-03-16 22:47:14 +0000126
Eric Andersen44608e92002-10-22 12:21:15 +0000127 pidList = xmalloc(sizeof(long));
128 while ((p = procps_scan(0)) != 0) {
129 if (strcmp(p->short_cmd, pidName) == 0) {
Eric Andersenb24d6562001-12-06 14:52:32 +0000130 pidList=xrealloc( pidList, sizeof(long) * (i+2));
Eric Andersen44608e92002-10-22 12:21:15 +0000131 pidList[i++]=p->pid;
Eric Andersenaad1a882001-03-16 22:47:14 +0000132 }
133 }
134
Eric Andersen44608e92002-10-22 12:21:15 +0000135 pidList[i] = i==0 ? -1 : 0;
Eric Andersenaad1a882001-03-16 22:47:14 +0000136 return pidList;
137}
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000138#endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */
Eric Andersenaad1a882001-03-16 22:47:14 +0000139
140/* END CODE */
141/*
142Local Variables:
143c-file-style: "linux"
144c-basic-offset: 4
145tab-width: 4
146End:
147*/