b9328d58da401e98d397d3e21746f6af3fa89e9e
[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 <string.h>
30 #include <errno.h>
31 #include <stdlib.h>
32
33 #undef GET_PROGRAM_NAME
34
35 #if defined(__linux__) && defined(HAVE_PROGRAM_INVOCATION_NAME)
36
37 static char *path = NULL;
38
39 static void __freeProgramPath()
40 {
41 free(path);
42 path = NULL;
43 }
44
45 static const char *
46 __getProgramName()
47 {
48 char * arg = strrchr(program_invocation_name, '/');
49 if (arg) {
50 /* If the / character was found this is likely a linux path or
51 * an invocation path for a 64-bit wine program.
52 *
53 * However, some programs pass command line arguments into argv[0].
54 * Strip these arguments out by using the realpath only if it was
55 * a prefix of the invocation name.
56 */
57 if (!path) {
58 path = realpath("/proc/self/exe", NULL);
59 atexit(__freeProgramPath);
60 }
61
62 if (path && strncmp(path, program_invocation_name, strlen(path)) == 0) {
63 /* This shouldn't be null because path is a a prefix,
64 * but check it anyway since path is static. */
65 char * name = strrchr(path, '/');
66 if (name)
67 return name + 1;
68 }
69
70 return arg+1;
71 }
72
73 /* If there was no '/' at all we likely have a windows like path from
74 * a wine application.
75 */
76 arg = strrchr(program_invocation_name, '\\');
77 if (arg)
78 return arg+1;
79
80 return program_invocation_name;
81 }
82 # define GET_PROGRAM_NAME() __getProgramName()
83 #elif defined(HAVE_PROGRAM_INVOCATION_NAME)
84 # define GET_PROGRAM_NAME() program_invocation_short_name
85 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
86 # include <osreldate.h>
87 # if (__FreeBSD_version >= 440000)
88 # define GET_PROGRAM_NAME() getprogname()
89 # endif
90 #elif defined(__NetBSD__)
91 # include <sys/param.h>
92 # if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000100)
93 # define GET_PROGRAM_NAME() getprogname()
94 # endif
95 #elif defined(__DragonFly__)
96 # define GET_PROGRAM_NAME() getprogname()
97 #elif defined(__APPLE__)
98 # define GET_PROGRAM_NAME() getprogname()
99 #elif defined(ANDROID)
100 # define GET_PROGRAM_NAME() getprogname()
101 #elif defined(__sun)
102 /* Solaris has getexecname() which returns the full path - return just
103 the basename to match BSD getprogname() */
104 # include <libgen.h>
105
106 static const char *
107 __getProgramName()
108 {
109 static const char *progname;
110
111 if (progname == NULL) {
112 const char *e = getexecname();
113 if (e != NULL) {
114 /* Have to make a copy since getexecname can return a readonly
115 string, but basename expects to be able to modify its arg. */
116 char *n = strdup(e);
117 if (n != NULL) {
118 progname = basename(n);
119 }
120 }
121 }
122 return progname;
123 }
124
125 # define GET_PROGRAM_NAME() __getProgramName()
126 #endif
127
128 #if !defined(GET_PROGRAM_NAME)
129 # if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__UCLIBC__) || defined(ANDROID)
130 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
131 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
132 * used as a last resort, if there is no documented facility available. */
133 static const char *
134 __getProgramName()
135 {
136 extern const char *__progname;
137 char * arg = strrchr(__progname, '/');
138 if (arg)
139 return arg+1;
140 else
141 return __progname;
142 }
143 # define GET_PROGRAM_NAME() __getProgramName()
144 # else
145 # define GET_PROGRAM_NAME() ""
146 # pragma message ( "Warning: Per application configuration won't work with your OS version." )
147 # endif
148 #endif
149
150 const char *
151 util_get_process_name(void)
152 {
153 return GET_PROGRAM_NAME();
154 }