blob: 8ef292ed1296c0e3942d0e7e54e9ce2bce1093b1 [file] [log] [blame]
Patrick Williams03907ee2022-05-01 06:28:52 -05001From 0e441712d0e366a0384ff3fa879f5a2d2607c24f Mon Sep 17 00:00:00 2001
Andrew Geissler82c905d2020-04-13 13:39:40 -05002From: 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
Andrew Geissler595f6302022-01-24 19:11:47 +000013Upstream-Status: Backport
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014Imported 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
Patrick Williams03907ee2022-05-01 06:28:52 -050024index 6866cb3..a87d878 100644
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025--- a/man/pidof.8
26+++ b/man/pidof.8
Patrick Williams03907ee2022-05-01 06:28:52 -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
Patrick Williams03907ee2022-05-01 06:28:52 -050035@@ -77,6 +78,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
Patrick Williams03907ee2022-05-01 06:28:52 -050037 pid \fB%PPID\fP can be used to name the parent process of the \fBpidof\fP
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 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 Geissler595f6302022-01-24 19:11:47 +000048index b0728fa..72289e3 100644
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049--- a/src/killall5.c
50+++ b/src/killall5.c
Andrew Geissler595f6302022-01-24 19:11:47 +000051@@ -121,6 +121,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 Geissler595f6302022-01-24 19:11:47 +000059@@ -356,6 +357,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 Geissler595f6302022-01-24 19:11:47 +000080@@ -481,6 +496,7 @@ int readproc()
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;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086 char path[PATH_MAX+1];
Andrew Geissler595f6302022-01-24 19:11:47 +000087 char buf[PATH_MAX+1];
88@@ -670,6 +686,17 @@ int readproc()
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 Geissler595f6302022-01-24 19:11:47 +0000106@@ -870,6 +897,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 Geissler595f6302022-01-24 19:11:47 +0000133@@ -920,6 +967,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 Geissler595f6302022-01-24 19:11:47 +0000141@@ -937,6 +985,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 Geissler595f6302022-01-24 19:11:47 +0000149@@ -944,7 +993,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 Geissler595f6302022-01-24 19:11:47 +0000158@@ -995,6 +1044,9 @@ int main_pidof(int argc, char **argv)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500159 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 Geissler595f6302022-01-24 19:11:47 +0000168@@ -1026,10 +1078,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 Geissler595f6302022-01-24 19:11:47 +0000184@@ -1071,6 +1126,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();