From owner-freebsd-bugs Tue Dec 16 10:40:07 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.7/8.8.7) id KAA17147 for bugs-outgoing; Tue, 16 Dec 1997 10:40:07 -0800 (PST) (envelope-from owner-freebsd-bugs) Received: (from gnats@localhost) by hub.freebsd.org (8.8.7/8.8.7) id KAA17100; Tue, 16 Dec 1997 10:40:02 -0800 (PST) (envelope-from gnats) Resent-Date: Tue, 16 Dec 1997 10:40:02 -0800 (PST) Resent-Message-Id: <199712161840.KAA17100@hub.freebsd.org> Resent-From: gnats (GNATS Management) Resent-To: freebsd-bugs Resent-Reply-To: FreeBSD-gnats@FreeBSD.ORG, imdave@mcs.net Received: from base486.home.org (root@imdave.pr.mcs.net [205.164.3.77]) by hub.freebsd.org (8.8.7/8.8.7) with ESMTP id KAA17045 for ; Tue, 16 Dec 1997 10:39:53 -0800 (PST) (envelope-from imdave@mcs.net) Received: from base586.home.org (imdave@base586.home.org [10.0.0.2]) by base486.home.org (8.8.8/8.8.8) with ESMTP id MAA12168 for ; Tue, 16 Dec 1997 12:40:10 -0600 (CST) Received: (from imdave@localhost) by base586.home.org (8.8.8/8.8.8) id MAA05349; Tue, 16 Dec 1997 12:40:05 -0600 (CST) Message-Id: <199712161840.MAA05349@base586.home.org> Date: Tue, 16 Dec 1997 12:40:05 -0600 (CST) From: Dave Bodenstab Reply-To: imdave@mcs.net To: FreeBSD-gnats-submit@FreeBSD.ORG X-Send-Pr-Version: 3.2 Subject: bin/5322: enhancement for kill(1) to search for a file ``/var/run/*pid'' Sender: owner-freebsd-bugs@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk >Number: 5322 >Category: bin >Synopsis: enhancement for kill(1) to search for a file ``/var/run/*pid'' >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Class: change-request >Submitter-Id: current-users >Arrival-Date: Tue Dec 16 10:40:00 PST 1997 >Last-Modified: >Originator: Dave Bodenstab >Organization: myself >Release: FreeBSD 2.2.5-RELEASE i386 >Environment: run kill(1) as root >Description: It's really convenient to be able to name a process to be sent a signal by name rather than having to do a ``ps'' and search for the correct pid. Since many of the system daemons write a file with a name that matches the pattern "/var/run/.*[-.]pid", why not make use of it? This patch modifies kill(1) to treat a non-numeric process-id argument as a partial pattern. This partial pattern is used to construct the regular expression "[-.]pid". For instance, a ``kill -1 sendmail'' constructs the pattern "^\\s\\e\\n\\d\\m\\a\\i\\l[-.]pid" If the effective uid is root, and a file is found in /var/run that matches the pattern, and the first item read from the file is a positive number greater than zero, then that number is take to be a process-id. Thus, kill(1) will now allow the super-user to specify process-id's by name in addition to by number. >How-To-Repeat: >Fix: --- kill.1 1997/11/22 00:54:34 225.0 +++ kill.1 1997/11/22 00:58:04 225.0.1.1 @@ -64,6 +64,16 @@ .Pp Only the super-user may send signals to other users' processes. .Pp +If the kill utility is run with an effective user id of zero (the +super-user,) then a non-numeric pid operand with an initial alphabetic +character will initiate +a search of the \fB/var/run\fP directory for files matching the +non case-sensitive regular expression "^\fIpid\fP[-.]pid$". +If a matching file is found, and if the first item read from the file +is a positive +decimal integer greater than zero, then that number is taken to be the +process-id of the process to receive the signal. +.Pp The options are as follows: .Pp .Bl -tag -width Ds @@ -137,6 +147,8 @@ .Nm kill command appeared in .At v6 . +.br +Pid-file search enhancement by Dave Bodenstab . .Sh BUGS A replacement for the command .Dq Li kill 0 --- kill.c 1997/11/22 00:54:34 225.0 +++ kill.c 1997/11/22 00:58:04 225.0.1.1 @@ -43,13 +43,18 @@ static char const sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95"; #endif /* not lint */ +#include #include +#include #include #include +#include +#include #include #include #include #include +#include void nosig __P((char *)); void printsignals __P((FILE *)); @@ -124,7 +129,7 @@ for (errors = 0; argc; argc--, argv++) { pid = strtol(*argv, &ep, 10); - if (!**argv || *ep) { + if ((!**argv || *ep) && !pidfile(*argv,&pid)) { warnx("illegal process id: %s", *argv); errors = 1; } else if (kill(pid, numsig) == -1) { @@ -134,6 +139,89 @@ } exit(errors); +} + +/* + * If ROOT, then look for a file _PATH_VARRUN file "[-.]pid" + * and, if found, extract the pid + */ +int +pidfile(file, pidp) + char *file; + int *pidp; +{ + static char suffix[] = "[-.]pid$"; + char *path, *pattern, *p; + int rc, c, length; + FILE *fp; + DIR *dirp; + struct dirent *dp; + regex_t re; + struct stat statbuf; + + if (file == NULL || ! isalpha(*file) || geteuid() != 0) + return 0; + + /* sizeof(suffix) includes the NUL */ + length = 1 + 2 * strlen(file) + sizeof(suffix); + + if ((pattern = malloc(length)) == NULL) + return 0; + + pattern[0] = '^'; + for(p = pattern + 1; (c = *file++) != '\0'; *p++ = c) + *p++ = '\\'; + strcpy(p, suffix); + + if (regcomp(&re,pattern, REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0) { + free(pattern); + return 0; + } + + if ((dirp = opendir(_PATH_VARRUN)) == NULL) { + regfree(&re); + free(pattern); + return 0; + } + + rc = 0; + + while((dp = readdir(dirp)) != NULL) { + if (regexec(&re, dp->d_name, 0, NULL, 0) == 0) { + /* + * A match... (only the first match is considered) + */ + length = strlen(_PATH_VARRUN) + 1 + strlen(dp->d_name) + 1; + + if ((path = malloc(length)) == NULL) + /* just quit */ + break; + + strcpy(path, _PATH_VARRUN); + strcat(path, "/"); + strcat(path, dp->d_name); + + if (stat(path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) { + if ((fp = fopen(path, "r")) != NULL) { + if (fscanf(fp, " %d", pidp) == 1 && *pidp > 0) + /* + * ...and we have the pid! + */ + rc = 1; + + fclose(fp); + } + } + + free(path); + break; + } + } + + closedir(dirp); + regfree(&re); + free(pattern); + return rc; } int >Audit-Trail: >Unformatted: