]> andersk Git - openssh.git/blame - openbsd-compat/setproctitle.c
- (dtucker) [openbsd-compat/regress/closefromtest.c] Bug #1358: Always
[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.
dda8edc3 17 * 3. Neither the name of the University nor the names of its contributors
3a2b2b44 18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
f5af5cd5 20 *
3a2b2b44 21 * 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.
f5af5cd5 32 */
33
f5af5cd5 34#include "includes.h"
35
260d427b 36#ifndef HAVE_SETPROCTITLE
37
baede55a 38#include <stdarg.h>
f8688ddd 39#include <stdlib.h>
d0104542 40#include <unistd.h>
41#ifdef HAVE_SYS_PSTAT_H
3a2b2b44 42#include <sys/pstat.h>
d0104542 43#endif
28cb0a43 44#include <string.h>
d0104542 45
3a2b2b44 46#define SPT_NONE 0 /* don't use it at all */
45a3410a 47#define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */
48#define SPT_REUSEARGV 2 /* cover argv with title information */
e80fb2a0 49
3a2b2b44 50#ifndef SPT_TYPE
51# define SPT_TYPE SPT_NONE
f5af5cd5 52#endif
53
3a2b2b44 54#ifndef SPT_PADCHAR
55# define SPT_PADCHAR '\0'
d0104542 56#endif
877c5ea2 57
3a2b2b44 58#if SPT_TYPE == SPT_REUSEARGV
59static char *argv_start = NULL;
60static size_t argv_env_len = 0;
e80fb2a0 61#endif
d0104542 62
3a2b2b44 63#endif /* HAVE_SETPROCTITLE */
f5af5cd5 64
f5af5cd5 65void
3a2b2b44 66compat_init_setproctitle(int argc, char *argv[])
f5af5cd5 67{
39ef3618 68#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
3a2b2b44 69 extern char **environ;
70 char *lastargv = NULL;
71 char **envp = environ;
72 int i;
d0104542 73
74 /*
3a2b2b44 75 * NB: This assumes that argv has already been copied out of the
76 * way. This is true for sshd, but may not be true for other
77 * programs. Beware.
d0104542 78 */
d0104542 79
3a2b2b44 80 if (argc == 0 || argv[0] == NULL)
81 return;
82
83 /* Fail if we can't allocate room for the new environment */
84 for (i = 0; envp[i] != NULL; i++)
85 ;
01d35895 86 if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
3a2b2b44 87 environ = envp; /* put it back */
88 return;
89 }
d0104542 90
91 /*
3a2b2b44 92 * Find the last argv string or environment variable within
93 * our process memory area.
d0104542 94 */
3a2b2b44 95 for (i = 0; i < argc; i++) {
96 if (lastargv == NULL || lastargv + 1 == argv[i])
97 lastargv = argv[i] + strlen(argv[i]);
98 }
99 for (i = 0; envp[i] != NULL; i++) {
100 if (lastargv + 1 == envp[i])
101 lastargv = envp[i] + strlen(envp[i]);
d0104542 102 }
f5af5cd5 103
3a2b2b44 104 argv[1] = NULL;
105 argv_start = argv[0];
106 argv_env_len = lastargv - argv[0] - 1;
d0104542 107
3a2b2b44 108 /*
109 * Copy environment
110 * XXX - will truncate env on strdup fail
111 */
112 for (i = 0; envp[i] != NULL; i++)
113 environ[i] = strdup(envp[i]);
114 environ[i] = NULL;
115#endif /* SPT_REUSEARGV */
f5af5cd5 116}
d0104542 117
3a2b2b44 118#ifndef HAVE_SETPROCTITLE
d0104542 119void
3a2b2b44 120setproctitle(const char *fmt, ...)
d0104542 121{
3a2b2b44 122#if SPT_TYPE != SPT_NONE
123 va_list ap;
124 char buf[1024];
125 size_t len;
126 extern char *__progname;
127#if SPT_TYPE == SPT_PSTAT
128 union pstun pst;
d0104542 129#endif
130
3a2b2b44 131#if SPT_TYPE == SPT_REUSEARGV
132 if (argv_env_len <= 0)
d0104542 133 return;
3a2b2b44 134#endif
d0104542 135
3a2b2b44 136 strlcpy(buf, __progname, sizeof(buf));
137
138 va_start(ap, fmt);
139 if (fmt != NULL) {
140 len = strlcat(buf, ": ", sizeof(buf));
141 if (len < sizeof(buf))
142 vsnprintf(buf + len, sizeof(buf) - len , fmt, ap);
d0104542 143 }
3a2b2b44 144 va_end(ap);
d0104542 145
3a2b2b44 146#if SPT_TYPE == SPT_PSTAT
147 pst.pst_command = buf;
148 pstat(PSTAT_SETCMD, pst, strlen(buf), 0, 0);
149#elif SPT_TYPE == SPT_REUSEARGV
150/* debug("setproctitle: copy \"%s\" into len %d",
151 buf, argv_env_len); */
152 len = strlcpy(argv_start, buf, argv_env_len);
153 for(; len < argv_env_len; len++)
154 argv_start[len] = SPT_PADCHAR;
155#endif
d0104542 156
3a2b2b44 157#endif /* SPT_NONE */
d0104542 158}
159
3a2b2b44 160#endif /* HAVE_SETPROCTITLE */
This page took 0.244129 seconds and 5 git commands to generate.