summaryrefslogtreecommitdiffstats
path: root/sway/commands/exec_always.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2017-11-18 11:22:02 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2017-11-18 11:22:02 -0500
commit733993a651c71f7e2198d505960d6bbd31e0e107 (patch)
treee51732c5872b624e73355f9e5b3f762101f3cd0d /sway/commands/exec_always.c
parentInitial (awful) pass on xdg shell support (diff)
downloadsway-733993a651c71f7e2198d505960d6bbd31e0e107.tar.gz
sway-733993a651c71f7e2198d505960d6bbd31e0e107.tar.zst
sway-733993a651c71f7e2198d505960d6bbd31e0e107.zip
Move everything to sway/old/
Diffstat (limited to 'sway/commands/exec_always.c')
-rw-r--r--sway/commands/exec_always.c85
1 files changed, 0 insertions, 85 deletions
diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c
deleted file mode 100644
index ab2d8622..00000000
--- a/sway/commands/exec_always.c
+++ /dev/null
@@ -1,85 +0,0 @@
1#define _XOPEN_SOURCE 500
2#include <string.h>
3#include <sys/wait.h>
4#include <unistd.h>
5#include "sway/commands.h"
6#include "sway/config.h"
7#include "log.h"
8#include "stringop.h"
9
10struct cmd_results *cmd_exec_always(int argc, char **argv) {
11 struct cmd_results *error = NULL;
12 if (!config->active) return cmd_results_new(CMD_DEFER, NULL, NULL);
13 if ((error = checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0))) {
14 return error;
15 }
16
17 char *tmp = NULL;
18 if (strcmp((char*)*argv, "--no-startup-id") == 0) {
19 sway_log(L_INFO, "exec switch '--no-startup-id' not supported, ignored.");
20 if ((error = checkarg(argc - 1, "exec_always", EXPECTED_MORE_THAN, 0))) {
21 return error;
22 }
23
24 tmp = join_args(argv + 1, argc - 1);
25 } else {
26 tmp = join_args(argv, argc);
27 }
28
29 // Put argument into cmd array
30 char cmd[4096];
31 strncpy(cmd, tmp, sizeof(cmd));
32 cmd[sizeof(cmd) - 1] = 0;
33 free(tmp);
34 sway_log(L_DEBUG, "Executing %s", cmd);
35
36 int fd[2];
37 if (pipe(fd) != 0) {
38 sway_log(L_ERROR, "Unable to create pipe for fork");
39 }
40
41 pid_t pid;
42 pid_t *child = malloc(sizeof(pid_t)); // malloc'd so that Linux can avoid copying the process space
43 if (!child) {
44 return cmd_results_new(CMD_FAILURE, "exec_always", "Unable to allocate child pid");
45 }
46 // Fork process
47 if ((pid = fork()) == 0) {
48 // Fork child process again
49 setsid();
50 if ((*child = fork()) == 0) {
51 execl("/bin/sh", "/bin/sh", "-c", cmd, (void *)NULL);
52 /* Not reached */
53 }
54 close(fd[0]);
55 ssize_t s = 0;
56 while ((size_t)s < sizeof(pid_t)) {
57 s += write(fd[1], ((uint8_t *)child) + s, sizeof(pid_t) - s);
58 }
59 close(fd[1]);
60 _exit(0); // Close child process
61 } else if (pid < 0) {
62 free(child);
63 return cmd_results_new(CMD_FAILURE, "exec_always", "fork() failed");
64 }
65 close(fd[1]); // close write
66 ssize_t s = 0;
67 while ((size_t)s < sizeof(pid_t)) {
68 s += read(fd[0], ((uint8_t *)child) + s, sizeof(pid_t) - s);
69 }
70 close(fd[0]);
71 // cleanup child process
72 wait(0);
73 swayc_t *ws = swayc_active_workspace();
74 if (*child > 0 && ws) {
75 sway_log(L_DEBUG, "Child process created with pid %d for workspace %s", *child, ws->name);
76 struct pid_workspace *pw = malloc(sizeof(struct pid_workspace));
77 pw->pid = child;
78 pw->workspace = strdup(ws->name);
79 pid_workspace_add(pw);
80 } else {
81 free(child);
82 }
83
84 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
85}