]> granicus.if.org Git - postgresql/blob - src/backend/utils/misc/ps_status.c
Remove silly use of DLLIMPORT.
[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.27 2005/11/22 18:17:26 momjian Exp $
9  *
10  * Copyright (c) 2000-2005, 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 #elif defined (WIN32)
69 #define PS_USE_WIN32
70 #else
71 #define PS_USE_NONE
72 #endif
73
74
75 /* Different systems want the buffer padded differently */
76 #if defined(_AIX) || defined(__linux__) || defined(__QNX__) || defined(__svr4__)
77 #define PS_PADDING '\0'
78 #else
79 #define PS_PADDING ' '
80 #endif
81
82
83 #ifndef PS_USE_CLOBBER_ARGV
84 /* all but one options need a buffer to write their ps line in */
85 #define PS_BUFFER_SIZE 256
86 static char ps_buffer[PS_BUFFER_SIZE];
87 static const size_t ps_buffer_size = PS_BUFFER_SIZE;
88 #else                                                   /* PS_USE_CLOBBER_ARGV */
89 static char *ps_buffer;                 /* will point to argv area */
90 static size_t ps_buffer_size;   /* space determined at run time */
91 #endif   /* PS_USE_CLOBBER_ARGV */
92
93 static size_t ps_buffer_fixed_size;             /* size of the constant prefix */
94
95 /* save the original argv[] location here */
96 static int      save_argc;
97 static char **save_argv;
98
99 #ifdef WIN32
100
101  /*
102   * Win32 does not support showing any changed arguments. To make it at all
103   * possible to track which backend is doing what, we create a named object
104   * that can be viewed with for example Process Explorer
105   */
106 static HANDLE ident_handle = INVALID_HANDLE_VALUE;
107 static void
108 pgwin32_update_ident(char *ident)
109 {
110         char            name[PS_BUFFER_SIZE + 32];
111
112         if (ident_handle != INVALID_HANDLE_VALUE)
113                 CloseHandle(ident_handle);
114
115         sprintf(name, "pgident: %s", ident);
116
117         ident_handle = CreateEvent(NULL,
118                                                            TRUE,
119                                                            FALSE,
120                                                            name);
121 }
122 #endif
123
124 /*
125  * Call this early in startup to save the original argc/argv values.
126  * If needed, we make a copy of the original argv[] array to preserve it
127  * from being clobbered by subsequent ps_display actions.
128  *
129  * (The original argv[] will not be overwritten by this routine, but may be
130  * overwritten during init_ps_display.  Also, the physical location of the
131  * environment strings may be moved, so this should be called before any code
132  * that might try to hang onto a getenv() result.)
133  */
134 char      **
135 save_ps_display_args(int argc, char **argv)
136 {
137         save_argc = argc;
138         save_argv = argv;
139
140 #if defined(PS_USE_CLOBBER_ARGV)
141
142         /*
143          * If we're going to overwrite the argv area, count the available space.
144          * Also move the environment to make additional room.
145          */
146         {
147                 char       *end_of_area = NULL;
148                 char      **new_environ;
149                 int                     i;
150
151                 /*
152                  * check for contiguous argv strings
153                  */
154                 for (i = 0; i < argc; i++)
155                 {
156                         if (i == 0 || end_of_area + 1 == argv[i])
157                                 end_of_area = argv[i] + strlen(argv[i]);
158                 }
159
160                 if (end_of_area == NULL)        /* probably can't happen? */
161                 {
162                         ps_buffer = NULL;
163                         ps_buffer_size = 0;
164                         return argv;
165                 }
166
167                 /*
168                  * check for contiguous environ strings following argv
169                  */
170                 for (i = 0; environ[i] != NULL; i++)
171                 {
172                         if (end_of_area + 1 == environ[i])
173                                 end_of_area = environ[i] + strlen(environ[i]);
174                 }
175
176                 ps_buffer = argv[0];
177                 ps_buffer_size = end_of_area - argv[0];
178
179                 /*
180                  * move the environment out of the way
181                  */
182                 new_environ = (char **) malloc((i + 1) * sizeof(char *));
183                 for (i = 0; environ[i] != NULL; i++)
184                         new_environ[i] = strdup(environ[i]);
185                 new_environ[i] = NULL;
186                 environ = new_environ;
187         }
188 #endif   /* PS_USE_CLOBBER_ARGV */
189
190 #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
191
192         /*
193          * If we're going to change the original argv[] then make a copy for
194          * argument parsing purposes.
195          *
196          * (NB: do NOT think to remove the copying of argv[], even though
197          * postmaster.c finishes looking at argv[] long before we ever consider
198          * changing the ps display.  On some platforms, getopt() keeps pointers
199          * into the argv array, and will get horribly confused when it is
200          * re-called to analyze a subprocess' argument string if the argv storage
201          * has been clobbered meanwhile.  Other platforms have other dependencies
202          * on argv[].
203          */
204         {
205                 char      **new_argv;
206                 int                     i;
207
208                 new_argv = (char **) malloc((argc + 1) * sizeof(char *));
209                 for (i = 0; i < argc; i++)
210                         new_argv[i] = strdup(argv[i]);
211                 new_argv[argc] = NULL;
212
213 #if defined(__darwin__)
214
215                 /*
216                  * Darwin (and perhaps other NeXT-derived platforms?) has a static
217                  * copy of the argv pointer, which we may fix like so:
218                  */
219                 *_NSGetArgv() = new_argv;
220 #endif
221
222                 argv = new_argv;
223         }
224 #endif   /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
225
226         return argv;
227 }
228
229 /*
230  * Call this once during subprocess startup to set the identification
231  * values.      At this point, the original argv[] array may be overwritten.
232  */
233 void
234 init_ps_display(const char *username, const char *dbname,
235                                 const char *host_info)
236 {
237         Assert(username);
238         Assert(dbname);
239         Assert(host_info);
240
241 #ifndef PS_USE_NONE
242         /* no ps display for stand-alone backend */
243         if (!IsUnderPostmaster)
244                 return;
245
246         /* no ps display if you didn't call save_ps_display_args() */
247         if (!save_argv)
248                 return;
249 #ifdef PS_USE_CLOBBER_ARGV
250         /* If ps_buffer is a pointer, it might still be null */
251         if (!ps_buffer)
252                 return;
253 #endif
254
255         /*
256          * Overwrite argv[] to point at appropriate space, if needed
257          */
258
259 #ifdef PS_USE_CHANGE_ARGV
260         save_argv[0] = ps_buffer;
261         save_argv[1] = NULL;
262 #endif   /* PS_USE_CHANGE_ARGV */
263
264 #ifdef PS_USE_CLOBBER_ARGV
265         {
266                 int                     i;
267
268                 /* make extra argv slots point at end_of_area (a NUL) */
269                 for (i = 1; i < save_argc; i++)
270                         save_argv[i] = ps_buffer + ps_buffer_size;
271         }
272 #endif   /* PS_USE_CLOBBER_ARGV */
273
274         /*
275          * Make fixed prefix of ps display.
276          */
277
278 #ifdef PS_USE_SETPROCTITLE
279
280         /*
281          * apparently setproctitle() already adds a `progname:' prefix to the ps
282          * line
283          */
284         snprintf(ps_buffer, ps_buffer_size,
285                          "%s %s %s ",
286                          username, dbname, host_info);
287 #else
288         snprintf(ps_buffer, ps_buffer_size,
289                          "postgres: %s %s %s ",
290                          username, dbname, host_info);
291 #endif
292
293         ps_buffer_fixed_size = strlen(ps_buffer);
294
295 #ifdef WIN32
296         pgwin32_update_ident(ps_buffer);
297 #endif
298 #endif   /* not PS_USE_NONE */
299 }
300
301
302
303 /*
304  * Call this to update the ps status display to a fixed prefix plus an
305  * indication of what you're currently doing passed in the argument.
306  */
307 void
308 set_ps_display(const char *activity)
309 {
310 #ifndef PS_USE_NONE
311         /* no ps display for stand-alone backend */
312         if (!IsUnderPostmaster)
313                 return;
314
315 #ifdef PS_USE_CLOBBER_ARGV
316         /* If ps_buffer is a pointer, it might still be null */
317         if (!ps_buffer)
318                 return;
319 #endif
320
321         /* Update ps_buffer to contain both fixed part and activity */
322         StrNCpy(ps_buffer + ps_buffer_fixed_size, activity,
323                         ps_buffer_size - ps_buffer_fixed_size);
324
325         /* Transmit new setting to kernel, if necessary */
326
327 #ifdef PS_USE_SETPROCTITLE
328         setproctitle("%s", ps_buffer);
329 #endif
330
331 #ifdef PS_USE_PSTAT
332         {
333                 union pstun pst;
334
335                 pst.pst_command = ps_buffer;
336                 pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
337         }
338 #endif   /* PS_USE_PSTAT */
339
340 #ifdef PS_USE_PS_STRINGS
341         PS_STRINGS->ps_nargvstr = 1;
342         PS_STRINGS->ps_argvstr = ps_buffer;
343 #endif   /* PS_USE_PS_STRINGS */
344
345 #ifdef PS_USE_CLOBBER_ARGV
346         {
347                 int                     buflen;
348
349                 /* pad unused memory */
350                 buflen = strlen(ps_buffer);
351                 MemSet(ps_buffer + buflen, PS_PADDING, ps_buffer_size - buflen);
352         }
353 #endif   /* PS_USE_CLOBBER_ARGV */
354
355 #ifdef WIN32
356         pgwin32_update_ident(ps_buffer);
357 #endif
358 #endif   /* not PS_USE_NONE */
359 }
360
361
362 /*
363  * Returns what's currently in the ps display, in case someone needs
364  * it.  Note that only the activity part is returned.  On some platforms
365  * the string will not be null-terminated, so return the effective
366  * length into *displen.
367  */
368 const char *
369 get_ps_display(int *displen)
370 {
371 #ifdef PS_USE_CLOBBER_ARGV
372         size_t          offset;
373
374         /* If ps_buffer is a pointer, it might still be null */
375         if (!ps_buffer)
376         {
377                 *displen = 0;
378                 return "";
379         }
380
381         /* Remove any trailing spaces to offset the effect of PS_PADDING */
382         offset = ps_buffer_size;
383         while (offset > ps_buffer_fixed_size && ps_buffer[offset - 1] == PS_PADDING)
384                 offset--;
385
386         *displen = offset - ps_buffer_fixed_size;
387 #else
388         *displen = strlen(ps_buffer + ps_buffer_fixed_size);
389 #endif
390
391         return ps_buffer + ps_buffer_fixed_size;
392 }