aboutsummaryrefslogtreecommitdiff
path: root/sway/main.c
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2015-12-12 13:01:00 -0500
committerDrew DeVault <sir@cmpwn.com>2015-12-12 13:01:00 -0500
commitaf80b12addedcb6021a1eafb9ffd7153eadc605a (patch)
tree36e0875128768785b8dc8f5608294b4bf399f15e /sway/main.c
parent182a6dc8fbdd647d6e55f80cfc89f741a518fd69 (diff)
Implement invoking `sway` as IPC client
As an alternative to invoking swaymsg.
Diffstat (limited to 'sway/main.c')
-rw-r--r--sway/main.c25
1 files changed, 25 insertions, 0 deletions
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 <sys/types.h>
#include <sys/un.h>
#include <signal.h>
+#include <unistd.h>
#include <getopt.h>
#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);