]> andersk Git - openssh.git/commitdiff
- (djm) Replace setproctitle replacement with code derived from
authordjm <djm>
Tue, 3 Jun 2003 00:14:28 +0000 (00:14 +0000)
committerdjm <djm>
Tue, 3 Jun 2003 00:14:28 +0000 (00:14 +0000)
   UCB sendmail

ChangeLog
acconfig.h
configure.ac
openbsd-compat/setproctitle.c

index 9eba9534e224abc5c67db5d257226a7148d6962f..775f13696b7a3f06d42185490ddf72373671c8a0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+20030603
+ - (djm) Replace setproctitle replacement with code derived from 
+   UCB sendmail
+
 20030602
  - (djm) Fix segv from bad reordering in auth-pam.c
  - (djm) Always use saved_argv in sshd.c as compat_init_setproctitle may 
index 6bd973b6d7a6cfb381278d8b26c40c841c7f6ae9..0cf4e0a755438521f47a8b5799f6f097547d40da 100644 (file)
@@ -11,6 +11,7 @@
 /* Define to a Set Process Title type if your system is */
 /* supported by bsd-setproctitle.c */
 #undef SPT_TYPE
+#undef SPT_PADCHAR
 
 /* setgroups() NOOP allowed */
 #undef SETGROUPS_NOOP
 /* Silly mkstemp() */
 #undef HAVE_STRICT_MKSTEMP
 
-/* Setproctitle emulation */
-#undef SETPROCTITLE_STRATEGY
-#undef SETPROCTITLE_PS_PADDING
-
 /* Some systems put this outside of libc */
 #undef HAVE_NANOSLEEP
 
index 8344161929df9b2c81cb4288df69ca0f29ee7704..11b7b4b200c5c309b1a63a70b2f214fe0a70b8a6 100644 (file)
@@ -86,8 +86,7 @@ case "$host" in
        dnl AIX handles lastlog as part of its login message
        AC_DEFINE(DISABLE_LASTLOG)
        AC_DEFINE(LOGIN_NEEDS_UTMPX)
-       AC_DEFINE(SETPROCTITLE_STRATEGY,PS_USE_CLOBBER_ARGV)
-       AC_DEFINE(SETPROCTITLE_PS_PADDING, '\0')
+       AC_DEFINE(SPT_TYPE,SPT_REUSEARGV)
        ;;
 *-*-cygwin*)
        check_for_libcrypt_later=1
@@ -128,7 +127,7 @@ main() { if (NSVersionOfRunTimeLibrary("System") >= (60 << 16))
        AC_DEFINE(LOGIN_NEEDS_UTMPX)
        AC_DEFINE(DISABLE_SHADOW)
        AC_DEFINE(DISABLE_UTMP)
-       AC_DEFINE(SETPROCTITLE_STRATEGY,PS_USE_PSTAT)
+       AC_DEFINE(SPT_TYPE,SPT_PSTAT)
        LIBS="$LIBS -lsec -lsecpw"
        AC_CHECK_LIB(xnet, t_error, ,AC_MSG_ERROR([*** -lxnet needed on HP-UX - check config.log ***]))
        disable_ptmx_check=yes
@@ -144,7 +143,7 @@ main() { if (NSVersionOfRunTimeLibrary("System") >= (60 << 16))
        AC_DEFINE(LOGIN_NEEDS_UTMPX)
        AC_DEFINE(DISABLE_SHADOW)
        AC_DEFINE(DISABLE_UTMP)
-       AC_DEFINE(SETPROCTITLE_STRATEGY,PS_USE_PSTAT)
+       AC_DEFINE(SPT_TYPE,SPT_PSTAT)
        LIBS="$LIBS -lsec"
        AC_CHECK_LIB(xnet, t_error, ,AC_MSG_ERROR([*** -lxnet needed on HP-UX - check config.log ***]))
        ;;
@@ -157,7 +156,7 @@ main() { if (NSVersionOfRunTimeLibrary("System") >= (60 << 16))
        AC_DEFINE(LOGIN_NEEDS_UTMPX)
        AC_DEFINE(DISABLE_SHADOW)
        AC_DEFINE(DISABLE_UTMP)
-       AC_DEFINE(SETPROCTITLE_STRATEGY,PS_USE_PSTAT)
+       AC_DEFINE(SPT_TYPE,SPT_PSTAT)
        LIBS="$LIBS -lsec"
        AC_CHECK_LIB(xnet, t_error, ,AC_MSG_ERROR([*** -lxnet needed on HP-UX - check config.log ***]))
        ;;
@@ -184,8 +183,7 @@ main() { if (NSVersionOfRunTimeLibrary("System") >= (60 << 16))
        check_for_libcrypt_later=1
        AC_DEFINE(DONT_TRY_OTHER_AF)
        AC_DEFINE(PAM_TTY_KLUDGE)
-       AC_DEFINE(SETPROCTITLE_STRATEGY,PS_USE_CLOBBER_ARGV)
-       AC_DEFINE(SETPROCTITLE_PS_PADDING, '\0')
+       AC_DEFINE(SPT_TYPE,SPT_REUSEARGV)
        inet6_default_4in6=yes
        # openpty can be in libutil, needed for controlling tty test
        AC_SEARCH_LIBS(openpty, util)
index cd5a6747399ad1ce1e549b2a61bb13bedd9116ba..24cff0c80dcfdf692e7fde8678afb2b5d0743ac2 100644 (file)
@@ -1,41 +1,38 @@
-/*
- * Based on src/backend/utils/misc/pg_status.c from 
- * PostgreSQL Database Management System
- * 
- * Portions Copyright (c) 1996-2001, The PostgreSQL Global Development Group
- * 
- * Portions Copyright (c) 1994, The Regents of the University of California
- * 
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose, without fee, and without a written agreement
- * is hereby granted, provided that the above copyright notice and this
- * paragraph and the following two paragraphs appear in all copies.
- * 
- * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
- * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
- * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
- * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- * 
- * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
- * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
- * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
- */
+/* Based on conf.c from UCB sendmail 8.8.8 */
 
-/*--------------------------------------------------------------------
- * ps_status.c
- *
- * Routines to support changing the ps display of PostgreSQL backends
- * to contain some useful information. Mechanism differs wildly across
- * platforms.
+/*
+ * Copyright 2003 Damien Miller
+ * Copyright (c) 1983, 1995-1997 Eric P. Allman
+ * Copyright (c) 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
- * $Header$
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
- * Copyright 2000 by PostgreSQL Global Development Group
- * various details abducted from various places
- *--------------------------------------------------------------------
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #include "includes.h"
 
 #include <unistd.h>
 #ifdef HAVE_SYS_PSTAT_H
-#include <sys/pstat.h>         /* for HP-UX */
-#endif
-#ifdef HAVE_PS_STRINGS
-#include <machine/vmparam.h>   /* for old BSD */
-#include <sys/exec.h>
+#include <sys/pstat.h>
 #endif
 
-/*------
- * Alternative ways of updating ps display:
- *
- * SETPROCTITLE_STRATEGY == PS_USE_PSTAT
- *        use the pstat(PSTAT_SETCMD, )
- *        (HPUX)
- * SETPROCTITLE_STRATEGY == PS_USE_PS_STRINGS
- *        assign PS_STRINGS->ps_argvstr = "string"
- *        (some BSD systems)
- * SETPROCTITLE_STRATEGY == PS_USE_CHANGE_ARGV
- *        assign argv[0] = "string"
- *        (some other BSD systems)
- * SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
- *        write over the argv and environment area
- *        (most SysV-like systems)
- * SETPROCTITLE_STRATEGY == PS_USE_NONE
- *        don't update ps display
- *        (This is the default, as it is safest.)
- */
+#define SPT_NONE       0       /* don't use it at all */
+#define SPT_PSTAT      1       /* cover argv with title information */
+#define SPT_REUSEARGV  2       /* use pstat(PSTAT_SETCMD, ...) */
 
-#define PS_USE_NONE                    0
-#define PS_USE_PSTAT                   1
-#define PS_USE_PS_STRINGS              2
-#define PS_USE_CHANGE_ARGV             3
-#define PS_USE_CLOBBER_ARGV            4
-
-#ifndef SETPROCTITLE_STRATEGY
-# define SETPROCTITLE_STRATEGY PS_USE_NONE 
+#ifndef SPT_TYPE
+# define SPT_TYPE      SPT_NONE
 #endif
 
-#ifndef SETPROCTITLE_PS_PADDING
-# define SETPROCTITLE_PS_PADDING       ' '
+#ifndef SPT_PADCHAR
+# define SPT_PADCHAR   '\0'
 #endif
-#endif /* HAVE_SETPROCTITLE */
 
-extern char **environ;
-
-/*
- * argv clobbering uses existing argv space, all other methods need a buffer
- */
-#if SETPROCTITLE_STRATEGY != PS_USE_CLOBBER_ARGV
-static char ps_buffer[256];
-static const size_t ps_buffer_size = sizeof(ps_buffer);
-#else
-static char *ps_buffer;                        /* will point to argv area */
-static size_t ps_buffer_size;          /* space determined at run time */
+#if SPT_TYPE == SPT_REUSEARGV
+static char *argv_start = NULL;
+static size_t argv_env_len = 0;
 #endif
 
-/* save the original argv[] location here */
-static int     save_argc;
-static char **save_argv;
-
-extern char *__progname;
+#endif /* HAVE_SETPROCTITLE */
 
-#ifndef HAVE_SETPROCTITLE
-/*
- * Call this to update the ps status display to a fixed prefix plus an
- * indication of what you're currently doing passed in the argument.
- */
 void
-setproctitle(const char *fmt, ...)
+compat_init_setproctitle(int argc, char *argv[])
 {
-#if SETPROCTITLE_STRATEGY == PS_USE_PSTAT
-       union pstun pst;
-#endif
-#if SETPROCTITLE_STRATEGY != PS_USE_NONE
-       ssize_t used;
-       va_list ap;
-
-       /* no ps display if you didn't call save_ps_display_args() */
-       if (save_argv == NULL)
-               return;
-#if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
-       /* If ps_buffer is a pointer, it might still be null */
-       if (ps_buffer == NULL)
-               return;
-#endif /* PS_USE_CLOBBER_ARGV */
+#if SPT_TYPE == SPT_REUSEARGV
+       extern char **environ;
+       char *lastargv = NULL;
+       char **envp = environ;
+       int i;
 
        /*
-        * Overwrite argv[] to point at appropriate space, if needed
+        * NB: This assumes that argv has already been copied out of the
+        * way. This is true for sshd, but may not be true for other 
+        * programs. Beware.
         */
-#if SETPROCTITLE_STRATEGY == PS_USE_CHANGE_ARGV
-       save_argv[0] = ps_buffer;
-       save_argv[1] = NULL;
-#endif /* PS_USE_CHANGE_ARGV */
 
-#if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
-       save_argv[1] = NULL;
-#endif /* PS_USE_CLOBBER_ARGV */
+       if (argc == 0 || argv[0] == NULL)
+               return;
+
+       /* Fail if we can't allocate room for the new environment */
+       for (i = 0; envp[i] != NULL; i++)
+               ;
+       if ((environ = malloc(sizeof(*environ) * (i + 1))) == NULL) {
+               environ = envp; /* put it back */
+               return;
+       }
 
        /*
-        * Make fixed prefix of ps display.
+        * Find the last argv string or environment variable within 
+        * our process memory area.
         */
-
-       va_start(ap, fmt);
-       if (fmt == NULL)
-               snprintf(ps_buffer, ps_buffer_size, "%s", __progname);
-       else {
-               used = snprintf(ps_buffer, ps_buffer_size, "%s: ", __progname);
-               if (used == -1 || used >= ps_buffer_size)
-                       used = ps_buffer_size;
-               vsnprintf(ps_buffer + used, ps_buffer_size - used, fmt, ap);
+       for (i = 0; i < argc; i++) {
+               if (lastargv == NULL || lastargv + 1 == argv[i])
+                       lastargv = argv[i] + strlen(argv[i]);
+       }
+       for (i = 0; envp[i] != NULL; i++) {
+               if (lastargv + 1 == envp[i])
+                       lastargv = envp[i] + strlen(envp[i]);
        }
-       va_end(ap);
-
-#if SETPROCTITLE_STRATEGY == PS_USE_PSTAT
-       pst.pst_command = ps_buffer;
-       pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
-#endif   /* PS_USE_PSTAT */
-
-#if SETPROCTITLE_STRATEGY == PS_USE_PS_STRINGS
-       PS_STRINGS->ps_nargvstr = 1;
-       PS_STRINGS->ps_argvstr = ps_buffer;
-#endif   /* PS_USE_PS_STRINGS */
 
-#if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
-       /* pad unused memory */
-       used = strlen(ps_buffer);
-       memset(ps_buffer + used, SETPROCTITLE_PS_PADDING, 
-           ps_buffer_size - used);
-#endif   /* PS_USE_CLOBBER_ARGV */
+       argv[1] = NULL;
+       argv_start = argv[0];
+       argv_env_len = lastargv - argv[0] - 1;
 
-#endif /* PS_USE_NONE */
+       /* 
+        * Copy environment 
+        * XXX - will truncate env on strdup fail
+        */
+       for (i = 0; envp[i] != NULL; i++)
+               environ[i] = strdup(envp[i]);
+       environ[i] = NULL;
+#endif /* SPT_REUSEARGV */
 }
 
-#endif /* HAVE_SETPROCTITLE */
-
-/*
- * Call this early in startup to save the original argc/argv values.
- *
- * argv[] will not be overwritten by this routine, but may be overwritten
- * during setproctitle. Also, the physical location of the environment
- * strings may be moved, so this should be called before any code that
- * might try to hang onto a getenv() result.
- */
+#ifndef HAVE_SETPROCTITLE
 void
-compat_init_setproctitle(int argc, char *argv[])
+setproctitle(const char *fmt, ...)
 {
-#if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
-       char *end_of_area = NULL;
-       char **new_environ;
-       int i;
+#if SPT_TYPE != SPT_NONE
+       va_list ap;
+       char buf[1024];
+       size_t len;
+       extern char *__progname;
+#if SPT_TYPE == SPT_PSTAT
+       union pstun pst;
 #endif
 
-       save_argc = argc;
-       save_argv = argv;
-
-#if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
-       /*
-        * If we're going to overwrite the argv area, count the available
-        * space.  Also move the environment to make additional room.
-        */
-
-       /*
-        * check for contiguous argv strings
-        */
-       for (i = 0; i < argc; i++) {
-               if (i == 0 || end_of_area + 1 == argv[i])
-                       end_of_area = argv[i] + strlen(argv[i]);
-       }
-
-       /* probably can't happen? */
-       if (end_of_area == NULL) {
-               ps_buffer = NULL;
-               ps_buffer_size = 0;
+#if SPT_TYPE == SPT_REUSEARGV
+       if (argv_env_len <= 0)
                return;
-       }
+#endif
 
-       /*
-        * check for contiguous environ strings following argv
-        */
-       for (i = 0; environ[i] != NULL; i++) {
-               if (end_of_area + 1 == environ[i])
-                       end_of_area = environ[i] + strlen(environ[i]);
+       strlcpy(buf, __progname, sizeof(buf));
+
+       va_start(ap, fmt);
+       if (fmt != NULL) {
+               len = strlcat(buf, ": ", sizeof(buf));
+               if (len < sizeof(buf))
+                       vsnprintf(buf + len, sizeof(buf) - len , fmt, ap);
        }
+       va_end(ap);
 
-       ps_buffer = argv[0];
-       ps_buffer_size = end_of_area - argv[0] - 1;
+#if SPT_TYPE == SPT_PSTAT
+       pst.pst_command = buf;
+       pstat(PSTAT_SETCMD, pst, strlen(buf), 0, 0);
+#elif SPT_TYPE == SPT_REUSEARGV
+/*     debug("setproctitle: copy \"%s\" into len %d", 
+           buf, argv_env_len); */
+       len = strlcpy(argv_start, buf, argv_env_len);
+       for(; len < argv_env_len; len++)
+               argv_start[len] = SPT_PADCHAR;
+#endif
 
-       /*
-        * Duplicate and move the environment out of the way
-        */
-       new_environ = malloc(sizeof(char *) * (i + 1));
-       for (i = 0; environ[i] != NULL; i++)
-               new_environ[i] = strdup(environ[i]);
-       new_environ[i] = NULL;
-       environ = new_environ;
-#endif /* PS_USE_CLOBBER_ARGV */
+#endif /* SPT_NONE */
 }
 
+#endif /* HAVE_SETPROCTITLE */
This page took 0.075371 seconds and 5 git commands to generate.