os_process.c revision 848b8605
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
47/**
48 * Return the name of the current process.
49 * \param procname  returns the process name
50 * \param size  size of the procname buffer
51 * \return  TRUE or FALSE for success, failure
52 */
53boolean
54os_get_process_name(char *procname, size_t size)
55{
56   const char *name;
57#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
58   char szProcessPath[MAX_PATH];
59   char *lpProcessName;
60   char *lpProcessExt;
61
62   GetModuleFileNameA(NULL, szProcessPath, Elements(szProcessPath));
63
64   lpProcessName = strrchr(szProcessPath, '\\');
65   lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
66
67   lpProcessExt = strrchr(lpProcessName, '.');
68   if (lpProcessExt) {
69      *lpProcessExt = '\0';
70   }
71
72   name = lpProcessName;
73
74#elif defined(__GLIBC__) || defined(__CYGWIN__)
75   name = program_invocation_short_name;
76#elif defined(PIPE_OS_BSD) || defined(PIPE_OS_APPLE)
77   /* *BSD and OS X */
78   name = getprogname();
79#elif defined(PIPE_OS_HAIKU)
80   image_info info;
81   get_image_info(B_CURRENT_TEAM, &info);
82   name = info.name;
83#else
84#warning unexpected platform in os_process.c
85   return FALSE;
86#endif
87
88   assert(size > 0);
89   assert(procname);
90
91   if (name && procname && size > 0) {
92      strncpy(procname, name, size);
93      procname[size - 1] = '\0';
94      return TRUE;
95   }
96   else {
97      return FALSE;
98   }
99}
100