From 3e1a800c14f427bfbaaead69f4811b52823affad Mon Sep 17 00:00:00 2001 From: Johan Malm Date: Thu, 6 Oct 2022 21:54:49 +0100 Subject: [PATCH] Add --exit and --reconfigure --- docs/labwc.1.scd | 10 ++++++++++ src/main.c | 29 ++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/docs/labwc.1.scd b/docs/labwc.1.scd index 32ba3c61..ed2f317f 100644 --- a/docs/labwc.1.scd +++ b/docs/labwc.1.scd @@ -24,6 +24,10 @@ kill -s $LABWC_PID killall -s labwc ``` +Each running instance of labwc sets the environment variable `LABWC_PID` to +its PID. This is useful for sending signals to a specific instance and is what +the `--exit` and `--reconfigure` options use. + # OPTIONS *-c, --config* @@ -35,9 +39,15 @@ killall -s labwc *-d, --debug* Enable full logging, including debug information +*-e, --exit* + Exit the compositor + *-h, --help* Show help message and quit +*-r, --reconfigure* + Reload the compositor configuration + *-s, --startup* Run command on startup diff --git a/src/main.c b/src/main.c index 88b304d6..04d020cb 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only #define _POSIX_C_SOURCE 200809L +#include #include #include #include "common/dir.h" @@ -18,7 +19,9 @@ static const struct option long_options[] = { {"config", required_argument, NULL, 'c'}, {"config-dir", required_argument, NULL, 'C'}, {"debug", no_argument, NULL, 'd'}, + {"exit", no_argument, NULL, 'e'}, {"help", no_argument, NULL, 'h'}, + {"reconfigure", no_argument, NULL, 'r'}, {"startup", required_argument, NULL, 's'}, {"version", no_argument, NULL, 'v'}, {"verbose", no_argument, NULL, 'V'}, @@ -30,7 +33,9 @@ static const char labwc_usage[] = " -c, --config Specify config file (with path)\n" " -C, --config-dir Specify config directory\n" " -d, --debug Enable full logging, including debug information\n" +" -e, --exit Exit the compositor\n" " -h, --help Show help message and quit\n" +" -r, --reconfigure Reload the compositor configuration\n" " -s, --startup Run command on startup\n" " -v, --version Show version number and quit\n" " -V, --verbose Enable more verbose logging\n"; @@ -42,6 +47,22 @@ usage(void) exit(0); } +static void +send_signal_to_labwc_pid(int signal) +{ + char *labwc_pid = getenv("LABWC_PID"); + if (!labwc_pid) { + wlr_log(WLR_ERROR, "LABWC_PID not set"); + exit(EXIT_FAILURE); + } + int pid = atoi(labwc_pid); + if (!pid) { + wlr_log(WLR_ERROR, "should not send signal to pid 0"); + exit(EXIT_FAILURE); + } + kill(pid, signal); +} + int main(int argc, char *argv[]) { @@ -57,7 +78,7 @@ main(int argc, char *argv[]) int c; while (1) { int index = 0; - c = getopt_long(argc, argv, "c:C:dhs:vV", long_options, &index); + c = getopt_long(argc, argv, "c:C:dehrs:vV", long_options, &index); if (c == -1) { break; } @@ -71,6 +92,12 @@ main(int argc, char *argv[]) case 'd': verbosity = WLR_DEBUG; break; + case 'e': + send_signal_to_labwc_pid(SIGTERM); + exit(0); + case 'r': + send_signal_to_labwc_pid(SIGHUP); + exit(0); case 's': startup_cmd = optarg; break;