135276253SDamien Miller /* Based on conf.c from UCB sendmail 8.8.8 */
23c06f6a0SBen Lindstrom 
335276253SDamien Miller /*
435276253SDamien Miller  * Copyright 2003 Damien Miller
535276253SDamien Miller  * Copyright (c) 1983, 1995-1997 Eric P. Allman
635276253SDamien Miller  * Copyright (c) 1988, 1993
735276253SDamien Miller  *	The Regents of the University of California.  All rights reserved.
83c06f6a0SBen Lindstrom  *
935276253SDamien Miller  * Redistribution and use in source and binary forms, with or without
1035276253SDamien Miller  * modification, are permitted provided that the following conditions
1135276253SDamien Miller  * are met:
1235276253SDamien Miller  * 1. Redistributions of source code must retain the above copyright
1335276253SDamien Miller  *    notice, this list of conditions and the following disclaimer.
1435276253SDamien Miller  * 2. Redistributions in binary form must reproduce the above copyright
1535276253SDamien Miller  *    notice, this list of conditions and the following disclaimer in the
1635276253SDamien Miller  *    documentation and/or other materials provided with the distribution.
174636d93cSDamien Miller  * 3. Neither the name of the University nor the names of its contributors
1835276253SDamien Miller  *    may be used to endorse or promote products derived from this software
1935276253SDamien Miller  *    without specific prior written permission.
203c06f6a0SBen Lindstrom  *
2135276253SDamien Miller  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2235276253SDamien Miller  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2335276253SDamien Miller  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2435276253SDamien Miller  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2535276253SDamien Miller  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2635276253SDamien Miller  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2735276253SDamien Miller  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2835276253SDamien Miller  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2935276253SDamien Miller  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3035276253SDamien Miller  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3135276253SDamien Miller  * SUCH DAMAGE.
323c06f6a0SBen Lindstrom  */
333c06f6a0SBen Lindstrom 
343c06f6a0SBen Lindstrom #include "includes.h"
353c06f6a0SBen Lindstrom 
363c06f6a0SBen Lindstrom #ifndef HAVE_SETPROCTITLE
373c06f6a0SBen Lindstrom 
38767e4134SDarren Tucker #include <stdarg.h>
39*73eb6cefSDarren Tucker #include <stdio.h>
40e6b641a9SDarren Tucker #include <stdlib.h>
41a8ed44b7SDamien Miller #include <unistd.h>
42a8ed44b7SDamien Miller #ifdef HAVE_SYS_PSTAT_H
4335276253SDamien Miller #include <sys/pstat.h>
443c06f6a0SBen Lindstrom #endif
45b8fe89c4SDamien Miller #include <string.h>
463c06f6a0SBen Lindstrom 
47c5750226SDamien Miller #include <vis.h>
48c5750226SDamien Miller 
4935276253SDamien Miller #define SPT_NONE	0	/* don't use it at all */
502e45cb0fSDamien Miller #define SPT_PSTAT	1	/* use pstat(PSTAT_SETCMD, ...) */
512e45cb0fSDamien Miller #define SPT_REUSEARGV	2	/* cover argv with title information */
52ec201964SDamien Miller 
5335276253SDamien Miller #ifndef SPT_TYPE
5435276253SDamien Miller # define SPT_TYPE	SPT_NONE
55a8ed44b7SDamien Miller #endif
56a8ed44b7SDamien Miller 
5735276253SDamien Miller #ifndef SPT_PADCHAR
5835276253SDamien Miller # define SPT_PADCHAR	'\0'
59ec201964SDamien Miller #endif
60a8ed44b7SDamien Miller 
6135276253SDamien Miller #if SPT_TYPE == SPT_REUSEARGV
6235276253SDamien Miller static char *argv_start = NULL;
6335276253SDamien Miller static size_t argv_env_len = 0;
64a8ed44b7SDamien Miller #endif
65a8ed44b7SDamien Miller 
663c06f6a0SBen Lindstrom #endif /* HAVE_SETPROCTITLE */
67a8ed44b7SDamien Miller 
68a8ed44b7SDamien Miller void
compat_init_setproctitle(int argc,char * argv[])69a8ed44b7SDamien Miller compat_init_setproctitle(int argc, char *argv[])
70a8ed44b7SDamien Miller {
71ee878387SDamien Miller #if !defined(HAVE_SETPROCTITLE) && \
72ee878387SDamien Miller     defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
7335276253SDamien Miller 	extern char **environ;
7435276253SDamien Miller 	char *lastargv = NULL;
7535276253SDamien Miller 	char **envp = environ;
76a8ed44b7SDamien Miller 	int i;
77a8ed44b7SDamien Miller 
78a8ed44b7SDamien Miller 	/*
7935276253SDamien Miller 	 * NB: This assumes that argv has already been copied out of the
8035276253SDamien Miller 	 * way. This is true for sshd, but may not be true for other
8135276253SDamien Miller 	 * programs. Beware.
82a8ed44b7SDamien Miller 	 */
83a8ed44b7SDamien Miller 
8435276253SDamien Miller 	if (argc == 0 || argv[0] == NULL)
8535276253SDamien Miller 		return;
8635276253SDamien Miller 
8735276253SDamien Miller 	/* Fail if we can't allocate room for the new environment */
8835276253SDamien Miller 	for (i = 0; envp[i] != NULL; i++)
8935276253SDamien Miller 		;
90d8093e49SDarren Tucker 	if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
9135276253SDamien Miller 		environ = envp;	/* put it back */
92a8ed44b7SDamien Miller 		return;
93a8ed44b7SDamien Miller 	}
94a8ed44b7SDamien Miller 
95a8ed44b7SDamien Miller 	/*
9635276253SDamien Miller 	 * Find the last argv string or environment variable within
9735276253SDamien Miller 	 * our process memory area.
98a8ed44b7SDamien Miller 	 */
9935276253SDamien Miller 	for (i = 0; i < argc; i++) {
10035276253SDamien Miller 		if (lastargv == NULL || lastargv + 1 == argv[i])
10135276253SDamien Miller 			lastargv = argv[i] + strlen(argv[i]);
10235276253SDamien Miller 	}
10335276253SDamien Miller 	for (i = 0; envp[i] != NULL; i++) {
10435276253SDamien Miller 		if (lastargv + 1 == envp[i])
10535276253SDamien Miller 			lastargv = envp[i] + strlen(envp[i]);
106a8ed44b7SDamien Miller 	}
107a8ed44b7SDamien Miller 
10835276253SDamien Miller 	argv[1] = NULL;
10935276253SDamien Miller 	argv_start = argv[0];
11035276253SDamien Miller 	argv_env_len = lastargv - argv[0] - 1;
111a8ed44b7SDamien Miller 
112a8ed44b7SDamien Miller 	/*
11335276253SDamien Miller 	 * Copy environment
11435276253SDamien Miller 	 * XXX - will truncate env on strdup fail
115a8ed44b7SDamien Miller 	 */
11635276253SDamien Miller 	for (i = 0; envp[i] != NULL; i++)
11735276253SDamien Miller 		environ[i] = strdup(envp[i]);
11835276253SDamien Miller 	environ[i] = NULL;
11935276253SDamien Miller #endif /* SPT_REUSEARGV */
120a8ed44b7SDamien Miller }
121a8ed44b7SDamien Miller 
12235276253SDamien Miller #ifndef HAVE_SETPROCTITLE
12335276253SDamien Miller void
setproctitle(const char * fmt,...)12435276253SDamien Miller setproctitle(const char *fmt, ...)
12535276253SDamien Miller {
12635276253SDamien Miller #if SPT_TYPE != SPT_NONE
12735276253SDamien Miller 	va_list ap;
128c5750226SDamien Miller 	char buf[1024], ptitle[1024];
129039bf2a8SDarren Tucker 	size_t len = 0;
130710f3747SDarren Tucker 	int r;
13135276253SDamien Miller 	extern char *__progname;
13235276253SDamien Miller #if SPT_TYPE == SPT_PSTAT
13335276253SDamien Miller 	union pstun pst;
13435276253SDamien Miller #endif
13535276253SDamien Miller 
13635276253SDamien Miller #if SPT_TYPE == SPT_REUSEARGV
13735276253SDamien Miller 	if (argv_env_len <= 0)
13835276253SDamien Miller 		return;
13935276253SDamien Miller #endif
14035276253SDamien Miller 
14135276253SDamien Miller 	strlcpy(buf, __progname, sizeof(buf));
14235276253SDamien Miller 
143710f3747SDarren Tucker 	r = -1;
14435276253SDamien Miller 	va_start(ap, fmt);
14535276253SDamien Miller 	if (fmt != NULL) {
14635276253SDamien Miller 		len = strlcat(buf, ": ", sizeof(buf));
14735276253SDamien Miller 		if (len < sizeof(buf))
148710f3747SDarren Tucker 			r = vsnprintf(buf + len, sizeof(buf) - len , fmt, ap);
14935276253SDamien Miller 	}
15035276253SDamien Miller 	va_end(ap);
151710f3747SDarren Tucker 	if (r == -1 || (size_t)r >= sizeof(buf) - len)
152710f3747SDarren Tucker 		return;
153c5750226SDamien Miller 	strnvis(ptitle, buf, sizeof(ptitle),
154c5750226SDamien Miller 	    VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL);
15535276253SDamien Miller 
15635276253SDamien Miller #if SPT_TYPE == SPT_PSTAT
157c5750226SDamien Miller 	pst.pst_command = ptitle;
158c5750226SDamien Miller 	pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0);
15935276253SDamien Miller #elif SPT_TYPE == SPT_REUSEARGV
16035276253SDamien Miller /*	debug("setproctitle: copy \"%s\" into len %d",
16135276253SDamien Miller 	    buf, argv_env_len); */
162c5750226SDamien Miller 	len = strlcpy(argv_start, ptitle, argv_env_len);
16335276253SDamien Miller 	for(; len < argv_env_len; len++)
16435276253SDamien Miller 		argv_start[len] = SPT_PADCHAR;
16535276253SDamien Miller #endif
16635276253SDamien Miller 
16735276253SDamien Miller #endif /* SPT_NONE */
16835276253SDamien Miller }
16935276253SDamien Miller 
17035276253SDamien Miller #endif /* HAVE_SETPROCTITLE */
171