Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (16 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/libbb/find_pid_by_name.c

    r821 r1765  
    55 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
    66 *
    7  * Licensed under the GPL v2, see the file LICENSE in this tarball.
     7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
    88 */
    99
    10 #include <stdio.h>
    11 #include <ctype.h>
    12 #include <string.h>
    13 #include <stdlib.h>
    1410#include "libbb.h"
     11
     12/*
     13In Linux we have three ways to determine "process name":
     141. /proc/PID/stat has "...(name)...", among other things. It's so-called "comm" field.
     152. /proc/PID/cmdline's first NUL-terminated string. It's argv[0] from exec syscall.
     163. /proc/PID/exe symlink. Points to the running executable file.
     17
     18kernel threads:
     19 comm: thread name
     20 cmdline: empty
     21 exe: <readlink fails>
     22
     23executable
     24 comm: first 15 chars of base name
     25 (if executable is a symlink, then first 15 chars of symlink name are used)
     26 cmdline: argv[0] from exec syscall
     27 exe: points to executable (resolves symlink, unlike comm)
     28
     29script (an executable with #!/path/to/interpreter):
     30 comm: first 15 chars of script's base name (symlinks are not resolved)
     31 cmdline: /path/to/interpreter (symlinks are not resolved)
     32 (script name is in argv[1], args are pushed into argv[2] etc)
     33 exe: points to interpreter's executable (symlinks are resolved)
     34
     35If FEATURE_PREFER_APPLETS=y (and more so if FEATURE_SH_STANDALONE=y),
     36some commands started from busybox shell, xargs or find are started by
     37execXXX("/proc/self/exe", applet_name, params....)
     38and therefore comm field contains "exe".
     39*/
    1540
    1641/* find_pid_by_name()
     
    2449 *  It is the caller's duty to free the returned pidlist.
    2550 */
    26 long* find_pid_by_name( const char* pidName)
     51pid_t* find_pid_by_name(const char* procName)
    2752{
    28     long* pidList;
    29     int i=0;
    30     procps_status_t * p;
     53    pid_t* pidList;
     54    int i = 0;
     55    procps_status_t* p = NULL;
    3156
    32     pidList = xmalloc(sizeof(long));
    33     while ((p = procps_scan(0)) != 0)
    34     {
    35         if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) {
    36             pidList=xrealloc( pidList, sizeof(long) * (i+2));
    37             pidList[i++]=p->pid;
     57    pidList = xmalloc(sizeof(*pidList));
     58    while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0))) {
     59        if (
     60        /* we require comm to match and to not be truncated */
     61        /* in Linux, if comm is 15 chars, it may be a truncated
     62         * name, so we don't allow that to match */
     63            (!p->comm[sizeof(p->comm)-2] && strcmp(p->comm, procName) == 0)
     64        /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
     65         || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
     66        /* TOOD: we can also try exe, do we want that? */
     67        ) {
     68            pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
     69            pidList[i++] = p->pid;
    3870        }
    3971    }
    4072
    41     pidList[i] = i==0 ? -1 : 0;
     73    pidList[i] = 0;
    4274    return pidList;
    4375}
    4476
    45 long *pidlist_reverse(long *pidList)
     77pid_t *pidlist_reverse(pid_t *pidList)
    4678{
    47     int i=0;
    48     while (pidList[i] > 0 && ++i);
    49     if ( i-- > 0) {
    50         long k;
     79    int i = 0;
     80    while (pidList[i])
     81        i++;
     82    if (--i >= 0) {
     83        pid_t k;
    5184        int j;
    5285        for (j = 0; i > j; i--, j++) {
Note: See TracChangeset for help on using the changeset viewer.