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 87 74.7 %
Date: 2014-11-22 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-2014, 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             : #else
      46             : #include "php_config.h"
      47             : extern char** environ;
      48             : #endif
      49             : 
      50             : #ifdef HAVE_SYS_PSTAT_H
      51             : #include <sys/pstat.h> /* for HP-UX */
      52             : #endif
      53             : #ifdef HAVE_PS_STRINGS
      54             : #include <machine/vmparam.h> /* for old BSD */
      55             : #include <sys/exec.h>
      56             : #endif
      57             : #if defined(DARWIN)
      58             : #include <crt_externs.h>
      59             : #endif
      60             : 
      61             : /*
      62             :  * Ways of updating ps display:
      63             :  *
      64             :  * PS_USE_SETPROCTITLE
      65             :  *         use the function setproctitle(const char *, ...)
      66             :  *         (newer BSD systems)
      67             :  * PS_USE_PSTAT
      68             :  *         use the pstat(PSTAT_SETCMD, )
      69             :  *         (HPUX)
      70             :  * PS_USE_PS_STRINGS
      71             :  *         assign PS_STRINGS->ps_argvstr = "string"
      72             :  *         (some BSD systems)
      73             :  * PS_USE_CHANGE_ARGV
      74             :  *         assign argv[0] = "string"
      75             :  *         (some other BSD systems)
      76             :  * PS_USE_CLOBBER_ARGV
      77             :  *         write over the argv and environment area
      78             :  *         (Linux and most SysV-like systems)
      79             :  * PS_USE_WIN32
      80             :  *         push the string out as the name of a Windows event
      81             :  * PS_USE_NONE
      82             :  *         don't update ps display
      83             :  *         (This is the default, as it is safest.)
      84             :  */
      85             : #if defined(HAVE_SETPROCTITLE)
      86             : #define PS_USE_SETPROCTITLE
      87             : #elif defined(HAVE_SYS_PSTAT_H) && defined(PSTAT_SETCMD)
      88             : #define PS_USE_PSTAT
      89             : #elif defined(HAVE_PS_STRINGS)
      90             : #define PS_USE_PS_STRINGS
      91             : #elif defined(BSD) && !defined(DARWIN)
      92             : #define PS_USE_CHANGE_ARGV
      93             : #elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__osf__) || defined(DARWIN)
      94             : #define PS_USE_CLOBBER_ARGV
      95             : #elif defined(PHP_WIN32)
      96             : #define PS_USE_WIN32
      97             : #else
      98             : #define PS_USE_NONE
      99             : #endif
     100             : 
     101             : /* Different systems want the buffer padded differently */
     102             : #if defined(_AIX) || defined(__linux__) || defined(DARWIN)
     103             : #define PS_PADDING '\0'
     104             : #else
     105             : #define PS_PADDING ' '
     106             : #endif
     107             : 
     108             : #ifdef PS_USE_WIN32
     109             : static char windows_error_details[64];
     110             : static char ps_buffer[MAX_PATH];
     111             : static const size_t ps_buffer_size = MAX_PATH;
     112             : #elif defined(PS_USE_CLOBBER_ARGV)
     113             : static char *ps_buffer;         /* will point to argv area */
     114             : static size_t ps_buffer_size;   /* space determined at run time */
     115             : static char *empty_environ[] = {0}; /* empty environment */
     116             : #else
     117             : #define PS_BUFFER_SIZE 256
     118             : static char ps_buffer[PS_BUFFER_SIZE];
     119             : static const size_t ps_buffer_size = PS_BUFFER_SIZE;
     120             : #endif
     121             : 
     122             : static size_t ps_buffer_cur_len; /* actual string length in ps_buffer */
     123             : 
     124             : /* save the original argv[] location here */
     125             : static int save_argc;
     126             : static char** save_argv;
     127             : 
     128             : /* 
     129             :  * This holds the 'locally' allocated environ from the save_ps_args method.
     130             :  * This is subsequently free'd at exit.
     131             :  */
     132             : #if defined(PS_USE_CLOBBER_ARGV)
     133             : static char** frozen_environ, **new_environ;
     134             : #endif
     135             : 
     136             : /*
     137             :  * Call this method early, before any code has used the original argv passed in
     138             :  * from main().
     139             :  * If needed, this code will make deep copies of argv and environ and return
     140             :  * these to the caller for further use. The original argv is then 'clobbered'
     141             :  * to store the process title.
     142             :  */
     143       20104 : char** save_ps_args(int argc, char** argv)
     144             : {
     145       20104 :     save_argc = argc;
     146       20104 :     save_argv = argv;
     147             : 
     148             : #if defined(PS_USE_CLOBBER_ARGV)
     149             :     /*
     150             :      * If we're going to overwrite the argv area, count the available space.
     151             :      * Also move the environment to make additional room.
     152             :      */
     153             :     {
     154       20104 :         char* end_of_area = NULL;
     155       20104 :         int non_contiguous_area = 0;
     156             :         int i;
     157             : 
     158             :         /*
     159             :          * check for contiguous argv strings
     160             :          */
     161     1400258 :         for (i = 0; (non_contiguous_area == 0) && (i < argc); i++)
     162             :         {
     163     1380154 :             if (i != 0 && end_of_area + 1 != argv[i])
     164           0 :                 non_contiguous_area = 1;
     165     1380154 :             end_of_area = argv[i] + strlen(argv[i]);
     166             :         }
     167             : 
     168             :         /*
     169             :          * check for contiguous environ strings following argv
     170             :          */
     171      949329 :         for (i = 0; (non_contiguous_area == 0) && (environ[i] != NULL); i++)
     172             :         {
     173      929225 :             if (end_of_area + 1 != environ[i])
     174           0 :                 non_contiguous_area = 1;
     175      929225 :             end_of_area = environ[i] + strlen(environ[i]);
     176             :         }
     177             : 
     178       20104 :         if (non_contiguous_area != 0)
     179           0 :             goto clobber_error;
     180             : 
     181       20104 :         ps_buffer = argv[0];
     182       20104 :         ps_buffer_size = end_of_area - argv[0];
     183             : 
     184             :         /*
     185             :          * move the environment out of the way
     186             :          */
     187       20104 :         new_environ = (char **) malloc((i + 1) * sizeof(char *));
     188       20104 :         frozen_environ = (char **) malloc((i + 1) * sizeof(char *));
     189       20104 :         if (!new_environ || !frozen_environ)
     190             :             goto clobber_error;
     191      949329 :         for (i = 0; environ[i] != NULL; i++)
     192             :         {
     193      929225 :             new_environ[i] = strdup(environ[i]);
     194      929225 :             if (!new_environ[i])
     195           0 :                 goto clobber_error;
     196             :         }
     197       20104 :         new_environ[i] = NULL;
     198       20104 :         environ = new_environ;
     199       20104 :         memcpy((char *)frozen_environ, (char *)new_environ, sizeof(char *) * (i + 1));
     200             : 
     201             :     }
     202             : #endif /* PS_USE_CLOBBER_ARGV */
     203             : 
     204             : #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
     205             :     /*
     206             :      * If we're going to change the original argv[] then make a copy for
     207             :      * argument parsing purposes.
     208             :      *
     209             :      * (NB: do NOT think to remove the copying of argv[]!
     210             :      * On some platforms, getopt() keeps pointers into the argv array, and
     211             :      * will get horribly confused when it is re-called to analyze a subprocess'
     212             :      * argument string if the argv storage has been clobbered meanwhile.
     213             :      * Other platforms have other dependencies on argv[].)
     214             :      */
     215             :     {
     216             :         char** new_argv;
     217             :         int i;
     218             : 
     219       20104 :         new_argv = (char **) malloc((argc + 1) * sizeof(char *));
     220       20104 :         if (!new_argv)
     221           0 :             goto clobber_error;
     222     1400258 :         for (i = 0; i < argc; i++)
     223             :         {
     224     1380154 :             new_argv[i] = strdup(argv[i]);
     225     1380154 :             if (!new_argv[i])
     226           0 :                 goto clobber_error;
     227             :         }
     228       20104 :         new_argv[argc] = NULL;
     229             : 
     230             : #if defined(DARWIN)
     231             :         /*
     232             :          * Darwin (and perhaps other NeXT-derived platforms?) has a static
     233             :          * copy of the argv pointer, which we may fix like so:
     234             :          */
     235             :         *_NSGetArgv() = new_argv;
     236             : #endif
     237             : 
     238       20104 :         argv = new_argv;
     239             : 
     240             :     }
     241             : #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
     242             : 
     243             : #if defined(PS_USE_CLOBBER_ARGV)
     244             :     {
     245             :         /* make extra argv slots point at end_of_area (a NUL) */
     246             :         int i;
     247     1380154 :         for (i = 1; i < save_argc; i++)
     248     1360050 :             save_argv[i] = ps_buffer + ps_buffer_size;
     249             :     }
     250             : #endif /* PS_USE_CLOBBER_ARGV */
     251             : 
     252             : #ifdef PS_USE_CHANGE_ARGV
     253             :     save_argv[0] = ps_buffer; /* ps_buffer here is a static const array of size PS_BUFFER_SIZE */
     254             :     save_argv[1] = NULL;
     255             : #endif /* PS_USE_CHANGE_ARGV */
     256             : 
     257       20104 :     return argv;
     258             : 
     259             : #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
     260             : clobber_error:
     261             :     /* probably can't happen?!
     262             :      * if we ever get here, argv still points to originally passed
     263             :      * in argument
     264             :      */
     265           0 :     save_argv = NULL;
     266           0 :     save_argc = 0;
     267           0 :     ps_buffer = NULL;
     268           0 :     ps_buffer_size = 0;
     269           0 :     return argv;
     270             : #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
     271             : }
     272             : 
     273             : /*
     274             :  * Returns PS_TITLE_SUCCESS if the OS supports this functionality
     275             :  * and the init function was called.
     276             :  * Otherwise returns NOT_AVAILABLE or NOT_INITIALIZED
     277             :  */
     278           2 : int is_ps_title_available()
     279             : {
     280             : #ifdef PS_USE_NONE
     281             :     return PS_TITLE_NOT_AVAILABLE; /* disabled functionality */
     282             : #endif
     283             : 
     284           2 :     if (!save_argv)
     285           0 :         return PS_TITLE_NOT_INITIALIZED;
     286             : 
     287             : #ifdef PS_USE_CLOBBER_ARGV
     288           2 :     if (!ps_buffer)
     289           0 :         return PS_TITLE_BUFFER_NOT_AVAILABLE;
     290             : #endif /* PS_USE_CLOBBER_ARGV */
     291             : 
     292           2 :     return PS_TITLE_SUCCESS;
     293             : }
     294             : 
     295             : /*
     296             :  * Convert error codes into error strings
     297             :  */
     298           0 : const char* ps_title_errno(int rc)
     299             : {
     300           0 :     switch(rc)
     301             :     {
     302             :     case PS_TITLE_SUCCESS:
     303           0 :         return "Success";
     304             : 
     305             :     case PS_TITLE_NOT_AVAILABLE:
     306           0 :         return "Not available on this OS";
     307             : 
     308             :     case PS_TITLE_NOT_INITIALIZED:
     309           0 :         return "Not initialized correctly";
     310             : 
     311             :     case PS_TITLE_BUFFER_NOT_AVAILABLE:
     312           0 :         return "Buffer not contiguous";
     313             : 
     314             : #ifdef PS_USE_WIN32
     315             :     case PS_TITLE_WINDOWS_ERROR:
     316             :         sprintf(windows_error_details, "Windows error code: %lu", GetLastError());
     317             :         return windows_error_details;
     318             : #endif
     319             :     }
     320             : 
     321           0 :     return "Unknown error code";
     322             : }
     323             : 
     324             : /*
     325             :  * Set a new process title.
     326             :  * Returns the appropriate error code if if there's an error
     327             :  * (like the functionality is compile time disabled, or the
     328             :  * save_ps_args() was not called.
     329             :  * Else returns 0 on success.
     330             :  */
     331           1 : int set_ps_title(const char* title)
     332             : {
     333           1 :     int rc = is_ps_title_available();
     334           1 :     if (rc != PS_TITLE_SUCCESS)
     335           0 :         return rc;
     336             : 
     337           1 :     strncpy(ps_buffer, title, ps_buffer_size);
     338           1 :     ps_buffer[ps_buffer_size - 1] = '\0';
     339           1 :     ps_buffer_cur_len = strlen(ps_buffer);
     340             : 
     341             : #ifdef PS_USE_SETPROCTITLE
     342             :     setproctitle("%s", ps_buffer);
     343             : #endif
     344             : 
     345             : #ifdef PS_USE_PSTAT
     346             :     {
     347             :         union pstun pst;
     348             : 
     349             :         pst.pst_command = ps_buffer;
     350             :         pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
     351             :     }
     352             : #endif /* PS_USE_PSTAT */
     353             : 
     354             : #ifdef PS_USE_PS_STRINGS
     355             :     PS_STRINGS->ps_nargvstr = 1;
     356             :     PS_STRINGS->ps_argvstr = ps_buffer;
     357             : #endif /* PS_USE_PS_STRINGS */
     358             : 
     359             : #ifdef PS_USE_CLOBBER_ARGV
     360             :     /* pad unused memory */
     361           1 :     if (ps_buffer_cur_len < ps_buffer_size)
     362             :     {
     363           1 :         memset(ps_buffer + ps_buffer_cur_len, PS_PADDING,
     364             :                ps_buffer_size - ps_buffer_cur_len);
     365             :     }
     366             : #endif /* PS_USE_CLOBBER_ARGV */
     367             : 
     368             : #ifdef PS_USE_WIN32
     369             :     {
     370             :         if (!SetConsoleTitle(ps_buffer))
     371             :             return PS_TITLE_WINDOWS_ERROR;
     372             :     }
     373             : #endif /* PS_USE_WIN32 */
     374             : 
     375           1 :     return PS_TITLE_SUCCESS;
     376             : }
     377             : 
     378             : /*
     379             :  * Returns the current ps_buffer value into string.  On some platforms
     380             :  * the string will not be null-terminated, so return the effective
     381             :  * length into *displen.
     382             :  * The return code indicates the error.
     383             :  */
     384           1 : int get_ps_title(int *displen, const char** string)
     385             : {
     386           1 :     int rc = is_ps_title_available();
     387           1 :     if (rc != PS_TITLE_SUCCESS)
     388           0 :         return rc;
     389             : 
     390             : #ifdef PS_USE_WIN32
     391             :     if (!(ps_buffer_cur_len = GetConsoleTitle(ps_buffer, (DWORD)ps_buffer_size)))
     392             :         return PS_TITLE_WINDOWS_ERROR;
     393             : #endif
     394           1 :     *displen = (int)ps_buffer_cur_len;
     395           1 :     *string = ps_buffer;
     396           1 :     return PS_TITLE_SUCCESS;
     397             : }
     398             : 
     399             : /*
     400             :  * Clean up the allocated argv and environ if applicable. Only call
     401             :  * this right before exiting.
     402             :  * This isn't needed per-se because the OS will clean-up anyway, but
     403             :  * having and calling this will ensure Valgrind doesn't output 'false
     404             :  * positives'.
     405             :  */
     406       20143 : void cleanup_ps_args(char **argv)
     407             : {
     408             : #ifndef PS_USE_NONE
     409       20143 :     if (save_argv)
     410             :     {
     411       20143 :         save_argv = NULL;
     412       20143 :         save_argc = 0;
     413             : 
     414             : #ifdef PS_USE_CLOBBER_ARGV
     415             :         {
     416             :             int i;
     417      951240 :             for (i = 0; frozen_environ[i] != NULL; i++)
     418      931097 :                 free(frozen_environ[i]);
     419       20143 :             free(frozen_environ);
     420       20143 :             free(new_environ);
     421             :             /* leave a sane environment behind since some atexit() handlers
     422             :                 call getenv(). */
     423       20143 :             environ = empty_environ;
     424             :         }
     425             : #endif /* PS_USE_CLOBBER_ARGV */
     426             : 
     427             : #if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
     428             :         {
     429             :             int i;
     430     1402949 :             for (i=0; argv[i] != NULL; i++)
     431     1382806 :                 free(argv[i]);
     432       20143 :             free(argv);
     433             :         }
     434             : #endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
     435             :     }
     436             : #endif /* PS_USE_NONE */
     437             : 
     438       20143 :     return;
     439             : }

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:31 +0000 (6 days ago)

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