1 /* Program name management.
2 Copyright (C) 2016-2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20 #include "getprogname.h"
22 #include <errno.h> /* get program_invocation_name declaration */
23 #include <stdlib.h> /* get __argv declaration */
27 # include <procinfo.h>
41 # include <sys/param.h>
42 # include <sys/pstat.h>
51 # include <sys/procfs.h>
54 #if defined __SCO_VERSION__ || defined __sysv5__
60 #include "basename-lgpl.h"
62 #ifndef HAVE_GETPROGNAME /* not Mac OS X, FreeBSD, NetBSD, OpenBSD >= 5.4, Cygwin */
66 # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME /* glibc, BeOS */
67 /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
68 return program_invocation_short_name
;
69 # elif HAVE_DECL_PROGRAM_INVOCATION_NAME /* glibc, BeOS */
70 /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
71 return last_component (program_invocation_name
);
72 # elif HAVE_GETEXECNAME /* Solaris */
73 /* https://docs.oracle.com/cd/E19253-01/816-5168/6mbb3hrb1/index.html */
74 const char *p
= getexecname ();
77 return last_component (p
);
78 # elif HAVE_DECL___ARGV /* mingw, MSVC */
79 /* https://docs.microsoft.com/en-us/cpp/c-runtime-library/argc-argv-wargv */
80 const char *p
= __argv
&& __argv
[0] ? __argv
[0] : "?";
81 return last_component (p
);
82 # elif HAVE_VAR___PROGNAME /* OpenBSD, Android, QNX */
83 /* https://man.openbsd.org/style.9 */
84 /* http://www.qnx.de/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.neutrino_lib_ref%2Fp%2F__progname.html */
85 /* Be careful to declare this only when we absolutely need it
86 (OpenBSD 5.1), rather than when it's available. Otherwise,
87 its mere declaration makes program_invocation_short_name
88 malfunction (have zero length) with Fedora 25's glibc. */
89 extern char *__progname
;
90 const char *p
= __progname
;
91 # if defined __ANDROID__
92 return last_component (p
);
94 return p
&& p
[0] ? p
: "?";
97 /* Idea by Bastien ROUCARIÈS,
98 https://lists.gnu.org/r/bug-gnulib/2010-12/msg00095.html
99 Reference: https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.basetrf1/getprocs.htm
102 static int first
= 1;
106 pid_t pid
= getpid ();
107 struct procentry64 procs
;
108 p
= (0 < getprocs64 (&procs
, sizeof procs
, NULL
, 0, &pid
, 1)
109 ? strdup (procs
.pi_comm
)
115 # elif defined __hpux
117 static int first
= 1;
121 pid_t pid
= getpid ();
122 struct pst_status status
;
123 if (pstat_getproc (&status
, sizeof status
, 0, pid
) > 0)
125 char *ucomm
= status
.pst_ucomm
;
126 char *cmd
= status
.pst_cmd
;
127 if (strlen (ucomm
) < PST_UCOMMLEN
- 1)
131 /* ucomm is truncated to length PST_UCOMMLEN - 1.
132 Look at cmd instead. */
133 char *space
= strchr (cmd
, ' ');
136 p
= strrchr (cmd
, '/');
141 if (strlen (p
) > PST_UCOMMLEN
- 1
142 && memcmp (p
, ucomm
, PST_UCOMMLEN
- 1) == 0)
143 /* p is less truncated than ucomm. */
152 # if !defined __LP64__
153 /* Support for 32-bit programs running in 64-bit HP-UX.
154 The documented way to do this is to use the same source code
155 as above, but in a compilation unit where '#define _PSTAT64 1'
156 is in effect. I prefer a single compilation unit; the struct
157 size and the offsets are not going to change. */
159 if (__pstat_getproc64 (status64
, sizeof status64
, 0, pid
) > 0)
161 char *ucomm
= status64
+ 288;
162 char *cmd
= status64
+ 168;
163 if (strlen (ucomm
) < PST_UCOMMLEN
- 1)
167 /* ucomm is truncated to length PST_UCOMMLEN - 1.
168 Look at cmd instead. */
169 char *space
= strchr (cmd
, ' ');
172 p
= strrchr (cmd
, '/');
177 if (strlen (p
) > PST_UCOMMLEN
- 1
178 && memcmp (p
, ucomm
, PST_UCOMMLEN
- 1) == 0)
179 /* p is less truncated than ucomm. */
194 # elif __MVS__ /* z/OS */
195 /* https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwgetp.htm */
196 static char *p
= "?";
197 static int first
= 1;
200 pid_t pid
= getpid ();
204 memset (&buf
, 0, sizeof(buf
));
205 buf
.ps_cmdptr
= (char *) malloc (buf
.ps_cmdlen
= PS_CMDBLEN_LONG
);
206 buf
.ps_conttyptr
= (char *) malloc (buf
.ps_conttylen
= PS_CONTTYBLEN
);
207 buf
.ps_pathptr
= (char *) malloc (buf
.ps_pathlen
= PS_PATHBLEN
);
208 if (buf
.ps_cmdptr
&& buf
.ps_conttyptr
&& buf
.ps_pathptr
)
210 for (token
= 0; token
>= 0;
211 token
= w_getpsent (token
, &buf
, sizeof(buf
)))
213 if (token
> 0 && buf
.ps_pid
== pid
)
215 char *s
= strdup (last_component (buf
.ps_pathptr
));
222 free (buf
.ps_cmdptr
);
223 free (buf
.ps_conttyptr
);
224 free (buf
.ps_pathptr
);
227 # elif defined __sgi /* IRIX */
231 sprintf (filename
, "/proc/pinfo/%d", (int) getpid ());
232 fd
= open (filename
, O_RDONLY
| O_CLOEXEC
);
236 int ioctl_ok
= 0 <= ioctl (fd
, PIOCPSINFO
, &buf
);
240 char *name
= buf
.pr_fname
;
241 size_t namesize
= sizeof buf
.pr_fname
;
242 /* It may not be NUL-terminated. */
243 char *namenul
= memchr (name
, '\0', namesize
);
244 size_t namelen
= namenul
? namenul
- name
: namesize
;
245 char *namecopy
= malloc (namelen
+ 1);
248 namecopy
[namelen
] = '\0';
249 return memcpy (namecopy
, name
, namelen
);
254 # elif defined __SCO_VERSION__ || defined __sysv5__ /* SCO OpenServer6/UnixWare */
257 sprintf (buf
, "/proc/%d/cmdline", getpid());
258 fd
= open (buf
, O_RDONLY
);
261 size_t n
= read (fd
, buf
, 79);
264 buf
[n
] = '\0'; /* Guarantee null-termination */
266 progname
= strrchr (buf
, '/');
269 progname
= progname
+ 1; /* Skip the '/' */
276 ret
= malloc (strlen (progname
) + 1);
279 strcpy (ret
, progname
);
287 # error "getprogname module not ported to this OS"