blob: 494aa0e01266a45fa0aa672c0393dedcc433a3f9 [file] [log] [blame]
Andrew Geissler82c905d2020-04-13 13:39:40 -05001From 6c490ea6579a132fabb7dbd25387bb521f820371 Mon Sep 17 00:00:00 2001
2From: Hongxu Jia <hongxu.jia@windriver.com>
3Date: Wed, 24 Jul 2013 17:07:22 +0800
4Subject: [PATCH] pidof: add -m option
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005
6When used with -o, will also omit any processes that have the same
7argv[0] and argv[1] as any explicitly omitted process ids. This can be
8used to avoid multiple shell scripts concurrently calling pidof returning
9each other's pids.
10
11https://bugzilla.redhat.com/show_bug.cgi?id=883856
12
13Upstream-Status: backport
14Imported patch from: https://bugzilla.redhat.com/attachment.cgi?id=658166
15
16Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
Andrew Geissler82c905d2020-04-13 13:39:40 -050017
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018---
Andrew Geissler82c905d2020-04-13 13:39:40 -050019 man/pidof.8 | 6 +++++
20 src/killall5.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++---
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021 2 files changed, 65 insertions(+), 3 deletions(-)
22
23diff --git a/man/pidof.8 b/man/pidof.8
Andrew Geissler82c905d2020-04-13 13:39:40 -050024index ebe5f55..2fdc4d3 100644
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025--- a/man/pidof.8
26+++ b/man/pidof.8
Andrew Geissler82c905d2020-04-13 13:39:40 -050027@@ -25,6 +25,7 @@ pidof -- find the process ID of a running program.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028 .RB [ \-n ]
29 .RB [ \-x ]
Andrew Geissler82c905d2020-04-13 13:39:40 -050030 .RB [ \-z ]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031+.RB [ \-m ]
32 .RB [ \-o
Andrew Geissler82c905d2020-04-13 13:39:40 -050033 .IR omitpid[,omitpid...] ]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 .RB [ \-o
Andrew Geissler82c905d2020-04-13 13:39:40 -050035@@ -76,6 +77,11 @@ is shown. The default separator is a space.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036 Tells \fIpidof\fP to omit processes with that process id. The special
37 pid \fB%PPID\fP can be used to name the parent process of the \fIpidof\fP
38 program, in other words the calling shell or shell script.
39+.IP -m
40+When used with -o, will also omit any processes that have the same
41+argv[0] and argv[1] as any explicitly omitted process ids. This can be
42+used to avoid multiple shell scripts concurrently calling pidof returning
43+each other's pids.
44 .SH "EXIT STATUS"
45 .TP
46 .B 0
47diff --git a/src/killall5.c b/src/killall5.c
Andrew Geissler82c905d2020-04-13 13:39:40 -050048index 8b5cb38..a664954 100644
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049--- a/src/killall5.c
50+++ b/src/killall5.c
Andrew Geissler82c905d2020-04-13 13:39:40 -050051@@ -126,6 +126,7 @@ typedef struct _s_nfs
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052
53 /* List of processes. */
54 PROC *plist;
55+PROC *olist;
56
57 /* List of processes to omit. */
58 OMIT *omit;
Andrew Geissler82c905d2020-04-13 13:39:40 -050059@@ -361,6 +362,20 @@ static void clear_mnt(void)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060 }
61 }
62
63+static void clear_omit(void)
64+{
65+ OMIT *o;
66+ PROC *p;
67+ for (o = omit; o; o = omit) {
68+ omit = omit->next;
69+ free(o);
70+ }
71+ for (p = olist; p; p = olist) {
72+ olist = olist->next;
73+ free(p);
74+ }
75+}
76+
77 /*
Andrew Geissler82c905d2020-04-13 13:39:40 -050078 * Check if path is a shadow off a NFS partition.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 */
Andrew Geissler82c905d2020-04-13 13:39:40 -050080@@ -486,6 +501,7 @@ int readproc(int do_stat)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 DIR *dir;
82 FILE *fp;
83 PROC *p, *n;
84+ OMIT *o, *m;
85 struct dirent *d;
86 struct stat st;
87 char path[PATH_MAX+1];
Andrew Geissler82c905d2020-04-13 13:39:40 -050088@@ -733,6 +749,17 @@ int readproc(int do_stat)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 p->next = plist;
90 plist = p;
91 p->pid = pid;
92+ /* Could be smarter, but it's a small list. */
93+ m = omit;
94+ for (o = omit; m; o = m) {
95+ m = o->next;
96+ if (o->pid == p->pid) {
97+ n = (PROC*)xmalloc(sizeof(PROC));
98+ *n = *p;
99+ n->next = olist;
100+ olist = n;
101+ }
102+ }
103 }
104 closedir(dir);
105
Andrew Geissler82c905d2020-04-13 13:39:40 -0500106@@ -944,6 +971,26 @@ PIDQ_HEAD *pidof(char *prog)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 return q;
108 }
109
110+int matches(PROC *o, PROC *p)
111+{
112+ int ret = 0;
113+ char *oargv1, *pargv1;
114+ if ((o->argv0 && p->argv0 && !strcmp(o->argv0,p->argv0))) {
115+ if (o->argv1 && p->argv1) {
116+ if ((oargv1 = canonicalize_file_name(o->argv1)) == NULL)
117+ oargv1 = strdup(o->argv1);
118+ if ((pargv1 = canonicalize_file_name(p->argv1)) == NULL)
119+ pargv1 = strdup(p->argv1);
120+ if (! strcmp(oargv1, pargv1)) {
121+ ret = 1;
122+ }
123+ free(oargv1);
124+ free(pargv1);
125+ }
126+ }
127+ return ret;
128+}
129+
130 /* Give usage message and exit. */
131 void usage(void)
132 {
Andrew Geissler82c905d2020-04-13 13:39:40 -0500133@@ -994,6 +1041,7 @@ void nsyslog(int pri, char *fmt, ...)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500134 #define PIDOF_OMIT 0x02
135 #define PIDOF_NETFS 0x04
Andrew Geissler82c905d2020-04-13 13:39:40 -0500136 #define PIDOF_QUIET 0x08
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137+#define PIDOF_OMIT_OMIT_MATCHES 0x08
138
139 /*
140 * Pidof functionality.
Andrew Geissler82c905d2020-04-13 13:39:40 -0500141@@ -1011,6 +1059,7 @@ int main_pidof(int argc, char **argv)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142 char tmp[512];
Andrew Geissler82c905d2020-04-13 13:39:40 -0500143 char sep = ' ';
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144
145+ olist = (PROC*)0;
146 omit = (OMIT*)0;
147 nlist = (NFS*)0;
148 opterr = 0;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500149@@ -1018,7 +1067,7 @@ int main_pidof(int argc, char **argv)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150 if ((token = getenv("PIDOF_NETFS")) && (strcmp(token,"no") != 0))
151 flags |= PIDOF_NETFS;
152
Andrew Geissler82c905d2020-04-13 13:39:40 -0500153- while ((opt = getopt(argc,argv,"qhco:d:sxzn")) != EOF) switch (opt) {
154+ while ((opt = getopt(argc,argv,"qhcmo:d:sxzn")) != EOF) switch (opt) {
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155 case '?':
156 nsyslog(LOG_ERR,"invalid options on command line!\n");
157 closelog();
Andrew Geissler82c905d2020-04-13 13:39:40 -0500158@@ -1069,6 +1118,9 @@ int main_pidof(int argc, char **argv)
159 case 'z':
160 list_dz_processes = TRUE;
161 break;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162+ case 'm':
163+ flags |= PIDOF_OMIT_OMIT_MATCHES;
164+ break;
165 case 'n':
166 flags |= PIDOF_NETFS;
167 break;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500168@@ -1100,10 +1152,13 @@ int main_pidof(int argc, char **argv)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 pid_t spid = 0;
170 while ((p = get_next_from_pid_q(q))) {
171 if ((flags & PIDOF_OMIT) && omit) {
172- OMIT * optr;
173- for (optr = omit; optr; optr = optr->next) {
174+ PROC * optr;
175+ for (optr = olist; optr; optr = optr->next) {
176 if (optr->pid == p->pid)
177 break;
178+ if (flags & PIDOF_OMIT_OMIT_MATCHES)
179+ if (matches(optr, p))
180+ break;
181 }
182
183 /*
Andrew Geissler82c905d2020-04-13 13:39:40 -0500184@@ -1145,6 +1200,7 @@ int main_pidof(int argc, char **argv)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185 printf("\n");
Andrew Geissler82c905d2020-04-13 13:39:40 -0500186 }
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187
188+ clear_omit();
189 clear_mnt();
190
191 closelog();