2 * Based on src/backend/utils/misc/pg_status.c from
3 * PostgreSQL Database Management System
5 * Portions Copyright (c) 1996-2001, The PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, The Regents of the University of California
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.
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.
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.
27 /*--------------------------------------------------------------------
30 * Routines to support changing the ps display of PostgreSQL backends
31 * to contain some useful information. Mechanism differs wildly across
36 * Copyright 2000 by PostgreSQL Global Development Group
37 * various details abducted from various places
38 *--------------------------------------------------------------------
43 #ifndef HAVE_SETPROCTITLE
46 #ifdef HAVE_SYS_PSTAT_H
47 #include <sys/pstat.h> /* for HP-UX */
49 #ifdef HAVE_PS_STRINGS
50 #include <machine/vmparam.h> /* for old BSD */
55 * Alternative ways of updating ps display:
57 * SETPROCTITLE_STRATEGY == PS_USE_PSTAT
58 * use the pstat(PSTAT_SETCMD, )
60 * SETPROCTITLE_STRATEGY == PS_USE_PS_STRINGS
61 * assign PS_STRINGS->ps_argvstr = "string"
63 * SETPROCTITLE_STRATEGY == PS_USE_CHANGE_ARGV
64 * assign argv[0] = "string"
65 * (some other BSD systems)
66 * SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
67 * write over the argv and environment area
68 * (most SysV-like systems)
69 * SETPROCTITLE_STRATEGY == PS_USE_NONE
70 * don't update ps display
71 * (This is the default, as it is safest.)
75 #define PS_USE_PSTAT 1
76 #define PS_USE_PS_STRINGS 2
77 #define PS_USE_CHANGE_ARGV 3
78 #define PS_USE_CLOBBER_ARGV 4
80 #ifndef SETPROCTITLE_STRATEGY
81 # define SETPROCTITLE_STRATEGY PS_USE_NONE
84 #ifndef SETPROCTITLE_PS_PADDING
85 # define SETPROCTITLE_PS_PADDING ' '
87 #endif /* HAVE_SETPROCTITLE */
89 extern char **environ;
92 * argv clobbering uses existing argv space, all other methods need a buffer
94 #if SETPROCTITLE_STRATEGY != PS_USE_CLOBBER_ARGV
95 static char ps_buffer[256];
96 static const size_t ps_buffer_size = sizeof(ps_buffer);
98 static char *ps_buffer; /* will point to argv area */
99 static size_t ps_buffer_size; /* space determined at run time */
102 /* save the original argv[] location here */
103 static int save_argc;
104 static char **save_argv;
106 extern char *__progname;
108 #ifndef HAVE_SETPROCTITLE
110 * Call this to update the ps status display to a fixed prefix plus an
111 * indication of what you're currently doing passed in the argument.
114 setproctitle(const char *fmt, ...)
116 #if SETPROCTITLE_STRATEGY == PS_USE_PSTAT
119 #if SETPROCTITLE_STRATEGY != PS_USE_NONE
123 /* no ps display if you didn't call save_ps_display_args() */
124 if (save_argv == NULL)
126 #if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
127 /* If ps_buffer is a pointer, it might still be null */
128 if (ps_buffer == NULL)
130 #endif /* PS_USE_CLOBBER_ARGV */
133 * Overwrite argv[] to point at appropriate space, if needed
135 #if SETPROCTITLE_STRATEGY == PS_USE_CHANGE_ARGV
136 save_argv[0] = ps_buffer;
138 #endif /* PS_USE_CHANGE_ARGV */
140 #if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
142 #endif /* PS_USE_CLOBBER_ARGV */
145 * Make fixed prefix of ps display.
150 snprintf(ps_buffer, ps_buffer_size, "%s", __progname);
152 used = snprintf(ps_buffer, ps_buffer_size, "%s: ", __progname);
153 if (used == -1 || used >= ps_buffer_size)
154 used = ps_buffer_size;
155 vsnprintf(ps_buffer + used, ps_buffer_size - used, fmt, ap);
159 #if SETPROCTITLE_STRATEGY == PS_USE_PSTAT
160 pst.pst_command = ps_buffer;
161 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
162 #endif /* PS_USE_PSTAT */
164 #if SETPROCTITLE_STRATEGY == PS_USE_PS_STRINGS
165 PS_STRINGS->ps_nargvstr = 1;
166 PS_STRINGS->ps_argvstr = ps_buffer;
167 #endif /* PS_USE_PS_STRINGS */
169 #if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
170 /* pad unused memory */
171 used = strlen(ps_buffer);
172 memset(ps_buffer + used, SETPROCTITLE_PS_PADDING,
173 ps_buffer_size - used);
174 #endif /* PS_USE_CLOBBER_ARGV */
176 #endif /* PS_USE_NONE */
179 #endif /* HAVE_SETPROCTITLE */
182 * Call this early in startup to save the original argc/argv values.
184 * argv[] will not be overwritten by this routine, but may be overwritten
185 * during setproctitle. Also, the physical location of the environment
186 * strings may be moved, so this should be called before any code that
187 * might try to hang onto a getenv() result.
190 compat_init_setproctitle(int argc, char *argv[])
192 #if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
193 char *end_of_area = NULL;
201 #if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV
203 * If we're going to overwrite the argv area, count the available
204 * space. Also move the environment to make additional room.
208 * check for contiguous argv strings
210 for (i = 0; i < argc; i++) {
211 if (i == 0 || end_of_area + 1 == argv[i])
212 end_of_area = argv[i] + strlen(argv[i]);
215 /* probably can't happen? */
216 if (end_of_area == NULL) {
223 * check for contiguous environ strings following argv
225 for (i = 0; environ[i] != NULL; i++) {
226 if (end_of_area + 1 == environ[i])
227 end_of_area = environ[i] + strlen(environ[i]);
231 ps_buffer_size = end_of_area - argv[0] - 1;
234 * Duplicate and move the environment out of the way
236 new_environ = malloc(sizeof(char *) * (i + 1));
237 for (i = 0; environ[i] != NULL; i++)
238 new_environ[i] = strdup(environ[i]);
239 new_environ[i] = NULL;
240 environ = new_environ;
241 #endif /* PS_USE_CLOBBER_ARGV */