2021-09-24 21:45:48 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2020-10-08 20:50:20 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/stat.h>
|
2021-07-22 21:30:17 +01:00
|
|
|
#include <wlr/util/log.h>
|
2021-10-11 22:15:44 +01:00
|
|
|
#include "common/buf.h"
|
2022-09-16 18:41:02 -04:00
|
|
|
#include "common/mem.h"
|
2020-10-08 20:50:20 +01:00
|
|
|
#include "common/spawn.h"
|
2020-10-09 19:46:59 +01:00
|
|
|
#include "common/string-helpers.h"
|
2023-01-31 03:35:13 +01:00
|
|
|
#include "config/session.h"
|
2020-10-08 20:50:20 +01:00
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
isfile(const char *path)
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
|
|
|
|
return (!stat(path, &st));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
string_empty(const char *s)
|
|
|
|
|
{
|
|
|
|
|
return !s || !*s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
process_line(char *line)
|
|
|
|
|
{
|
|
|
|
|
if (string_empty(line) || line[0] == '#') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-11 22:15:44 +01:00
|
|
|
char *key = NULL;
|
2020-10-08 20:50:20 +01:00
|
|
|
char *p = strchr(line, '=');
|
|
|
|
|
if (!p) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
*p = '\0';
|
2020-10-09 19:46:59 +01:00
|
|
|
key = string_strip(line);
|
2021-10-11 22:15:44 +01:00
|
|
|
|
|
|
|
|
struct buf value;
|
|
|
|
|
buf_init(&value);
|
|
|
|
|
buf_add(&value, string_strip(++p));
|
|
|
|
|
buf_expand_shell_variables(&value);
|
|
|
|
|
if (string_empty(key) || !value.len) {
|
|
|
|
|
goto error;
|
2020-10-08 20:50:20 +01:00
|
|
|
}
|
2021-10-11 22:15:44 +01:00
|
|
|
setenv(key, value.buf, 1);
|
|
|
|
|
error:
|
|
|
|
|
free(value.buf);
|
2020-10-08 20:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-31 03:35:13 +01:00
|
|
|
static void
|
2020-10-08 20:50:20 +01:00
|
|
|
read_environment_file(const char *filename)
|
|
|
|
|
{
|
|
|
|
|
char *line = NULL;
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
FILE *stream = fopen(filename, "r");
|
|
|
|
|
if (!stream) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-22 21:30:17 +01:00
|
|
|
wlr_log(WLR_INFO, "read environment file %s", filename);
|
2020-10-08 20:50:20 +01:00
|
|
|
while (getline(&line, &len, stream) != -1) {
|
|
|
|
|
char *p = strrchr(line, '\n');
|
|
|
|
|
if (p) {
|
|
|
|
|
*p = '\0';
|
|
|
|
|
}
|
|
|
|
|
process_line(line);
|
|
|
|
|
}
|
|
|
|
|
free(line);
|
|
|
|
|
fclose(stream);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-26 06:25:40 +01:00
|
|
|
static char *
|
2022-04-22 17:00:36 +01:00
|
|
|
build_path(const char *dir, const char *filename)
|
2020-10-08 20:50:20 +01:00
|
|
|
{
|
2022-04-22 17:00:36 +01:00
|
|
|
if (string_empty(dir) || string_empty(filename)) {
|
2020-10-08 20:50:20 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2023-06-25 09:00:41 +01:00
|
|
|
return strdup_printf("%s/%s", dir, filename);
|
2020-10-08 20:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-28 01:06:56 +02:00
|
|
|
static void
|
|
|
|
|
update_activation_env(const char *env_keys)
|
|
|
|
|
{
|
|
|
|
|
if (!getenv("DBUS_SESSION_BUS_ADDRESS")) {
|
|
|
|
|
/* Prevent accidentally auto-launching a dbus session */
|
|
|
|
|
wlr_log(WLR_INFO, "Not updating dbus execution environment: "
|
|
|
|
|
"DBUS_SESSION_BUS_ADDRESS not set");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
wlr_log(WLR_INFO, "Updating dbus execution environment");
|
|
|
|
|
|
2023-06-25 09:00:41 +01:00
|
|
|
char *cmd = strdup_printf("dbus-update-activation-environment %s", env_keys);
|
2022-09-16 18:41:02 -04:00
|
|
|
spawn_async_no_shell(cmd);
|
|
|
|
|
free(cmd);
|
2022-07-28 01:06:56 +02:00
|
|
|
|
2023-06-25 09:00:41 +01:00
|
|
|
cmd = strdup_printf("systemctl --user import-environment %s", env_keys);
|
2022-09-16 18:41:02 -04:00
|
|
|
spawn_async_no_shell(cmd);
|
|
|
|
|
free(cmd);
|
2022-07-28 01:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-08 20:50:20 +01:00
|
|
|
void
|
2022-04-22 17:00:36 +01:00
|
|
|
session_environment_init(const char *dir)
|
2020-10-08 20:50:20 +01:00
|
|
|
{
|
2022-07-28 01:06:56 +02:00
|
|
|
/*
|
|
|
|
|
* Set default for XDG_CURRENT_DESKTOP so xdg-desktop-portal-wlr is happy.
|
|
|
|
|
* May be overriden either by already having a value set or by the user
|
|
|
|
|
* supplied environment file.
|
|
|
|
|
*/
|
|
|
|
|
setenv("XDG_CURRENT_DESKTOP", "wlroots", 0);
|
|
|
|
|
|
2023-06-26 06:25:40 +01:00
|
|
|
char *environment = build_path(dir, "environment");
|
2020-10-22 19:42:06 +01:00
|
|
|
if (!environment) {
|
2020-10-08 20:50:20 +01:00
|
|
|
return;
|
2020-10-22 19:42:06 +01:00
|
|
|
}
|
2020-10-08 20:50:20 +01:00
|
|
|
read_environment_file(environment);
|
2023-06-26 06:25:40 +01:00
|
|
|
free(environment);
|
2020-10-08 20:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2022-04-22 17:00:36 +01:00
|
|
|
session_autostart_init(const char *dir)
|
2020-10-08 20:50:20 +01:00
|
|
|
{
|
2022-07-28 01:06:56 +02:00
|
|
|
/* Update dbus and systemd user environment, each may fail gracefully */
|
|
|
|
|
update_activation_env("DISPLAY WAYLAND_DISPLAY XDG_CURRENT_DESKTOP");
|
|
|
|
|
|
2023-06-26 06:25:40 +01:00
|
|
|
char *autostart = build_path(dir, "autostart");
|
2020-10-22 19:42:06 +01:00
|
|
|
if (!autostart) {
|
2020-10-08 20:50:20 +01:00
|
|
|
return;
|
2020-10-22 19:42:06 +01:00
|
|
|
}
|
2020-10-08 20:50:20 +01:00
|
|
|
if (!isfile(autostart)) {
|
2021-07-23 21:15:55 +01:00
|
|
|
wlr_log(WLR_ERROR, "no autostart file");
|
2020-10-28 20:44:35 +00:00
|
|
|
goto out;
|
2020-10-08 20:50:20 +01:00
|
|
|
}
|
2021-07-22 21:30:17 +01:00
|
|
|
wlr_log(WLR_INFO, "run autostart file %s", autostart);
|
2023-06-25 09:00:41 +01:00
|
|
|
char *cmd = strdup_printf("sh %s", autostart);
|
2020-10-08 20:50:20 +01:00
|
|
|
spawn_async_no_shell(cmd);
|
2020-10-22 19:42:06 +01:00
|
|
|
free(cmd);
|
2020-10-28 20:44:35 +00:00
|
|
|
out:
|
2023-06-26 06:25:40 +01:00
|
|
|
free(autostart);
|
2020-10-08 20:50:20 +01:00
|
|
|
}
|