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