LCOV - code coverage report
Current view: top level - src/backend/utils/misc - ps_status.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 53 71 74.6 %
Date: 2020-02-27 20:07:29 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             :  * src/backend/utils/misc/ps_status.c
       9             :  *
      10             :  * Copyright (c) 2000-2020, 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/guc.h"
      32             : #include "utils/ps_status.h"
      33             : 
      34             : extern char **environ;
      35             : bool        update_process_title = true;
      36             : 
      37             : 
      38             : /*
      39             :  * Alternative ways of updating ps display:
      40             :  *
      41             :  * PS_USE_SETPROCTITLE_FAST
      42             :  *     use the function setproctitle_fast(const char *, ...)
      43             :  *     (newer FreeBSD systems)
      44             :  * PS_USE_SETPROCTITLE
      45             :  *     use the function setproctitle(const char *, ...)
      46             :  *     (newer BSD systems)
      47             :  * PS_USE_PSTAT
      48             :  *     use the pstat(PSTAT_SETCMD, )
      49             :  *     (HPUX)
      50             :  * PS_USE_PS_STRINGS
      51             :  *     assign PS_STRINGS->ps_argvstr = "string"
      52             :  *     (some BSD systems)
      53             :  * PS_USE_CHANGE_ARGV
      54             :  *     assign argv[0] = "string"
      55             :  *     (some other BSD systems)
      56             :  * PS_USE_CLOBBER_ARGV
      57             :  *     write over the argv and environment area
      58             :  *     (Linux and most SysV-like systems)
      59             :  * PS_USE_WIN32
      60             :  *     push the string out as the name of a Windows event
      61             :  * PS_USE_NONE
      62             :  *     don't update ps display
      63             :  *     (This is the default, as it is safest.)
      64             :  */
      65             : #if defined(HAVE_SETPROCTITLE_FAST)
      66             : #define PS_USE_SETPROCTITLE_FAST
      67             : #elif defined(HAVE_SETPROCTITLE)
      68             : #define PS_USE_SETPROCTITLE
      69             : #elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
      70             : #define PS_USE_PSTAT
      71             : #elif defined(HAVE_PS_STRINGS)
      72             : #define PS_USE_PS_STRINGS
      73             : #elif (defined(BSD) || defined(__hurd__)) && !defined(__darwin__)
      74             : #define PS_USE_CHANGE_ARGV
      75             : #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(__svr5__) || defined(__darwin__)
      76             : #define PS_USE_CLOBBER_ARGV
      77             : #elif defined(WIN32)
      78             : #define PS_USE_WIN32
      79             : #else
      80             : #define PS_USE_NONE
      81             : #endif
      82             : 
      83             : 
      84             : /* Different systems want the buffer padded differently */
      85             : #if defined(_AIX) || defined(__linux__) || defined(__darwin__)
      86             : #define PS_PADDING '\0'
      87             : #else
      88             : #define PS_PADDING ' '
      89             : #endif
      90             : 
      91             : 
      92             : #ifndef PS_USE_NONE
      93             : 
      94             : #ifndef PS_USE_CLOBBER_ARGV
      95             : /* all but one option need a buffer to write their ps line in */
      96             : #define PS_BUFFER_SIZE 256
      97             : static char ps_buffer[PS_BUFFER_SIZE];
      98             : static const size_t ps_buffer_size = PS_BUFFER_SIZE;
      99             : #else                           /* PS_USE_CLOBBER_ARGV */
     100             : static char *ps_buffer;         /* will point to argv area */
     101             : static size_t ps_buffer_size;   /* space determined at run time */
     102             : static size_t last_status_len;  /* use to minimize length of clobber */
     103             : #endif                          /* PS_USE_CLOBBER_ARGV */
     104             : 
     105             : static size_t ps_buffer_cur_len;    /* nominal strlen(ps_buffer) */
     106             : 
     107             : static size_t ps_buffer_fixed_size; /* size of the constant prefix */
     108             : 
     109             : #endif                          /* not PS_USE_NONE */
     110             : 
     111             : /* save the original argv[] location here */
     112             : static int  save_argc;
     113             : static char **save_argv;
     114             : 
     115             : 
     116             : /*
     117             :  * Call this early in startup to save the original argc/argv values.
     118             :  * If needed, we make a copy of the original argv[] array to preserve it
     119             :  * from being clobbered by subsequent ps_display actions.
     120             :  *
     121             :  * (The original argv[] will not be overwritten by this routine, but may be
     122             :  * overwritten during init_ps_display.  Also, the physical location of the
     123             :  * environment strings may be moved, so this should be called before any code
     124             :  * that might try to hang onto a getenv() result.)
     125             :  *
     126             :  * Note that in case of failure this cannot call elog() as that is not
     127             :  * initialized yet.  We rely on write_stderr() instead.
     128             :  */
     129             : char      **
     130        2644 : save_ps_display_args(int argc, char **argv)
     131             : {
     132        2644 :     save_argc = argc;
     133        2644 :     save_argv = argv;
     134             : 
     135             : #if defined(PS_USE_CLOBBER_ARGV)
     136             : 
     137             :     /*
     138             :      * If we're going to overwrite the argv area, count the available space.
     139             :      * Also move the environment to make additional room.
     140             :      */
     141             :     {
     142        2644 :         char       *end_of_area = NULL;
     143             :         char      **new_environ;
     144             :         int         i;
     145             : 
     146             :         /*
     147             :          * check for contiguous argv strings
     148             :          */
     149       18890 :         for (i = 0; i < argc; i++)
     150             :         {
     151       16246 :             if (i == 0 || end_of_area + 1 == argv[i])
     152       16246 :                 end_of_area = argv[i] + strlen(argv[i]);
     153             :         }
     154             : 
     155        2644 :         if (end_of_area == NULL)    /* probably can't happen? */
     156             :         {
     157           0 :             ps_buffer = NULL;
     158           0 :             ps_buffer_size = 0;
     159           0 :             return argv;
     160             :         }
     161             : 
     162             :         /*
     163             :          * check for contiguous environ strings following argv
     164             :          */
     165       79728 :         for (i = 0; environ[i] != NULL; i++)
     166             :         {
     167       77084 :             if (end_of_area + 1 == environ[i])
     168       77084 :                 end_of_area = environ[i] + strlen(environ[i]);
     169             :         }
     170             : 
     171        2644 :         ps_buffer = argv[0];
     172        2644 :         last_status_len = ps_buffer_size = end_of_area - argv[0];
     173             : 
     174             :         /*
     175             :          * move the environment out of the way
     176             :          */
     177        2644 :         new_environ = (char **) malloc((i + 1) * sizeof(char *));
     178        2644 :         if (!new_environ)
     179             :         {
     180           0 :             write_stderr("out of memory\n");
     181           0 :             exit(1);
     182             :         }
     183       79728 :         for (i = 0; environ[i] != NULL; i++)
     184             :         {
     185       77084 :             new_environ[i] = strdup(environ[i]);
     186       77084 :             if (!new_environ[i])
     187             :             {
     188           0 :                 write_stderr("out of memory\n");
     189           0 :                 exit(1);
     190             :             }
     191             :         }
     192        2644 :         new_environ[i] = NULL;
     193        2644 :         environ = new_environ;
     194             :     }
     195             : #endif                          /* PS_USE_CLOBBER_ARGV */
     196             : 
     197             : #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
     198             : 
     199             :     /*
     200             :      * If we're going to change the original argv[] then make a copy for
     201             :      * argument parsing purposes.
     202             :      *
     203             :      * (NB: do NOT think to remove the copying of argv[], even though
     204             :      * postmaster.c finishes looking at argv[] long before we ever consider
     205             :      * changing the ps display.  On some platforms, getopt() keeps pointers
     206             :      * into the argv array, and will get horribly confused when it is
     207             :      * re-called to analyze a subprocess' argument string if the argv storage
     208             :      * has been clobbered meanwhile.  Other platforms have other dependencies
     209             :      * on argv[].
     210             :      */
     211             :     {
     212             :         char      **new_argv;
     213             :         int         i;
     214             : 
     215        2644 :         new_argv = (char **) malloc((argc + 1) * sizeof(char *));
     216        2644 :         if (!new_argv)
     217             :         {
     218           0 :             write_stderr("out of memory\n");
     219           0 :             exit(1);
     220             :         }
     221       18890 :         for (i = 0; i < argc; i++)
     222             :         {
     223       16246 :             new_argv[i] = strdup(argv[i]);
     224       16246 :             if (!new_argv[i])
     225             :             {
     226           0 :                 write_stderr("out of memory\n");
     227           0 :                 exit(1);
     228             :             }
     229             :         }
     230        2644 :         new_argv[argc] = NULL;
     231             : 
     232             : #if defined(__darwin__)
     233             : 
     234             :         /*
     235             :          * macOS (and perhaps other NeXT-derived platforms?) has a static copy
     236             :          * of the argv pointer, which we may fix like so:
     237             :          */
     238             :         *_NSGetArgv() = new_argv;
     239             : #endif
     240             : 
     241        2644 :         argv = new_argv;
     242             :     }
     243             : #endif                          /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
     244             : 
     245        2644 :     return argv;
     246             : }
     247             : 
     248             : /*
     249             :  * Call this once during subprocess startup to set the identification
     250             :  * values.  At this point, the original argv[] array may be overwritten.
     251             :  */
     252             : void
     253       11906 : init_ps_display(const char *username, const char *dbname,
     254             :                 const char *host_info, const char *initial_str)
     255             : {
     256             :     Assert(username);
     257             :     Assert(dbname);
     258             :     Assert(host_info);
     259             : 
     260             : #ifndef PS_USE_NONE
     261             :     /* no ps display for stand-alone backend */
     262       11906 :     if (!IsUnderPostmaster)
     263           0 :         return;
     264             : 
     265             :     /* no ps display if you didn't call save_ps_display_args() */
     266       11906 :     if (!save_argv)
     267           0 :         return;
     268             : 
     269             : #ifdef PS_USE_CLOBBER_ARGV
     270             :     /* If ps_buffer is a pointer, it might still be null */
     271       11906 :     if (!ps_buffer)
     272           0 :         return;
     273             : #endif
     274             : 
     275             :     /*
     276             :      * Overwrite argv[] to point at appropriate space, if needed
     277             :      */
     278             : 
     279             : #ifdef PS_USE_CHANGE_ARGV
     280             :     save_argv[0] = ps_buffer;
     281             :     save_argv[1] = NULL;
     282             : #endif                          /* PS_USE_CHANGE_ARGV */
     283             : 
     284             : #ifdef PS_USE_CLOBBER_ARGV
     285             :     {
     286             :         int         i;
     287             : 
     288             :         /* make extra argv slots point at end_of_area (a NUL) */
     289       70274 :         for (i = 1; i < save_argc; i++)
     290       58368 :             save_argv[i] = ps_buffer + ps_buffer_size;
     291             :     }
     292             : #endif                          /* PS_USE_CLOBBER_ARGV */
     293             : 
     294             :     /*
     295             :      * Make fixed prefix of ps display.
     296             :      */
     297             : 
     298             : #if defined(PS_USE_SETPROCTITLE) || defined(PS_USE_SETPROCTITLE_FAST)
     299             : 
     300             :     /*
     301             :      * apparently setproctitle() already adds a `progname:' prefix to the ps
     302             :      * line
     303             :      */
     304             : #define PROGRAM_NAME_PREFIX ""
     305             : #else
     306             : #define PROGRAM_NAME_PREFIX "postgres: "
     307             : #endif
     308             : 
     309       11906 :     if (*cluster_name == '\0')
     310             :     {
     311        6098 :         snprintf(ps_buffer, ps_buffer_size,
     312             :                  PROGRAM_NAME_PREFIX "%s %s %s ",
     313             :                  username, dbname, host_info);
     314             :     }
     315             :     else
     316             :     {
     317        5808 :         snprintf(ps_buffer, ps_buffer_size,
     318             :                  PROGRAM_NAME_PREFIX "%s: %s %s %s ",
     319             :                  cluster_name, username, dbname, host_info);
     320             :     }
     321             : 
     322       11906 :     ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
     323             : 
     324       11906 :     set_ps_display(initial_str, true);
     325             : #endif                          /* not PS_USE_NONE */
     326             : }
     327             : 
     328             : 
     329             : 
     330             : /*
     331             :  * Call this to update the ps status display to a fixed prefix plus an
     332             :  * indication of what you're currently doing passed in the argument.
     333             :  */
     334             : void
     335     1036856 : set_ps_display(const char *activity, bool force)
     336             : {
     337             : #ifndef PS_USE_NONE
     338             :     /* update_process_title=off disables updates, unless force = true */
     339     1036856 :     if (!force && !update_process_title)
     340           0 :         return;
     341             : 
     342             :     /* no ps display for stand-alone backend */
     343     1036856 :     if (!IsUnderPostmaster)
     344      329566 :         return;
     345             : 
     346             : #ifdef PS_USE_CLOBBER_ARGV
     347             :     /* If ps_buffer is a pointer, it might still be null */
     348      707290 :     if (!ps_buffer)
     349           0 :         return;
     350             : #endif
     351             : 
     352             :     /* Update ps_buffer to contain both fixed part and activity */
     353      707290 :     strlcpy(ps_buffer + ps_buffer_fixed_size, activity,
     354             :             ps_buffer_size - ps_buffer_fixed_size);
     355      707290 :     ps_buffer_cur_len = strlen(ps_buffer);
     356             : 
     357             :     /* Transmit new setting to kernel, if necessary */
     358             : 
     359             : #ifdef PS_USE_SETPROCTITLE
     360             :     setproctitle("%s", ps_buffer);
     361             : #elif defined(PS_USE_SETPROCTITLE_FAST)
     362             :     setproctitle_fast("%s", ps_buffer);
     363             : #endif
     364             : 
     365             : #ifdef PS_USE_PSTAT
     366             :     {
     367             :         union pstun pst;
     368             : 
     369             :         pst.pst_command = ps_buffer;
     370             :         pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
     371             :     }
     372             : #endif                          /* PS_USE_PSTAT */
     373             : 
     374             : #ifdef PS_USE_PS_STRINGS
     375             :     PS_STRINGS->ps_nargvstr = 1;
     376             :     PS_STRINGS->ps_argvstr = ps_buffer;
     377             : #endif                          /* PS_USE_PS_STRINGS */
     378             : 
     379             : #ifdef PS_USE_CLOBBER_ARGV
     380             :     /* pad unused memory; need only clobber remainder of old status string */
     381      707290 :     if (last_status_len > ps_buffer_cur_len)
     382      353870 :         MemSet(ps_buffer + ps_buffer_cur_len, PS_PADDING,
     383             :                last_status_len - ps_buffer_cur_len);
     384      707290 :     last_status_len = ps_buffer_cur_len;
     385             : #endif                          /* PS_USE_CLOBBER_ARGV */
     386             : 
     387             : #ifdef PS_USE_WIN32
     388             :     {
     389             :         /*
     390             :          * Win32 does not support showing any changed arguments. To make it at
     391             :          * all possible to track which backend is doing what, we create a
     392             :          * named object that can be viewed with for example Process Explorer.
     393             :          */
     394             :         static HANDLE ident_handle = INVALID_HANDLE_VALUE;
     395             :         char        name[PS_BUFFER_SIZE + 32];
     396             : 
     397             :         if (ident_handle != INVALID_HANDLE_VALUE)
     398             :             CloseHandle(ident_handle);
     399             : 
     400             :         sprintf(name, "pgident(%d): %s", MyProcPid, ps_buffer);
     401             : 
     402             :         ident_handle = CreateEvent(NULL, TRUE, FALSE, name);
     403             :     }
     404             : #endif                          /* PS_USE_WIN32 */
     405             : #endif                          /* not PS_USE_NONE */
     406             : }
     407             : 
     408             : 
     409             : /*
     410             :  * Returns what's currently in the ps display, in case someone needs
     411             :  * it.  Note that only the activity part is returned.  On some platforms
     412             :  * the string will not be null-terminated, so return the effective
     413             :  * length into *displen.
     414             :  */
     415             : const char *
     416        1300 : get_ps_display(int *displen)
     417             : {
     418             : #ifdef PS_USE_CLOBBER_ARGV
     419             :     /* If ps_buffer is a pointer, it might still be null */
     420        1300 :     if (!ps_buffer)
     421             :     {
     422           0 :         *displen = 0;
     423           0 :         return "";
     424             :     }
     425             : #endif
     426             : 
     427             : #ifndef PS_USE_NONE
     428        1300 :     *displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
     429             : 
     430        1300 :     return ps_buffer + ps_buffer_fixed_size;
     431             : #else
     432             :     return "";
     433             : #endif
     434             : }

Generated by: LCOV version 1.13