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 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 10 | #include "busybox.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 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 19 | int pidof_main(int argc, char **argv) |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 20 | { |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame^] | 21 | unsigned first = 1; |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 22 | unsigned fail = 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 | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame^] | 30 | opt = getopt32(argc, argv, "" |
| 31 | USE_FEATURE_PIDOF_SINGLE ("s") |
| 32 | USE_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 | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame^] | 37 | char getppid_str[sizeof(int)*3 + 1]; |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 38 | llist_t * omits_p = omits; |
| 39 | while (omits_p) { |
| 40 | /* are we asked to exclude the parent's process ID? */ |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 41 | if (!strncmp(omits_p->data, "%PPID", 5)) { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 42 | llist_pop(&omits_p); |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame^] | 43 | snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid()); |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 44 | llist_add_to(&omits_p, getppid_str); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 45 | } |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 46 | omits_p = omits_p->link; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 47 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 48 | } |
| 49 | #endif |
| 50 | /* Looks like everything is set to go. */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 51 | while (optind < argc) { |
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. */ |
| 56 | pidList = pidlist_reverse(find_pid_by_name(argv[optind])); |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 57 | for (pl = pidList; *pl; pl++) { |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame^] | 58 | SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 59 | #if ENABLE_FEATURE_PIDOF_OMIT |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame^] | 60 | if (opt & OPT_OMIT) { |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 61 | llist_t *omits_p = omits; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 62 | while (omits_p) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 63 | if (xatoul(omits_p->data) == *pl) { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 64 | omitted = 1; |
| 65 | break; |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 66 | } else |
| 67 | omits_p = omits_p->link; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 68 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 69 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 70 | #endif |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 71 | if (!omitted) { |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame^] | 72 | printf(" %u" + first, (unsigned)*pl); |
| 73 | first = 0; |
Bernhard Reutner-Fischer | ab52db8 | 2005-10-07 15:44:37 +0000 | [diff] [blame] | 74 | } |
| 75 | fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); |
| 76 | |
Denis Vlasenko | 048c93c | 2006-11-01 09:17:47 +0000 | [diff] [blame^] | 77 | if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) |
Robert Griebl | d11edf9 | 2002-05-22 23:38:12 +0000 | [diff] [blame] | 78 | break; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 79 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 80 | free(pidList); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 81 | optind++; |
| 82 | } |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 83 | putchar('\n'); |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 84 | |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 85 | #if ENABLE_FEATURE_PIDOF_OMIT |
| 86 | if (ENABLE_FEATURE_CLEAN_UP) |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 87 | llist_free(omits, NULL); |
Bernhard Reutner-Fischer | 81c3a51 | 2005-10-06 15:37:02 +0000 | [diff] [blame] | 88 | #endif |
Robert Griebl | d11edf9 | 2002-05-22 23:38:12 +0000 | [diff] [blame] | 89 | return fail ? EXIT_FAILURE : EXIT_SUCCESS; |
Eric Andersen | 221b2ea | 2001-07-31 19:06:07 +0000 | [diff] [blame] | 90 | } |