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 | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 19 | int pidof_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 20 | int pidof_main(int argc, 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 | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 50 | while (optind < argc) { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 51 | pid_t *pidList; |
| 52 | pid_t *pl; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 53 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 54 | /* reverse the pidlist like GNU pidof does. */ |
| 55 | pidList = pidlist_reverse(find_pid_by_name(argv[optind])); |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 56 | for (pl = pidList; *pl; pl++) { |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 57 | #if ENABLE_FEATURE_PIDOF_OMIT |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 58 | if (opt & OPT_OMIT) { |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 59 | llist_t *omits_p = omits; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 60 | while (omits_p) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 61 | if (xatoul(omits_p->data) == *pl) { |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 62 | goto omitting; |
| 63 | } |
| 64 | omits_p = omits_p->link; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 65 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 66 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 67 | #endif |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 68 | printf(" %u" + first, (unsigned)*pl); |
| 69 | first = 0; |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame] | 70 | if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) |
Robert Griebl | d11edf9 | 2002-05-22 23:38:12 +0000 | [diff] [blame] | 71 | break; |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 72 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 73 | omitting: ; |
| 74 | #endif |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 75 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 76 | free(pidList); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 77 | optind++; |
| 78 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 79 | 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 | } |