PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - sapi/cli - ps_title.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 65 88 73.9 %
Date: 2016-09-18 Functions: 5 6 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * PostgreSQL is released under the PostgreSQL License, a liberal Open Source
       3             :  * license, similar to the BSD or MIT licenses.
       4             :  * PostgreSQL Database Management System (formerly known as Postgres, then as
       5             :  * Postgres95)
       6             :  *
       7             :  * Portions Copyright (c) 1996-2015, The PostgreSQL Global Development Group
       8             :  *
       9             :  * Portions Copyright (c) 1994, The Regents of the University of California
      10             :  *
      11             :  * Permission to use, copy, modify, and distribute this software and its
      12             :  * documentation for any purpose, without fee, and without a written
      13             :  * agreement is hereby granted, provided that the above copyright notice
      14             :  * and this paragraph and the following two paragraphs appear in all copies.
      15             :  *
      16             :  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
      17             :  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
      18             :  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
      19             :  * EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
      20             :  * SUCH DAMAGE.
      21             :  *
      22             :  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
      23             :  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
      24             :  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
      25             :  * "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
      26             :  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
      27             :  *
      28             :  * The following code is adopted from the PostgreSQL's ps_status(.h/.c).
      29             :  */
      30             : 
      31             : #include "ps_title.h"
      32             : #include <stdio.h>
      33             : 
      34             : #ifdef HAVE_UNISTD_H
      35             : #include <unistd.h>
      36             : #endif
      37             : 
      38             : #include <string.h>
      39             : #include <stdlib.h>
      40             : 
      41             : #ifdef PHP_WIN32
      42             : #include "config.w32.h"
      43             : #include <windows.h>
      44             : #include <process.h>
      45             : #include "win32/codepage.h"
      46             : #else
      47             : #include "php_config.h"
      48             : extern char** environ;
      49             : #endif
      50             : 
      51             : #ifdef HAVE_SYS_PSTAT_H
      52             : #include <sys/pstat.h> /* for HP-UX */
      53             : #endif
      54             : #ifdef HAVE_PS_STRINGS
      55             : #include <machine/vmparam.h> /* for old BSD */
      56             : #include <sys/exec.h>
      57             : #endif
      58             : #if defined(DARWIN)
      59             : #include <crt_externs.h>
      60             : #endif
      61             : 
      62             : /*
      63             :  * Ways of updating ps display:
      64             :  *
      65             :  * PS_USE_SETPROCTITLE
      66             :  *         use the function setproctitle(const char *, ...)
      67             :  *         (newer BSD systems)
      68             :  * PS_USE_PSTAT
      69             :  *         use the pstat(PSTAT_SETCMD, )
      70             :  *         (HPUX)
      71             :  * PS_USE_PS_STRINGS
      72             :  *         assign PS_STRINGS->ps_argvstr = "string"
      73             :  *         (some BSD systems)
      74             :  * PS_USE_CHANGE_ARGV
      75             :  *         assign argv[0] = "string"
      76             :  *         (some other BSD systems)
      77             :  * PS_USE_CLOBBER_ARGV
      78             :  *         write over the argv and environment area
      79             :  *         (Linux and most SysV-like systems)
      80             :  * PS_USE_WIN32
      81             :  *         push the string out as the name of a Windows event
      82             :  * PS_USE_NONE
      83             :  *         don't update ps display
      84             :  *         (This is the default, as it is safest.)
      85             :  */
      86             : #if defined(HAVE_SETPROCTITLE)
      87             : #define PS_USE_SETPROCTITLE
      88             : #elif defined(HAVE_SYS_PSTAT_H) && defined(PSTAT_SETCMD)
      89             : #define PS_USE_PSTAT
      90             : #elif defined(HAVE_PS_STRINGS)
      91             : #define PS_USE_PS_STRINGS
      92             : #elif defined(BSD) && !defined(DARWIN)
      93             : #define PS_USE_CHANGE_ARGV
      94             : #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__osf__) || defined(DARWIN)
      95             : #define PS_USE_CLOBBER_ARGV
      96             : #elif defined(PHP_WIN32)
      97             : #define PS_USE_WIN32
      98             : #else
      99             : #define PS_USE_NONE
     100             : #endif
     101             : 
     102             : /* Different systems want the buffer padded differently */
     103             : #if defined(_AIX) || defined(__linux__) || defined(DARWIN)
     104             : #define PS_PADDING '\0'
     105             : #else
     106             : #define PS_PADDING ' '
     107             : #endif
     108             : 
     109             : #ifdef PS_USE_WIN32
     110             : static char windows_error_details[64];
     111             : static char ps_buffer[MAX_PATH];
     112             : static const size_t ps_buffer_size = MAX_PATH;
     113             : #elif defined(PS_USE_CLOBBER_ARGV)
     114             : static char *ps_buffer;         /* will point to argv area */
     115             : static size_t ps_buffer_size;   /* space determined at run time */
     116             : static char *empty_environ[] = {0}; /* empty environment */
     117             : #else
     118             : #define PS_BUFFER_SIZE 256
     119             : static char ps_buffer[PS_BUFFER_SIZE];
     120             : static const size_t ps_buffer_size = PS_BUFFER_SIZE;
     121             : #endif
     122             : 
     123             : static size_t ps_buffer_cur_len; /* actual string length in ps_buffer */
     124             : 
     125             : /* save the original argv[] location here */
     126             : static int save_argc;
     127             : static char** save_argv;
     128             : 
     129             : /*
     130             :  * This holds the 'locally' allocated environ from the save_ps_args method.
     131             :  * This is subsequently free'd at exit.
     132             :  */
     133             : #if defined(PS_USE_CLOBBER_ARGV)
     134             : static char** frozen_environ, **new_environ;
     135             : #endif
     136             : 
     137             : /*
     138             :  * Call this method early, before any code has used the original argv passed in
     139             :  * from main().
     140             :  * If needed, this code will make deep copies of argv and environ and return
     141             :  * these to the caller for further use. The original argv is then 'clobbered'
     142             :  * to store the process title.
     143             :  */
     144       23447 : char** save_ps_args(int argc, char** argv)
     145             : {
     146       23447 :     save_argc = argc;
     147       23447 :     save_argv = argv;
     148             : 
     149             : #if defined(PS_USE_CLOBBER_ARGV)
     150             :     /*
     151             :      * If we're going to overwrite the argv area, count the available space.
     152             :      * Also move the environment to make additional room.
     153             :      */
     154             :     {
     155       23447 :         char* end_of_area = NULL;
     156       23447 :         int non_contiguous_area = 0;
     157             :         int i;
     158             : 
     159             :         /*
     160             :          * check for contiguous argv strings
     161             :          */
     162     1624239 :         for (i = 0; (non_contiguous_area == 0) && (i < argc); i++)
     163             :         {
     164     1600792 :             if (i != 0 && end_of_area + 1 != argv[i])
     165           0 :                 non_contiguous_area = 1;
     166     1600792 :             end_of_area = argv[i] + strlen(argv[i]);
     167             :         }
     168             : 
     169             :         /*
     170             :          * check for contiguous environ strings following argv
     171             :          */
     172     1177438 :         for (i = 0; (non_contiguous_area == 0) && (environ[i] != NULL); i++)
     173             :         {
     174     1153991 :             if (end_of_area + 1 != environ[i])
     175           0 :                 non_contiguous_area = 1;
     176     1153991 :             end_of_area = environ[i] + strlen(environ[i]);
     177             :         }
     178             : 
     179       23447 :         if (non_contiguous_area != 0)
     180           0 :             goto clobber_error;
     181             : 
     182       23447 :         ps_buffer = argv[0];
     183       23447 :         ps_buffer_size = end_of_area - argv[0];
     184             : 
     185             :         /*
     186             :          * move the environment out of the way
     187             :          */
     188       23447 :         new_environ = (char **) malloc((i + 1) * sizeof(char *));
     189       23447 :         frozen_environ = (char **) malloc((i + 1) * sizeof(char *));
     190       23447 :         if (!new_environ || !frozen_environ)
     191             :             goto clobber_error;
     192     1177438 :         for (i = 0; environ[i] != NULL; i++)
     193             :         {
     194     1153991 :             new_environ[i] = strdup(environ[i]);
     195     1153991 :             if (!new_environ[i])
     196           0 :                 goto clobber_error;
     197             :         }
     198       23447 :         new_environ[i] = NULL;
     199       23447 :         environ = new_environ;
     200       23447 :         memcpy((char *)frozen_environ, (char *)new_environ, sizeof(char *) * (i + 1));
     201             : 
     202             :     }
     203             : #endif /* PS_USE_CLOBBER_ARGV */
     204             : 
     205             : #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
     206             :     /*
     207             :      * If we're going to change the original argv[] then make a copy for
     208             :      * argument parsing purposes.
     209             :      *
     210             :      * (NB: do NOT think to remove the copying of argv[]!
     211             :      * On some platforms, getopt() keeps pointers into the argv array, and
     212             :      * will get horribly confused when it is re-called to analyze a subprocess'
     213             :      * argument string if the argv storage has been clobbered meanwhile.
     214             :      * Other platforms have other dependencies on argv[].)
     215             :      */
     216             :     {
     217             :         char** new_argv;
     218             :         int i;
     219             : 
     220       23447 :         new_argv = (char **) malloc((argc + 1) * sizeof(char *));
     221       23447 :         if (!new_argv)
     222           0 :             goto clobber_error;
     223     1624239 :         for (i = 0; i < argc; i++)
     224             :         {
     225     1600792 :             new_argv[i] = strdup(argv[i]);
     226     1600792 :             if (!new_argv[i]) {
     227           0 :                 free(new_argv);
     228           0 :                 goto clobber_error;
     229             :             }
     230             :         }
     231       23447 :         new_argv[argc] = NULL;
     232             : 
     233             : #if defined(DARWIN)
     234             :         /*
     235             :          * Darwin (and perhaps other NeXT-derived platforms?) has a static
     236             :          * copy of the argv pointer, which we may fix like so:
     237             :          */
     238             :         *_NSGetArgv() = new_argv;
     239             : #endif
     240             : 
     241       23447 :         argv = new_argv;
     242             : 
     243             :     }
     244             : #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
     245             : 
     246             : #if defined(PS_USE_CLOBBER_ARGV)
     247             :     {
     248             :         /* make extra argv slots point at end_of_area (a NUL) */
     249             :         int i;
     250     1600792 :         for (i = 1; i < save_argc; i++)
     251     1577345 :             save_argv[i] = ps_buffer + ps_buffer_size;
     252             :     }
     253             : #endif /* PS_USE_CLOBBER_ARGV */
     254             : 
     255             : #ifdef PS_USE_CHANGE_ARGV
     256             :     save_argv[0] = ps_buffer; /* ps_buffer here is a static const array of size PS_BUFFER_SIZE */
     257             :     save_argv[1] = NULL;
     258             : #endif /* PS_USE_CHANGE_ARGV */
     259             : 
     260       23447 :     return argv;
     261             : 
     262             : #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
     263             : clobber_error:
     264             :     /* probably can't happen?!
     265             :      * if we ever get here, argv still points to originally passed
     266             :      * in argument
     267             :      */
     268           0 :     save_argv = NULL;
     269           0 :     save_argc = 0;
     270           0 :     ps_buffer = NULL;
     271           0 :     ps_buffer_size = 0;
     272           0 :     return argv;
     273             : #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
     274             : }
     275             : 
     276             : /*
     277             :  * Returns PS_TITLE_SUCCESS if the OS supports this functionality
     278             :  * and the init function was called.
     279             :  * Otherwise returns NOT_AVAILABLE or NOT_INITIALIZED
     280             :  */
     281           2 : int is_ps_title_available()
     282             : {
     283             : #ifdef PS_USE_NONE
     284             :     return PS_TITLE_NOT_AVAILABLE; /* disabled functionality */
     285             : #endif
     286             : 
     287           2 :     if (!save_argv)
     288           0 :         return PS_TITLE_NOT_INITIALIZED;
     289             : 
     290             : #ifdef PS_USE_CLOBBER_ARGV
     291           2 :     if (!ps_buffer)
     292           0 :         return PS_TITLE_BUFFER_NOT_AVAILABLE;
     293             : #endif /* PS_USE_CLOBBER_ARGV */
     294             : 
     295           2 :     return PS_TITLE_SUCCESS;
     296             : }
     297             : 
     298             : /*
     299             :  * Convert error codes into error strings
     300             :  */
     301           0 : const char* ps_title_errno(int rc)
     302             : {
     303           0 :     switch(rc)
     304             :     {
     305             :     case PS_TITLE_SUCCESS:
     306           0 :         return "Success";
     307             : 
     308             :     case PS_TITLE_NOT_AVAILABLE:
     309           0 :         return "Not available on this OS";
     310             : 
     311             :     case PS_TITLE_NOT_INITIALIZED:
     312           0 :         return "Not initialized correctly";
     313             : 
     314             :     case PS_TITLE_BUFFER_NOT_AVAILABLE:
     315           0 :         return "Buffer not contiguous";
     316             : 
     317             : #ifdef PS_USE_WIN32
     318             :     case PS_TITLE_WINDOWS_ERROR:
     319             :         sprintf(windows_error_details, "Windows error code: %lu", GetLastError());
     320             :         return windows_error_details;
     321             : #endif
     322             :     }
     323             : 
     324           0 :     return "Unknown error code";
     325             : }
     326             : 
     327             : /*
     328             :  * Set a new process title.
     329             :  * Returns the appropriate error code if if there's an error
     330             :  * (like the functionality is compile time disabled, or the
     331             :  * save_ps_args() was not called.
     332             :  * Else returns 0 on success.
     333             :  */
     334           1 : int set_ps_title(const char* title)
     335             : {
     336           1 :     int rc = is_ps_title_available();
     337           1 :     if (rc != PS_TITLE_SUCCESS)
     338           0 :         return rc;
     339             : 
     340           1 :     strncpy(ps_buffer, title, ps_buffer_size);
     341           1 :     ps_buffer[ps_buffer_size - 1] = '\0';
     342           1 :     ps_buffer_cur_len = strlen(ps_buffer);
     343             : 
     344             : #ifdef PS_USE_SETPROCTITLE
     345             :     setproctitle("%s", ps_buffer);
     346             : #endif
     347             : 
     348             : #ifdef PS_USE_PSTAT
     349             :     {
     350             :         union pstun pst;
     351             : 
     352             :         pst.pst_command = ps_buffer;
     353             :         pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
     354             :     }
     355             : #endif /* PS_USE_PSTAT */
     356             : 
     357             : #ifdef PS_USE_PS_STRINGS
     358             :     PS_STRINGS->ps_nargvstr = 1;
     359             :     PS_STRINGS->ps_argvstr = ps_buffer;
     360             : #endif /* PS_USE_PS_STRINGS */
     361             : 
     362             : #ifdef PS_USE_CLOBBER_ARGV
     363             :     /* pad unused memory */
     364           1 :     if (ps_buffer_cur_len < ps_buffer_size)
     365             :     {
     366           1 :         memset(ps_buffer + ps_buffer_cur_len, PS_PADDING,
     367             :                ps_buffer_size - ps_buffer_cur_len);
     368             :     }
     369             : #endif /* PS_USE_CLOBBER_ARGV */
     370             : 
     371             : #ifdef PS_USE_WIN32
     372             :     {
     373             :         wchar_t *ps_buffer_w = php_win32_cp_any_to_w(ps_buffer);
     374             : 
     375             :         if (!ps_buffer_w || !SetConsoleTitleW(ps_buffer_w)) {
     376             :             return PS_TITLE_WINDOWS_ERROR;
     377             :         }
     378             : 
     379             :         free(ps_buffer_w);
     380             :     }
     381             : #endif /* PS_USE_WIN32 */
     382             : 
     383           1 :     return PS_TITLE_SUCCESS;
     384             : }
     385             : 
     386             : /*
     387             :  * Returns the current ps_buffer value into string.  On some platforms
     388             :  * the string will not be null-terminated, so return the effective
     389             :  * length into *displen.
     390             :  * The return code indicates the error.
     391             :  */
     392           1 : int get_ps_title(int *displen, const char** string)
     393             : {
     394           1 :     int rc = is_ps_title_available();
     395           1 :     if (rc != PS_TITLE_SUCCESS)
     396           0 :         return rc;
     397             : 
     398             : #ifdef PS_USE_WIN32
     399             :     {
     400             :         wchar_t ps_buffer_w[MAX_PATH];
     401             :         char *tmp;
     402             : 
     403             :         if (!(ps_buffer_cur_len = GetConsoleTitleW(ps_buffer_w, (DWORD)sizeof(ps_buffer_w)))) {
     404             :             return PS_TITLE_WINDOWS_ERROR;
     405             :         }
     406             : 
     407             :         tmp = php_win32_cp_conv_w_to_any(ps_buffer_w, PHP_WIN32_CP_IGNORE_LEN, &ps_buffer_cur_len);
     408             :         if (!tmp) {
     409             :             return PS_TITLE_WINDOWS_ERROR;
     410             :         }
     411             : 
     412             :         ps_buffer_cur_len = ps_buffer_cur_len > sizeof(ps_buffer)-1 ? sizeof(ps_buffer)-1 : ps_buffer_cur_len;
     413             : 
     414             :         memmove(ps_buffer, tmp, ps_buffer_size);
     415             :         ps_buffer[ps_buffer_cur_len] = '\0';
     416             :         free(tmp);
     417             :     }
     418             : #endif
     419           1 :     *displen = (int)ps_buffer_cur_len;
     420           1 :     *string = ps_buffer;
     421           1 :     return PS_TITLE_SUCCESS;
     422             : }
     423             : 
     424             : /*
     425             :  * Clean up the allocated argv and environ if applicable. Only call
     426             :  * this right before exiting.
     427             :  * This isn't needed per-se because the OS will clean-up anyway, but
     428             :  * having and calling this will ensure Valgrind doesn't output 'false
     429             :  * positives'.
     430             :  */
     431       23493 : void cleanup_ps_args(char **argv)
     432             : {
     433             : #ifndef PS_USE_NONE
     434       23493 :     if (save_argv)
     435             :     {
     436       23493 :         save_argv = NULL;
     437       23493 :         save_argc = 0;
     438             : 
     439             : #ifdef PS_USE_CLOBBER_ARGV
     440             :         {
     441             :             int i;
     442     1179830 :             for (i = 0; frozen_environ[i] != NULL; i++)
     443     1156337 :                 free(frozen_environ[i]);
     444       23493 :             free(frozen_environ);
     445       23493 :             free(new_environ);
     446             :             /* leave a sane environment behind since some atexit() handlers
     447             :                 call getenv(). */
     448       23493 :             environ = empty_environ;
     449             :         }
     450             : #endif /* PS_USE_CLOBBER_ARGV */
     451             : 
     452             : #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
     453             :         {
     454             :             int i;
     455     1627321 :             for (i=0; argv[i] != NULL; i++)
     456     1603828 :                 free(argv[i]);
     457       23493 :             free(argv);
     458             :         }
     459             : #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
     460             :     }
     461             : #endif /* PS_USE_NONE */
     462             : 
     463       23493 :     return;
     464             : }

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:22 +0000 (8 days ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.