source: MondoRescue/branches/3.2/mindi-busybox/procps/pidof.c@ 3232

Last change on this file since 3232 was 3232, checked in by Bruno Cornec, 10 years ago
  • Update mindi-busybox to 1.21.1
File size: 2.9 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * pidof implementation for busybox
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
[2725]7 * Licensed under GPLv2, see file LICENSE in this source tree.
[821]8 */
9
[3232]10//usage:#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
11//usage:#define pidof_trivial_usage
12//usage: "[OPTIONS] [NAME]..."
13//usage:#define USAGE_PIDOF "\n"
14//usage:#else
15//usage:#define pidof_trivial_usage
16//usage: "[NAME]..."
17//usage:#define USAGE_PIDOF /* none */
18//usage:#endif
19//usage:#define pidof_full_usage "\n\n"
20//usage: "List PIDs of all processes with names that match NAMEs"
21//usage: USAGE_PIDOF
22//usage: IF_FEATURE_PIDOF_SINGLE(
23//usage: "\n -s Show only one PID"
24//usage: )
25//usage: IF_FEATURE_PIDOF_OMIT(
26//usage: "\n -o PID Omit given pid"
27//usage: "\n Use %PPID to omit pid of pidof's parent"
28//usage: )
29//usage:
30//usage:#define pidof_example_usage
31//usage: "$ pidof init\n"
32//usage: "1\n"
33//usage: IF_FEATURE_PIDOF_OMIT(
34//usage: "$ pidof /bin/sh\n20351 5973 5950\n")
35//usage: IF_FEATURE_PIDOF_OMIT(
36//usage: "$ pidof /bin/sh -o %PPID\n20351 5950")
37
[1765]38#include "libbb.h"
[821]39
[1765]40enum {
[2725]41 IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
42 IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
43 OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
44 OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
[1765]45};
[821]46
[2725]47int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
48int pidof_main(int argc UNUSED_PARAM, char **argv)
[821]49{
[1765]50 unsigned first = 1;
51 unsigned opt;
[821]52#if ENABLE_FEATURE_PIDOF_OMIT
53 llist_t *omits = NULL; /* list of pids to omit */
[1765]54 opt_complementary = "o::";
[821]55#endif
56
57 /* do unconditional option parsing */
[1765]58 opt = getopt32(argv, ""
[2725]59 IF_FEATURE_PIDOF_SINGLE ("s")
60 IF_FEATURE_PIDOF_OMIT("o:", &omits));
[821]61
62#if ENABLE_FEATURE_PIDOF_OMIT
63 /* fill omit list. */
64 {
[1765]65 llist_t *omits_p = omits;
[2725]66 while (1) {
67 omits_p = llist_find_str(omits_p, "%PPID");
68 if (!omits_p)
69 break;
[821]70 /* are we asked to exclude the parent's process ID? */
[2725]71 omits_p->data = utoa((unsigned)getppid());
[821]72 }
73 }
74#endif
75 /* Looks like everything is set to go. */
[2725]76 argv += optind;
77 while (*argv) {
[1765]78 pid_t *pidList;
79 pid_t *pl;
[821]80
81 /* reverse the pidlist like GNU pidof does. */
[2725]82 pidList = pidlist_reverse(find_pid_by_name(*argv));
[1765]83 for (pl = pidList; *pl; pl++) {
[821]84#if ENABLE_FEATURE_PIDOF_OMIT
[1765]85 if (opt & OPT_OMIT) {
[821]86 llist_t *omits_p = omits;
[1765]87 while (omits_p) {
[2725]88 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
[1765]89 goto omitting;
90 }
91 omits_p = omits_p->link;
92 }
[821]93 }
94#endif
[1765]95 printf(" %u" + first, (unsigned)*pl);
96 first = 0;
97 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
[821]98 break;
[1765]99#if ENABLE_FEATURE_PIDOF_OMIT
100 omitting: ;
101#endif
[821]102 }
103 free(pidList);
[2725]104 argv++;
[821]105 }
[2725]106 if (!first)
107 bb_putchar('\n');
[821]108
109#if ENABLE_FEATURE_PIDOF_OMIT
110 if (ENABLE_FEATURE_CLEAN_UP)
111 llist_free(omits, NULL);
112#endif
[1765]113 return first; /* 1 (failure) - no processes found */
[821]114}
Note: See TracBrowser for help on using the repository browser.