]> andersk Git - openssh.git/blame - openbsd-compat/setproctitle.c
- (djm) OpenBSD CVS Sync
[openssh.git] / openbsd-compat / setproctitle.c
CommitLineData
3a2b2b44 1/* Based on conf.c from UCB sendmail 8.8.8 */
f5af5cd5 2
3a2b2b44 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.
f5af5cd5 8 *
3a2b2b44 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.
f5af5cd5 24 *
3a2b2b44 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.
f5af5cd5 36 */
37
f5af5cd5 38#include "includes.h"
39
260d427b 40#ifndef HAVE_SETPROCTITLE
41
d0104542 42#include <unistd.h>
43#ifdef HAVE_SYS_PSTAT_H
3a2b2b44 44#include <sys/pstat.h>
d0104542 45#endif
46
3a2b2b44 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, ...) */
e80fb2a0 50
3a2b2b44 51#ifndef SPT_TYPE
52# define SPT_TYPE SPT_NONE
f5af5cd5 53#endif
54
3a2b2b44 55#ifndef SPT_PADCHAR
56# define SPT_PADCHAR '\0'
d0104542 57#endif
877c5ea2 58
3a2b2b44 59#if SPT_TYPE == SPT_REUSEARGV
60static char *argv_start = NULL;
61static size_t argv_env_len = 0;
e80fb2a0 62#endif
d0104542 63
3a2b2b44 64#endif /* HAVE_SETPROCTITLE */
f5af5cd5 65
f5af5cd5 66void
3a2b2b44 67compat_init_setproctitle(int argc, char *argv[])
f5af5cd5 68{
3a2b2b44 69#if SPT_TYPE == SPT_REUSEARGV
70 extern char **environ;
71 char *lastargv = NULL;
72 char **envp = environ;
73 int i;
d0104542 74
75 /*
3a2b2b44 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.
d0104542 79 */
d0104542 80
3a2b2b44 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 }
d0104542 91
92 /*
3a2b2b44 93 * Find the last argv string or environment variable within
94 * our process memory area.
d0104542 95 */
3a2b2b44 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]);
d0104542 103 }
f5af5cd5 104
3a2b2b44 105 argv[1] = NULL;
106 argv_start = argv[0];
107 argv_env_len = lastargv - argv[0] - 1;
d0104542 108
3a2b2b44 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 */
f5af5cd5 117}
d0104542 118
3a2b2b44 119#ifndef HAVE_SETPROCTITLE
d0104542 120void
3a2b2b44 121setproctitle(const char *fmt, ...)
d0104542 122{
3a2b2b44 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;
d0104542 130#endif
131
3a2b2b44 132#if SPT_TYPE == SPT_REUSEARGV
133 if (argv_env_len <= 0)
d0104542 134 return;
3a2b2b44 135#endif
d0104542 136
3a2b2b44 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);
d0104542 144 }
3a2b2b44 145 va_end(ap);
d0104542 146
3a2b2b44 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
d0104542 157
3a2b2b44 158#endif /* SPT_NONE */
d0104542 159}
160
3a2b2b44 161#endif /* HAVE_SETPROCTITLE */
This page took 0.170882 seconds and 5 git commands to generate.