aboutsummaryrefslogtreecommitdiffstats
path: root/sway/main.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-12-12 13:01:00 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-12-12 13:01:00 -0500
commitaf80b12addedcb6021a1eafb9ffd7153eadc605a (patch)
tree36e0875128768785b8dc8f5608294b4bf399f15e /sway/main.c
parentFix build issue (diff)
downloadsway-af80b12addedcb6021a1eafb9ffd7153eadc605a.tar.gz
sway-af80b12addedcb6021a1eafb9ffd7153eadc605a.tar.zst
sway-af80b12addedcb6021a1eafb9ffd7153eadc605a.zip
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 @@
6#include <sys/types.h> 6#include <sys/types.h>
7#include <sys/un.h> 7#include <sys/un.h>
8#include <signal.h> 8#include <signal.h>
9#include <unistd.h>
9#include <getopt.h> 10#include <getopt.h>
10#include "extensions.h" 11#include "extensions.h"
11#include "layout.h" 12#include "layout.h"
@@ -14,6 +15,7 @@
14#include "log.h" 15#include "log.h"
15#include "readline.h" 16#include "readline.h"
16#include "handlers.h" 17#include "handlers.h"
18#include "ipc-client.h"
17#include "ipc-server.h" 19#include "ipc-server.h"
18#include "sway.h" 20#include "sway.h"
19 21
@@ -51,6 +53,14 @@ void detect_nvidia() {
51 fclose(f); 53 fclose(f);
52} 54}
53 55
56void run_as_ipc_client(char *command, char *socket_path) {
57 int socketfd = ipc_open_socket(socket_path);
58 uint32_t len = strlen(command);
59 char *resp = ipc_single_command(socketfd, IPC_COMMAND, command, &len);
60 printf("%s\n", resp);
61 close(socketfd);
62}
63
54int main(int argc, char **argv) { 64int main(int argc, char **argv) {
55 static int verbose = 0, debug = 0, validate = 0; 65 static int verbose = 0, debug = 0, validate = 0;
56 66
@@ -126,6 +136,21 @@ int main(int argc, char **argv) {
126 } 136 }
127 } 137 }
128 138
139 if (optind < argc) { // Behave as IPC client
140 if (getuid() != geteuid() || getgid() != getegid()) {
141 if (setgid(getgid()) != 0 || setuid(getuid()) != 0) {
142 sway_abort("Unable to drop root");
143 }
144 }
145 char *socket_path = getenv("SWAYSOCK");
146 if (!socket_path) {
147 sway_abort("Unable to retrieve socket path");
148 }
149 char *command = join_args(argv + optind, argc - optind);
150 run_as_ipc_client(command, socket_path);
151 return 0;
152 }
153
129 // we need to setup logging before wlc_init in case it fails. 154 // we need to setup logging before wlc_init in case it fails.
130 if (debug) { 155 if (debug) {
131 init_log(L_DEBUG); 156 init_log(L_DEBUG);