diff --git a/sway/main.c b/sway/main.c index c7696e2e..9a5e351c 100644 --- a/sway/main.c +++ b/sway/main.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "extensions.h" #include "layout.h" @@ -14,6 +15,7 @@ #include "log.h" #include "readline.h" #include "handlers.h" +#include "ipc-client.h" #include "ipc-server.h" #include "sway.h" @@ -51,6 +53,14 @@ void detect_nvidia() { fclose(f); } +void run_as_ipc_client(char *command, char *socket_path) { + int socketfd = ipc_open_socket(socket_path); + uint32_t len = strlen(command); + char *resp = ipc_single_command(socketfd, IPC_COMMAND, command, &len); + printf("%s\n", resp); + close(socketfd); +} + int main(int argc, char **argv) { static int verbose = 0, debug = 0, validate = 0; @@ -126,6 +136,21 @@ int main(int argc, char **argv) { } } + if (optind < argc) { // Behave as IPC client + if (getuid() != geteuid() || getgid() != getegid()) { + if (setgid(getgid()) != 0 || setuid(getuid()) != 0) { + sway_abort("Unable to drop root"); + } + } + char *socket_path = getenv("SWAYSOCK"); + if (!socket_path) { + sway_abort("Unable to retrieve socket path"); + } + char *command = join_args(argv + optind, argc - optind); + run_as_ipc_client(command, socket_path); + return 0; + } + // we need to setup logging before wlc_init in case it fails. if (debug) { init_log(L_DEBUG);