Skip to content
Commits on Source (14)
......@@ -41,22 +41,23 @@
#include <assert.h>
#include <wayland-client.h>
#include "window.h"
#include "shared/cairo-util.h"
#include <libweston/config-parser.h>
#include <libweston/zalloc.h>
#include "shared/helpers.h"
#include "shared/xalloc.h"
#include <libweston/zalloc.h>
#include "shared/cairo-util.h"
#include "shared/file-util.h"
#include "shared/process-util.h"
#include "shared/timespec-util.h"
#include "window.h"
#include "weston-desktop-shell-client-protocol.h"
#define DEFAULT_CLOCK_FORMAT CLOCK_FORMAT_MINUTES
#define DEFAULT_SPACING 10
extern char **environ; /* defined by libc */
enum clock_format {
CLOCK_FORMAT_MINUTES,
CLOCK_FORMAT_SECONDS,
......@@ -144,8 +145,9 @@ struct panel_launcher {
char *path;
char *displayname;
struct wl_list link;
struct wl_array envp;
struct wl_array argv;
struct custom_env env;
char * const *argp;
char * const *envp;
};
struct panel_clock {
......@@ -212,7 +214,6 @@ check_desktop_ready(struct window *window)
static void
panel_launcher_activate(struct panel_launcher *widget)
{
char **argv;
pid_t pid;
pid = fork();
......@@ -224,13 +225,11 @@ panel_launcher_activate(struct panel_launcher *widget)
if (pid)
return;
argv = widget->argv.data;
if (setsid() == -1)
exit(EXIT_FAILURE);
if (execve(argv[0], argv, widget->envp.data) < 0) {
fprintf(stderr, "execl '%s' failed: %s\n", argv[0],
if (execve(widget->argp[0], widget->argp, widget->envp) < 0) {
fprintf(stderr, "execl '%s' failed: %s\n", widget->argp[0],
strerror(errno));
exit(1);
}
......@@ -576,8 +575,7 @@ panel_configure(void *data,
static void
panel_destroy_launcher(struct panel_launcher *launcher)
{
wl_array_release(&launcher->argv);
wl_array_release(&launcher->envp);
custom_env_fini(&launcher->env);
free(launcher->path);
free(launcher->displayname);
......@@ -683,56 +681,16 @@ static void
panel_add_launcher(struct panel *panel, const char *icon, const char *path, const char *displayname)
{
struct panel_launcher *launcher;
char *start, *p, *eq, **ps;
int i, j, k;
launcher = xzalloc(sizeof *launcher);
launcher->icon = load_icon_or_fallback(icon);
launcher->path = xstrdup(path);
launcher->displayname = xstrdup(displayname);
wl_array_init(&launcher->envp);
wl_array_init(&launcher->argv);
for (i = 0; environ[i]; i++) {
ps = wl_array_add(&launcher->envp, sizeof *ps);
*ps = environ[i];
}
j = 0;
start = launcher->path;
while (*start) {
for (p = start, eq = NULL; *p && !isspace(*p); p++)
if (*p == '=')
eq = p;
if (eq && j == 0) {
ps = launcher->envp.data;
for (k = 0; k < i; k++)
if (strncmp(ps[k], start, eq - start) == 0) {
ps[k] = start;
break;
}
if (k == i) {
ps = wl_array_add(&launcher->envp, sizeof *ps);
*ps = start;
i++;
}
} else {
ps = wl_array_add(&launcher->argv, sizeof *ps);
*ps = start;
j++;
}
while (*p && isspace(*p))
*p++ = '\0';
start = p;
}
ps = wl_array_add(&launcher->envp, sizeof *ps);
*ps = NULL;
ps = wl_array_add(&launcher->argv, sizeof *ps);
*ps = NULL;
custom_env_init_from_environ(&launcher->env);
custom_env_add_from_exec_string(&launcher->env, launcher->path);
launcher->envp = custom_env_get_envp(&launcher->env);
launcher->argp = custom_env_get_argp(&launcher->env);
launcher->panel = panel;
wl_list_insert(panel->launcher_list.prev, &launcher->link);
......
......@@ -51,6 +51,7 @@
#include <libweston/libweston.h>
#include "shared/os-compatibility.h"
#include "shared/helpers.h"
#include "shared/process-util.h"
#include "shared/string-helpers.h"
#include "git-version.h"
#include <libweston/version.h>
......@@ -367,69 +368,48 @@ sigchld_handler(int signal_number, void *data)
return 1;
}
static void
child_client_exec(int sockfd, const char *path)
{
int clientfd;
char s[32];
sigset_t allsigs;
/* do not give our signal mask to the new process */
sigfillset(&allsigs);
sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
/* Launch clients as the user. Do not launch clients with wrong euid. */
if (seteuid(getuid()) == -1) {
weston_log("compositor: failed seteuid\n");
return;
}
/* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
* non-CLOEXEC fd to pass through exec. */
clientfd = dup(sockfd);
if (clientfd == -1) {
weston_log("compositor: dup failed: %s\n", strerror(errno));
return;
}
snprintf(s, sizeof s, "%d", clientfd);
setenv("WAYLAND_SOCKET", s, 1);
if (execl(path, path, NULL) < 0)
weston_log("compositor: executing '%s' failed: %s\n",
path, strerror(errno));
}
WL_EXPORT struct wl_client *
weston_client_launch(struct weston_compositor *compositor,
struct weston_process *proc,
const char *path,
weston_process_cleanup_func_t cleanup)
{
int sv[2];
struct wl_client *client = NULL;
struct custom_env child_env;
struct fdstr wayland_socket;
const char *fail_cloexec = "Couldn't unset CLOEXEC on client socket";
const char *fail_seteuid = "Couldn't call seteuid";
char *fail_exec;
char * const *argp;
char * const *envp;
sigset_t allsigs;
pid_t pid;
struct wl_client *client;
bool ret;
weston_log("launching '%s'\n", path);
str_printf(&fail_exec, "Error: Couldn't launch client '%s'\n", path);
if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
custom_env_init_from_environ(&child_env);
custom_env_add_arg(&child_env, path);
if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0,
wayland_socket.fds) < 0) {
weston_log("weston_client_launch: "
"socketpair failed while launching '%s': %s\n",
path, strerror(errno));
custom_env_fini(&child_env);
return NULL;
}
fdstr_update_str1(&wayland_socket);
custom_env_set_env_var(&child_env, "WAYLAND_SOCKET",
wayland_socket.str1);
pid = fork();
if (pid == -1) {
close(sv[0]);
close(sv[1]);
weston_log("weston_client_launch: "
"fork failed while launching '%s': %s\n", path,
strerror(errno));
return NULL;
}
argp = custom_env_get_argp(&child_env);
envp = custom_env_get_envp(&child_env);
if (pid == 0) {
pid = fork();
switch (pid) {
case 0:
/* Put the client in a new session so it won't catch signals
* intended for the parent. Sharing a session can be
* confusing when launching weston under gdb, as the ctrl-c
......@@ -437,24 +417,60 @@ weston_client_launch(struct weston_compositor *compositor,
* will cleanly shut down when the child exits.
*/
setsid();
child_client_exec(sv[1], path);
_exit(-1);
}
close(sv[1]);
/* do not give our signal mask to the new process */
sigfillset(&allsigs);
sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
/* Launch clients as the user. Do not launch clients with wrong euid. */
if (seteuid(getuid()) == -1) {
write(STDERR_FILENO, fail_seteuid,
strlen(fail_seteuid));
_exit(EXIT_FAILURE);
}
ret = fdstr_clear_cloexec_fd1(&wayland_socket);
if (!ret) {
write(STDERR_FILENO, fail_cloexec,
strlen(fail_cloexec));
_exit(EXIT_FAILURE);
}
execve(argp[0], argp, envp);
if (fail_exec)
write(STDERR_FILENO, fail_exec, strlen(fail_exec));
_exit(EXIT_FAILURE);
default:
close(wayland_socket.fds[1]);
client = wl_client_create(compositor->wl_display,
wayland_socket.fds[0]);
if (!client) {
custom_env_fini(&child_env);
close(wayland_socket.fds[0]);
free(fail_exec);
weston_log("weston_client_launch: "
"wl_client_create failed while launching '%s'.\n",
path);
return NULL;
}
client = wl_client_create(compositor->wl_display, sv[0]);
if (!client) {
close(sv[0]);
proc->pid = pid;
proc->cleanup = cleanup;
wet_watch_process(compositor, proc);
break;
case -1:
fdstr_close_all(&wayland_socket);
weston_log("weston_client_launch: "
"wl_client_create failed while launching '%s'.\n",
path);
return NULL;
"fork failed while launching '%s': %s\n", path,
strerror(errno));
break;
}
proc->pid = pid;
proc->cleanup = cleanup;
wet_watch_process(compositor, proc);
custom_env_fini(&child_env);
free(fail_exec);
return client;
}
......
......@@ -37,6 +37,7 @@
#include <libweston/xwayland-api.h>
#include "shared/helpers.h"
#include "shared/os-compatibility.h"
#include "shared/process-util.h"
#include "shared/string-helpers.h"
#ifdef HAVE_XWAYLAND_LISTENFD
......@@ -90,127 +91,6 @@ out:
return 0;
}
struct fdstr {
char str1[12];
int fds[2];
};
static void
fdstr_update_str1(struct fdstr *s)
{
snprintf(s->str1, sizeof(s->str1), "%d", s->fds[1]);
}
static void
fdstr_set_fd1(struct fdstr *s, int fd)
{
s->fds[0] = -1;
s->fds[1] = fd;
fdstr_update_str1(s);
}
static bool
fdstr_clear_cloexec_fd1(struct fdstr *s)
{
return os_fd_clear_cloexec(s->fds[1]) >= 0;
}
static void
fdstr_close_all(struct fdstr *s)
{
unsigned i;
for (i = 0; i < ARRAY_LENGTH(s->fds); i++) {
close(s->fds[i]);
s->fds[i] = -1;
}
}
struct custom_env {
struct wl_array p;
bool finalized;
};
static void
custom_env_init_from_environ(struct custom_env *env)
{
char **it;
char **ep;
wl_array_init(&env->p);
env->finalized = false;
for (it = environ; *it; it++) {
ep = wl_array_add(&env->p, sizeof *ep);
assert(ep);
*ep = strdup(*it);
assert(*ep);
}
}
static void
custom_env_fini(struct custom_env *env)
{
char **ep;
wl_array_for_each(ep, &env->p)
free(*ep);
wl_array_release(&env->p);
}
static char **
custom_env_find_element(struct custom_env *env, const char *name)
{
char **ep;
size_t name_len = strlen(name);
wl_array_for_each(ep, &env->p) {
char *entry = *ep;
if (strncmp(entry, name, name_len) == 0 &&
entry[name_len] == '=') {
return ep;
}
}
return NULL;
}
static void
custom_env_set(struct custom_env *env, const char *name, const char *value)
{
char **ep;
assert(strchr(name, '=') == NULL);
assert(!env->finalized);
ep = custom_env_find_element(env, name);
if (ep)
free(*ep);
else
ep = wl_array_add(&env->p, sizeof *ep);
assert(ep);
str_printf(ep, "%s=%s", name, value);
assert(*ep);
}
static char *const *
custom_env_get_envp(struct custom_env *env)
{
char **ep;
/* add terminating NULL */
ep = wl_array_add(&env->p, sizeof *ep);
assert(ep);
*ep = NULL;
env->finalized = true;
return env->p.data;
}
static pid_t
spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
{
......@@ -228,15 +108,16 @@ spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd
char *exec_failure_msg;
struct custom_env child_env;
char *const *envp;
char *const *argp;
bool ret;
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wayland_socket.fds) < 0) {
if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, wayland_socket.fds) < 0) {
weston_log("wl connection socketpair failed\n");
return 1;
}
fdstr_update_str1(&wayland_socket);
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, x11_wm_socket.fds) < 0) {
if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, x11_wm_socket.fds) < 0) {
weston_log("X wm connection socketpair failed\n");
return 1;
}
......@@ -257,20 +138,23 @@ spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd
str_printf(&exec_failure_msg,
"Error: executing Xwayland as '%s' failed.\n", xserver);
custom_env_init_from_environ(&child_env);
custom_env_set(&child_env, "WAYLAND_SOCKET", wayland_socket.str1);
envp = custom_env_get_envp(&child_env);
custom_env_set_env_var(&child_env, "WAYLAND_SOCKET", wayland_socket.str1);
custom_env_add_arg(&child_env, xserver);
custom_env_add_arg(&child_env, display);
custom_env_add_arg(&child_env, "-rootless");
custom_env_add_arg(&child_env, LISTEN_STR);
custom_env_add_arg(&child_env, x11_abstract_socket.str1);
custom_env_add_arg(&child_env, LISTEN_STR);
custom_env_add_arg(&child_env, x11_unix_socket.str1);
custom_env_add_arg(&child_env, "-displayfd");
custom_env_add_arg(&child_env, display_pipe.str1);
custom_env_add_arg(&child_env, "-wm");
custom_env_add_arg(&child_env, x11_wm_socket.str1);
custom_env_add_arg(&child_env, "-terminate");
const char *const argv[] = {
xserver,
display,
"-rootless",
LISTEN_STR, x11_abstract_socket.str1,
LISTEN_STR, x11_unix_socket.str1,
"-displayfd", display_pipe.str1,
"-wm", x11_wm_socket.str1,
"-terminate",
NULL
};
envp = custom_env_get_envp(&child_env);
argp = custom_env_get_argp(&child_env);
pid = fork();
switch (pid) {
......@@ -285,7 +169,7 @@ spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd
if (!ret)
_exit(EXIT_FAILURE);
execve(xserver, (char *const *)argv, envp);
execve(xserver, argp, envp);
/* execve does not return on success, so it failed */
if (exec_failure_msg) {
......
......@@ -4,6 +4,7 @@ srcs_libshared = [
'signal.c',
'file-util.c',
'os-compatibility.c',
'process-util.c',
]
deps_libshared = [dep_wayland_client, dep_wayland_server]
......
/*
* Copyright 2022 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-util.h>
#include "helpers.h"
#include "os-compatibility.h"
#include "process-util.h"
#include "string-helpers.h"
extern char **environ; /* defined by libc */
void
fdstr_update_str1(struct fdstr *s)
{
snprintf(s->str1, sizeof(s->str1), "%d", s->fds[1]);
}
void
fdstr_set_fd1(struct fdstr *s, int fd)
{
s->fds[0] = -1;
s->fds[1] = fd;
fdstr_update_str1(s);
}
bool
fdstr_clear_cloexec_fd1(struct fdstr *s)
{
return os_fd_clear_cloexec(s->fds[1]) >= 0;
}
void
fdstr_close_all(struct fdstr *s)
{
unsigned i;
for (i = 0; i < ARRAY_LENGTH(s->fds); i++) {
close(s->fds[i]);
s->fds[i] = -1;
}
}
void
custom_env_init_from_environ(struct custom_env *env)
{
char **it;
char **ep;
wl_array_init(&env->envp);
env->env_finalized = false;
wl_array_init(&env->argp);
env->arg_finalized = false;
for (it = environ; *it; it++) {
ep = wl_array_add(&env->envp, sizeof *ep);
assert(ep);
*ep = strdup(*it);
assert(*ep);
}
}
void
custom_env_fini(struct custom_env *env)
{
char **p;
wl_array_for_each(p, &env->envp)
free(*p);
wl_array_release(&env->envp);
wl_array_for_each(p, &env->argp)
free(*p);
wl_array_release(&env->argp);
}
static char **
custom_env_get_env_var(struct custom_env *env, const char *name)
{
char **ep;
size_t name_len = strlen(name);
wl_array_for_each(ep, &env->envp) {
char *entry = *ep;
if (strncmp(entry, name, name_len) == 0 &&
entry[name_len] == '=') {
return ep;
}
}
return NULL;
}
void
custom_env_add_arg(struct custom_env *env, const char *arg)
{
char **ap;
assert(!env->arg_finalized);
ap = wl_array_add(&env->argp, sizeof *ap);
assert(ap);
*ap = strdup(arg);
assert(*ap);
}
void
custom_env_set_env_var(struct custom_env *env, const char *name, const char *value)
{
char **ep;
assert(strchr(name, '=') == NULL);
assert(!env->env_finalized);
ep = custom_env_get_env_var(env, name);
if (ep)
free(*ep);
else
ep = wl_array_add(&env->envp, sizeof *ep);
assert(ep);
str_printf(ep, "%s=%s", name, value);
assert(*ep);
}
/**
* Add information from a parsed exec string to a custom_env
*
* An 'exec string' is a string in the format:
* ENVFOO=bar ENVBAR=baz /path/to/exec --arg anotherarg
*
* This function will parse such a string and add the specified environment
* variables (in the format KEY=value) up until it sees a non-environment
* string, after which point every entry will be interpreted as a new
* argument.
*
* Entries are space-separated; there is no support for quoting.
*/
void
custom_env_add_from_exec_string(struct custom_env *env, const char *exec_str)
{
char *dup_path = strdup(exec_str);
char *start = dup_path;
assert(dup_path);
/* Build the environment array (if any) by handling any number of
* equal-separated key=value at the start of the string, split by
* spaces; uses "foo=bar baz=quux meh argh" as the example, where
* "foo=bar" and "baz=quux" should go into the environment, and
* "meh" should be executed with "argh" as its first argument */
while (*start) {
char *k = NULL, *v = NULL;
char *p;
/* Leaves us with "foo\0bar baz=quux meh argh", with k pointing
* to "foo" and v pointing to "bar baz=quux meh argh" */
for (p = start; *p && !isspace(*p); p++) {
if (*p == '=') {
*p++ = '\0';
k = start;
v = p;
break;
}
}
if (!v)
break;
/* Walk to the next space or NUL, filling any trailing spaces
* with NUL, to give us "foo\0bar\0\0baz=quux meh argh".
* k will point to "foo", v will point to "bar", and
* start will point to "baz=quux meh argh". */
while (*p && !isspace(*p))
p++;
while (*p && isspace(*p))
*p++ = '\0';
start = p;
custom_env_set_env_var(env, k, v);
}
/* Now build the argv array by splitting on spaces */
while (*start) {
char *p;
bool valid = false;
for (p = start; *p && !isspace(*p); p++)
valid = true;
if (!valid)
break;
while (*p && isspace(*p))
*p++ = '\0';
custom_env_add_arg(env, start);
start = p;
}
free(dup_path);
}
char *const *
custom_env_get_envp(struct custom_env *env)
{
char **ep;
assert(!env->env_finalized);
/* add terminating NULL */
ep = wl_array_add(&env->envp, sizeof *ep);
assert(ep);
*ep = NULL;
env->env_finalized = true;
return env->envp.data;
}
char *const *
custom_env_get_argp(struct custom_env *env)
{
char **ap;
assert(!env->arg_finalized);
/* add terminating NULL */
ap = wl_array_add(&env->argp, sizeof *ap);
assert(ap);
*ap = NULL;
env->arg_finalized = true;
return env->argp.data;
}
/*
* Copyright 2022 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-util.h>
#include "os-compatibility.h"
#include "string-helpers.h"
/**
* A container for file descriptors and their string representations, designed
* to be used when forking child processes.
*
* fds[0] is generally used as the file descriptor held within the parent
* process to communicate with the child, and fds[1] as the child's counterpart.
*
* str1[] is used as a string representation of fds[1].
*/
struct fdstr {
char str1[12];
int fds[2];
};
void
fdstr_update_str1(struct fdstr *s);
void
fdstr_set_fd1(struct fdstr *s, int fd);
bool
fdstr_clear_cloexec_fd1(struct fdstr *s);
void
fdstr_close_all(struct fdstr *s);
/**
* A container for environment variables and/or process arguments, designed to
* be used when forking child processes, as setenv() and anything which
* allocates memory cannot be used between fork() and exec().
*/
struct custom_env {
struct wl_array envp;
bool env_finalized;
struct wl_array argp;
bool arg_finalized;
};
void
custom_env_init_from_environ(struct custom_env *env);
void
custom_env_fini(struct custom_env *env);
void
custom_env_set_env_var(struct custom_env *env, const char *name, const char *value);
void
custom_env_add_arg(struct custom_env *env, const char *arg);
void
custom_env_add_from_exec_string(struct custom_env *env, const char *exec_str);
char *const *
custom_env_get_envp(struct custom_env *env);
char *const *
custom_env_get_argp(struct custom_env *env);
/*
* Copyright 2022 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-util.h>
#include "shared/helpers.h"
#include "shared/os-compatibility.h"
#include "shared/process-util.h"
#include "shared/string-helpers.h"
#include "weston-test-runner.h"
#define ASSERT_STR_MATCH(_as, _bs) do { \
const char *as = _as; \
const char *bs = _bs; \
assert(!!as == !!bs); \
assert(!as || strcmp(as, bs) == 0); \
} while (0)
#define ASSERT_STR_ARRAY_MATCH(_name, _aa, _ba) do { \
char * const *aa = _aa; \
char * const *ba = _ba; \
testlog("\tcomparing " _name ":\n"); \
for (int _i = 0; aa[_i] || ba[_i]; _i++) { \
testlog("\t\t[%d] '%s' == '%s'?\n", _i, aa[_i], ba[_i]); \
ASSERT_STR_MATCH(aa[_i], ba[_i]); \
} \
testlog("\tsuccessfully compared " _name "\n"); \
} while (0)
static enum test_result_code
setup_env(struct weston_test_harness *harness)
{
/* as this is a standalone test, we can clear the environment here */
clearenv();
putenv("ENV1=one");
setenv("ENV2", "two", 1);
setenv("ENV3", "three", 1);
return weston_test_harness_execute_standalone(harness);
}
DECLARE_FIXTURE_SETUP(setup_env);
#define DEFAULT_ENVP (char * const []) { "ENV1=one", "ENV2=two", "ENV3=three", NULL }
TEST(basic_env)
{
struct custom_env env;
char *const envp[] = { "ENV1=one", "ENV2=two", "ENV3=four", "ENV5=five", NULL };
custom_env_init_from_environ(&env);
custom_env_set_env_var(&env, "ENV5", "five");
custom_env_set_env_var(&env, "ENV3", "four");
ASSERT_STR_ARRAY_MATCH("envp", custom_env_get_envp(&env), envp);
assert(env.env_finalized);
custom_env_fini(&env);
}
TEST(basic_env_arg)
{
struct custom_env env;
char *const argp[] = { "arg1", "arg2", "arg3", NULL };
custom_env_init_from_environ(&env);
custom_env_add_arg(&env, "arg1");
custom_env_add_arg(&env, "arg2");
custom_env_add_arg(&env, "arg3");
ASSERT_STR_ARRAY_MATCH("envp", custom_env_get_envp(&env), DEFAULT_ENVP);
assert(env.env_finalized);
ASSERT_STR_ARRAY_MATCH("argp", custom_env_get_argp(&env), argp);
assert(env.arg_finalized);
custom_env_fini(&env);
}
struct test_str {
const char *exec_str;
char * const *envp;
char * const *argp;
};
static struct test_str str_tests[] = {
{
.exec_str = "ENV1=1 ENV2=owt two-arghs",
.envp = (char * const []) { "ENV1=1", "ENV2=owt", "ENV3=three", NULL },
.argp = (char * const []) { "two-arghs", NULL },
},
{
.exec_str = "ENV2=owt one-argh",
.envp = (char * const []) { "ENV1=one", "ENV2=owt", "ENV3=three", NULL },
.argp = (char * const []) { "one-argh", NULL },
},
{
.exec_str = "FOO=bar one-argh-again",
.envp = (char * const []) { "ENV1=one", "ENV2=two", "ENV3=three", "FOO=bar", NULL },
.argp = (char * const []) { "one-argh-again", NULL },
},
{
.exec_str = "ENV1=number=7 one-argh-eq",
.envp = (char * const []) { "ENV1=number=7", "ENV2=two", "ENV3=three", NULL },
.argp = (char * const []) { "one-argh-eq", NULL },
},
{
.exec_str = "no-arg-h",
.envp = DEFAULT_ENVP,
.argp = (char * const []) { "no-arg-h", NULL },
},
{
.exec_str = "argh-w-arg argequals=thing plainarg ",
.envp = DEFAULT_ENVP,
.argp = (char * const []) { "argh-w-arg", "argequals=thing", "plainarg", NULL },
},
};
TEST_P(env_parse_string, str_tests)
{
struct custom_env env;
struct test_str *test = data;
testlog("checking exec_str '%s'\n", test->exec_str);
custom_env_init_from_environ(&env);
custom_env_add_from_exec_string(&env, test->exec_str);
ASSERT_STR_ARRAY_MATCH("envp", custom_env_get_envp(&env), test->envp);
ASSERT_STR_ARRAY_MATCH("argp", custom_env_get_argp(&env), test->argp);
custom_env_fini(&env);
}
......@@ -146,6 +146,7 @@ tests = [
'dep_objs': dep_libexec_weston,
},
{ 'name': 'color-manager', },
{ 'name': 'custom-env', },
{ 'name': 'devices', },
{
'name': 'drm-formats',
......