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