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