1*09d4459fSDaniel Fojt /* Program name management.
2*09d4459fSDaniel Fojt Copyright (C) 2016-2020 Free Software Foundation, Inc.
3*09d4459fSDaniel Fojt
4*09d4459fSDaniel Fojt This program is free software: you can redistribute it and/or modify
5*09d4459fSDaniel Fojt it under the terms of the GNU General Public License as published by
6*09d4459fSDaniel Fojt the Free Software Foundation; either version 3 of the License, or
7*09d4459fSDaniel Fojt (at your option) any later version.
8*09d4459fSDaniel Fojt
9*09d4459fSDaniel Fojt This program is distributed in the hope that it will be useful,
10*09d4459fSDaniel Fojt but WITHOUT ANY WARRANTY; without even the implied warranty of
11*09d4459fSDaniel Fojt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*09d4459fSDaniel Fojt GNU General Public License for more details.
13*09d4459fSDaniel Fojt
14*09d4459fSDaniel Fojt You should have received a copy of the GNU General Public License
15*09d4459fSDaniel Fojt along with this program. If not, see <https://www.gnu.org/licenses/>. */
16*09d4459fSDaniel Fojt
17*09d4459fSDaniel Fojt #include <config.h>
18*09d4459fSDaniel Fojt
19*09d4459fSDaniel Fojt /* Specification. */
20*09d4459fSDaniel Fojt #include "getprogname.h"
21*09d4459fSDaniel Fojt
22*09d4459fSDaniel Fojt #include <errno.h> /* get program_invocation_name declaration */
23*09d4459fSDaniel Fojt #include <stdlib.h> /* get __argv declaration */
24*09d4459fSDaniel Fojt
25*09d4459fSDaniel Fojt #ifdef _AIX
26*09d4459fSDaniel Fojt # include <unistd.h>
27*09d4459fSDaniel Fojt # include <procinfo.h>
28*09d4459fSDaniel Fojt # include <string.h>
29*09d4459fSDaniel Fojt #endif
30*09d4459fSDaniel Fojt
31*09d4459fSDaniel Fojt #ifdef __MVS__
32*09d4459fSDaniel Fojt # ifndef _OPEN_SYS
33*09d4459fSDaniel Fojt # define _OPEN_SYS
34*09d4459fSDaniel Fojt # endif
35*09d4459fSDaniel Fojt # include <string.h>
36*09d4459fSDaniel Fojt # include <sys/ps.h>
37*09d4459fSDaniel Fojt #endif
38*09d4459fSDaniel Fojt
39*09d4459fSDaniel Fojt #ifdef __hpux
40*09d4459fSDaniel Fojt # include <unistd.h>
41*09d4459fSDaniel Fojt # include <sys/param.h>
42*09d4459fSDaniel Fojt # include <sys/pstat.h>
43*09d4459fSDaniel Fojt # include <string.h>
44*09d4459fSDaniel Fojt #endif
45*09d4459fSDaniel Fojt
46*09d4459fSDaniel Fojt #ifdef __sgi
47*09d4459fSDaniel Fojt # include <string.h>
48*09d4459fSDaniel Fojt # include <unistd.h>
49*09d4459fSDaniel Fojt # include <stdio.h>
50*09d4459fSDaniel Fojt # include <fcntl.h>
51*09d4459fSDaniel Fojt # include <sys/procfs.h>
52*09d4459fSDaniel Fojt #endif
53*09d4459fSDaniel Fojt
54*09d4459fSDaniel Fojt #include "dirname.h"
55*09d4459fSDaniel Fojt
56*09d4459fSDaniel Fojt #ifndef HAVE_GETPROGNAME /* not Mac OS X, FreeBSD, NetBSD, OpenBSD >= 5.4, Cygwin */
57*09d4459fSDaniel Fojt char const *
getprogname(void)58*09d4459fSDaniel Fojt getprogname (void)
59*09d4459fSDaniel Fojt {
60*09d4459fSDaniel Fojt # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME /* glibc, BeOS */
61*09d4459fSDaniel Fojt /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
62*09d4459fSDaniel Fojt return program_invocation_short_name;
63*09d4459fSDaniel Fojt # elif HAVE_DECL_PROGRAM_INVOCATION_NAME /* glibc, BeOS */
64*09d4459fSDaniel Fojt /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
65*09d4459fSDaniel Fojt return last_component (program_invocation_name);
66*09d4459fSDaniel Fojt # elif HAVE_GETEXECNAME /* Solaris */
67*09d4459fSDaniel Fojt /* https://docs.oracle.com/cd/E19253-01/816-5168/6mbb3hrb1/index.html */
68*09d4459fSDaniel Fojt const char *p = getexecname ();
69*09d4459fSDaniel Fojt if (!p)
70*09d4459fSDaniel Fojt p = "?";
71*09d4459fSDaniel Fojt return last_component (p);
72*09d4459fSDaniel Fojt # elif HAVE_DECL___ARGV /* mingw, MSVC */
73*09d4459fSDaniel Fojt /* https://docs.microsoft.com/en-us/cpp/c-runtime-library/argc-argv-wargv */
74*09d4459fSDaniel Fojt const char *p = __argv && __argv[0] ? __argv[0] : "?";
75*09d4459fSDaniel Fojt return last_component (p);
76*09d4459fSDaniel Fojt # elif HAVE_VAR___PROGNAME /* OpenBSD, Android, QNX */
77*09d4459fSDaniel Fojt /* https://man.openbsd.org/style.9 */
78*09d4459fSDaniel Fojt /* http://www.qnx.de/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.neutrino_lib_ref%2Fp%2F__progname.html */
79*09d4459fSDaniel Fojt /* Be careful to declare this only when we absolutely need it
80*09d4459fSDaniel Fojt (OpenBSD 5.1), rather than when it's available. Otherwise,
81*09d4459fSDaniel Fojt its mere declaration makes program_invocation_short_name
82*09d4459fSDaniel Fojt malfunction (have zero length) with Fedora 25's glibc. */
83*09d4459fSDaniel Fojt extern char *__progname;
84*09d4459fSDaniel Fojt const char *p = __progname;
85*09d4459fSDaniel Fojt # if defined __ANDROID__
86*09d4459fSDaniel Fojt return last_component (p);
87*09d4459fSDaniel Fojt # else
88*09d4459fSDaniel Fojt return p && p[0] ? p : "?";
89*09d4459fSDaniel Fojt # endif
90*09d4459fSDaniel Fojt # elif _AIX /* AIX */
91*09d4459fSDaniel Fojt /* Idea by Bastien ROUCARIÈS,
92*09d4459fSDaniel Fojt https://lists.gnu.org/r/bug-gnulib/2010-12/msg00095.html
93*09d4459fSDaniel Fojt Reference: https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.basetrf1/getprocs.htm
94*09d4459fSDaniel Fojt */
95*09d4459fSDaniel Fojt static char *p;
96*09d4459fSDaniel Fojt static int first = 1;
97*09d4459fSDaniel Fojt if (first)
98*09d4459fSDaniel Fojt {
99*09d4459fSDaniel Fojt first = 0;
100*09d4459fSDaniel Fojt pid_t pid = getpid ();
101*09d4459fSDaniel Fojt struct procentry64 procs;
102*09d4459fSDaniel Fojt p = (0 < getprocs64 (&procs, sizeof procs, NULL, 0, &pid, 1)
103*09d4459fSDaniel Fojt ? strdup (procs.pi_comm)
104*09d4459fSDaniel Fojt : NULL);
105*09d4459fSDaniel Fojt if (!p)
106*09d4459fSDaniel Fojt p = "?";
107*09d4459fSDaniel Fojt }
108*09d4459fSDaniel Fojt return p;
109*09d4459fSDaniel Fojt # elif defined __hpux
110*09d4459fSDaniel Fojt static char *p;
111*09d4459fSDaniel Fojt static int first = 1;
112*09d4459fSDaniel Fojt if (first)
113*09d4459fSDaniel Fojt {
114*09d4459fSDaniel Fojt first = 0;
115*09d4459fSDaniel Fojt pid_t pid = getpid ();
116*09d4459fSDaniel Fojt struct pst_status status;
117*09d4459fSDaniel Fojt if (pstat_getproc (&status, sizeof status, 0, pid) > 0)
118*09d4459fSDaniel Fojt {
119*09d4459fSDaniel Fojt char *ucomm = status.pst_ucomm;
120*09d4459fSDaniel Fojt char *cmd = status.pst_cmd;
121*09d4459fSDaniel Fojt if (strlen (ucomm) < PST_UCOMMLEN - 1)
122*09d4459fSDaniel Fojt p = ucomm;
123*09d4459fSDaniel Fojt else
124*09d4459fSDaniel Fojt {
125*09d4459fSDaniel Fojt /* ucomm is truncated to length PST_UCOMMLEN - 1.
126*09d4459fSDaniel Fojt Look at cmd instead. */
127*09d4459fSDaniel Fojt char *space = strchr (cmd, ' ');
128*09d4459fSDaniel Fojt if (space != NULL)
129*09d4459fSDaniel Fojt *space = '\0';
130*09d4459fSDaniel Fojt p = strrchr (cmd, '/');
131*09d4459fSDaniel Fojt if (p != NULL)
132*09d4459fSDaniel Fojt p++;
133*09d4459fSDaniel Fojt else
134*09d4459fSDaniel Fojt p = cmd;
135*09d4459fSDaniel Fojt if (strlen (p) > PST_UCOMMLEN - 1
136*09d4459fSDaniel Fojt && memcmp (p, ucomm, PST_UCOMMLEN - 1) == 0)
137*09d4459fSDaniel Fojt /* p is less truncated than ucomm. */
138*09d4459fSDaniel Fojt ;
139*09d4459fSDaniel Fojt else
140*09d4459fSDaniel Fojt p = ucomm;
141*09d4459fSDaniel Fojt }
142*09d4459fSDaniel Fojt p = strdup (p);
143*09d4459fSDaniel Fojt }
144*09d4459fSDaniel Fojt else
145*09d4459fSDaniel Fojt {
146*09d4459fSDaniel Fojt # if !defined __LP64__
147*09d4459fSDaniel Fojt /* Support for 32-bit programs running in 64-bit HP-UX.
148*09d4459fSDaniel Fojt The documented way to do this is to use the same source code
149*09d4459fSDaniel Fojt as above, but in a compilation unit where '#define _PSTAT64 1'
150*09d4459fSDaniel Fojt is in effect. I prefer a single compilation unit; the struct
151*09d4459fSDaniel Fojt size and the offsets are not going to change. */
152*09d4459fSDaniel Fojt char status64[1216];
153*09d4459fSDaniel Fojt if (__pstat_getproc64 (status64, sizeof status64, 0, pid) > 0)
154*09d4459fSDaniel Fojt {
155*09d4459fSDaniel Fojt char *ucomm = status64 + 288;
156*09d4459fSDaniel Fojt char *cmd = status64 + 168;
157*09d4459fSDaniel Fojt if (strlen (ucomm) < PST_UCOMMLEN - 1)
158*09d4459fSDaniel Fojt p = ucomm;
159*09d4459fSDaniel Fojt else
160*09d4459fSDaniel Fojt {
161*09d4459fSDaniel Fojt /* ucomm is truncated to length PST_UCOMMLEN - 1.
162*09d4459fSDaniel Fojt Look at cmd instead. */
163*09d4459fSDaniel Fojt char *space = strchr (cmd, ' ');
164*09d4459fSDaniel Fojt if (space != NULL)
165*09d4459fSDaniel Fojt *space = '\0';
166*09d4459fSDaniel Fojt p = strrchr (cmd, '/');
167*09d4459fSDaniel Fojt if (p != NULL)
168*09d4459fSDaniel Fojt p++;
169*09d4459fSDaniel Fojt else
170*09d4459fSDaniel Fojt p = cmd;
171*09d4459fSDaniel Fojt if (strlen (p) > PST_UCOMMLEN - 1
172*09d4459fSDaniel Fojt && memcmp (p, ucomm, PST_UCOMMLEN - 1) == 0)
173*09d4459fSDaniel Fojt /* p is less truncated than ucomm. */
174*09d4459fSDaniel Fojt ;
175*09d4459fSDaniel Fojt else
176*09d4459fSDaniel Fojt p = ucomm;
177*09d4459fSDaniel Fojt }
178*09d4459fSDaniel Fojt p = strdup (p);
179*09d4459fSDaniel Fojt }
180*09d4459fSDaniel Fojt else
181*09d4459fSDaniel Fojt # endif
182*09d4459fSDaniel Fojt p = NULL;
183*09d4459fSDaniel Fojt }
184*09d4459fSDaniel Fojt if (!p)
185*09d4459fSDaniel Fojt p = "?";
186*09d4459fSDaniel Fojt }
187*09d4459fSDaniel Fojt return p;
188*09d4459fSDaniel Fojt # elif __MVS__ /* z/OS */
189*09d4459fSDaniel Fojt /* https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwgetp.htm */
190*09d4459fSDaniel Fojt static char *p = "?";
191*09d4459fSDaniel Fojt static int first = 1;
192*09d4459fSDaniel Fojt if (first)
193*09d4459fSDaniel Fojt {
194*09d4459fSDaniel Fojt pid_t pid = getpid ();
195*09d4459fSDaniel Fojt int token;
196*09d4459fSDaniel Fojt W_PSPROC buf;
197*09d4459fSDaniel Fojt first = 0;
198*09d4459fSDaniel Fojt memset (&buf, 0, sizeof(buf));
199*09d4459fSDaniel Fojt buf.ps_cmdptr = (char *) malloc (buf.ps_cmdlen = PS_CMDBLEN_LONG);
200*09d4459fSDaniel Fojt buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN);
201*09d4459fSDaniel Fojt buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN);
202*09d4459fSDaniel Fojt if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr)
203*09d4459fSDaniel Fojt {
204*09d4459fSDaniel Fojt for (token = 0; token >= 0;
205*09d4459fSDaniel Fojt token = w_getpsent (token, &buf, sizeof(buf)))
206*09d4459fSDaniel Fojt {
207*09d4459fSDaniel Fojt if (token > 0 && buf.ps_pid == pid)
208*09d4459fSDaniel Fojt {
209*09d4459fSDaniel Fojt char *s = strdup (last_component (buf.ps_pathptr));
210*09d4459fSDaniel Fojt if (s)
211*09d4459fSDaniel Fojt p = s;
212*09d4459fSDaniel Fojt break;
213*09d4459fSDaniel Fojt }
214*09d4459fSDaniel Fojt }
215*09d4459fSDaniel Fojt }
216*09d4459fSDaniel Fojt free (buf.ps_cmdptr);
217*09d4459fSDaniel Fojt free (buf.ps_conttyptr);
218*09d4459fSDaniel Fojt free (buf.ps_pathptr);
219*09d4459fSDaniel Fojt }
220*09d4459fSDaniel Fojt return p;
221*09d4459fSDaniel Fojt # elif defined __sgi /* IRIX */
222*09d4459fSDaniel Fojt char filename[50];
223*09d4459fSDaniel Fojt int fd;
224*09d4459fSDaniel Fojt
225*09d4459fSDaniel Fojt sprintf (filename, "/proc/pinfo/%d", (int) getpid ());
226*09d4459fSDaniel Fojt fd = open (filename, O_RDONLY);
227*09d4459fSDaniel Fojt if (0 <= fd)
228*09d4459fSDaniel Fojt {
229*09d4459fSDaniel Fojt prpsinfo_t buf;
230*09d4459fSDaniel Fojt int ioctl_ok = 0 <= ioctl (fd, PIOCPSINFO, &buf);
231*09d4459fSDaniel Fojt close (fd);
232*09d4459fSDaniel Fojt if (ioctl_ok)
233*09d4459fSDaniel Fojt {
234*09d4459fSDaniel Fojt char *name = buf.pr_fname;
235*09d4459fSDaniel Fojt size_t namesize = sizeof buf.pr_fname;
236*09d4459fSDaniel Fojt /* It may not be NUL-terminated. */
237*09d4459fSDaniel Fojt char *namenul = memchr (name, '\0', namesize);
238*09d4459fSDaniel Fojt size_t namelen = namenul ? namenul - name : namesize;
239*09d4459fSDaniel Fojt char *namecopy = malloc (namelen + 1);
240*09d4459fSDaniel Fojt if (namecopy)
241*09d4459fSDaniel Fojt {
242*09d4459fSDaniel Fojt namecopy[namelen] = '\0';
243*09d4459fSDaniel Fojt return memcpy (namecopy, name, namelen);
244*09d4459fSDaniel Fojt }
245*09d4459fSDaniel Fojt }
246*09d4459fSDaniel Fojt }
247*09d4459fSDaniel Fojt return NULL;
248*09d4459fSDaniel Fojt # else
249*09d4459fSDaniel Fojt # error "getprogname module not ported to this OS"
250*09d4459fSDaniel Fojt # endif
251*09d4459fSDaniel Fojt }
252*09d4459fSDaniel Fojt
253*09d4459fSDaniel Fojt #endif
254*09d4459fSDaniel Fojt
255*09d4459fSDaniel Fojt /*
256*09d4459fSDaniel Fojt * Hey Emacs!
257*09d4459fSDaniel Fojt * Local Variables:
258*09d4459fSDaniel Fojt * coding: utf-8
259*09d4459fSDaniel Fojt * End:
260*09d4459fSDaniel Fojt */
261