Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * pidof implementation for busybox |
| 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 | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 12 | enum { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 13 | IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,) |
| 14 | IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,) |
| 15 | OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0, |
| 16 | OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0, |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 17 | }; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 19 | int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 20 | int pidof_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 21 | { |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 22 | unsigned first = 1; |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 23 | unsigned opt; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 24 | #if ENABLE_FEATURE_PIDOF_OMIT |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 25 | llist_t *omits = NULL; /* list of pids to omit */ |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 26 | opt_complementary = "o::"; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 27 | #endif |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 28 | |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 29 | /* do unconditional option parsing */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 30 | opt = getopt32(argv, "" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 31 | IF_FEATURE_PIDOF_SINGLE ("s") |
| 32 | IF_FEATURE_PIDOF_OMIT("o:", &omits)); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 33 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 34 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 35 | /* fill omit list. */ |
| 36 | { |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 37 | llist_t *omits_p = omits; |
Denis Vlasenko | 0b791d9 | 2009-04-13 20:52:00 +0000 | [diff] [blame] | 38 | while (1) { |
| 39 | omits_p = llist_find_str(omits_p, "%PPID"); |
| 40 | if (!omits_p) |
| 41 | break; |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 42 | /* are we asked to exclude the parent's process ID? */ |
Denis Vlasenko | 0b791d9 | 2009-04-13 20:52:00 +0000 | [diff] [blame] | 43 | omits_p->data = utoa((unsigned)getppid()); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 44 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 45 | } |
| 46 | #endif |
| 47 | /* Looks like everything is set to go. */ |
Denis Vlasenko | 6446c2d | 2007-11-25 04:54:13 +0000 | [diff] [blame] | 48 | argv += optind; |
| 49 | while (*argv) { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 50 | pid_t *pidList; |
| 51 | pid_t *pl; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 52 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 53 | /* reverse the pidlist like GNU pidof does. */ |
Denis Vlasenko | 6446c2d | 2007-11-25 04:54:13 +0000 | [diff] [blame] | 54 | pidList = pidlist_reverse(find_pid_by_name(*argv)); |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 55 | for (pl = pidList; *pl; pl++) { |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 56 | #if ENABLE_FEATURE_PIDOF_OMIT |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 57 | if (opt & OPT_OMIT) { |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 58 | llist_t *omits_p = omits; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 59 | while (omits_p) { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 60 | if (xatoul(omits_p->data) == (unsigned long)(*pl)) { |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 61 | goto omitting; |
| 62 | } |
| 63 | omits_p = omits_p->link; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 64 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 65 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 66 | #endif |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 67 | printf(" %u" + first, (unsigned)*pl); |
| 68 | first = 0; |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 69 | if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) |
Robert Griebl | d11edf9 | 2002-05-22 23:38:12 +0000 | [diff] [blame] | 70 | break; |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 71 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 72 | omitting: ; |
| 73 | #endif |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 74 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 75 | free(pidList); |
Denis Vlasenko | 6446c2d | 2007-11-25 04:54:13 +0000 | [diff] [blame] | 76 | argv++; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 77 | } |
Denis Vlasenko | 6446c2d | 2007-11-25 04:54:13 +0000 | [diff] [blame] | 78 | if (!first) |
| 79 | bb_putchar('\n'); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 80 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 81 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 82 | if (ENABLE_FEATURE_CLEAN_UP) |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 83 | llist_free(omits, NULL); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 84 | #endif |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 85 | return first; /* 1 (failure) - no processes found */ |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 86 | } |