Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/procps/pgrep.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 983 by niro, Fri Apr 24 18:33:46 2009 UTC revision 984 by niro, Sun May 30 11:32:42 2010 UTC
# Line 6  Line 6 
6   *   *
7   * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.   * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8   */   */
   
9  #include "libbb.h"  #include "libbb.h"
10  #include "xregex.h"  #include "xregex.h"
11    
# Line 15  Line 14 
14  #define pkill (ENABLE_PKILL && applet_name[1] == 'k')  #define pkill (ENABLE_PKILL && applet_name[1] == 'k')
15    
16  enum {  enum {
17   /* "vlfxon" */   /* "vlfxons:P:" */
18   PGREPOPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */   OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
19   PGREPOPTBIT_L,   OPTBIT_L,
20   PGREPOPTBIT_F,   OPTBIT_F,
21   PGREPOPTBIT_X,   OPTBIT_X,
22   PGREPOPTBIT_O,   OPTBIT_O,
23   PGREPOPTBIT_N,   OPTBIT_N,
24     OPTBIT_S,
25     OPTBIT_P,
26  };  };
27    
28  #define OPT_INVERT (opt & (1 << PGREPOPTBIT_V))  #define OPT_INVERT (opt & (1 << OPTBIT_V))
29  #define OPT_LIST (opt & (1 << PGREPOPTBIT_L))  #define OPT_LIST (opt & (1 << OPTBIT_L))
30  #define OPT_FULL (opt & (1 << PGREPOPTBIT_F))  #define OPT_FULL (opt & (1 << OPTBIT_F))
31  #define OPT_ANCHOR (opt & (1 << PGREPOPTBIT_X))  #define OPT_ANCHOR (opt & (1 << OPTBIT_X))
32  #define OPT_FIRST (opt & (1 << PGREPOPTBIT_O))  #define OPT_FIRST (opt & (1 << OPTBIT_O))
33  #define OPT_LAST (opt & (1 << PGREPOPTBIT_N))  #define OPT_LAST (opt & (1 << OPTBIT_N))
34    #define OPT_SID (opt & (1 << OPTBIT_S))
35    #define OPT_PPID (opt & (1 << OPTBIT_P))
36    
37  static void act(unsigned pid, char *cmd, int signo, unsigned opt)  static void act(unsigned pid, char *cmd, int signo)
38  {  {
39   if (pgrep) {   if (pgrep) {
40   if (OPT_LIST)   if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */
41   printf("%d %s\n", pid, cmd);   printf("%d %s\n", pid, cmd);
42   else   else
43   printf("%d\n", pid);   printf("%d\n", pid);
# Line 45  static void act(unsigned pid, char *cmd, Line 48  static void act(unsigned pid, char *cmd,
48  int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;  int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49  int pgrep_main(int argc UNUSED_PARAM, char **argv)  int pgrep_main(int argc UNUSED_PARAM, char **argv)
50  {  {
51   unsigned pid = getpid();   unsigned pid;
52   int signo = SIGTERM;   int signo;
53   unsigned opt;   unsigned opt;
54   int scan_mask = PSSCAN_COMM;   int scan_mask;
  char *first_arg;  
  int first_arg_idx;  
55   int matched_pid;   int matched_pid;
56     int sid2match, ppid2match;
57   char *cmd_last;   char *cmd_last;
58   procps_status_t *proc;   procps_status_t *proc;
59   /* These are initialized to 0 */   /* These are initialized to 0 */
# Line 64  int pgrep_main(int argc UNUSED_PARAM, ch Line 66  int pgrep_main(int argc UNUSED_PARAM, ch
66    
67   memset(&Z, 0, sizeof(Z));   memset(&Z, 0, sizeof(Z));
68    
69   /* We must avoid interpreting -NUM (signal num) as an option */   /* Parse -SIGNAL for pkill. Must be first option, if present */
70   first_arg_idx = 1;   signo = SIGTERM;
71   while (1) {   if (pkill && argv[1] && argv[1][0] == '-') {
72   first_arg = argv[first_arg_idx];   int temp = get_signum(argv[1]+1);
73   if (!first_arg)   if (temp != -1) {
74   break;   signo = temp;
75   /* not "-<small_letter>..."? */   argv++;
  if (first_arg[0] != '-' || first_arg[1] < 'a' || first_arg[1] > 'z') {  
  argv[first_arg_idx] = NULL; /* terminate argv here */  
  break;  
76   }   }
  first_arg_idx++;  
77   }   }
  opt = getopt32(argv, "vlfxon");  
  argv[first_arg_idx] = first_arg;  
78    
79     /* Parse remaining options */
80     ppid2match = -1;
81     sid2match = -1;
82     opt_complementary = "s+:P+"; /* numeric opts */
83     opt = getopt32(argv, "vlfxons:P:", &sid2match, &ppid2match);
84   argv += optind;   argv += optind;
  //argc -= optind; - unused anyway  
  if (OPT_FULL)  
  scan_mask |= PSSCAN_ARGVN;  
85    
86   if (pkill) {   if (pkill && OPT_LIST) { /* -l: print the whole signal list */
87   if (OPT_LIST) { /* -l: print the whole signal list */   print_signames();
88   print_signames();   return 0;
  return 0;  
  }  
  if (first_arg && first_arg[0] == '-') {  
  signo = get_signum(&first_arg[1]);  
  if (signo < 0) /* || signo > MAX_SIGNUM ? */  
  bb_error_msg_and_die("bad signal name '%s'", &first_arg[1]);  
  argv++;  
  }  
89   }   }
90    
91   /* One pattern is required */   pid = getpid();
92   if (!argv[0] || argv[1])   if (sid2match == 0)
93     sid2match = getsid(pid);
94    
95     scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
96     if (OPT_FULL)
97     scan_mask |= PSSCAN_ARGVN;
98    
99     /* One pattern is required, if no -s and no -P */
100     if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
101   bb_show_usage();   bb_show_usage();
102    
103   xregcomp(&re_buffer, argv[0], 0);   if (argv[0])
104     xregcomp(&re_buffer, argv[0], 0);
105    
106   matched_pid = 0;   matched_pid = 0;
107   cmd_last = NULL;   cmd_last = NULL;
108   proc = NULL;   proc = NULL;
109   while ((proc = procps_scan(proc, scan_mask)) != NULL) {   while ((proc = procps_scan(proc, scan_mask)) != NULL) {
110   char *cmd;   char *cmd;
111    
112   if (proc->pid == pid)   if (proc->pid == pid)
113   continue;   continue;
114    
115   cmd = proc->argv0;   cmd = proc->argv0;
116   if (!cmd) {   if (!cmd) {
117   cmd = proc->comm;   cmd = proc->comm;
118   } else {   } else {
119   int i = proc->argv_len;   int i = proc->argv_len;
120   while (i) {   while (--i >= 0) {
121   if (!cmd[i]) cmd[i] = ' ';   if ((unsigned char)cmd[i] < ' ')
122   i--;   cmd[i] = ' ';
123   }   }
124   }   }
125    
126     if (ppid2match >= 0 && ppid2match != proc->ppid)
127     continue;
128     if (sid2match >= 0  && sid2match != proc->sid)
129     continue;
130    
131   /* NB: OPT_INVERT is always 0 or 1 */   /* NB: OPT_INVERT is always 0 or 1 */
132   if ((regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */   if (!argv[0] ||
133        (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
134       && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))) ^ OPT_INVERT       && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))) ^ OPT_INVERT
135   ) {   ) {
136   matched_pid = proc->pid;   matched_pid = proc->pid;
# Line 130  int pgrep_main(int argc UNUSED_PARAM, ch Line 139  int pgrep_main(int argc UNUSED_PARAM, ch
139   cmd_last = xstrdup(cmd);   cmd_last = xstrdup(cmd);
140   continue;   continue;
141   }   }
142   act(proc->pid, cmd, signo, opt);   act(proc->pid, cmd, signo);
143   if (OPT_FIRST)   if (OPT_FIRST)
144   break;   break;
145   }   }
146   }   }
147    
148   if (cmd_last) {   if (cmd_last) {
149   act(matched_pid, cmd_last, signo, opt);   act(matched_pid, cmd_last, signo);
150   if (ENABLE_FEATURE_CLEAN_UP)   if (ENABLE_FEATURE_CLEAN_UP)
151   free(cmd_last);   free(cmd_last);
152   }   }

Legend:
Removed from v.983  
changed lines
  Added in v.984