util/u_process: add util_get_process_exec_path
[mesa.git] / src / util / u_process.c
1 /*
2 * Copyright © 2003 Felix Kuehling
3 * Copyright © 2018 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27
28 #include "u_process.h"
29 #include "detect_os.h"
30 #include "macros.h"
31 #include <string.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #undef GET_PROGRAM_NAME
37
38 #if DETECT_OS_WINDOWS
39 #include <windows.h>
40 #endif
41
42 #if defined(__linux__) && defined(HAVE_PROGRAM_INVOCATION_NAME)
43
44 static char *path = NULL;
45
46 static void __freeProgramPath()
47 {
48 free(path);
49 path = NULL;
50 }
51
52 static const char *
53 __getProgramName()
54 {
55 char * arg = strrchr(program_invocation_name, '/');
56 if (arg) {
57 /* If the / character was found this is likely a linux path or
58 * an invocation path for a 64-bit wine program.
59 *
60 * However, some programs pass command line arguments into argv[0].
61 * Strip these arguments out by using the realpath only if it was
62 * a prefix of the invocation name.
63 */
64 if (!path) {
65 path = realpath("/proc/self/exe", NULL);
66 atexit(__freeProgramPath);
67 }
68
69 if (path && strncmp(path, program_invocation_name, strlen(path)) == 0) {
70 /* This shouldn't be null because path is a a prefix,
71 * but check it anyway since path is static. */
72 char * name = strrchr(path, '/');
73 if (name)
74 return name + 1;
75 }
76
77 return arg+1;
78 }
79
80 /* If there was no '/' at all we likely have a windows like path from
81 * a wine application.
82 */
83 arg = strrchr(program_invocation_name, '\\');
84 if (arg)
85 return arg+1;
86
87 return program_invocation_name;
88 }
89 # define GET_PROGRAM_NAME() __getProgramName()
90 #elif defined(HAVE_PROGRAM_INVOCATION_NAME)
91 # define GET_PROGRAM_NAME() program_invocation_short_name
92 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
93 # include <osreldate.h>
94 # if (__FreeBSD_version >= 440000)
95 # define GET_PROGRAM_NAME() getprogname()
96 # endif
97 #elif defined(__NetBSD__)
98 # include <sys/param.h>
99 # if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000100)
100 # define GET_PROGRAM_NAME() getprogname()
101 # endif
102 #elif defined(__DragonFly__)
103 # define GET_PROGRAM_NAME() getprogname()
104 #elif defined(__APPLE__)
105 # define GET_PROGRAM_NAME() getprogname()
106 #elif defined(ANDROID)
107 # define GET_PROGRAM_NAME() getprogname()
108 #elif defined(__sun)
109 /* Solaris has getexecname() which returns the full path - return just
110 the basename to match BSD getprogname() */
111 # include <libgen.h>
112
113 static const char *
114 __getProgramName()
115 {
116 static const char *progname;
117
118 if (progname == NULL) {
119 const char *e = getexecname();
120 if (e != NULL) {
121 /* Have to make a copy since getexecname can return a readonly
122 string, but basename expects to be able to modify its arg. */
123 char *n = strdup(e);
124 if (n != NULL) {
125 progname = basename(n);
126 }
127 }
128 }
129 return progname;
130 }
131
132 # define GET_PROGRAM_NAME() __getProgramName()
133 #endif
134
135 #if !defined(GET_PROGRAM_NAME)
136 # if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__UCLIBC__) || defined(ANDROID)
137 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
138 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
139 * used as a last resort, if there is no documented facility available. */
140 static const char *
141 __getProgramName()
142 {
143 extern const char *__progname;
144 char * arg = strrchr(__progname, '/');
145 if (arg)
146 return arg+1;
147 else
148 return __progname;
149 }
150 # define GET_PROGRAM_NAME() __getProgramName()
151 # else
152 # define GET_PROGRAM_NAME() ""
153 # pragma message ( "Warning: Per application configuration won't work with your OS version." )
154 # endif
155 #endif
156
157 const char *
158 util_get_process_name(void)
159 {
160 return GET_PROGRAM_NAME();
161 }
162
163 size_t
164 util_get_process_exec_path(char* process_path, size_t len)
165 {
166 #if DETECT_OS_WINDOWS
167 return GetModuleFileNameA(NULL, process_path, len);
168 #elif DETECT_OS_UNIX
169 ssize_t r;
170
171 if ((r = readlink("/proc/self/exe", process_path, len)) > 0)
172 goto success;
173 if ((r = readlink("/proc/curproc/exe", process_path, len)) > 0)
174 goto success;
175 if ((r = readlink("/proc/curproc/file", process_path, len)) > 0)
176 goto success;
177
178 return 0;
179 success:
180 process_path[r] = '\0';
181 return r;
182
183 #endif
184 return 0;
185 }