e36c64709bb351c98355d2355403b807a7d5c8a2
[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                 close(s->fds[i]);
72                 s->fds[i] = -1;
73         }
74 }
75
76 void
77 custom_env_init_from_environ(struct custom_env *env)
78 {
79         char **it;
80         char **ep;
81
82         wl_array_init(&env->envp);
83         env->env_finalized = false;
84         wl_array_init(&env->argp);
85         env->arg_finalized = false;
86
87         for (it = environ; *it; it++) {
88                 ep = wl_array_add(&env->envp, sizeof *ep);
89                 assert(ep);
90                 *ep = strdup(*it);
91                 assert(*ep);
92         }
93 }
94
95 void
96 custom_env_fini(struct custom_env *env)
97 {
98         char **p;
99
100         wl_array_for_each(p, &env->envp)
101                 free(*p);
102         wl_array_release(&env->envp);
103
104         wl_array_for_each(p, &env->argp)
105                 free(*p);
106         wl_array_release(&env->argp);
107 }
108
109 static char **
110 custom_env_get_env_var(struct custom_env *env, const char *name)
111 {
112         char **ep;
113         size_t name_len = strlen(name);
114
115         wl_array_for_each(ep, &env->envp) {
116                 char *entry = *ep;
117
118                 if (strncmp(entry, name, name_len) == 0 &&
119                     entry[name_len] == '=') {
120                         return ep;
121                 }
122         }
123
124         return NULL;
125 }
126
127 void
128 custom_env_add_arg(struct custom_env *env, const char *arg)
129 {
130         char **ap;
131
132         assert(!env->arg_finalized);
133
134         ap = wl_array_add(&env->argp, sizeof *ap);
135         assert(ap);
136
137         *ap = strdup(arg);
138         assert(*ap);
139 }
140
141 void
142 custom_env_set_env_var(struct custom_env *env, const char *name, const char *value)
143 {
144         char **ep;
145
146         assert(strchr(name, '=') == NULL);
147         assert(!env->env_finalized);
148
149         ep = custom_env_get_env_var(env, name);
150         if (ep)
151                 free(*ep);
152         else
153                 ep = wl_array_add(&env->envp, sizeof *ep);
154         assert(ep);
155
156         str_printf(ep, "%s=%s", name, value);
157         assert(*ep);
158 }
159
160 /**
161  * Add information from a parsed exec string to a custom_env
162  *
163  * An 'exec string' is a string in the format:
164  *   ENVFOO=bar ENVBAR=baz /path/to/exec --arg anotherarg
165  *
166  * This function will parse such a string and add the specified environment
167  * variables (in the format KEY=value) up until it sees a non-environment
168  * string, after which point every entry will be interpreted as a new
169  * argument.
170  *
171  * Entries are space-separated; there is no support for quoting.
172  */
173 void
174 custom_env_add_from_exec_string(struct custom_env *env, const char *exec_str)
175 {
176         char *dup_path = strdup(exec_str);
177         char *start = dup_path;
178
179         assert(dup_path);
180
181         /* Build the environment array (if any) by handling any number of
182          * equal-separated key=value at the start of the string, split by
183          * spaces; uses "foo=bar  baz=quux meh argh" as the example, where
184          * "foo=bar" and "baz=quux" should go into the environment, and
185          * "meh" should be executed with "argh" as its first argument */
186         while (*start) {
187                 char *k = NULL, *v = NULL;
188                 char *p;
189
190                 /* Leaves us with "foo\0bar  baz=quux meh argh", with k pointing
191                  * to "foo" and v pointing to "bar  baz=quux meh argh" */
192                 for (p = start; *p && !isspace(*p); p++) {
193                         if (*p == '=') {
194                                 *p++ = '\0';
195                                 k = start;
196                                 v = p;
197                                 break;
198                         }
199                 }
200
201                 if (!v)
202                         break;
203
204                 /* Walk to the next space or NUL, filling any trailing spaces
205                  * with NUL, to give us "foo\0bar\0\0baz=quux meh argh".
206                  * k will point to "foo", v will point to "bar", and
207                  * start will point to "baz=quux meh argh". */
208                 while (*p && !isspace(*p))
209                         p++;
210                 while (*p && isspace(*p))
211                         *p++ = '\0';
212                 start = p;
213
214                 custom_env_set_env_var(env, k, v);
215         }
216
217         /* Now build the argv array by splitting on spaces */
218         while (*start) {
219                 char *p;
220                 bool valid = false;
221
222                 for (p = start; *p && !isspace(*p); p++)
223                         valid = true;
224
225                 if (!valid)
226                         break;
227
228                 while (*p && isspace(*p))
229                         *p++ = '\0';
230
231                 custom_env_add_arg(env, start);
232                 start = p;
233         }
234
235         free(dup_path);
236 }
237
238 char *const *
239 custom_env_get_envp(struct custom_env *env)
240 {
241         char **ep;
242
243         assert(!env->env_finalized);
244
245         /* add terminating NULL */
246         ep = wl_array_add(&env->envp, sizeof *ep);
247         assert(ep);
248         *ep = NULL;
249
250         env->env_finalized = true;
251
252         return env->envp.data;
253 }
254
255 char *const *
256 custom_env_get_argp(struct custom_env *env)
257 {
258         char **ap;
259
260         assert(!env->arg_finalized);
261
262         /* add terminating NULL */
263         ap = wl_array_add(&env->argp, sizeof *ap);
264         assert(ap);
265         *ap = NULL;
266
267         env->arg_finalized = true;
268
269         return env->argp.data;
270 }