1 /* Based on conf.c from UCB sendmail 8.8.8 */
2 
3 /*
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.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include "includes.h"
39 
40 #ifndef HAVE_SETPROCTITLE
41 
42 #include <unistd.h>
43 #ifdef HAVE_SYS_PSTAT_H
44 #include <sys/pstat.h>
45 #endif
46 
47 #define SPT_NONE	0	/* don't use it at all */
48 #define SPT_PSTAT	1	/* cover argv with title information */
49 #define SPT_REUSEARGV	2	/* use pstat(PSTAT_SETCMD, ...) */
50 
51 #ifndef SPT_TYPE
52 # define SPT_TYPE	SPT_NONE
53 #endif
54 
55 #ifndef SPT_PADCHAR
56 # define SPT_PADCHAR	'\0'
57 #endif
58 
59 #if SPT_TYPE == SPT_REUSEARGV
60 static char *argv_start = NULL;
61 static size_t argv_env_len = 0;
62 #endif
63 
64 #endif /* HAVE_SETPROCTITLE */
65 
66 void
67 compat_init_setproctitle(int argc, char *argv[])
68 {
69 #if SPT_TYPE == SPT_REUSEARGV
70 	extern char **environ;
71 	char *lastargv = NULL;
72 	char **envp = environ;
73 	int i;
74 
75 	/*
76 	 * NB: This assumes that argv has already been copied out of the
77 	 * way. This is true for sshd, but may not be true for other
78 	 * programs. Beware.
79 	 */
80 
81 	if (argc == 0 || argv[0] == NULL)
82 		return;
83 
84 	/* Fail if we can't allocate room for the new environment */
85 	for (i = 0; envp[i] != NULL; i++)
86 		;
87 	if ((environ = malloc(sizeof(*environ) * (i + 1))) == NULL) {
88 		environ = envp;	/* put it back */
89 		return;
90 	}
91 
92 	/*
93 	 * Find the last argv string or environment variable within
94 	 * our process memory area.
95 	 */
96 	for (i = 0; i < argc; i++) {
97 		if (lastargv == NULL || lastargv + 1 == argv[i])
98 			lastargv = argv[i] + strlen(argv[i]);
99 	}
100 	for (i = 0; envp[i] != NULL; i++) {
101 		if (lastargv + 1 == envp[i])
102 			lastargv = envp[i] + strlen(envp[i]);
103 	}
104 
105 	argv[1] = NULL;
106 	argv_start = argv[0];
107 	argv_env_len = lastargv - argv[0] - 1;
108 
109 	/*
110 	 * Copy environment
111 	 * XXX - will truncate env on strdup fail
112 	 */
113 	for (i = 0; envp[i] != NULL; i++)
114 		environ[i] = strdup(envp[i]);
115 	environ[i] = NULL;
116 #endif /* SPT_REUSEARGV */
117 }
118 
119 #ifndef HAVE_SETPROCTITLE
120 void
121 setproctitle(const char *fmt, ...)
122 {
123 #if SPT_TYPE != SPT_NONE
124 	va_list ap;
125 	char buf[1024];
126 	size_t len;
127 	extern char *__progname;
128 #if SPT_TYPE == SPT_PSTAT
129 	union pstun pst;
130 #endif
131 
132 #if SPT_TYPE == SPT_REUSEARGV
133 	if (argv_env_len <= 0)
134 		return;
135 #endif
136 
137 	strlcpy(buf, __progname, sizeof(buf));
138 
139 	va_start(ap, fmt);
140 	if (fmt != NULL) {
141 		len = strlcat(buf, ": ", sizeof(buf));
142 		if (len < sizeof(buf))
143 			vsnprintf(buf + len, sizeof(buf) - len , fmt, ap);
144 	}
145 	va_end(ap);
146 
147 #if SPT_TYPE == SPT_PSTAT
148 	pst.pst_command = buf;
149 	pstat(PSTAT_SETCMD, pst, strlen(buf), 0, 0);
150 #elif SPT_TYPE == SPT_REUSEARGV
151 /*	debug("setproctitle: copy \"%s\" into len %d",
152 	    buf, argv_env_len); */
153 	len = strlcpy(argv_start, buf, argv_env_len);
154 	for(; len < argv_env_len; len++)
155 		argv_start[len] = SPT_PADCHAR;
156 #endif
157 
158 #endif /* SPT_NONE */
159 }
160 
161 #endif /* HAVE_SETPROCTITLE */
162