From owner-freebsd-bugs@FreeBSD.ORG Wed Oct 30 14:50:02 2013 Return-Path: Delivered-To: freebsd-bugs@smarthost.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id EA8B3995 for ; Wed, 30 Oct 2013 14:50:01 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:1900:2254:206c::16:87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id C5D41278E for ; Wed, 30 Oct 2013 14:50:01 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.7/8.14.7) with ESMTP id r9UEo0Ij019917 for ; Wed, 30 Oct 2013 14:50:00 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.7/8.14.7/Submit) id r9UEo0QR019902; Wed, 30 Oct 2013 14:50:00 GMT (envelope-from gnats) Date: Wed, 30 Oct 2013 14:50:00 GMT Message-Id: <201310301450.r9UEo0QR019902@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org Cc: From: Stefan Neudorf Subject: Re: bin/183484: [PATCH] usr.bin/limits: make -e work without /proc X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: Stefan Neudorf List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Oct 2013 14:50:02 -0000 The following reply was made to PR bin/183484; it has been noted by GNATS. From: Stefan Neudorf To: bug-followup@freebsd.org Cc: Subject: Re: bin/183484: [PATCH] usr.bin/limits: make -e work without /proc Date: Wed, 30 Oct 2013 15:43:17 +0100 --=-=-= Content-Type: text/plain Stefan Neudorf writes: > if (shell != NULL && stat(shell, &st) != -1) { > struct stat st1; > > mib[0] = CTL_KERN; > mib[1] = KERN_PROC; > mib[2] = KERN_PROC_PATHNAME; > mib[3] = ppid; > len = sizeof(path); > sysctl(mib, 4, path, &len, NULL, 0); > /* $SHELL is actual shell? */ > if (stat(path, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0) > return getshellbyname(shell); > } > mib[2] = KERN_PROC_PID; > len = sizeof(kp); > sysctl(mib, 4, &kp, &len, NULL, 0); I was a bit too hasty, mib[] may be partially unset. > if (kp.ki_comm != NULL) > return getshellbyname(kp.ki_comm); --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=noprocdir.diff Index: usr.bin/limits/limits.c =================================================================== --- usr.bin/limits/limits.c (revision 257346) +++ usr.bin/limits/limits.c (working copy) @@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -704,27 +705,32 @@ getshelltype(void) pid_t ppid = getppid(); if (ppid != 1) { - FILE * fp; + struct kinfo_proc kp; struct stat st; - char procdir[MAXPATHLEN], buf[128]; - int l = sprintf(procdir, "/proc/%ld/", (long)ppid); + char path[MAXPATHLEN]; char * shell = getenv("SHELL"); + int mib[4]; + size_t len; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[3] = ppid; + if (shell != NULL && stat(shell, &st) != -1) { struct stat st1; - strcpy(procdir+l, "file"); + mib[2] = KERN_PROC_PATHNAME; + len = sizeof(path); + sysctl(mib, 4, path, &len, NULL, 0); /* $SHELL is actual shell? */ - if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0) + if (stat(path, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0) return getshellbyname(shell); } - strcpy(procdir+l, "status"); - if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) { - char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t"); - fclose(fp); - if (p != NULL) - return getshellbyname(p); - } + mib[2] = KERN_PROC_PID; + len = sizeof(kp); + sysctl(mib, 4, &kp, &len, NULL, 0); + if (kp.ki_comm != NULL) + return getshellbyname(kp.ki_comm); } return SH_SH; } --=-=-=--