summaryrefslogtreecommitdiff
path: root/openbsd-compat
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/bsd-misc.c12
-rw-r--r--openbsd-compat/setproctitle.c288
-rw-r--r--openbsd-compat/setproctitle.h3
3 files changed, 229 insertions, 74 deletions
diff --git a/openbsd-compat/bsd-misc.c b/openbsd-compat/bsd-misc.c
index 1c1e43a52..d7180d424 100644
--- a/openbsd-compat/bsd-misc.c
+++ b/openbsd-compat/bsd-misc.c
@@ -23,15 +23,20 @@
23 */ 23 */
24 24
25#include "includes.h" 25#include "includes.h"
26#include "xmalloc.h"
26 27
27RCSID("$Id: bsd-misc.c,v 1.10 2002/07/08 21:09:41 mouring Exp $"); 28RCSID("$Id: bsd-misc.c,v 1.11 2003/01/09 22:53:13 djm Exp $");
28 29
30/*
31 * NB. duplicate __progname in case it is an alias for argv[0]
32 * Otherwise it may get clobbered by setproctitle()
33 */
29char *get_progname(char *argv0) 34char *get_progname(char *argv0)
30{ 35{
31#ifdef HAVE___PROGNAME 36#ifdef HAVE___PROGNAME
32 extern char *__progname; 37 extern char *__progname;
33 38
34 return __progname; 39 return xstrdup(__progname);
35#else 40#else
36 char *p; 41 char *p;
37 42
@@ -42,7 +47,8 @@ char *get_progname(char *argv0)
42 p = argv0; 47 p = argv0;
43 else 48 else
44 p++; 49 p++;
45 return p; 50
51 return xstrdup(p);
46#endif 52#endif
47} 53}
48 54
diff --git a/openbsd-compat/setproctitle.c b/openbsd-compat/setproctitle.c
index e165dd13c..5439bd07e 100644
--- a/openbsd-compat/setproctitle.c
+++ b/openbsd-compat/setproctitle.c
@@ -1,102 +1,250 @@
1/* 1/*
2 * Modified for OpenSSH by Kevin Steves 2 * Based on src/backend/utils/misc/pg_status.c from
3 * October 2000 3 * PostgreSQL Database Management System
4 *
5 * Portions Copyright (c) 1996-2001, The PostgreSQL Global Development Group
6 *
7 * Portions Copyright (c) 1994, The Regents of the University of California
8 *
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose, without fee, and without a written agreement
11 * is hereby granted, provided that the above copyright notice and this
12 * paragraph and the following two paragraphs appear in all copies.
13 *
14 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
16 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
17 * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 *
20 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
4 */ 25 */
5 26
6/* 27/*--------------------------------------------------------------------
7 * Copyright (c) 1994, 1995 Christopher G. Demetriou 28 * ps_status.c
8 * All rights reserved. 29 *
30 * Routines to support changing the ps display of PostgreSQL backends
31 * to contain some useful information. Mechanism differs wildly across
32 * platforms.
9 * 33 *
10 * Redistribution and use in source and binary forms, with or without 34 * $Header: /var/cvs/openssh/openbsd-compat/setproctitle.c,v 1.3 2003/01/09 22:53:13 djm Exp $
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Christopher G. Demetriou
21 * for the NetBSD Project.
22 * 4. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission
24 * 35 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 36 * Copyright 2000 by PostgreSQL Global Development Group
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * various details abducted from various places
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 38 *--------------------------------------------------------------------
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */ 39 */
36 40
37#if defined(LIBC_SCCS) && !defined(lint)
38static char rcsid[] = "$OpenBSD: setproctitle.c,v 1.8 2001/11/06 19:21:40 art Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include "includes.h" 41#include "includes.h"
42 42
43#ifndef HAVE_SETPROCTITLE 43#ifndef HAVE_SETPROCTITLE
44 44
45#define SPT_NONE 0 45#include <unistd.h>
46#define SPT_PSTAT 1 46#ifdef HAVE_SYS_PSTAT_H
47#include <sys/pstat.h> /* for HP-UX */
48#endif
49#ifdef HAVE_PS_STRINGS
50#include <machine/vmparam.h> /* for old BSD */
51#include <sys/exec.h>
52#endif
53
54extern char **environ;
47 55
48#ifndef SPT_TYPE 56/*------
49#define SPT_TYPE SPT_NONE 57 * Alternative ways of updating ps display:
58 *
59 * PS_USE_PSTAT
60 * use the pstat(PSTAT_SETCMD, )
61 * (HPUX)
62 * PS_USE_PS_STRINGS
63 * assign PS_STRINGS->ps_argvstr = "string"
64 * (some BSD systems)
65 * PS_USE_CHANGE_ARGV
66 * assign argv[0] = "string"
67 * (some other BSD systems)
68 * PS_USE_CLOBBER_ARGV
69 * write over the argv and environment area
70 * (most SysV-like systems)
71 * PS_USE_NONE
72 * don't update ps display
73 * (This is the default, as it is safest.)
74 */
75#if defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
76#define PS_USE_PSTAT
77#elif defined(HAVE_PS_STRINGS)
78#define PS_USE_PS_STRINGS
79#elif defined(BSD) || defined(__bsdi__) || defined(__hurd__)
80#define PS_USE_CHANGE_ARGV
81#elif defined(__linux__) || defined(_AIX)
82#define PS_USE_CLOBBER_ARGV
83#else
84#define PS_USE_NONE
50#endif 85#endif
51 86
52#if SPT_TYPE == SPT_PSTAT 87/* Different systems want the buffer padded differently */
53#include <sys/param.h> 88#if defined(_AIX) || defined(__linux__) || defined(__QNX__) || defined(__svr4__)
54#include <sys/pstat.h> 89#define PS_PADDING '\0'
55#endif /* SPT_TYPE == SPT_PSTAT */ 90#else
91#define PS_PADDING ' '
92#endif
56 93
57#define MAX_PROCTITLE 2048 94/*
95 * argv clobbering uses existing argv space, all other methods need a buffer
96 */
97#ifndef PS_USE_CLOBBER_ARGV
98static char ps_buffer[256];
99static const size_t ps_buffer_size = sizeof(ps_buffer);
100#else /* PS_USE_CLOBBER_ARGV */
101static char *ps_buffer; /* will point to argv area */
102static size_t ps_buffer_size; /* space determined at run time */
103#endif /* PS_USE_CLOBBER_ARGV */
104
105/* save the original argv[] location here */
106static int save_argc;
107static char **save_argv;
58 108
59extern char *__progname; 109extern char *__progname;
60 110
61/* 111/*
62 * Set Process Title (SPT) defines. Modeled after sendmail's 112 * Call this to update the ps status display to a fixed prefix plus an
63 * SPT type definition strategy. 113 * indication of what you're currently doing passed in the argument.
64 *
65 * SPT_TYPE:
66 *
67 * SPT_NONE: Don't set the process title. Default.
68 * SPT_PSTAT: Use pstat(PSTAT_SETCMD). HP-UX specific.
69 */ 114 */
70
71void 115void
72setproctitle(const char *fmt, ...) 116setproctitle(const char *fmt, ...)
73{ 117{
74#if SPT_TYPE != SPT_NONE 118#ifdef PS_USE_PSTAT
119 union pstun pst;
120#endif
121#ifndef PS_USE_NONE
122 ssize_t used;
75 va_list ap; 123 va_list ap;
76
77 char buf[MAX_PROCTITLE];
78 size_t used;
79 124
80#if SPT_TYPE == SPT_PSTAT 125 /* no ps display if you didn't call save_ps_display_args() */
81 union pstun pst; 126 if (save_argv == NULL)
82#endif /* SPT_TYPE == SPT_PSTAT */ 127 return;
128#ifdef PS_USE_CLOBBER_ARGV
129 /* If ps_buffer is a pointer, it might still be null */
130 if (ps_buffer == NULL)
131 return;
132#endif /* PS_USE_CLOBBER_ARGV */
133
134 /*
135 * Overwrite argv[] to point at appropriate space, if needed
136 */
137#ifdef PS_USE_CHANGE_ARGV
138 save_argv[0] = ps_buffer;
139 save_argv[1] = NULL;
140#endif /* PS_USE_CHANGE_ARGV */
141
142#ifdef PS_USE_CLOBBER_ARGV
143 save_argv[1] = NULL;
144#endif /* PS_USE_CLOBBER_ARGV */
145
146 /*
147 * Make fixed prefix of ps display.
148 */
83 149
84 va_start(ap, fmt); 150 va_start(ap, fmt);
85 if (fmt != NULL) { 151 if (fmt == NULL)
86 used = snprintf(buf, MAX_PROCTITLE, "%s: ", __progname); 152 snprintf(ps_buffer, ps_buffer_size, "%s", __progname);
87 if (used >= MAX_PROCTITLE) 153 else {
88 used = MAX_PROCTITLE - 1; 154 used = snprintf(ps_buffer, ps_buffer_size, "%s: ", __progname);
89 (void)vsnprintf(buf + used, MAX_PROCTITLE - used, fmt, ap); 155 if (used == -1 || used >= ps_buffer_size)
90 } else 156 used = ps_buffer_size;
91 (void)snprintf(buf, MAX_PROCTITLE, "%s", __progname); 157 vsnprintf(ps_buffer + used, ps_buffer_size - used, fmt, ap);
158 }
92 va_end(ap); 159 va_end(ap);
93 used = strlen(buf);
94 160
95#if SPT_TYPE == SPT_PSTAT 161#if 0
96 pst.pst_command = buf; 162 error("XXXXXXXXX %s", __progname);
97 pstat(PSTAT_SETCMD, pst, used, 0, 0); 163 error("XXXXXXXXX %d", ps_buffer_size);
98#endif /* SPT_TYPE == SPT_PSTAT */ 164 error("XXXXXXXXX %s", ps_buffer);
165#endif
166
167#ifdef PS_USE_PSTAT
168 pst.pst_command = ps_buffer;
169 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
170#endif /* PS_USE_PSTAT */
171
172#ifdef PS_USE_PS_STRINGS
173 PS_STRINGS->ps_nargvstr = 1;
174 PS_STRINGS->ps_argvstr = ps_buffer;
175#endif /* PS_USE_PS_STRINGS */
99 176
100#endif /* SPT_TYPE != SPT_NONE */ 177#ifdef PS_USE_CLOBBER_ARGV
178 /* pad unused memory */
179 used = strlen(ps_buffer);
180 memset(ps_buffer + used, PS_PADDING, ps_buffer_size - used);
181#endif /* PS_USE_CLOBBER_ARGV */
182
183#endif /* PS_USE_NONE */
101} 184}
185
102#endif /* HAVE_SETPROCTITLE */ 186#endif /* HAVE_SETPROCTITLE */
187
188/*
189 * Call this early in startup to save the original argc/argv values.
190 *
191 * argv[] will not be overwritten by this routine, but may be overwritten
192 * during setproctitle. Also, the physical location of the environment
193 * strings may be moved, so this should be called before any code that
194 * might try to hang onto a getenv() result.
195 */
196void
197compat_init_setproctitle(int argc, char *argv[])
198{
199#ifdef PS_USE_CLOBBER_ARGV
200 char *end_of_area = NULL;
201 char **new_environ;
202 int i;
203#endif
204
205 save_argc = argc;
206 save_argv = argv;
207
208#ifdef PS_USE_CLOBBER_ARGV
209 /*
210 * If we're going to overwrite the argv area, count the available
211 * space. Also move the environment to make additional room.
212 */
213
214 /*
215 * check for contiguous argv strings
216 */
217 for (i = 0; i < argc; i++) {
218 if (i == 0 || end_of_area + 1 == argv[i])
219 end_of_area = argv[i] + strlen(argv[i]);
220 }
221
222 /* probably can't happen? */
223 if (end_of_area == NULL) {
224 ps_buffer = NULL;
225 ps_buffer_size = 0;
226 return;
227 }
228
229 /*
230 * check for contiguous environ strings following argv
231 */
232 for (i = 0; environ[i] != NULL; i++) {
233 if (end_of_area + 1 == environ[i])
234 end_of_area = environ[i] + strlen(environ[i]);
235 }
236
237 ps_buffer = argv[0];
238 ps_buffer_size = end_of_area - argv[0] - 1;
239
240 /*
241 * Duplicate and move the environment out of the way
242 */
243 new_environ = malloc(sizeof(char *) * (i + 1));
244 for (i = 0; environ[i] != NULL; i++)
245 new_environ[i] = strdup(environ[i]);
246 new_environ[i] = NULL;
247 environ = new_environ;
248#endif /* PS_USE_CLOBBER_ARGV */
249}
250
diff --git a/openbsd-compat/setproctitle.h b/openbsd-compat/setproctitle.h
index 8261bd0ee..48d26c6ea 100644
--- a/openbsd-compat/setproctitle.h
+++ b/openbsd-compat/setproctitle.h
@@ -1,4 +1,4 @@
1/* $Id: setproctitle.h,v 1.2 2001/02/09 01:55:36 djm Exp $ */ 1/* $Id: setproctitle.h,v 1.3 2003/01/09 22:53:13 djm Exp $ */
2 2
3#ifndef _BSD_SETPROCTITLE_H 3#ifndef _BSD_SETPROCTITLE_H
4#define _BSD_SETPROCTITLE_H 4#define _BSD_SETPROCTITLE_H
@@ -7,6 +7,7 @@
7 7
8#ifndef HAVE_SETPROCTITLE 8#ifndef HAVE_SETPROCTITLE
9void setproctitle(const char *fmt, ...); 9void setproctitle(const char *fmt, ...);
10void compat_init_setproctitle(int argc, char *argv[]);
10#endif 11#endif
11 12
12#endif /* _BSD_SETPROCTITLE_H */ 13#endif /* _BSD_SETPROCTITLE_H */