Home | History | Annotate | Download | only in os
      1 /**************************************************************************
      2  *
      3  * Copyright 2013 VMware, Inc.
      4  * All Rights Reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the
      8  * "Software"), to deal in the Software without restriction, including
      9  * without limitation the rights to use, copy, modify, merge, publish,
     10  * distribute, sub license, and/or sell copies of the Software, and to
     11  * permit persons to whom the Software is furnished to do so, subject to
     12  * the following conditions:
     13  *
     14  * The above copyright notice and this permission notice (including the
     15  * next paragraph) shall be included in all copies or substantial portions
     16  * of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     21  * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
     22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  **************************************************************************/
     27 
     28 
     29 #include "pipe/p_config.h"
     30 #include "os/os_process.h"
     31 #include "util/u_memory.h"
     32 
     33 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
     34 #  include <windows.h>
     35 #elif defined(__GLIBC__) || defined(__CYGWIN__)
     36 #  include <errno.h>
     37 #elif defined(PIPE_OS_BSD) || defined(PIPE_OS_APPLE)
     38 #  include <stdlib.h>
     39 #elif defined(PIPE_OS_HAIKU)
     40 #  include <kernel/OS.h>
     41 #  include <kernel/image.h>
     42 #else
     43 #warning unexpected platform in os_process.c
     44 #endif
     45 
     46 #if defined(PIPE_OS_LINUX)
     47 #  include <fcntl.h>
     48 #endif
     49 
     50 
     51 /**
     52  * Return the name of the current process.
     53  * \param procname  returns the process name
     54  * \param size  size of the procname buffer
     55  * \return  TRUE or FALSE for success, failure
     56  */
     57 boolean
     58 os_get_process_name(char *procname, size_t size)
     59 {
     60    const char *name;
     61 
     62    /* First, check if the GALLIUM_PROCESS_NAME env var is set to
     63     * override the normal process name query.
     64     */
     65    name = os_get_option("GALLIUM_PROCESS_NAME");
     66 
     67    if (!name) {
     68       /* do normal query */
     69 
     70 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
     71       char szProcessPath[MAX_PATH];
     72       char *lpProcessName;
     73       char *lpProcessExt;
     74 
     75       GetModuleFileNameA(NULL, szProcessPath, ARRAY_SIZE(szProcessPath));
     76 
     77       lpProcessName = strrchr(szProcessPath, '\\');
     78       lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
     79 
     80       lpProcessExt = strrchr(lpProcessName, '.');
     81       if (lpProcessExt) {
     82          *lpProcessExt = '\0';
     83       }
     84 
     85       name = lpProcessName;
     86 
     87 #elif defined(__GLIBC__) || defined(__CYGWIN__)
     88       name = program_invocation_short_name;
     89 #elif defined(PIPE_OS_BSD) || defined(PIPE_OS_APPLE)
     90       /* *BSD and OS X */
     91       name = getprogname();
     92 #elif defined(PIPE_OS_HAIKU)
     93       image_info info;
     94       get_image_info(B_CURRENT_TEAM, &info);
     95       name = info.name;
     96 #else
     97 #warning unexpected platform in os_process.c
     98       return FALSE;
     99 #endif
    100 
    101    }
    102 
    103    assert(size > 0);
    104    assert(procname);
    105 
    106    if (name && procname && size > 0) {
    107       strncpy(procname, name, size);
    108       procname[size - 1] = '\0';
    109       return TRUE;
    110    }
    111    else {
    112       return FALSE;
    113    }
    114 }
    115 
    116 
    117 /**
    118  * Return the command line for the calling process.  This is basically
    119  * the argv[] array with the arguments separated by spaces.
    120  * \param cmdline  returns the command line string
    121  * \param size  size of the cmdline buffer
    122  * \return  TRUE or FALSE for success, failure
    123  */
    124 boolean
    125 os_get_command_line(char *cmdline, size_t size)
    126 {
    127 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
    128    const char *args = GetCommandLine();
    129    if (args) {
    130       strncpy(cmdline, args, size);
    131       // make sure we terminate the string
    132       cmdline[size - 1] = 0;
    133       return TRUE;
    134    }
    135 #elif defined(PIPE_OS_LINUX)
    136    int f = open("/proc/self/cmdline", O_RDONLY);
    137    if (f) {
    138       const int n = read(f, cmdline, size - 1);
    139       int i;
    140       assert(n < size);
    141       // The arguments are separated by '\0' chars.  Convert them to spaces.
    142       for (i = 0; i < n; i++) {
    143          if (cmdline[i] == 0) {
    144             cmdline[i] = ' ';
    145          }
    146       }
    147       // terminate the string
    148       cmdline[n] = 0;
    149       close(f);
    150       return TRUE;
    151    }
    152 #endif
    153 
    154    /* XXX to-do: implement this function for other operating systems */
    155 
    156    cmdline[0] = 0;
    157    return FALSE;
    158 }
    159