Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/libbb/find_pid_by_name.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/libbb/find_pid_by_name.c
r1765 r2725 5 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 6 6 * 7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.7 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 8 8 */ 9 9 … … 39 39 */ 40 40 41 /* find_pid_by_name() 41 static int comm_match(procps_status_t *p, const char *procName) 42 { 43 int argv1idx; 44 const char *argv1; 45 46 if (strncmp(p->comm, procName, 15) != 0) 47 return 0; /* comm does not match */ 48 49 /* In Linux, if comm is 15 chars, it is truncated. 50 * (or maybe the name was exactly 15 chars, but there is 51 * no way to know that) */ 52 if (p->comm[14] == '\0') 53 return 1; /* comm is not truncated - matches */ 54 55 /* comm is truncated, but first 15 chars match. 56 * This can be crazily_long_script_name.sh! 57 * The telltale sign is basename(argv[1]) == procName */ 58 59 if (!p->argv0) 60 return 0; 61 62 argv1idx = strlen(p->argv0) + 1; 63 if (argv1idx >= p->argv_len) 64 return 0; 65 argv1 = p->argv0 + argv1idx; 66 67 if (strcmp(bb_basename(argv1), procName) != 0) 68 return 0; 69 70 return 1; 71 } 72 73 /* This finds the pid of the specified process. 74 * Currently, it's implemented by rummaging through 75 * the proc filesystem. 42 76 * 43 * Modified by Vladimir Oleynik for use with libbb/procps.c 44 * This finds the pid of the specified process. 45 * Currently, it's implemented by rummaging through 46 * the proc filesystem. 77 * Returns a list of all matching PIDs 78 * It is the caller's duty to free the returned pidlist. 47 79 * 48 * Returns a list of all matching PIDs 49 * It is the caller's duty to free the returned pidlist. 80 * Modified by Vladimir Oleynik for use with libbb/procps.c 50 81 */ 51 pid_t* find_pid_by_name(const char*procName)82 pid_t* FAST_FUNC find_pid_by_name(const char *procName) 52 83 { 53 84 pid_t* pidList; … … 55 86 procps_status_t* p = NULL; 56 87 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) 88 pidList = xzalloc(sizeof(*pidList)); 89 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN|PSSCAN_EXE))) { 90 if (comm_match(p, procName) 64 91 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/ 65 92 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0) 66 /* TOOD: we can also try exe, do we want that? */ 93 /* or we require /proc/PID/exe link to match */ 94 || (p->exe && strcmp(bb_basename(p->exe), procName) == 0) 67 95 ) { 68 pidList = xrealloc (pidList, sizeof(*pidList) * (i+2));96 pidList = xrealloc_vector(pidList, 2, i); 69 97 pidList[i++] = p->pid; 70 98 } … … 75 103 } 76 104 77 pid_t *pidlist_reverse(pid_t *pidList)105 pid_t* FAST_FUNC pidlist_reverse(pid_t *pidList) 78 106 { 79 107 int i = 0;
Note:
See TracChangeset
for help on using the changeset viewer.