1 /* Based on conf.c from UCB sendmail 8.8.8 */
4 * Copyright 2003 Damien Miller
5 * Copyright (c) 1983, 1995-1997 Eric P. Allman
6 * Copyright (c) 1988, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #ifndef HAVE_SETPROCTITLE
41 #ifdef HAVE_SYS_PSTAT_H
42 #include <sys/pstat.h>
48 #define SPT_NONE 0 /* don't use it at all */
49 #define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */
50 #define SPT_REUSEARGV 2 /* cover argv with title information */
53 # define SPT_TYPE SPT_NONE
57 # define SPT_PADCHAR '\0'
60 #if SPT_TYPE == SPT_REUSEARGV
61 static char *argv_start = NULL;
62 static size_t argv_env_len = 0;
65 #endif /* HAVE_SETPROCTITLE */
68 compat_init_setproctitle(int argc, char *argv[])
70 #if !defined(HAVE_SETPROCTITLE) && \
71 defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
72 extern char **environ;
73 char *lastargv = NULL;
74 char **envp = environ;
78 * NB: This assumes that argv has already been copied out of the
79 * way. This is true for sshd, but may not be true for other
83 if (argc == 0 || argv[0] == NULL)
86 /* Fail if we can't allocate room for the new environment */
87 for (i = 0; envp[i] != NULL; i++)
89 if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
90 environ = envp; /* put it back */
95 * Find the last argv string or environment variable within
96 * our process memory area.
98 for (i = 0; i < argc; i++) {
99 if (lastargv == NULL || lastargv + 1 == argv[i])
100 lastargv = argv[i] + strlen(argv[i]);
102 for (i = 0; envp[i] != NULL; i++) {
103 if (lastargv + 1 == envp[i])
104 lastargv = envp[i] + strlen(envp[i]);
108 argv_start = argv[0];
109 argv_env_len = lastargv - argv[0] - 1;
113 * XXX - will truncate env on strdup fail
115 for (i = 0; envp[i] != NULL; i++)
116 environ[i] = strdup(envp[i]);
118 #endif /* SPT_REUSEARGV */
121 #ifndef HAVE_SETPROCTITLE
123 setproctitle(const char *fmt, ...)
125 #if SPT_TYPE != SPT_NONE
127 char buf[1024], ptitle[1024];
130 extern char *__progname;
131 #if SPT_TYPE == SPT_PSTAT
135 #if SPT_TYPE == SPT_REUSEARGV
136 if (argv_env_len <= 0)
140 strlcpy(buf, __progname, sizeof(buf));
145 len = strlcat(buf, ": ", sizeof(buf));
146 if (len < sizeof(buf))
147 r = vsnprintf(buf + len, sizeof(buf) - len , fmt, ap);
150 if (r == -1 || (size_t)r >= sizeof(buf) - len)
152 strnvis(ptitle, buf, sizeof(ptitle),
153 VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL);
155 #if SPT_TYPE == SPT_PSTAT
156 pst.pst_command = ptitle;
157 pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0);
158 #elif SPT_TYPE == SPT_REUSEARGV
159 /* debug("setproctitle: copy \"%s\" into len %d",
160 buf, argv_env_len); */
161 len = strlcpy(argv_start, ptitle, argv_env_len);
162 for(; len < argv_env_len; len++)
163 argv_start[len] = SPT_PADCHAR;
166 #endif /* SPT_NONE */
169 #endif /* HAVE_SETPROCTITLE */