From BM-2cXppXU4T67w7j6NCir9T1WdzBHmFgBnLj@bitmessage.ch  Wed Oct 30 14:21:22 2013
Return-Path: <BM-2cXppXU4T67w7j6NCir9T1WdzBHmFgBnLj@bitmessage.ch>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTP id BB891283
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 30 Oct 2013 14:21:22 +0000 (UTC)
	(envelope-from BM-2cXppXU4T67w7j6NCir9T1WdzBHmFgBnLj@bitmessage.ch)
Received: from mail.bitmessage.ch (mail.bitmessage.ch [146.228.112.252])
	by mx1.freebsd.org (Postfix) with SMTP id 29F802560
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 30 Oct 2013 14:21:21 +0000 (UTC)
Received: from nil ([127.0.0.1])
	by mail.bitmessage.ch
	; Wed, 30 Oct 2013 15:21:13 +0100
Message-Id: <20131030.86li1aq1qz@bitmessage.ch>
Date: Wed, 30 Oct 2013 15:20:20 +0100
From: Stefan Neudorf <BM-2cXppXU4T67w7j6NCir9T1WdzBHmFgBnLj@bitmessage.ch>
To: FreeBSD-gnats-submit@freebsd.org
Subject: [PATCH] usr.bin/limits: make -e work without /proc
X-GNATS-Notify: kib@FreeBSD.org

>Number:         183484
>Category:       bin
>Synopsis:       [PATCH] limits(1): usr.bin/limits: make -e work without /proc
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 30 14:30:00 UTC 2013
>Closed-Date:    
>Last-Modified:  Sun May 04 03:39:08 UTC 2014
>Originator:     Stefan Neudorf
>Release:        
>Organization:
>Environment:
>Description:
>How-To-Repeat:
 $ tcsh
 > limits -e
  ulimit -t unlimited;
  ulimit -f unlimited;
  ulimit -d 33554432;
  ulimit -s 524288;
  ulimit -c unlimited;
  ulimit -m unlimited;
  ulimit -l 64;
  ulimit -u 8528;
  ulimit -n 117585;
  ulimit -b unlimited;
  ulimit -v unlimited;
  ulimit -p unlimited;
  ulimit -w unlimited;
  ulimit -k unlimited;
 > sudo mount /proc
 > limits -e
  limit cputime unlimited;
  limit filesize unlimited;
  limit datasize 33554432;
  limit stacksize 524288;
  limit coredumpsize unlimited;
  limit memoryuse unlimited;
  limit memorylocked 64;
  limit maxproc 8528;
  limit descriptors 117585;
  limit sbsize unlimited;
  limit vmemoryuse unlimited;
  limit pseudoterminals unlimited;
  limit swapuse unlimited;
  limit kqueues unlimited;
>Fix:
--- noprocdir.diff begins here ---
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 <sys/types.h>
 #include <sys/stat.h>
 #include <sys/sysctl.h>
+#include <sys/user.h>
 #include <sys/param.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -704,27 +705,31 @@ 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;
 
 	if (shell != NULL && stat(shell, &st) != -1) {
 	    struct stat st1;
 
-	    strcpy(procdir+l, "file");
+	    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(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;
 }
--- noprocdir.diff ends here ---

>Release-Note:
>Audit-Trail:

From: Stefan Neudorf <BM-2cXppXU4T67w7j6NCir9T1WdzBHmFgBnLj@bitmessage.ch>
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 <BM-2cXppXU4T67w7j6NCir9T1WdzBHmFgBnLj@bitmessage.ch>
 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 <sys/types.h>
  #include <sys/stat.h>
  #include <sys/sysctl.h>
 +#include <sys/user.h>
  #include <sys/param.h>
  #include <stdlib.h>
  #include <unistd.h>
 @@ -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;
  }
 
 --=-=-=--
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/183484: commit references a PR
Date: Thu, 31 Oct 2013 09:29:45 +0000 (UTC)

 Author: kib
 Date: Thu Oct 31 09:29:37 2013
 New Revision: 257430
 URL: http://svnweb.freebsd.org/changeset/base/257430
 
 Log:
   Remove the dependency on procfs.  Use sysctl KERN_PROC_PATHNAME and
   KERN_PROC_PID to obtain the parent process pathname and command, used
   to determine the calling shell.
   
   Submitted by:	Stefan Neudorf
   PR:	bin/183484
   MFC after:	1 week
 
 Modified:
   head/usr.bin/limits/limits.c
 
 Modified: head/usr.bin/limits/limits.c
 ==============================================================================
 --- head/usr.bin/limits/limits.c	Thu Oct 31 09:20:30 2013	(r257429)
 +++ head/usr.bin/limits/limits.c	Thu Oct 31 09:29:37 2013	(r257430)
 @@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$");
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <sys/sysctl.h>
 +#include <sys/user.h>
  #include <sys/param.h>
  #include <stdlib.h>
  #include <unistd.h>
 @@ -705,27 +706,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");
 -	    /* $SHELL is actual shell? */
 -	    if (stat(procdir, &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_PATHNAME;
 +	    len = sizeof(path);
 +	    if (sysctl(mib, 4, path, &len, NULL, 0) != -1) {
 +		/* $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);
 +	if (sysctl(mib, 4, &kp, &len, NULL, 0) != -1)
 +	    return getshellbyname(kp.ki_comm);
      }
      return SH_SH;
  }
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
