| 1 |
/* |
| 2 |
* Code has been calqued from PostgreSQL 9.1 by Elizabeth J. Myers. |
| 3 |
* Below is their copyright header. |
| 4 |
* |
| 5 |
* Do note I've made extensive changes to this code, including adding |
| 6 |
* Linux (and Irix?) prctl support. Also added support for searching for |
| 7 |
* argc/argv. |
| 8 |
*/ |
| 9 |
|
| 10 |
/*-------------------------------------------------------------------- |
| 11 |
* ps_status.c |
| 12 |
* |
| 13 |
* Routines to support changing the ps display of PostgreSQL backends |
| 14 |
* to contain some useful information. Mechanism differs wildly across |
| 15 |
* platforms. |
| 16 |
* |
| 17 |
* src/backend/utils/misc/ps_status.c |
| 18 |
* |
| 19 |
* Copyright (c) 2000-2011, PostgreSQL Global Development Group |
| 20 |
* various details abducted from various places |
| 21 |
***-------------------------------------------------------------------- |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "mowgli.h" |
| 25 |
|
| 26 |
#ifdef HAVE_SYS_PSTAT_H |
| 27 |
# include <sys/pstat.h> /* for HP-UX */ |
| 28 |
#endif |
| 29 |
#ifdef HAVE_PS_STRINGS |
| 30 |
# include <machine/vmparam.h> /* for old BSD */ |
| 31 |
# include <sys/exec.h> |
| 32 |
#endif |
| 33 |
#ifdef MOWGLI_OS_OSX |
| 34 |
# include <crt_externs.h> |
| 35 |
#endif |
| 36 |
#ifdef HAVE_SYS_PRCTL_H |
| 37 |
# include <sys/prctl.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#if !defined(MOWGLI_OS_WIN) && !defined(MOWGLI_OS_OSX) |
| 41 |
extern char **environ; |
| 42 |
#endif |
| 43 |
|
| 44 |
/* |
| 45 |
* Alternative ways of updating ps display: |
| 46 |
* |
| 47 |
* MOWGLI_SETPROC_USE_SETPROCTITLE |
| 48 |
* use the function setproctitle(const char *, ...) |
| 49 |
* (newer BSD systems) |
| 50 |
* MOWGLI_SETPROC_USE_PSTAT |
| 51 |
* use the pstat(PSTAT_SETCMD, ) |
| 52 |
* (HPUX) |
| 53 |
* MOWGLI_SETPROC_USE_PS_STRINGS |
| 54 |
* assign PS_STRINGS->ps_argvstr = "string" |
| 55 |
* (some BSD systems) |
| 56 |
* MOWGLI_SETPROC_USE_PRCTL |
| 57 |
* use prctl(PR_SET_NAME, ...) |
| 58 |
* (Newer Linux and possibly Irix? -- Note some utilities don't use this name) |
| 59 |
* MOWGLI_SETPROC_USE_CHANGE_ARGV |
| 60 |
* assign argv[0] = "string" |
| 61 |
* (some other BSD systems) |
| 62 |
* MOWGLI_SETPROC_USE_CLOBBER_ARGV |
| 63 |
* write over the argv and environment area |
| 64 |
* (Old Linux and most SysV-like systems) |
| 65 |
* MOWGLI_SETPROC_USE_WIN32 |
| 66 |
* push the string out as the name of a Windows event |
| 67 |
* MOWGLI_SETPROC_USE_NONE |
| 68 |
* don't update ps display |
| 69 |
* (This is the default, as it is safest.) |
| 70 |
*/ |
| 71 |
#if defined(HAVE_SETPROCTITLE) |
| 72 |
# define MOWGLI_SETPROC_USE_SETPROCTITLE |
| 73 |
#elif defined(PR_SET_NAME) && defined(HAVE_SYS_PRCTL_H) |
| 74 |
# define MOWGLI_SETPROC_USE_PRCTL |
| 75 |
#elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD) |
| 76 |
# define MOWGLI_SETPROC_USE_PSTAT |
| 77 |
#elif defined(HAVE_PS_STRINGS) |
| 78 |
# define MOWGLI_SETPROC_USE_PS_STRINGS |
| 79 |
#elif (defined(BSD) || defined(__bsdi__) || defined(__hurd__)) && !defined(__darwin__) |
| 80 |
# define MOWGLI_SETPROC_USE_CHANGE_ARGV |
| 81 |
#elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__ksr__) || defined(__osf__) || defined(__svr4__) || defined(__svr5__) || defined(__darwin__) |
| 82 |
# define MOWGLI_SETPROC_USE_CLOBBER_ARGV |
| 83 |
#elif defined(WIN32) |
| 84 |
# define MOWGLI_SETPROC_USE_WIN32 |
| 85 |
#else |
| 86 |
# define MOWGLI_SETPROC_USE_NONE |
| 87 |
#endif |
| 88 |
|
| 89 |
/* Different systems want the buffer padded differently */ |
| 90 |
#if defined(_AIX) || defined(__linux__) || defined(__svr4__) || defined(__darwin__) |
| 91 |
# define PS_PADDING '\0' |
| 92 |
#else |
| 93 |
# define PS_PADDING ' ' |
| 94 |
#endif |
| 95 |
|
| 96 |
#ifndef MOWGLI_SETPROC_USE_CLOBBER_ARGV |
| 97 |
|
| 98 |
/* all but one option need a buffer to write their ps line in */ |
| 99 |
# define PS_BUFFER_SIZE 256 |
| 100 |
static char ps_buffer[PS_BUFFER_SIZE]; |
| 101 |
static const size_t ps_buffer_size = PS_BUFFER_SIZE; |
| 102 |
#else /* MOWGLI_SETPROC_USE_CLOBBER_ARGV */ |
| 103 |
static char *ps_buffer; /* will point to argv area */ |
| 104 |
static size_t ps_buffer_size; /* space determined at run time */ |
| 105 |
#endif /* MOWGLI_SETPROC_USE_CLOBBER_ARGV */ |
| 106 |
|
| 107 |
static size_t ps_buffer_fixed_size; |
| 108 |
static size_t ps_buffer_cur_len;/* nominal strlen(ps_buffer) */ |
| 109 |
|
| 110 |
/* save the original argv[] location here */ |
| 111 |
static int save_argc; |
| 112 |
static char **save_argv; |
| 113 |
|
| 114 |
int mowgli_argc = 0; |
| 115 |
char **mowgli_argv = NULL; |
| 116 |
|
| 117 |
/* |
| 118 |
* Save the original argc/argv values. |
| 119 |
* If needed, we make a copy of the original argv[] array to preserve it |
| 120 |
* from being clobbered by subsequent ps_display actions. |
| 121 |
*/ |
| 122 |
char ** |
| 123 |
mowgli_proctitle_init(int argc, char **argv) |
| 124 |
{ |
| 125 |
if ((argc == 0) || (argv == NULL)) |
| 126 |
save_argc = argc; |
| 127 |
|
| 128 |
save_argv = argv; |
| 129 |
|
| 130 |
#if defined(MOWGLI_SETPROC_USE_CLOBBER_ARGV) |
| 131 |
|
| 132 |
/* |
| 133 |
* If we're going to overwrite the argv area, count the available space. |
| 134 |
* Also move the environment to make additional room. |
| 135 |
*/ |
| 136 |
char *end_of_area = NULL; |
| 137 |
char **new_environ; |
| 138 |
int i; |
| 139 |
|
| 140 |
/* |
| 141 |
* check for contiguous argv strings |
| 142 |
*/ |
| 143 |
for (i = 0; i < argc; i++) |
| 144 |
if ((i == 0) || (end_of_area + 1 == argv[i])) |
| 145 |
end_of_area = argv[i] + strlen(argv[i]); |
| 146 |
|
| 147 |
if (end_of_area == NULL)/* probably can't happen? */ |
| 148 |
{ |
| 149 |
ps_buffer = NULL; |
| 150 |
ps_buffer_size = 0; |
| 151 |
return argv; |
| 152 |
} |
| 153 |
|
| 154 |
/* |
| 155 |
* check for contiguous environ strings following argv |
| 156 |
*/ |
| 157 |
for (i = 0; environ[i] != NULL; i++) |
| 158 |
if (end_of_area + 1 == environ[i]) |
| 159 |
end_of_area = environ[i] + strlen(environ[i]); |
| 160 |
|
| 161 |
ps_buffer = argv[0]; |
| 162 |
ps_buffer_size = end_of_area - argv[0]; |
| 163 |
|
| 164 |
/* |
| 165 |
* move the environment out of the way |
| 166 |
*/ |
| 167 |
new_environ = mowgli_alloc((i + 1) * sizeof(char *)); |
| 168 |
|
| 169 |
for (i = 0; environ[i] != NULL; i++) |
| 170 |
new_environ[i] = mowgli_strdup(environ[i]); |
| 171 |
|
| 172 |
new_environ[i] = NULL; |
| 173 |
environ = new_environ; |
| 174 |
|
| 175 |
#endif /* MOWGLI_SETPROC_USE_CLOBBER_ARGV */ |
| 176 |
|
| 177 |
#if defined(MOWGLI_SETPROC_USE_CHANGE_ARGV) || defined(MOWGLI_SETPROC_USE_CLOBBER_ARGV) |
| 178 |
|
| 179 |
/* |
| 180 |
* If we're going to change the original argv[] then make a copy for |
| 181 |
* argument parsing purposes. |
| 182 |
* |
| 183 |
* (NB: do NOT think to remove the copying of argv[]. |
| 184 |
* On some platforms, getopt() keeps pointers into the argv array, and will |
| 185 |
* get horribly confused when it is re-called to analyze a subprocess' |
| 186 |
* argument string if the argv storage has been clobbered meanwhile. Other |
| 187 |
* platforms have other dependencies on argv[]. |
| 188 |
*/ |
| 189 |
|
| 190 |
char **new_argv; |
| 191 |
int j; |
| 192 |
|
| 193 |
new_argv = mowgli_alloc((argc + 1) * sizeof(char *)); |
| 194 |
|
| 195 |
for (j = 0; j < argc; j++) |
| 196 |
new_argv[j] = mowgli_strdup(argv[j]); |
| 197 |
|
| 198 |
new_argv[argc] = NULL; |
| 199 |
|
| 200 |
# ifdef MOWGLI_OS_OSX |
| 201 |
|
| 202 |
/* |
| 203 |
* Darwin (and perhaps other NeXT-derived platforms?) has a static |
| 204 |
* copy of the argv pointer, which we may fix like so: |
| 205 |
*/ |
| 206 |
*_NSGetArgv() = new_argv; |
| 207 |
# endif |
| 208 |
|
| 209 |
argv = new_argv; |
| 210 |
|
| 211 |
#endif /* MOWGLI_SETPROC_USE_CHANGE_ARGV or MOWGLI_SETPROC_USE_CLOBBER_ARGV */ |
| 212 |
|
| 213 |
mowgli_argc = argc; |
| 214 |
mowgli_argv = argv; |
| 215 |
|
| 216 |
return argv; |
| 217 |
} |
| 218 |
|
| 219 |
void |
| 220 |
mowgli_proctitle_set(const char *fmt, ...) |
| 221 |
{ |
| 222 |
#ifndef MOWGLI_SETPROC_USE_NONE |
| 223 |
va_list va; |
| 224 |
|
| 225 |
# if defined(MOWGLI_SETPROC_USE_CHANGE_ARGV) || defined(MOWGLI_SETPROC_USE_CLOBBER_ARGV) |
| 226 |
|
| 227 |
if (!save_argv) |
| 228 |
return; |
| 229 |
|
| 230 |
# endif |
| 231 |
|
| 232 |
va_start(va, fmt); |
| 233 |
vsnprintf(ps_buffer, ps_buffer_size, fmt, va); |
| 234 |
va_end(va); |
| 235 |
|
| 236 |
return_if_fail(*ps_buffer == '\0'); |
| 237 |
|
| 238 |
ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer); |
| 239 |
|
| 240 |
# ifdef MOWGLI_SETPROC_USE_CHANGE_ARGV |
| 241 |
save_argv[0] = ps_buffer; |
| 242 |
save_argv[1] = NULL; |
| 243 |
# endif |
| 244 |
|
| 245 |
# ifdef MOWGLI_SETPROC_USE_CLOBBER_ARGV |
| 246 |
|
| 247 |
for (int i = 1; i < save_argc; i++) |
| 248 |
save_argv[i] = ps_buffer + ps_buffer_size; |
| 249 |
|
| 250 |
/* Pad unused bytes */ |
| 251 |
memset(ps_buffer + ps_buffer_cur_len, PS_PADDING, ps_buffer_size - ps_buffer_cur_len + 1); |
| 252 |
# endif |
| 253 |
|
| 254 |
# ifdef MOWGLI_SETPROC_USE_SETPROCTITLE |
| 255 |
setproctitle("%s", ps_buffer); |
| 256 |
# endif |
| 257 |
|
| 258 |
# ifdef MOWGLI_SETPROC_USE_PRCTL |
| 259 |
|
| 260 |
/* Limit us to 16 chars to be safe */ |
| 261 |
char procbuf[16]; |
| 262 |
mowgli_strlcpy(procbuf, ps_buffer, sizeof(procbuf)); |
| 263 |
prctl(PR_SET_NAME, procbuf, 0, 0, 0); |
| 264 |
# endif |
| 265 |
|
| 266 |
# ifdef MOWGLI_SETPROC_USE_PSTAT |
| 267 |
union pstun pst; |
| 268 |
|
| 269 |
pst.pst_command = ps_buffer; |
| 270 |
pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0); |
| 271 |
# endif /* MOWGLI_SETPROC_USE_PSTAT */ |
| 272 |
|
| 273 |
# ifdef MOWGLI_SETPROC_USE_PS_STRINGS |
| 274 |
PS_STRINGS->ps_nargvstr = 1; |
| 275 |
PS_STRINGS->ps_argvstr = ps_buffer; |
| 276 |
# endif /* MOWGLI_SETPROC_USE_PS_STRINGS */ |
| 277 |
|
| 278 |
# ifdef MOWGLI_SETPROC_USE_WIN32 |
| 279 |
|
| 280 |
/* |
| 281 |
* Win32 does not support showing any changed arguments. To make it at |
| 282 |
* all possible to track which backend is doing what, we create a |
| 283 |
* named object that can be viewed with for example Process Explorer. |
| 284 |
*/ |
| 285 |
static HANDLE ident_handle = INVALID_HANDLE_VALUE; |
| 286 |
char name[PS_BUFFER_SIZE + 32]; |
| 287 |
|
| 288 |
if (ident_handle != INVALID_HANDLE_VALUE) |
| 289 |
CloseHandle(ident_handle); |
| 290 |
|
| 291 |
sprintf(name, "mowgli_ident(%d): %s", getpid(), ps_buffer); |
| 292 |
|
| 293 |
ident_handle = CreateEvent(NULL, TRUE, FALSE, name); |
| 294 |
# endif /* MOWGLI_SETPROC_USE_WIN32 */ |
| 295 |
#endif /* not MOWGLI_SETPROC_USE_NONE */ |
| 296 |
} |
| 297 |
|
| 298 |
/* |
| 299 |
* Returns what's currently in the ps display, in case someone needs |
| 300 |
* it. Note that only the activity part is returned. On some platforms |
| 301 |
* the string will not be null-terminated, so return the effective |
| 302 |
* length into *displen. |
| 303 |
*/ |
| 304 |
const char * |
| 305 |
mowgli_proctitle_get(int *displen) |
| 306 |
{ |
| 307 |
#ifdef MOWGLI_SETPROC_USE_CLOBBER_ARGV |
| 308 |
|
| 309 |
/* If ps_buffer is a pointer, it might still be null */ |
| 310 |
if (!ps_buffer) |
| 311 |
{ |
| 312 |
*displen = 0; |
| 313 |
return ""; |
| 314 |
} |
| 315 |
|
| 316 |
#endif |
| 317 |
|
| 318 |
*displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size); |
| 319 |
|
| 320 |
return ps_buffer + ps_buffer_fixed_size; |
| 321 |
} |