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 | |
| 10 | #include <stdio.h> |
| 11 | #include <ctype.h> |
| 12 | #include <string.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include "libbb.h" |
| 15 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 16 | /* find_pid_by_name() |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 17 | * |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 18 | * Modified by Vladimir Oleynik for use with libbb/procps.c |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 19 | * This finds the pid of the specified process. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 20 | * Currently, it's implemented by rummaging through |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 21 | * the proc filesystem. |
| 22 | * |
| 23 | * Returns a list of all matching PIDs |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 24 | * It is the caller's duty to free the returned pidlist. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 25 | */ |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 26 | long* find_pid_by_name(const char* pidName) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 28 | long* pidList; |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 29 | int i = 0; |
| 30 | procps_status_t* p; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 32 | pidList = xmalloc(sizeof(long)); |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 33 | while ((p = procps_scan(0)) != 0) { |
Eric Andersen | 5378fbc | 2003-08-06 08:22:10 +0000 | [diff] [blame] | 34 | if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) { |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 35 | pidList = xrealloc( pidList, sizeof(long) * (i+2)); |
| 36 | pidList[i++] = p->pid; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 40 | pidList[i] = i==0 ? -1 : 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 41 | return pidList; |
| 42 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 43 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 44 | long *pidlist_reverse(long *pidList) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 45 | { |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 46 | int i = 0; |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 47 | while (pidList[i] > 0 && ++i); |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 48 | if (i-- > 0) { |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 49 | long k; |
| 50 | int j; |
| 51 | for (j = 0; i > j; i--, j++) { |
| 52 | k = pidList[i]; |
| 53 | pidList[i] = pidList[j]; |
| 54 | pidList[j] = k; |
| 55 | } |
| 56 | } |
| 57 | return pidList; |
| 58 | } |