2 #if defined( FREEBSD ) || defined( DRAGONFLYBSD )
3 # include <sys/types.h>
4 #else /* OPENBSD || NETBSD */
5 # include <sys/param.h>
7 #include <sys/sysctl.h>
11 #if defined( OPENBSD )
13 # include <limits.h> /* _POSIX2_LINE_MAX */
19 #if defined( OPENBSD )
25 #include <WINGs/WUtil.h>
30 * copy argc and argv for an existing process identified by `pid'
31 * into suitable storage given in ***argv and *argc.
33 * subsequent calls use the same static area for argv and argc.
35 * returns 0 for failure, in which case argc := 0 and argv := NULL
36 * returns 1 for success
39 * NetBSD, FreeBSD and DragonFlyBSD supply the necessary information via
40 * sysctl(3). The result is a plain simple flat octet stream and its length.
41 * The octet stream represents argv, with members separated by a null character.
42 * The argv array returned by GetCommandForPid() consists of pointers into this
43 * stream (which is stored in a static array, `args'). Net and Free/DFly only
44 * differ slightly in the MIB vector given to sysctl(3). Free and DFly are
47 * OpenBSD supplies the necessary informationvia kvm(3) in the form of a real
48 * argv array. This array is flattened to be in the same way as Net/Free/DFly
49 * returns the arguments in the first place. This is done in order for the
50 * storage (`args') to easily be made static, which means some memory bytes
51 * are sacrificed to save hoops of memory management.
53 Bool
GetCommandForPid(int pid
, char ***argv
, int *argc
)
56 * it just returns failure if the sysctl calls fail; since there's
57 * apparently no harm done to the caller because of this, it seems
58 * more user-friendly than to bomb out.
63 static char *args
= NULL
;
64 static int argmax
= 0;
65 #if defined( OPENBSD )
66 char kvmerr
[_POSIX2_LINE_MAX
]; /* for kvm*() error reporting */
67 int procs
; /* kvm_getprocs() */
69 struct kinfo_proc
*kp
;
70 char **nargv
; /* kvm_getarg() */
76 /* the system-wide limit */
77 if (argmax
== 0) { /* it hopefully doesn't change at runtime *g* */
83 count
= sizeof(argmax
);
84 if (sysctl(mib
, 2, &argmax
, &count
, NULL
, 0) == -1)
88 /* if argmax is still 0, something went very seriously wrong */
91 /* space for args; no need to free before returning even on errors */
93 args
= (char *)wmalloc(argmax
);
95 #if defined( OPENBSD )
97 if ((kd
= kvm_openfiles(NULL
, NULL
, NULL
, KVM_NO_FILES
, kvmerr
)) == NULL
)
101 /* the process we are interested in */
102 if ((kp
= kvm_getprocs(kd
, KERN_PROC_PID
, pid
, &procs
)) == NULL
|| procs
== 0)
103 /* if kvm_getprocs() bombs out or does not find the process */
107 if ((nargv
= kvm_getargv(kd
, kp
, 0)) == NULL
)
110 /* flatten nargv into args */
112 memset(args
, 0, argmax
);
114 * must have this much free space in `args' in order for the current
115 * iteration not to overflow it: we are at `count', and will append
116 * the next ((*argc)+1) arg and a null (+1)
117 * technically, overflow (or truncation, which isn't handled) can not
118 * happen (should not, at least).
120 #define ARGSPACE ( count + strlen(nargv[ (*argc) + 1 ] ) + 1 )
121 while (nargv
[*argc
] && ARGSPACE
< argmax
) {
122 memcpy(args
+ count
, nargv
[*argc
], strlen(nargv
[*argc
]));
123 count
+= strlen(nargv
[*argc
]) + 1;
127 /* by now *argc is correct as a byproduct */
130 #else /* FREEBSD || NETBSD || DRAGONFLYBSD */
133 #if defined( NETBSD )
134 mib
[1] = KERN_PROC_ARGS
;
136 mib
[3] = KERN_PROC_ARGV
;
137 #elif defined( FREEBSD ) || defined( DRAGONFLYBSD )
139 mib
[2] = KERN_PROC_ARGS
;
146 if (sysctl(mib
, 4, args
, &count
, NULL
, 0) == -1 || *args
== 0)
149 /* args is a flattened series of null-terminated strings */
150 for (i
= 0; i
< count
; i
++)
155 *argv
= (char **)wmalloc(sizeof(char *) * (*argc
+ 1 /* term. null ptr */));
158 /* go through args, set argv[$next] to the beginning of each string */
159 for (i
= 0, j
= 1; i
< count
; i
++) {
163 (*argv
)[j
++] = &args
[i
+ 1];
168 /* the list of arguments must be terminated by a null pointer */