2 * Copyright 2022 Collabora, Ltd.
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:
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.
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
36 #include <wayland-util.h>
39 #include "os-compatibility.h"
40 #include "process-util.h"
41 #include "string-helpers.h"
43 extern char **environ; /* defined by libc */
46 fdstr_update_str1(struct fdstr *s)
48 snprintf(s->str1, sizeof(s->str1), "%d", s->fds[1]);
52 fdstr_set_fd1(struct fdstr *s, int fd)
60 fdstr_clear_cloexec_fd1(struct fdstr *s)
62 return os_fd_clear_cloexec(s->fds[1]) >= 0;
66 fdstr_close_all(struct fdstr *s)
70 for (i = 0; i < ARRAY_LENGTH(s->fds); i++) {
77 custom_env_init_from_environ(struct custom_env *env)
82 wl_array_init(&env->envp);
83 env->env_finalized = false;
84 wl_array_init(&env->argp);
85 env->arg_finalized = false;
87 for (it = environ; *it; it++) {
88 ep = wl_array_add(&env->envp, sizeof *ep);
96 custom_env_fini(struct custom_env *env)
100 wl_array_for_each(p, &env->envp)
102 wl_array_release(&env->envp);
104 wl_array_for_each(p, &env->argp)
106 wl_array_release(&env->argp);
110 custom_env_get_env_var(struct custom_env *env, const char *name)
113 size_t name_len = strlen(name);
115 wl_array_for_each(ep, &env->envp) {
118 if (strncmp(entry, name, name_len) == 0 &&
119 entry[name_len] == '=') {
128 custom_env_add_arg(struct custom_env *env, const char *arg)
132 assert(!env->arg_finalized);
134 ap = wl_array_add(&env->argp, sizeof *ap);
142 custom_env_set_env_var(struct custom_env *env, const char *name, const char *value)
146 assert(strchr(name, '=') == NULL);
147 assert(!env->env_finalized);
149 ep = custom_env_get_env_var(env, name);
153 ep = wl_array_add(&env->envp, sizeof *ep);
156 str_printf(ep, "%s=%s", name, value);
161 * Add information from a parsed exec string to a custom_env
163 * An 'exec string' is a string in the format:
164 * ENVFOO=bar ENVBAR=baz /path/to/exec --arg anotherarg
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
171 * Entries are space-separated; there is no support for quoting.
174 custom_env_add_from_exec_string(struct custom_env *env, const char *exec_str)
176 char *dup_path = strdup(exec_str);
177 char *start = dup_path;
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 */
187 char *k = NULL, *v = NULL;
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++) {
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))
210 while (*p && isspace(*p))
214 custom_env_set_env_var(env, k, v);
217 /* Now build the argv array by splitting on spaces */
222 for (p = start; *p && !isspace(*p); p++)
228 while (*p && isspace(*p))
231 custom_env_add_arg(env, start);
239 custom_env_get_envp(struct custom_env *env)
243 assert(!env->env_finalized);
245 /* add terminating NULL */
246 ep = wl_array_add(&env->envp, sizeof *ep);
250 env->env_finalized = true;
252 return env->envp.data;
256 custom_env_get_argp(struct custom_env *env)
260 assert(!env->arg_finalized);
262 /* add terminating NULL */
263 ap = wl_array_add(&env->argp, sizeof *ap);
267 env->arg_finalized = true;
269 return env->argp.data;