Remove uncompiled code from src/window.c
[wmaker-crm.git] / src / osdep_bsd.c
blobe3063a97b4ff2bee80633b4234b63b02656618b0
2 #if defined( FREEBSD ) || defined( DRAGONFLYBSD )
3 # include <sys/types.h>
4 #else /* OPENBSD || NETBSD */
5 # include <sys/param.h>
6 #endif
7 #include <sys/sysctl.h>
9 #include <assert.h>
11 #if defined( OPENBSD )
12 # include <kvm.h>
13 # include <limits.h> /* _POSIX2_LINE_MAX */
14 #endif
16 #include <stdio.h>
17 #include <stdlib.h>
19 #if defined( OPENBSD )
20 # include <string.h>
21 #endif
23 #include <unistd.h>
25 #include <WINGs/WUtil.h>
27 #include "wconfig.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
45 * identical.
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.
60 int j, mib[4];
61 unsigned int i;
62 size_t count;
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() */
68 kvm_t *kd;
69 struct kinfo_proc *kp;
70 char **nargv; /* kvm_getarg() */
71 #endif
73 *argv = NULL;
74 *argc = 0;
76 /* the system-wide limit */
77 if (argmax == 0) { /* it hopefully doesn't change at runtime *g* */
78 mib[0] = CTL_KERN;
79 mib[1] = KERN_ARGMAX;
80 mib[2] = 0;
81 mib[4] = 0;
83 count = sizeof(argmax);
84 if (sysctl(mib, 2, &argmax, &count, NULL, 0) == -1)
85 return False;
88 /* if argmax is still 0, something went very seriously wrong */
89 assert( argmax > 0);
91 /* space for args; no need to free before returning even on errors */
92 if (args == NULL)
93 args = (char *)wmalloc(argmax);
95 #if defined( OPENBSD )
96 /* kvm descriptor */
97 if ((kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, kvmerr)) == NULL)
98 return False;
100 procs = 0;
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 */
104 return False;
106 /* get it's argv */
107 if ((nargv = kvm_getargv(kd, kp, 0)) == NULL)
108 return False;
110 /* flatten nargv into args */
111 count = 0;
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;
124 (*argc)++;
126 #undef ARGSPACE
127 /* by now *argc is correct as a byproduct */
129 kvm_close(kd);
130 #else /* FREEBSD || NETBSD || DRAGONFLYBSD */
132 mib[0] = CTL_KERN;
133 #if defined( NETBSD )
134 mib[1] = KERN_PROC_ARGS;
135 mib[2] = pid;
136 mib[3] = KERN_PROC_ARGV;
137 #elif defined( FREEBSD ) || defined( DRAGONFLYBSD )
138 mib[1] = KERN_PROC;
139 mib[2] = KERN_PROC_ARGS;
140 mib[3] = pid;
141 #endif
143 count = argmax;
144 /* canary */
145 *args = 0;
146 if (sysctl(mib, 4, args, &count, NULL, 0) == -1 || *args == 0)
147 return False;
149 /* args is a flattened series of null-terminated strings */
150 for (i = 0; i < count; i++)
151 if (args[i] == '\0')
152 (*argc)++;
153 #endif
155 *argv = (char **)wmalloc(sizeof(char *) * (*argc + 1 /* term. null ptr */));
156 (*argv)[0] = args;
158 /* go through args, set argv[$next] to the beginning of each string */
159 for (i = 0, j = 1; i < count; i++) {
160 if (args[i] != '\0')
161 continue;
162 if (i < count - 1)
163 (*argv)[j++] = &args[i + 1];
164 if (j == *argc)
165 break;
168 /* the list of arguments must be terminated by a null pointer */
169 (*argv)[j] = NULL;
170 return True;