Module Name:    src
Committed By:   christos
Date:           Wed Dec  7 22:52:54 UTC 2011

Modified Files:
        src/usr.sbin/sup/source: setproctitle.c

Log Message:
be more portable, explain what we are doing, simplify.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/usr.sbin/sup/source/setproctitle.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/sup/source/setproctitle.c
diff -u src/usr.sbin/sup/source/setproctitle.c:1.3 src/usr.sbin/sup/source/setproctitle.c:1.4
--- src/usr.sbin/sup/source/setproctitle.c:1.3	Mon Apr 28 16:24:17 2008
+++ src/usr.sbin/sup/source/setproctitle.c	Wed Dec  7 17:52:54 2011
@@ -27,10 +27,13 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <sys/param.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
+#include <limits.h>
+#include <unistd.h>
 
 #ifdef NEED_SETPROCTITLE
 
@@ -42,18 +45,42 @@ setproctitle(const char *fmt, ...)
 	va_list ap;
 	char buf[1024];
 	int len;
-	char *pname, *p;
-	char **args = __environ - 2;
+	char *pname, *p, *s;
+	/*
+	 * Assumes that stack grows down, and than environ has not bee
+	 * reallocated because of setenv() required growth. Stack layout:
+	 * 
+	 * argc
+	 * argv[0]
+	 * ...
+	 * argv[n]
+	 * NULL
+	 * environ[0]
+	 * ...
+	 * environ[n]
+	 * NULL
+	 */
 
+	/* 1 for the first entry, 1 for the NULL */
+	char **args = __environ - 2, *s;
+#ifdef _SC_ARG_MAX
+	s = (char *)sysconf(_SC_ARG_MAX);
+#elifdef ARG_MAX
+	s = (char *)ARG_MAX;
+#elifdef NCARGS
+	s = (char *)NCARGS;
+#else
+	s = (char *)(256 * 1024);
+#endif
 	/*
 	 * Keep going while it looks like a pointer. We'll stop at argc,
-	 * Assume that we have < 10K args.
+	 * Which is a lot smaller than a pointer, limited by ARG_MAX
 	 */
-	while (*args > (char *)10240)
+	while (*args > s)
 		args--;
 
-	pname = *++args;
-	*(int *)((int *)pname - 1) = 1; /* *argc = 1; */
+	*(int *)args = 1; /* *argc = 1; */
+	pname = *++args;  /* pname = argv[0] */
  
 	/* Just the last component of the name */
 	if ((p = strrchr(pname, '/')) != NULL)

Reply via email to