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