1 /*
2  * Copyright (c) 1997-2012 Motonori Nakamura <motonori@wide.ad.jp>
3  * Copyright (c) 1997-2012 WIDE Project
4  * Copyright (c) 1997-2003 Kyoto University
5  * Copyright (c) 2012 National Institute of Informatics
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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 WIDE Project and
20  *      its contributors.
21  * 4. Neither the name of the Project, the University nor the names of
22  *    its contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 /*
39  * Copyright (c) 1983, 1995-1997 Eric P. Allman
40  * Copyright (c) 1988, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71 
72 #ifndef lint
73 static char *_id_ = "$Id: proctitle.c,v 1.11 2012/06/07 05:45:28 motonori Exp $";
74 static char sccsid[] = "@(#)conf.c	8.379 (Berkeley) 10/20/97";
75 #endif /* not lint */
76 
77 # include "common.h"
78 # include "extern.h"
79 # include <sys/ioctl.h>
80 # include <sys/param.h>
81 
82 /*
83 **  SETPROCTITLE -- set process title for ps
84 **
85 **	Parameters:
86 **		fmt -- a printf style format string.
87 **		a, b, c -- possible parameters to fmt.
88 **
89 **	Returns:
90 **		none.
91 **
92 **	Side Effects:
93 **		Clobbers argv of our main procedure so ps(1) will
94 **		display the title.
95 */
96 
97 #define SPT_NONE	0	/* don't use it at all */
98 #define SPT_REUSEARGV	1	/* cover argv with title information */
99 #define SPT_BUILTIN	2	/* use libc builtin */
100 #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
101 #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
102 #define SPT_SYSMIPS	5	/* use sysmips() supported by NEWS-OS 6 */
103 #define SPT_SCO		6	/* write kernel u. area */
104 #define SPT_CHANGEARGV	7	/* write our own strings into argv[] */
105 
106 #ifndef SPT_TYPE
107 # define SPT_TYPE	SPT_REUSEARGV
108 #endif
109 
110 #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
111 
112 # if SPT_TYPE == SPT_PSTAT
113 #  include <sys/pstat.h>
114 # endif
115 # if SPT_TYPE == SPT_PSSTRINGS
116 #  include <machine/vmparam.h>
117 #  include <sys/exec.h>
118 #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
119 #   undef SPT_TYPE
120 #   define SPT_TYPE	SPT_REUSEARGV
121 #  else
122 #   ifndef NKPDE			/* FreeBSD 2.0 */
123 #    define NKPDE 63
124 typedef unsigned int	*pt_entry_t;
125 #   endif
126 #  endif
127 # endif
128 
129 # if SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV
130 #  define SETPROC_STATIC	static
131 # else
132 #  define SETPROC_STATIC
133 # endif
134 
135 # if SPT_TYPE == SPT_SYSMIPS
136 #  include <sys/sysmips.h>
137 #  include <sys/sysnews.h>
138 # endif
139 
140 # if SPT_TYPE == SPT_SCO
141 #  include <sys/immu.h>
142 #  include <sys/dir.h>
143 #  include <sys/user.h>
144 #  include <sys/fs/s5param.h>
145 #  if PSARGSZ > MAXLINE
146 #   define SPT_BUFSIZE	PSARGSZ
147 #  endif
148 # endif
149 
150 # ifndef SPT_PADCHAR
151 #  define SPT_PADCHAR	' '
152 # endif
153 
154 # ifndef SPT_BUFSIZE
155 #  define SPT_BUFSIZE	MAXLINE
156 # endif
157 
158 #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
159 
160 /*
161 **  Pointers for setproctitle.
162 **	This allows "ps" listings to give more useful information.
163 */
164 
165 char		**Argv = NULL;		/* pointer to argument vector */
166 char		*LastArgv = NULL;	/* end of argv */
167 
168 void
initsetproctitle(argc,argv,envp)169 initsetproctitle(argc, argv, envp)
170 	int argc;
171 	char **argv;
172 	char **envp;
173 {
174 	register int i, envpsize = 0;
175 	extern char **environ;
176 
177 	/*
178 	**  Move the environment so setproctitle can use the space at
179 	**  the top of memory.
180 	*/
181 
182 	for (i = 0; envp[i] != NULL; i++)
183 		envpsize += strlen(envp[i]) + 1;
184 	environ = (char **) MALLOC(sizeof (char *) * (i + 1));
185 	if (environ == NULL)
186 	{
187 		/* XXX */
188 		return;
189 	}
190 	for (i = 0; envp[i] != NULL; i++)
191 		environ[i] = newstr(envp[i]);
192 	environ[i] = NULL;
193 
194 	/*
195 	**  Save start and extent of argv for setproctitle.
196 	*/
197 
198 	Argv = argv;
199 
200 	/*
201 	**  Find the last environment variable within smtpfeed's
202 	**  process memory area.
203 	*/
204 	while (i > 0 && (envp[i - 1] < argv[0] ||
205 			 envp[i - 1] > (argv[argc - 1] +
206 					strlen(argv[argc - 1]) + 1 + envpsize)))
207 		i--;
208 
209 	if (i > 0)
210 		LastArgv = envp[i - 1] + strlen(envp[i - 1]);
211 	else
212 		LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
213 }
214 
215 #if SPT_TYPE != SPT_BUILTIN
216 
217 
218 /*VARARGS1*/
219 void
220 # ifdef __STDC__
setproctitle(const char * fmt,...)221 setproctitle(const char *fmt, ...)
222 # else
223 setproctitle(fmt, va_alist)
224 	const char *fmt;
225 	va_dcl
226 # endif
227 {
228 # if SPT_TYPE != SPT_NONE
229 	register char *p;
230 	register int i;
231 	SETPROC_STATIC char buf[SPT_BUFSIZE];
232 	VA_LOCAL_DECL
233 #  if SPT_TYPE == SPT_PSTAT
234 	union pstun pst;
235 #  endif
236 #  if SPT_TYPE == SPT_SCO
237 	off_t seek_off;
238 	static int kmem = -1;
239 	static int kmempid = -1;
240 	struct user u;
241 #  endif
242 
243 	p = buf;
244 
245 	/* print smtpfeed: heading for grep */
246 	(void) strcpy(p, "smtpfeed: ");
247 	p += strlen(p);
248 
249 	/* print the argument string */
250 	VA_START(fmt);
251 #define SPACELEFT(buf, ptr)	(sizeof buf - ((ptr) - buf))
252 	(void) vsnprintf(p, SPACELEFT(buf, p), fmt, ap);
253 	VA_END;
254 
255 	i = strlen(buf);
256 
257 #  if SPT_TYPE == SPT_PSTAT
258 	pst.pst_command = buf;
259 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
260 #  endif
261 #  if SPT_TYPE == SPT_PSSTRINGS
262 	PS_STRINGS->ps_nargvstr = 1;
263 	PS_STRINGS->ps_argvstr = buf;
264 #  endif
265 #  if SPT_TYPE == SPT_SYSMIPS
266 	sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
267 #  endif
268 #  if SPT_TYPE == SPT_SCO
269 	if (kmem < 0 || kmempid != getpid())
270 	{
271 		if (kmem >= 0)
272 			close(kmem);
273 		kmem = open(_PATH_KMEM, O_RDWR, 0);
274 		if (kmem < 0)
275 			return;
276 		(void) fcntl(kmem, F_SETFD, 1);
277 		kmempid = getpid();
278 	}
279 	buf[PSARGSZ - 1] = '\0';
280 	seek_off = UVUBLK + (off_t) u.u_psargs - (off_t) &u;
281 	if (lseek(kmem, (off_t) seek_off, SEEK_SET) == seek_off)
282 		(void) write(kmem, buf, PSARGSZ);
283 #  endif
284 #  if SPT_TYPE == SPT_REUSEARGV
285 	if (LastArgv == NULL)
286 		return;
287 	if (i > LastArgv - Argv[0] - 2)
288 	{
289 		i = LastArgv - Argv[0] - 2;
290 		buf[i] = '\0';
291 	}
292 	(void) strcpy(Argv[0], buf);
293 	p = &Argv[0][i];
294 	while (p < LastArgv)
295 		*p++ = SPT_PADCHAR;
296 	Argv[1] = NULL;
297 #  endif
298 #  if SPT_TYPE == SPT_CHANGEARGV
299 	Argv[0] = buf;
300 	Argv[1] = 0;
301 #  endif
302 # endif /* SPT_TYPE != SPT_NONE */
303 }
304 
305 #endif /* SPT_TYPE != SPT_BUILTIN */
306