compositor: Use stdint for specifing integer storage
[src/agl-compositor.git] / shared / process-util.c
1 /*
2  * Copyright 2022 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include <wayland-util.h>
37
38 #include "helpers.h"
39 #include "os-compatibility.h"
40 #include "process-util.h"
41 #include "string-helpers.h"
42
43 extern char **environ; /* defined by libc */
44
45 void
46 fdstr_update_str1(struct fdstr *s)
47 {
48         snprintf(s->str1, sizeof(s->str1), "%d", s->fds[1]);
49 }
50
51 void
52 fdstr_set_fd1(struct fdstr *s, int fd)
53 {
54         s->fds[0] = -1;
55         s->fds[1] = fd;
56         fdstr_update_str1(s);
57 }
58
59 bool
60 fdstr_clear_cloexec_fd1(struct fdstr *s)
61 {
62         return os_fd_clear_cloexec(s->fds[1]) >= 0;
63 }
64
65 void
66 fdstr_close_all(struct fdstr *s)
67 {
68         unsigned i;
69
70         for (i = 0; i < ARRAY_LENGTH(s->fds); i++) {
71                 if (s->fds[i] >= 0)
72                         close(s->fds[i]);
73                 s->fds[i] = -1;
74         }
75 }
76
77 void
78 custom_env_init_from_environ(struct custom_env *env)
79 {
80         char **it;
81         char **ep;
82
83         wl_array_init(&env->envp);
84         env->env_finalized = false;
85         wl_array_init(&env->argp);
86         env->arg_finalized = false;
87
88         for (it = environ; *it; it++) {
89                 ep = wl_array_add(&env->envp, sizeof *ep);
90                 assert(ep);
91                 *ep = strdup(*it);
92                 assert(*ep);
93         }
94 }
95
96 void
97 custom_env_fini(struct custom_env *env)
98 {
99         char **p;
100
101         wl_array_for_each(p, &env->envp)
102                 free(*p);
103         wl_array_release(&env->envp);
104
105         wl_array_for_each(p, &env->argp)
106                 free(*p);
107         wl_array_release(&env->argp);
108 }
109
110 static char **
111 custom_env_get_env_var(struct custom_env *env, const char *name)
112 {
113         char **ep;
114         size_t name_len = strlen(name);
115
116         wl_array_for_each(ep, &env->envp) {
117                 char *entry = *ep;
118
119                 if (strncmp(entry, name, name_len) == 0 &&
120                     entry[name_len] == '=') {
121                         return ep;
122                 }
123         }
124
125         return NULL;
126 }
127
128 void
129 custom_env_add_arg(struct custom_env *env, const char *arg)
130 {
131         char **ap;
132
133         assert(!env->arg_finalized);
134
135         ap = wl_array_add(&env->argp, sizeof *ap);
136         assert(ap);
137
138         *ap = strdup(arg);
139         assert(*ap);
140 }
141
142 void
143 custom_env_set_env_var(struct custom_env *env, const char *name, const char *value)
144 {
145         char **ep;
146
147         assert(strchr(name, '=') == NULL);
148         assert(!env->env_finalized);
149
150         ep = custom_env_get_env_var(env, name);
151         if (ep)
152                 free(*ep);
153         else
154                 ep = wl_array_add(&env->envp, sizeof *ep);
155         assert(ep);
156
157         str_printf(ep, "%s=%s", name, value);
158         assert(*ep);
159 }
160
161 /**
162  * Add information from a parsed exec string to a custom_env
163  *
164  * An 'exec string' is a string in the format:
165  *   ENVFOO=bar ENVBAR=baz /path/to/exec --arg anotherarg
166  *
167  * This function will parse such a string and add the specified environment
168  * variables (in the format KEY=value) up until it sees a non-environment
169  * string, after which point every entry will be interpreted as a new
170  * argument.
171  *
172  * Entries are space-separated; there is no support for quoting.
173  */
174 void
175 custom_env_add_from_exec_string(struct custom_env *env, const char *exec_str)
176 {
177         char *dup_path = strdup(exec_str);
178         char *start = dup_path;
179
180         assert(dup_path);
181
182         /* Build the environment array (if any) by handling any number of
183          * equal-separated key=value at the start of the string, split by
184          * spaces; uses "foo=bar  baz=quux meh argh" as the example, where
185          * "foo=bar" and "baz=quux" should go into the environment, and
186          * "meh" should be executed with "argh" as its first argument */
187         while (*start) {
188                 char *k = NULL, *v = NULL;
189                 char *p;
190
191                 /* Leaves us with "foo\0bar  baz=quux meh argh", with k pointing
192                  * to "foo" and v pointing to "bar  baz=quux meh argh" */
193                 for (p = start; *p && !isspace(*p); p++) {
194                         if (*p == '=') {
195                                 *p++ = '\0';
196                                 k = start;
197                                 v = p;
198                                 break;
199                         }
200                 }
201
202                 if (!v)
203                         break;
204
205                 /* Walk to the next space or NUL, filling any trailing spaces
206                  * with NUL, to give us "foo\0bar\0\0baz=quux meh argh".
207                  * k will point to "foo", v will point to "bar", and
208                  * start will point to "baz=quux meh argh". */
209                 while (*p && !isspace(*p))
210                         p++;
211                 while (*p && isspace(*p))
212                         *p++ = '\0';
213                 start = p;
214
215                 custom_env_set_env_var(env, k, v);
216         }
217
218         /* Now build the argv array by splitting on spaces */
219         while (*start) {
220                 char *p;
221                 bool valid = false;
222
223                 for (p = start; *p && !isspace(*p); p++)
224                         valid = true;
225
226                 if (!valid)
227                         break;
228
229                 while (*p && isspace(*p))
230                         *p++ = '\0';
231
232                 custom_env_add_arg(env, start);
233                 start = p;
234         }
235
236         free(dup_path);
237 }
238
239 char *const *
240 custom_env_get_envp(struct custom_env *env)
241 {
242         char **ep;
243
244         assert(!env->env_finalized);
245
246         /* add terminating NULL */
247         ep = wl_array_add(&env->envp, sizeof *ep);
248         assert(ep);
249         *ep = NULL;
250
251         env->env_finalized = true;
252
253         return env->envp.data;
254 }
255
256 char *const *
257 custom_env_get_argp(struct custom_env *env)
258 {
259         char **ap;
260
261         assert(!env->arg_finalized);
262
263         /* add terminating NULL */
264         ap = wl_array_add(&env->argp, sizeof *ap);
265         assert(ap);
266         *ap = NULL;
267
268         env->arg_finalized = true;
269
270         return env->argp.data;
271 }