session: run shutdown script, clean up activation env before exit

This commit is contained in:
Andrew J. Hesford 2024-02-18 12:23:14 -05:00
parent 4ddeb3cd42
commit f90b7dca2a
6 changed files with 157 additions and 16 deletions

View file

@ -1,4 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
@ -83,3 +84,73 @@ strdup_printf(const char *fmt, ...)
}
return p;
}
char *
str_join(const char * const parts[],
const char *restrict fmt, const char *restrict sep)
{
assert(parts);
if (!fmt) {
fmt = "%s";
}
if (!sep) {
sep = " ";
}
size_t size = 0;
size_t n_parts = 0;
size_t sep_len = strlen(sep);
/* Count the length of each formatted string */
for (const char *const *s = parts; *s; ++s) {
int n = snprintf(NULL, 0, fmt, *s);
if (n < 0) {
return NULL;
}
size += (size_t)n;
++n_parts;
}
if (n_parts < 1) {
return NULL;
}
/* Need (n_parts - 1) separators, plus one NULL terminator */
size += (n_parts - 1) * sep_len + 1;
/* Concatenate the strings and separators */
char *buf = xzalloc(size);
char *p = buf;
for (const char *const *s = parts; *s; ++s) {
int n = 0;
if (p != buf) {
n = snprintf(p, size, "%s", sep);
if (n < 0 || (size_t)n >= size) {
p = NULL;
break;
}
size -= (size_t)n;
p += (size_t)n;
}
n = snprintf(p, size, fmt, *s);
if (n < 0 || (size_t)n >= size) {
p = NULL;
break;
}
size -= (size_t)n;
p += (size_t)n;
}
if (!p) {
free(buf);
return NULL;
}
return buf;
}

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@ -9,11 +10,19 @@
#include "common/buf.h"
#include "common/dir.h"
#include "common/file-helpers.h"
#include "common/mem.h"
#include "common/spawn.h"
#include "common/string-helpers.h"
#include "config/session.h"
#include "labwc.h"
static const char *const env_vars[] = {
"DISPLAY",
"WAYLAND_DISPLAY",
"XDG_CURRENT_DESKTOP",
NULL,
};
static void
process_line(char *line)
{
@ -65,7 +74,7 @@ read_environment_file(const char *filename)
}
static void
update_activation_env(const char *env_keys)
update_activation_env(bool initialize)
{
if (!getenv("DBUS_SESSION_BUS_ADDRESS")) {
/* Prevent accidentally auto-launching a dbus session */
@ -75,13 +84,22 @@ update_activation_env(const char *env_keys)
}
wlr_log(WLR_INFO, "Updating dbus execution environment");
char *cmd = strdup_printf("dbus-update-activation-environment %s", env_keys);
char *env_keys = str_join(env_vars, "%s", " ");
char *env_unset_keys = initialize ? NULL : str_join(env_vars, "%s=", " ");
char *cmd =
strdup_printf("dbus-update-activation-environment %s",
initialize ? env_keys : env_unset_keys);
spawn_async_no_shell(cmd);
free(cmd);
cmd = strdup_printf("systemctl --user import-environment %s", env_keys);
cmd = strdup_printf("systemctl --user %s %s",
initialize ? "import-environment" : "unset-environment", env_keys);
spawn_async_no_shell(cmd);
free(cmd);
free(env_keys);
free(env_unset_keys);
}
void
@ -120,14 +138,11 @@ session_environment_init(void)
paths_destroy(&paths);
}
void
session_autostart_init(void)
static void
run_session_script(const char *script)
{
/* Update dbus and systemd user environment, each may fail gracefully */
update_activation_env("DISPLAY WAYLAND_DISPLAY XDG_CURRENT_DESKTOP");
struct wl_list paths;
paths_config_create(&paths, "autostart");
paths_config_create(&paths, script);
bool should_merge_config = rc.merge_config;
struct wl_list *(*iter)(struct wl_list *list);
@ -138,7 +153,7 @@ session_autostart_init(void)
if (!file_exists(path->string)) {
continue;
}
wlr_log(WLR_INFO, "run autostart file %s", path->string);
wlr_log(WLR_INFO, "run session script %s", path->string);
char *cmd = strdup_printf("sh %s", path->string);
spawn_async_no_shell(cmd);
free(cmd);
@ -149,3 +164,20 @@ session_autostart_init(void)
}
paths_destroy(&paths);
}
void
session_autostart_init(void)
{
/* Update dbus and systemd user environment, each may fail gracefully */
update_activation_env(/* initialize */ true);
run_session_script("autostart");
}
void
session_shutdown(void)
{
run_session_script("shutdown");
/* Clear the dbus and systemd user environment, each may fail gracefully */
update_activation_env(/* initialize */ false);
}

View file

@ -178,6 +178,8 @@ main(int argc, char *argv[])
wl_display_run(server.wl_display);
session_shutdown();
server_finish(&server);
menu_finish(&server);