Added patch from Walter Harms which allows pidof to run without

displaying output. In this mode pidof simply returns true or false
without displaying PID values.

Updated manual page with new -q (quiet) mode.

Added -h flag for pidof, which was recognized before, but not used.
The -h flag now displays brief usage information for pidof.
This commit is contained in:
Jesse Smith 2018-09-08 17:19:21 -03:00
parent eda1f0d6ba
commit f1ca96e1c1
2 changed files with 38 additions and 7 deletions

View File

@ -32,7 +32,7 @@ pidof -- find the process ID of a running program.
.RB [ program.. ] .RB [ program.. ]
.SH DESCRIPTION .SH DESCRIPTION
.B Pidof .B Pidof
finds the process id's (pids) of the named programs. It prints those finds the process id's (PIDs) of the named programs. It prints those
id's on the standard output. This program is on some systems used in id's on the standard output. This program is on some systems used in
run-level change scripts, especially when the system has a run-level change scripts, especially when the system has a
\fISystem-V\fP like \fIrc\fP structure. In that case these scripts are \fISystem-V\fP like \fIrc\fP structure. In that case these scripts are
@ -44,7 +44,7 @@ a
.IP \-s .IP \-s
Single shot - this instructs the program to only return one \fIpid\fP. Single shot - this instructs the program to only return one \fIpid\fP.
.IP \-c .IP \-c
Only return process ids that are running with the same root directory. Only return process PIDs that are running with the same root directory.
This option is ignored for non-root users, as they will be unable to check This option is ignored for non-root users, as they will be unable to check
the current root directory of processes they do not own. the current root directory of processes they do not own.
.IP \-n .IP \-n
@ -56,6 +56,9 @@ based file systems like
Instead of using this option the the variable Instead of using this option the the variable
.B PIDOF_NETFS .B PIDOF_NETFS
may be set and exported. may be set and exported.
.IP \-q
Do not display matched PIDs to standard out. Simply exit with
a status of true or false to indicate whether a matching PID was found.
.IP \-x .IP \-x
Scripts too - this causes the program to also return process id's of Scripts too - this causes the program to also return process id's of
shells running the named scripts. shells running the named scripts.
@ -76,7 +79,7 @@ the program behaves according to the name under which it is called.
.PP .PP
When \fIpidof\fP is invoked with a full pathname to the program it When \fIpidof\fP is invoked with a full pathname to the program it
should find the pid of, it is reasonably safe. Otherwise it is possible should find the pid of, it is reasonably safe. Otherwise it is possible
that it returns pids of running programs that happen to have the same name that it returns PIDs of running programs that happen to have the same name
as the program you're after but are actually other programs. Note that as the program you're after but are actually other programs. Note that
that the executable name of running processes is calculated with that the executable name of running processes is calculated with
.BR readlink (2), .BR readlink (2),

View File

@ -919,6 +919,21 @@ void usage(void)
exit(1); exit(1);
} }
void pidof_usage(void)
{
printf("pidof usage: [options] <program-name>\n\n");
printf(" -c Return PIDs with the same root directory\n");
printf(" -h Display this help text\n");
printf(" -n Avoid using stat system function on network shares\n");
printf(" -o <pid> Omit results with a given PID\n");
printf(" -q Quiet mode. Do not display output\n");
printf(" -s Only return one PID\n");
printf(" -x Return PIDs of shells running scritps with a matchign name\n");
printf("\n");
}
/* write to syslog file if not open terminal */ /* write to syslog file if not open terminal */
#ifdef __GNUC__ #ifdef __GNUC__
__attribute__ ((format (printf, 2, 3))) __attribute__ ((format (printf, 2, 3)))
@ -943,6 +958,7 @@ void nsyslog(int pri, char *fmt, ...)
#define PIDOF_SINGLE 0x01 #define PIDOF_SINGLE 0x01
#define PIDOF_OMIT 0x02 #define PIDOF_OMIT 0x02
#define PIDOF_NETFS 0x04 #define PIDOF_NETFS 0x04
#define PIDOF_QUIET 0x08
/* /*
* Pidof functionality. * Pidof functionality.
@ -966,7 +982,7 @@ int main_pidof(int argc, char **argv)
if ((token = getenv("PIDOF_NETFS")) && (strcmp(token,"no") != 0)) if ((token = getenv("PIDOF_NETFS")) && (strcmp(token,"no") != 0))
flags |= PIDOF_NETFS; flags |= PIDOF_NETFS;
while ((opt = getopt(argc,argv,"hco:sxn")) != EOF) switch (opt) { while ((opt = getopt(argc,argv,"qhco:sxn")) != EOF) switch (opt) {
case '?': case '?':
nsyslog(LOG_ERR,"invalid options on command line!\n"); nsyslog(LOG_ERR,"invalid options on command line!\n");
closelog(); closelog();
@ -974,6 +990,9 @@ int main_pidof(int argc, char **argv)
case 'c': case 'c':
if (geteuid() == 0) chroot_check = 1; if (geteuid() == 0) chroot_check = 1;
break; break;
case 'h':
pidof_usage();
exit(0);
case 'o': case 'o':
here = optarg; here = optarg;
while ((token = strsep(&here, ",;:"))) { while ((token = strsep(&here, ",;:"))) {
@ -999,6 +1018,9 @@ int main_pidof(int argc, char **argv)
} }
flags |= PIDOF_OMIT; flags |= PIDOF_OMIT;
break; break;
case 'q':
flags |= PIDOF_QUIET;
break;
case 's': case 's':
flags |= PIDOF_SINGLE; flags |= PIDOF_SINGLE;
break; break;
@ -1065,15 +1087,21 @@ int main_pidof(int argc, char **argv)
continue; continue;
} }
} }
if (!first)
printf(" "); if ( ~flags & PIDOF_QUIET ) {
printf("%d", p->pid); if (!first)
printf(" ");
printf("%d", p->pid);
}
first = 0; first = 0;
} }
} }
} }
if (!first) if (!first)
{
if ( ~flags & PIDOF_QUIET )
printf("\n"); printf("\n");
}
clear_mnt(); clear_mnt();