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 | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 12 | /* find_pid_by_name() |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 13 | * |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 14 | * Modified by Vladimir Oleynik for use with libbb/procps.c |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 15 | * This finds the pid of the specified process. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 16 | * Currently, it's implemented by rummaging through |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | * the proc filesystem. |
| 18 | * |
| 19 | * Returns a list of all matching PIDs |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 20 | * It is the caller's duty to free the returned pidlist. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 21 | */ |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 22 | pid_t* find_pid_by_name(const char* procName) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 24 | pid_t* pidList; |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 25 | int i = 0; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 26 | procps_status_t* p = NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 28 | pidList = xmalloc(sizeof(*pidList)); |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 29 | while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM))) { |
| 30 | if (strncmp(p->comm, procName, sizeof(p->comm)-1) == 0) { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 31 | pidList = xrealloc(pidList, sizeof(*pidList) * (i+2)); |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 32 | pidList[i++] = p->pid; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 36 | pidList[i] = 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 37 | return pidList; |
| 38 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 40 | pid_t *pidlist_reverse(pid_t *pidList) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 41 | { |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 42 | int i = 0; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 43 | while (pidList[i]) |
| 44 | i++; |
| 45 | if (--i >= 0) { |
| 46 | pid_t k; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 47 | int j; |
| 48 | for (j = 0; i > j; i--, j++) { |
| 49 | k = pidList[i]; |
| 50 | pidList[i] = pidList[j]; |
| 51 | pidList[j] = k; |
| 52 | } |
| 53 | } |
| 54 | return pidList; |
| 55 | } |