2 * Copyright © 2003 Felix Kuehling
3 * Copyright © 2018 Advanced Micro Devices, Inc.
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:
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.
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
28 #include "u_process.h"
29 #include "detect_os.h"
35 #undef GET_PROGRAM_NAME
43 #if defined(__linux__) && defined(HAVE_PROGRAM_INVOCATION_NAME)
45 static char *path
= NULL
;
47 static void __freeProgramPath()
56 char * arg
= strrchr(program_invocation_name
, '/');
58 /* If the / character was found this is likely a linux path or
59 * an invocation path for a 64-bit wine program.
61 * However, some programs pass command line arguments into argv[0].
62 * Strip these arguments out by using the realpath only if it was
63 * a prefix of the invocation name.
66 path
= realpath("/proc/self/exe", NULL
);
67 atexit(__freeProgramPath
);
70 if (path
&& strncmp(path
, program_invocation_name
, strlen(path
)) == 0) {
71 /* This shouldn't be null because path is a a prefix,
72 * but check it anyway since path is static. */
73 char * name
= strrchr(path
, '/');
81 /* If there was no '/' at all we likely have a windows like path from
84 arg
= strrchr(program_invocation_name
, '\\');
88 return program_invocation_name
;
90 # define GET_PROGRAM_NAME() __getProgramName()
91 #elif defined(HAVE_PROGRAM_INVOCATION_NAME)
92 # define GET_PROGRAM_NAME() program_invocation_short_name
93 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
94 # include <osreldate.h>
95 # if (__FreeBSD_version >= 440000)
96 # define GET_PROGRAM_NAME() getprogname()
98 #elif defined(__NetBSD__)
99 # include <sys/param.h>
100 # if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000100)
101 # define GET_PROGRAM_NAME() getprogname()
103 #elif defined(__DragonFly__)
104 # define GET_PROGRAM_NAME() getprogname()
105 #elif defined(__APPLE__)
106 # define GET_PROGRAM_NAME() getprogname()
107 #elif defined(ANDROID)
108 # define GET_PROGRAM_NAME() getprogname()
110 /* Solaris has getexecname() which returns the full path - return just
111 the basename to match BSD getprogname() */
117 static const char *progname
;
119 if (progname
== NULL
) {
120 const char *e
= getexecname();
122 /* Have to make a copy since getexecname can return a readonly
123 string, but basename expects to be able to modify its arg. */
126 progname
= basename(n
);
133 # define GET_PROGRAM_NAME() __getProgramName()
136 #if !defined(GET_PROGRAM_NAME)
137 # if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__UCLIBC__) || defined(ANDROID)
138 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
139 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
140 * used as a last resort, if there is no documented facility available. */
144 extern const char *__progname
;
145 char * arg
= strrchr(__progname
, '/');
151 # define GET_PROGRAM_NAME() __getProgramName()
153 # define GET_PROGRAM_NAME() ""
154 # pragma message ( "Warning: Per application configuration won't work with your OS version." )
159 util_get_process_name(void)
161 return GET_PROGRAM_NAME();
165 util_get_process_exec_path(char* process_path
, size_t len
)
167 #if DETECT_OS_WINDOWS
168 return GetModuleFileNameA(NULL
, process_path
, len
);
172 if ((r
= readlink("/proc/self/exe", process_path
, len
)) > 0)
174 if ((r
= readlink("/proc/curproc/exe", process_path
, len
)) > 0)
176 if ((r
= readlink("/proc/curproc/file", process_path
, len
)) > 0)
181 process_path
[r
] = '\0';