Implement invoking sway as IPC client

As an alternative to invoking swaymsg.
This commit is contained in:
Drew DeVault 2015-12-12 13:01:00 -05:00
parent 182a6dc8fb
commit af80b12add

View file

@ -6,6 +6,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/un.h> #include <sys/un.h>
#include <signal.h> #include <signal.h>
#include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include "extensions.h" #include "extensions.h"
#include "layout.h" #include "layout.h"
@ -14,6 +15,7 @@
#include "log.h" #include "log.h"
#include "readline.h" #include "readline.h"
#include "handlers.h" #include "handlers.h"
#include "ipc-client.h"
#include "ipc-server.h" #include "ipc-server.h"
#include "sway.h" #include "sway.h"
@ -51,6 +53,14 @@ void detect_nvidia() {
fclose(f); 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) { int main(int argc, char **argv) {
static int verbose = 0, debug = 0, validate = 0; 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. // we need to setup logging before wlc_init in case it fails.
if (debug) { if (debug) {
init_log(L_DEBUG); init_log(L_DEBUG);