blob: 6e2b19bb4f584c50069196db7a6b4d0066b5d4ca [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.
Damien Miller4636d932003-06-03 11:23:32 +100017 * 3. Neither the name of the University nor the names of its contributors
Damien Miller35276252003-06-03 10:14:28 +100018 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
Kevin Steves8848b242000-10-18 13:11:44 +000020 *
Damien Miller35276252003-06-03 10:14:28 +100021 * 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
31 * SUCH DAMAGE.
Kevin Steves8848b242000-10-18 13:11:44 +000032 */
33
Kevin Steves8848b242000-10-18 13:11:44 +000034#include "includes.h"
35
Ben Lindstrom49a79c02000-11-17 03:47:20 +000036#ifndef HAVE_SETPROCTITLE
37
Damien Millera8ed44b2003-01-10 09:53:12 +110038#include <unistd.h>
39#ifdef HAVE_SYS_PSTAT_H
Damien Miller35276252003-06-03 10:14:28 +100040#include <sys/pstat.h>
Kevin Steves8848b242000-10-18 13:11:44 +000041#endif
42
Damien Miller35276252003-06-03 10:14:28 +100043#define SPT_NONE 0 /* don't use it at all */
Damien Miller2e45cb02004-02-20 20:37:44 +110044#define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */
45#define SPT_REUSEARGV 2 /* cover argv with title information */
Damien Millerec201962003-01-13 10:04:58 +110046
Damien Miller35276252003-06-03 10:14:28 +100047#ifndef SPT_TYPE
48# define SPT_TYPE SPT_NONE
Damien Millera8ed44b2003-01-10 09:53:12 +110049#endif
50
Damien Miller35276252003-06-03 10:14:28 +100051#ifndef SPT_PADCHAR
52# define SPT_PADCHAR '\0'
Damien Millerec201962003-01-13 10:04:58 +110053#endif
Damien Millera8ed44b2003-01-10 09:53:12 +110054
Damien Miller35276252003-06-03 10:14:28 +100055#if SPT_TYPE == SPT_REUSEARGV
56static char *argv_start = NULL;
57static size_t argv_env_len = 0;
Damien Millera8ed44b2003-01-10 09:53:12 +110058#endif
Damien Millera8ed44b2003-01-10 09:53:12 +110059
Kevin Steves8848b242000-10-18 13:11:44 +000060#endif /* HAVE_SETPROCTITLE */
Damien Millera8ed44b2003-01-10 09:53:12 +110061
Damien Millera8ed44b2003-01-10 09:53:12 +110062void
63compat_init_setproctitle(int argc, char *argv[])
64{
Darren Tucker6cf1a2c2003-06-22 18:32:10 +100065#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
Damien Miller35276252003-06-03 10:14:28 +100066 extern char **environ;
67 char *lastargv = NULL;
68 char **envp = environ;
Damien Millera8ed44b2003-01-10 09:53:12 +110069 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +110070
71 /*
Damien Miller35276252003-06-03 10:14:28 +100072 * NB: This assumes that argv has already been copied out of the
73 * way. This is true for sshd, but may not be true for other
74 * programs. Beware.
Damien Millera8ed44b2003-01-10 09:53:12 +110075 */
Damien Millera8ed44b2003-01-10 09:53:12 +110076
Damien Miller35276252003-06-03 10:14:28 +100077 if (argc == 0 || argv[0] == NULL)
78 return;
79
80 /* Fail if we can't allocate room for the new environment */
81 for (i = 0; envp[i] != NULL; i++)
82 ;
83 if ((environ = malloc(sizeof(*environ) * (i + 1))) == NULL) {
84 environ = envp; /* put it back */
Damien Millera8ed44b2003-01-10 09:53:12 +110085 return;
86 }
87
88 /*
Damien Miller35276252003-06-03 10:14:28 +100089 * Find the last argv string or environment variable within
90 * our process memory area.
Damien Millera8ed44b2003-01-10 09:53:12 +110091 */
Damien Miller35276252003-06-03 10:14:28 +100092 for (i = 0; i < argc; i++) {
93 if (lastargv == NULL || lastargv + 1 == argv[i])
94 lastargv = argv[i] + strlen(argv[i]);
95 }
96 for (i = 0; envp[i] != NULL; i++) {
97 if (lastargv + 1 == envp[i])
98 lastargv = envp[i] + strlen(envp[i]);
Damien Millera8ed44b2003-01-10 09:53:12 +110099 }
100
Damien Miller35276252003-06-03 10:14:28 +1000101 argv[1] = NULL;
102 argv_start = argv[0];
103 argv_env_len = lastargv - argv[0] - 1;
Damien Millera8ed44b2003-01-10 09:53:12 +1100104
Damien Miller35276252003-06-03 10:14:28 +1000105 /*
106 * Copy environment
107 * XXX - will truncate env on strdup fail
Damien Millera8ed44b2003-01-10 09:53:12 +1100108 */
Damien Miller35276252003-06-03 10:14:28 +1000109 for (i = 0; envp[i] != NULL; i++)
110 environ[i] = strdup(envp[i]);
111 environ[i] = NULL;
112#endif /* SPT_REUSEARGV */
Damien Millera8ed44b2003-01-10 09:53:12 +1100113}
114
Damien Miller35276252003-06-03 10:14:28 +1000115#ifndef HAVE_SETPROCTITLE
116void
117setproctitle(const char *fmt, ...)
118{
119#if SPT_TYPE != SPT_NONE
120 va_list ap;
121 char buf[1024];
122 size_t len;
123 extern char *__progname;
124#if SPT_TYPE == SPT_PSTAT
125 union pstun pst;
126#endif
127
128#if SPT_TYPE == SPT_REUSEARGV
129 if (argv_env_len <= 0)
130 return;
131#endif
132
133 strlcpy(buf, __progname, sizeof(buf));
134
135 va_start(ap, fmt);
136 if (fmt != NULL) {
137 len = strlcat(buf, ": ", sizeof(buf));
138 if (len < sizeof(buf))
139 vsnprintf(buf + len, sizeof(buf) - len , fmt, ap);
140 }
141 va_end(ap);
142
143#if SPT_TYPE == SPT_PSTAT
144 pst.pst_command = buf;
145 pstat(PSTAT_SETCMD, pst, strlen(buf), 0, 0);
146#elif SPT_TYPE == SPT_REUSEARGV
147/* debug("setproctitle: copy \"%s\" into len %d",
148 buf, argv_env_len); */
149 len = strlcpy(argv_start, buf, argv_env_len);
150 for(; len < argv_env_len; len++)
151 argv_start[len] = SPT_PADCHAR;
152#endif
153
154#endif /* SPT_NONE */
155}
156
157#endif /* HAVE_SETPROCTITLE */