Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | pidof: add -m option |
| 2 | |
| 3 | When used with -o, will also omit any processes that have the same |
| 4 | argv[0] and argv[1] as any explicitly omitted process ids. This can be |
| 5 | used to avoid multiple shell scripts concurrently calling pidof returning |
| 6 | each other's pids. |
| 7 | |
| 8 | https://bugzilla.redhat.com/show_bug.cgi?id=883856 |
| 9 | |
| 10 | Upstream-Status: backport |
| 11 | Imported patch from: https://bugzilla.redhat.com/attachment.cgi?id=658166 |
| 12 | |
| 13 | Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com> |
| 14 | --- |
| 15 | man/pidof.8 | 6 ++++++ |
| 16 | src/killall5.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
| 17 | 2 files changed, 65 insertions(+), 3 deletions(-) |
| 18 | |
| 19 | diff --git a/man/pidof.8 b/man/pidof.8 |
| 20 | --- a/man/pidof.8 |
| 21 | +++ b/man/pidof.8 |
| 22 | @@ -24,6 +24,7 @@ pidof -- find the process ID of a running program. |
| 23 | .RB [ \-c ] |
| 24 | .RB [ \-n ] |
| 25 | .RB [ \-x ] |
| 26 | +.RB [ \-m ] |
| 27 | .RB [ \-o |
| 28 | .IR omitpid[,omitpid..] ] |
| 29 | .RB [ \-o |
| 30 | @@ -63,6 +64,11 @@ shells running the named scripts. |
| 31 | Tells \fIpidof\fP to omit processes with that process id. The special |
| 32 | pid \fB%PPID\fP can be used to name the parent process of the \fIpidof\fP |
| 33 | program, in other words the calling shell or shell script. |
| 34 | +.IP -m |
| 35 | +When used with -o, will also omit any processes that have the same |
| 36 | +argv[0] and argv[1] as any explicitly omitted process ids. This can be |
| 37 | +used to avoid multiple shell scripts concurrently calling pidof returning |
| 38 | +each other's pids. |
| 39 | .SH "EXIT STATUS" |
| 40 | .TP |
| 41 | .B 0 |
| 42 | diff --git a/src/killall5.c b/src/killall5.c |
| 43 | index 5937d98..e73885e 100644 |
| 44 | --- a/src/killall5.c |
| 45 | +++ b/src/killall5.c |
| 46 | @@ -118,6 +118,7 @@ typedef struct _s_nfs |
| 47 | |
| 48 | /* List of processes. */ |
| 49 | PROC *plist; |
| 50 | +PROC *olist; |
| 51 | |
| 52 | /* List of processes to omit. */ |
| 53 | OMIT *omit; |
| 54 | @@ -345,6 +346,20 @@ static void clear_mnt(void) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | +static void clear_omit(void) |
| 59 | +{ |
| 60 | + OMIT *o; |
| 61 | + PROC *p; |
| 62 | + for (o = omit; o; o = omit) { |
| 63 | + omit = omit->next; |
| 64 | + free(o); |
| 65 | + } |
| 66 | + for (p = olist; p; p = olist) { |
| 67 | + olist = olist->next; |
| 68 | + free(p); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | /* |
| 73 | * Check if path is ia shadow off a NFS partition. |
| 74 | */ |
| 75 | @@ -452,6 +467,7 @@ int readproc(int do_stat) |
| 76 | DIR *dir; |
| 77 | FILE *fp; |
| 78 | PROC *p, *n; |
| 79 | + OMIT *o, *m; |
| 80 | struct dirent *d; |
| 81 | struct stat st; |
| 82 | char path[PATH_MAX+1]; |
| 83 | @@ -624,6 +640,17 @@ int readproc(int do_stat) |
| 84 | p->next = plist; |
| 85 | plist = p; |
| 86 | p->pid = pid; |
| 87 | + /* Could be smarter, but it's a small list. */ |
| 88 | + m = omit; |
| 89 | + for (o = omit; m; o = m) { |
| 90 | + m = o->next; |
| 91 | + if (o->pid == p->pid) { |
| 92 | + n = (PROC*)xmalloc(sizeof(PROC)); |
| 93 | + *n = *p; |
| 94 | + n->next = olist; |
| 95 | + olist = n; |
| 96 | + } |
| 97 | + } |
| 98 | } |
| 99 | closedir(dir); |
| 100 | |
| 101 | @@ -813,6 +840,26 @@ PIDQ_HEAD *pidof(char *prog) |
| 102 | return q; |
| 103 | } |
| 104 | |
| 105 | +int matches(PROC *o, PROC *p) |
| 106 | +{ |
| 107 | + int ret = 0; |
| 108 | + char *oargv1, *pargv1; |
| 109 | + if ((o->argv0 && p->argv0 && !strcmp(o->argv0,p->argv0))) { |
| 110 | + if (o->argv1 && p->argv1) { |
| 111 | + if ((oargv1 = canonicalize_file_name(o->argv1)) == NULL) |
| 112 | + oargv1 = strdup(o->argv1); |
| 113 | + if ((pargv1 = canonicalize_file_name(p->argv1)) == NULL) |
| 114 | + pargv1 = strdup(p->argv1); |
| 115 | + if (! strcmp(oargv1, pargv1)) { |
| 116 | + ret = 1; |
| 117 | + } |
| 118 | + free(oargv1); |
| 119 | + free(pargv1); |
| 120 | + } |
| 121 | + } |
| 122 | + return ret; |
| 123 | +} |
| 124 | + |
| 125 | /* Give usage message and exit. */ |
| 126 | void usage(void) |
| 127 | { |
| 128 | @@ -845,6 +892,7 @@ void nsyslog(int pri, char *fmt, ...) |
| 129 | #define PIDOF_SINGLE 0x01 |
| 130 | #define PIDOF_OMIT 0x02 |
| 131 | #define PIDOF_NETFS 0x04 |
| 132 | +#define PIDOF_OMIT_OMIT_MATCHES 0x08 |
| 133 | |
| 134 | /* |
| 135 | * Pidof functionality. |
| 136 | @@ -861,6 +909,7 @@ int main_pidof(int argc, char **argv) |
| 137 | struct stat st; |
| 138 | char tmp[512]; |
| 139 | |
| 140 | + olist = (PROC*)0; |
| 141 | omit = (OMIT*)0; |
| 142 | nlist = (NFS*)0; |
| 143 | opterr = 0; |
| 144 | @@ -868,7 +917,7 @@ int main_pidof(int argc, char **argv) |
| 145 | if ((token = getenv("PIDOF_NETFS")) && (strcmp(token,"no") != 0)) |
| 146 | flags |= PIDOF_NETFS; |
| 147 | |
| 148 | - while ((opt = getopt(argc,argv,"hco:sxn")) != EOF) switch (opt) { |
| 149 | + while ((opt = getopt(argc,argv,"hcmo:sxn")) != EOF) switch (opt) { |
| 150 | case '?': |
| 151 | nsyslog(LOG_ERR,"invalid options on command line!\n"); |
| 152 | closelog(); |
| 153 | @@ -907,6 +956,9 @@ int main_pidof(int argc, char **argv) |
| 154 | case 'x': |
| 155 | scripts_too++; |
| 156 | break; |
| 157 | + case 'm': |
| 158 | + flags |= PIDOF_OMIT_OMIT_MATCHES; |
| 159 | + break; |
| 160 | case 'n': |
| 161 | flags |= PIDOF_NETFS; |
| 162 | break; |
| 163 | @@ -938,10 +990,13 @@ int main_pidof(int argc, char **argv) |
| 164 | pid_t spid = 0; |
| 165 | while ((p = get_next_from_pid_q(q))) { |
| 166 | if ((flags & PIDOF_OMIT) && omit) { |
| 167 | - OMIT * optr; |
| 168 | - for (optr = omit; optr; optr = optr->next) { |
| 169 | + PROC * optr; |
| 170 | + for (optr = olist; optr; optr = optr->next) { |
| 171 | if (optr->pid == p->pid) |
| 172 | break; |
| 173 | + if (flags & PIDOF_OMIT_OMIT_MATCHES) |
| 174 | + if (matches(optr, p)) |
| 175 | + break; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | @@ -977,6 +1032,7 @@ int main_pidof(int argc, char **argv) |
| 180 | if (!first) |
| 181 | printf("\n"); |
| 182 | |
| 183 | + clear_omit(); |
| 184 | clear_mnt(); |
| 185 | |
| 186 | closelog(); |
| 187 | -- |
| 188 | 1.8.1.2 |
| 189 | |