]> granicus.if.org Git - postgresql/blob - src/backend/utils/misc/ps_status.c
Add:
[postgresql] / src / backend / utils / misc / ps_status.c
1 /*--------------------------------------------------------------------
2  * ps_status.c
3  *
4  * Routines to support changing the ps display of PostgreSQL backends
5  * to contain some useful information. Mechanism differs wildly across
6  * platforms.
7  *
8  * $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.18 2004/03/09 04:43:07 momjian Exp $
9  *
10  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
11  * various details abducted from various places
12  *--------------------------------------------------------------------
13  */
14
15 #include "postgres.h"
16
17 #include <unistd.h>
18 #ifdef HAVE_SYS_PSTAT_H
19 #include <sys/pstat.h>                  /* for HP-UX */
20 #endif
21 #ifdef HAVE_PS_STRINGS
22 #include <machine/vmparam.h>    /* for old BSD */
23 #include <sys/exec.h>
24 #endif
25 #if defined(__darwin__)
26 #include <crt_externs.h>
27 #endif
28
29 #include "libpq/libpq.h"
30 #include "miscadmin.h"
31 #include "utils/ps_status.h"
32
33 extern char **environ;
34
35
36 /*------
37  * Alternative ways of updating ps display:
38  *
39  * PS_USE_SETPROCTITLE
40  *         use the function setproctitle(const char *, ...)
41  *         (newer BSD systems)
42  * PS_USE_PSTAT
43  *         use the pstat(PSTAT_SETCMD, )
44  *         (HPUX)
45  * PS_USE_PS_STRINGS
46  *         assign PS_STRINGS->ps_argvstr = "string"
47  *         (some BSD systems)
48  * PS_USE_CHANGE_ARGV
49  *         assign argv[0] = "string"
50  *         (some other BSD systems)
51  * PS_USE_CLOBBER_ARGV
52  *         write over the argv and environment area
53  *         (most SysV-like systems)
54  * PS_USE_NONE
55  *         don't update ps display
56  *         (This is the default, as it is safest.)
57  */
58 #if defined(HAVE_SETPROCTITLE)
59 #define PS_USE_SETPROCTITLE
60 #elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
61 #define PS_USE_PSTAT
62 #elif defined(HAVE_PS_STRINGS)
63 #define PS_USE_PS_STRINGS
64 #elif (defined(BSD) || defined(__bsdi__) || defined(__hurd__)) && !defined(__darwin__)
65 #define PS_USE_CHANGE_ARGV
66 #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__ksr__) || defined(__osf__) || defined(__QNX__) || defined(__svr4__) || defined(__svr5__) || defined(__darwin__)
67 #define PS_USE_CLOBBER_ARGV
68 #else
69 #define PS_USE_NONE
70 #endif
71
72
73 /* Different systems want the buffer padded differently */
74 #if defined(_AIX) || defined(__linux__) || defined(__QNX__) || defined(__svr4__)
75 #define PS_PADDING '\0'
76 #else
77 #define PS_PADDING ' '
78 #endif
79
80
81 #ifndef PS_USE_CLOBBER_ARGV
82 /* all but one options need a buffer to write their ps line in */
83 #define PS_BUFFER_SIZE 256
84 static char ps_buffer[PS_BUFFER_SIZE];
85 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
86
87 #else                                                   /* PS_USE_CLOBBER_ARGV */
88 static char *ps_buffer;                 /* will point to argv area */
89 static size_t ps_buffer_size;   /* space determined at run time */
90 #endif   /* PS_USE_CLOBBER_ARGV */
91
92 static size_t ps_buffer_fixed_size;             /* size of the constant prefix */
93
94 /* save the original argv[] location here */
95 static int      save_argc;
96 static char **save_argv;
97
98
99 /*
100  * Call this early in startup to save the original argc/argv values.
101  * If needed, we make a copy of the original argv[] array to preserve it
102  * from being clobbered by subsequent ps_display actions.
103  *
104  * (The original argv[] will not be overwritten by this routine, but may be
105  * overwritten during init_ps_display.  Also, the physical location of the
106  * environment strings may be moved, so this should be called before any code
107  * that might try to hang onto a getenv() result.)
108  */
109 char **
110 save_ps_display_args(int argc, char **argv)
111 {
112         save_argc = argc;
113         save_argv = argv;
114
115 #if defined(PS_USE_CLOBBER_ARGV)
116
117         /*
118          * If we're going to overwrite the argv area, count the available
119          * space.  Also move the environment to make additional room.
120          */
121         {
122                 char       *end_of_area = NULL;
123                 char      **new_environ;
124                 int                     i;
125
126                 /*
127                  * check for contiguous argv strings
128                  */
129                 for (i = 0; i < argc; i++)
130                 {
131                         if (i == 0 || end_of_area + 1 == argv[i])
132                                 end_of_area = argv[i] + strlen(argv[i]);
133                 }
134
135                 if (end_of_area == NULL)        /* probably can't happen? */
136                 {
137                         ps_buffer = NULL;
138                         ps_buffer_size = 0;
139                         return argv;
140                 }
141
142                 /*
143                  * check for contiguous environ strings following argv
144                  */
145                 for (i = 0; environ[i] != NULL; i++)
146                 {
147                         if (end_of_area + 1 == environ[i])
148                                 end_of_area = environ[i] + strlen(environ[i]);
149                 }
150
151                 ps_buffer = argv[0];
152                 ps_buffer_size = end_of_area - argv[0];
153
154                 /*
155                  * move the environment out of the way
156                  */
157                 new_environ = (char **) malloc((i + 1) * sizeof(char *));
158                 for (i = 0; environ[i] != NULL; i++)
159                         new_environ[i] = strdup(environ[i]);
160                 new_environ[i] = NULL;
161                 environ = new_environ;
162         }
163 #endif   /* PS_USE_CLOBBER_ARGV */
164
165 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
166
167         /*
168          * If we're going to change the original argv[] then make a copy for
169          * argument parsing purposes.
170          *
171          * (NB: do NOT think to remove the copying of argv[], even though
172          * postmaster.c finishes looking at argv[] long before we ever
173          * consider changing the ps display.  On some platforms, getopt()
174          * keeps pointers into the argv array, and will get horribly confused
175          * when it is re-called to analyze a subprocess' argument string if
176          * the argv storage has been clobbered meanwhile.  Other platforms
177          * have other dependencies on argv[].
178          */
179         {
180                 char      **new_argv;
181                 int                     i;
182
183                 new_argv = (char **) malloc((argc + 1) * sizeof(char *));
184                 for (i = 0; i < argc; i++)
185                                 new_argv[i] = strdup(argv[i]);
186                 new_argv[argc] = NULL;
187
188 #if defined(__darwin__)
189                 /*
190                  * Darwin (and perhaps other NeXT-derived platforms?) has a static
191                  * copy of the argv pointer, which we may fix like so:
192                  */
193                 *_NSGetArgv() = new_argv;
194 #endif
195
196                 argv = new_argv;
197         }
198 #endif   /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
199
200         return argv;
201 }
202
203 /*
204  * Call this once during subprocess startup to set the identification
205  * values.      At this point, the original argv[] array may be overwritten.
206  */
207 void
208 init_ps_display(const char *username, const char *dbname,
209                                 const char *host_info)
210 {
211         Assert(username);
212         Assert(dbname);
213         Assert(host_info);
214
215 #ifndef PS_USE_NONE
216         /* no ps display for stand-alone backend */
217         if (!IsUnderPostmaster)
218                 return;
219
220         /* no ps display if you didn't call save_ps_display_args() */
221         if (!save_argv)
222                 return;
223 #ifdef PS_USE_CLOBBER_ARGV
224         /* If ps_buffer is a pointer, it might still be null */
225         if (!ps_buffer)
226                 return;
227 #endif
228
229         /*
230          * Overwrite argv[] to point at appropriate space, if needed
231          */
232
233 #ifdef PS_USE_CHANGE_ARGV
234         save_argv[0] = ps_buffer;
235         save_argv[1] = NULL;
236 #endif   /* PS_USE_CHANGE_ARGV */
237
238 #ifdef PS_USE_CLOBBER_ARGV
239         {
240                 int             i;
241
242                 /* make extra argv slots point at end_of_area (a NUL) */
243                 for (i = 1; i < save_argc; i++)
244                         save_argv[i] = ps_buffer + ps_buffer_size;
245         }
246 #endif   /* PS_USE_CLOBBER_ARGV */
247
248         /*
249          * Make fixed prefix of ps display.
250          */
251
252 #ifdef PS_USE_SETPROCTITLE
253
254         /*
255          * apparently setproctitle() already adds a `progname:' prefix to the
256          * ps line
257          */
258         snprintf(ps_buffer, ps_buffer_size,
259                          "%s %s %s ",
260                          username, dbname, host_info);
261 #else
262         snprintf(ps_buffer, ps_buffer_size,
263                          "postgres: %s %s %s ",
264                          username, dbname, host_info);
265 #endif
266
267         ps_buffer_fixed_size = strlen(ps_buffer);
268 #endif   /* not PS_USE_NONE */
269 }
270
271
272
273 /*
274  * Call this to update the ps status display to a fixed prefix plus an
275  * indication of what you're currently doing passed in the argument.
276  */
277 void
278 set_ps_display(const char *activity)
279 {
280         /* no ps display for stand-alone backend */
281         if (!IsUnderPostmaster)
282                 return;
283
284         /* save it for logging context */
285         if (MyProcPort)
286                 MyProcPort->commandTag = (char *) activity;
287
288 #ifndef PS_USE_NONE
289
290 #ifdef PS_USE_CLOBBER_ARGV
291         /* If ps_buffer is a pointer, it might still be null */
292         if (!ps_buffer)
293                 return;
294 #endif
295
296         /* Update ps_buffer to contain both fixed part and activity */
297         StrNCpy(ps_buffer + ps_buffer_fixed_size, activity,
298                         ps_buffer_size - ps_buffer_fixed_size);
299
300         /* Transmit new setting to kernel, if necessary */
301
302 #ifdef PS_USE_SETPROCTITLE
303         setproctitle("%s", ps_buffer);
304 #endif
305
306 #ifdef PS_USE_PSTAT
307         {
308                 union pstun pst;
309
310                 pst.pst_command = ps_buffer;
311                 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
312         }
313 #endif   /* PS_USE_PSTAT */
314
315 #ifdef PS_USE_PS_STRINGS
316         PS_STRINGS->ps_nargvstr = 1;
317         PS_STRINGS->ps_argvstr = ps_buffer;
318 #endif   /* PS_USE_PS_STRINGS */
319
320 #ifdef PS_USE_CLOBBER_ARGV
321         {
322                 int                     buflen;
323
324                 /* pad unused memory */
325                 buflen = strlen(ps_buffer);
326                 MemSet(ps_buffer + buflen, PS_PADDING, ps_buffer_size - buflen);
327         }
328 #endif   /* PS_USE_CLOBBER_ARGV */
329 #endif   /* not PS_USE_NONE */
330 }
331
332
333 /*
334  * Returns what's currently in the ps display, in case someone needs
335  * it.  Note that only the activity part is returned.
336  */
337 const char *
338 get_ps_display(void)
339 {
340 #ifdef PS_USE_CLOBBER_ARGV
341         /* If ps_buffer is a pointer, it might still be null */
342         if (!ps_buffer)
343                 return "";
344 #endif
345
346         return ps_buffer + ps_buffer_fixed_size;
347 }