2018-08-05 17:24:42 +10:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2018-08-03 11:37:29 +10:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2019-04-14 14:27:47 +10:00
|
|
|
#include <sys/socket.h>
|
2018-08-03 11:37:29 +10:00
|
|
|
#include <sys/types.h>
|
2019-04-14 14:27:47 +10:00
|
|
|
#include <sys/wait.h>
|
2018-08-03 11:37:29 +10:00
|
|
|
#include <unistd.h>
|
|
|
|
#include "log.h"
|
2019-04-14 14:27:47 +10:00
|
|
|
#include "sway/server.h"
|
2018-08-03 11:37:29 +10:00
|
|
|
#include "sway/swaynag.h"
|
2019-04-14 14:27:47 +10:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void handle_swaynag_client_destroy(struct wl_listener *listener,
|
|
|
|
void *data) {
|
|
|
|
struct swaynag_instance *swaynag =
|
|
|
|
wl_container_of(listener, swaynag, client_destroy);
|
|
|
|
wl_list_remove(&swaynag->client_destroy.link);
|
|
|
|
wl_list_init(&swaynag->client_destroy.link);
|
|
|
|
swaynag->client = NULL;
|
|
|
|
}
|
2018-08-03 11:37:29 +10:00
|
|
|
|
|
|
|
bool swaynag_spawn(const char *swaynag_command,
|
|
|
|
struct swaynag_instance *swaynag) {
|
2019-04-14 14:27:47 +10:00
|
|
|
if (swaynag->client != NULL) {
|
|
|
|
wl_client_destroy(swaynag->client);
|
|
|
|
}
|
|
|
|
|
2018-10-09 00:56:34 +11:00
|
|
|
if (!swaynag_command) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-03 11:37:29 +10:00
|
|
|
if (swaynag->detailed) {
|
|
|
|
if (pipe(swaynag->fd) != 0) {
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_ERROR, "Failed to create pipe for swaynag");
|
2018-08-03 11:37:29 +10:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-14 14:27:47 +10:00
|
|
|
if (!set_cloexec(swaynag->fd[1], true)) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2018-08-03 11:37:29 +10:00
|
|
|
}
|
|
|
|
|
2019-04-14 14:27:47 +10:00
|
|
|
int sockets[2];
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
|
|
|
|
sway_log_errno(SWAY_ERROR, "socketpair failed");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
if (!set_cloexec(sockets[0], true) || !set_cloexec(sockets[1], true)) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2018-08-03 11:37:29 +10:00
|
|
|
|
2019-04-14 14:27:47 +10:00
|
|
|
swaynag->client = wl_client_create(server.wl_display, sockets[0]);
|
|
|
|
if (swaynag->client == NULL) {
|
|
|
|
sway_log_errno(SWAY_ERROR, "wl_client_create failed");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
swaynag->client_destroy.notify = handle_swaynag_client_destroy;
|
|
|
|
wl_client_add_destroy_listener(swaynag->client, &swaynag->client_destroy);
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_ERROR, "Failed to create fork for swaynag");
|
2019-04-14 14:27:47 +10:00
|
|
|
goto failed;
|
|
|
|
} else if (pid == 0) {
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
sway_log_errno(SWAY_ERROR, "fork failed");
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
} else if (pid == 0) {
|
|
|
|
if (!set_cloexec(sockets[1], false)) {
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swaynag->detailed) {
|
|
|
|
close(swaynag->fd[1]);
|
|
|
|
dup2(swaynag->fd[0], STDIN_FILENO);
|
|
|
|
close(swaynag->fd[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
char wayland_socket_str[16];
|
|
|
|
snprintf(wayland_socket_str, sizeof(wayland_socket_str),
|
|
|
|
"%d", sockets[1]);
|
|
|
|
setenv("WAYLAND_SOCKET", wayland_socket_str, true);
|
|
|
|
|
|
|
|
size_t length = strlen(swaynag_command) + strlen(swaynag->args) + 2;
|
|
|
|
char *cmd = malloc(length);
|
|
|
|
snprintf(cmd, length, "%s %s", swaynag_command, swaynag->args);
|
|
|
|
execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
|
|
|
|
sway_log_errno(SWAY_ERROR, "execl failed");
|
|
|
|
_exit(EXIT_FAILURE);
|
2018-08-03 11:37:29 +10:00
|
|
|
}
|
2019-04-14 14:27:47 +10:00
|
|
|
_exit(EXIT_SUCCESS);
|
2018-08-03 11:37:29 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
if (swaynag->detailed) {
|
2019-04-14 14:27:47 +10:00
|
|
|
if (close(swaynag->fd[0]) != 0) {
|
|
|
|
sway_log_errno(SWAY_ERROR, "close failed");
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-03 11:37:29 +10:00
|
|
|
}
|
|
|
|
|
2019-04-14 14:27:47 +10:00
|
|
|
if (close(sockets[1]) != 0) {
|
|
|
|
sway_log_errno(SWAY_ERROR, "close failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (waitpid(pid, NULL, 0) < 0) {
|
|
|
|
sway_log_errno(SWAY_ERROR, "waitpid failed");
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-03 11:37:29 +10:00
|
|
|
|
2019-04-14 14:27:47 +10:00
|
|
|
return true;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
if (swaynag->detailed) {
|
|
|
|
if (close(swaynag->fd[0]) != 0) {
|
|
|
|
sway_log_errno(SWAY_ERROR, "close failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (close(swaynag->fd[1]) != 0) {
|
|
|
|
sway_log_errno(SWAY_ERROR, "close failed");
|
|
|
|
}
|
2018-08-03 11:37:29 +10:00
|
|
|
}
|
2019-04-14 14:27:47 +10:00
|
|
|
return false;
|
2018-08-03 11:37:29 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
void swaynag_log(const char *swaynag_command, struct swaynag_instance *swaynag,
|
|
|
|
const char *fmt, ...) {
|
2018-10-09 00:56:34 +11:00
|
|
|
if (!swaynag_command) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-03 11:37:29 +10:00
|
|
|
if (!swaynag->detailed) {
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_ERROR, "Attempting to write to non-detailed swaynag inst");
|
2018-08-03 11:37:29 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-14 14:27:47 +10:00
|
|
|
if (swaynag->client == NULL && !swaynag_spawn(swaynag_command, swaynag)) {
|
2018-08-03 11:37:29 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
size_t length = vsnprintf(NULL, 0, fmt, args) + 1;
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
char *temp = malloc(length + 1);
|
|
|
|
if (!temp) {
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_ERROR, "Failed to allocate buffer for swaynag log entry.");
|
2018-08-03 11:37:29 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(temp, length, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
write(swaynag->fd[1], temp, length);
|
|
|
|
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void swaynag_show(struct swaynag_instance *swaynag) {
|
2019-04-14 14:27:47 +10:00
|
|
|
if (swaynag->detailed && swaynag->client != NULL) {
|
2018-08-03 11:37:29 +10:00
|
|
|
close(swaynag->fd[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|