Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/sup/source be more portable, explain what we are do...



details:   https://anonhg.NetBSD.org/src/rev/36b17f9a74b7
branches:  trunk
changeset: 771909:36b17f9a74b7
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Dec 07 22:52:54 2011 +0000

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

diffstat:

 usr.sbin/sup/source/setproctitle.c |  39 ++++++++++++++++++++++++++++++++-----
 1 files changed, 33 insertions(+), 6 deletions(-)

diffs (66 lines):

diff -r cc85bf4d3351 -r 36b17f9a74b7 usr.sbin/sup/source/setproctitle.c
--- a/usr.sbin/sup/source/setproctitle.c        Wed Dec 07 22:14:43 2011 +0000
+++ b/usr.sbin/sup/source/setproctitle.c        Wed Dec 07 22:52:54 2011 +0000
@@ -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 @@
        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)



Home | Main Index | Thread Index | Old Index