blob: 24cff0c80dcfdf692e7fde8678afb2b5d0743ac2 [file] [log] [blame]
Damien Miller35276252003-06-03 10:14:28 +10001/* Based on conf.c from UCB sendmail 8.8.8 */
Kevin Steves8848b242000-10-18 13:11:44 +00002
Damien Miller35276252003-06-03 10:14:28 +10003/*
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.
Kevin Steves8848b242000-10-18 13:11:44 +00008 *
Damien Miller35276252003-06-03 10:14:28 +10009 * 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.
Kevin Steves8848b242000-10-18 13:11:44 +000024 *
Damien Miller35276252003-06-03 10:14:28 +100025 * 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.
Kevin Steves8848b242000-10-18 13:11:44 +000036 */
37
Kevin Steves8848b242000-10-18 13:11:44 +000038#include "includes.h"
39
Ben Lindstrom49a79c02000-11-17 03:47:20 +000040#ifndef HAVE_SETPROCTITLE
41
Damien Millera8ed44b2003-01-10 09:53:12 +110042#include <unistd.h>
43#ifdef HAVE_SYS_PSTAT_H
Damien Miller35276252003-06-03 10:14:28 +100044#include <sys/pstat.h>
Kevin Steves8848b242000-10-18 13:11:44 +000045#endif
46
Damien Miller35276252003-06-03 10:14:28 +100047#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, ...) */
Damien Millerec201962003-01-13 10:04:58 +110050
Damien Miller35276252003-06-03 10:14:28 +100051#ifndef SPT_TYPE
52# define SPT_TYPE SPT_NONE
Damien Millera8ed44b2003-01-10 09:53:12 +110053#endif
54
Damien Miller35276252003-06-03 10:14:28 +100055#ifndef SPT_PADCHAR
56# define SPT_PADCHAR '\0'
Damien Millerec201962003-01-13 10:04:58 +110057#endif
Damien Millera8ed44b2003-01-10 09:53:12 +110058
Damien Miller35276252003-06-03 10:14:28 +100059#if SPT_TYPE == SPT_REUSEARGV
60static char *argv_start = NULL;
61static size_t argv_env_len = 0;
Damien Millera8ed44b2003-01-10 09:53:12 +110062#endif
Damien Millera8ed44b2003-01-10 09:53:12 +110063
Kevin Steves8848b242000-10-18 13:11:44 +000064#endif /* HAVE_SETPROCTITLE */
Damien Millera8ed44b2003-01-10 09:53:12 +110065
Damien Millera8ed44b2003-01-10 09:53:12 +110066void
67compat_init_setproctitle(int argc, char *argv[])
68{
Damien Miller35276252003-06-03 10:14:28 +100069#if SPT_TYPE == SPT_REUSEARGV
70 extern char **environ;
71 char *lastargv = NULL;
72 char **envp = environ;
Damien Millera8ed44b2003-01-10 09:53:12 +110073 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +110074
75 /*
Damien Miller35276252003-06-03 10:14:28 +100076 * 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.
Damien Millera8ed44b2003-01-10 09:53:12 +110079 */
Damien Millera8ed44b2003-01-10 09:53:12 +110080
Damien Miller35276252003-06-03 10:14:28 +100081 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 */
Damien Millera8ed44b2003-01-10 09:53:12 +110089 return;
90 }
91
92 /*
Damien Miller35276252003-06-03 10:14:28 +100093 * Find the last argv string or environment variable within
94 * our process memory area.
Damien Millera8ed44b2003-01-10 09:53:12 +110095 */
Damien Miller35276252003-06-03 10:14:28 +100096 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]);
Damien Millera8ed44b2003-01-10 09:53:12 +1100103 }
104
Damien Miller35276252003-06-03 10:14:28 +1000105 argv[1] = NULL;
106 argv_start = argv[0];
107 argv_env_len = lastargv - argv[0] - 1;
Damien Millera8ed44b2003-01-10 09:53:12 +1100108
Damien Miller35276252003-06-03 10:14:28 +1000109 /*
110 * Copy environment
111 * XXX - will truncate env on strdup fail
Damien Millera8ed44b2003-01-10 09:53:12 +1100112 */
Damien Miller35276252003-06-03 10:14:28 +1000113 for (i = 0; envp[i] != NULL; i++)
114 environ[i] = strdup(envp[i]);
115 environ[i] = NULL;
116#endif /* SPT_REUSEARGV */
Damien Millera8ed44b2003-01-10 09:53:12 +1100117}
118
Damien Miller35276252003-06-03 10:14:28 +1000119#ifndef HAVE_SETPROCTITLE
120void
121setproctitle(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 */