From fcb2dbd80c8adc5581b4e0e92046d8f5b11d1147 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Mon, 12 Aug 2019 20:10:48 +0100 Subject: Add imv-msg command --- src/imv_msg.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/ipc.c | 17 ++++------------- src/ipc.h | 4 ++++ src/ipc_common.c | 14 ++++++++++++++ 4 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 src/imv_msg.c create mode 100644 src/ipc_common.c (limited to 'src') diff --git a/src/imv_msg.c b/src/imv_msg.c new file mode 100644 index 0000000..505b843 --- /dev/null +++ b/src/imv_msg.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include + +#include "ipc.h" + +int main(int argc, char **argv) +{ + if (argc < 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 0; + } + + int sockfd = socket(AF_UNIX, SOCK_STREAM, 0); + assert(sockfd); + + struct sockaddr_un desc = { + .sun_family = AF_UNIX + }; + imv_ipc_path(desc.sun_path, sizeof desc.sun_path, atoi(argv[1])); + + if (connect(sockfd, (struct sockaddr *)&desc, sizeof desc) < 0) { + perror("Failed to connect"); + return 1; + } + + char buf[4096] = {0}; + for (int i = 2; i < argc; ++i) { + strncat(buf, argv[i], sizeof buf - 1); + if (i + 1 < argc) { + strncat(buf, " ", sizeof buf - 1); + } + } + strncat(buf, "\n", sizeof buf - 1); + + write(sockfd, buf, strlen(buf)); + close(sockfd); + return 0; +} diff --git a/src/ipc.c b/src/ipc.c index f5f3e46..0b42e22 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -19,16 +19,7 @@ struct connection { int fd; }; -static void get_ipc_filename(char *buf, size_t len) -{ - const char *base = getenv("XDG_RUNTIME_DIR"); - if (!base) { - base = "/tmp"; - } - snprintf(buf, len, "%s/imv-%d.sock", base, getpid()); -} - -void *wait_for_commands(void* void_conn) +static void *wait_for_commands(void* void_conn) { struct connection *conn = void_conn; @@ -55,7 +46,7 @@ void *wait_for_commands(void* void_conn) return NULL; } -void *wait_for_connections(void* void_ipc) +static void *wait_for_connections(void* void_ipc) { struct imv_ipc *ipc = void_ipc; (void)ipc; @@ -86,7 +77,7 @@ struct imv_ipc *imv_ipc_create(void) struct sockaddr_un desc = { .sun_family = AF_UNIX }; - get_ipc_filename(desc.sun_path, sizeof desc.sun_path); + imv_ipc_path(desc.sun_path, sizeof desc.sun_path, getpid()); unlink(desc.sun_path); @@ -115,7 +106,7 @@ void imv_ipc_free(struct imv_ipc *ipc) } char ipc_filename[1024]; - get_ipc_filename(ipc_filename, sizeof ipc_filename); + imv_ipc_path(ipc_filename, sizeof ipc_filename, getpid()); unlink(ipc_filename); close(ipc->fd); diff --git a/src/ipc.h b/src/ipc.h index 9737a8f..539b684 100644 --- a/src/ipc.h +++ b/src/ipc.h @@ -1,6 +1,8 @@ #ifndef IMV_IPC_H #define IMV_IPC_H +#include + struct imv_ipc; struct imv_ipc *imv_ipc_create(void); @@ -12,4 +14,6 @@ typedef void (*imv_ipc_callback)(const char *command, void *data); void imv_ipc_set_command_callback(struct imv_ipc *ipc, imv_ipc_callback callback, void *data); +void imv_ipc_path(char *buf, size_t len, int pid); + #endif diff --git a/src/ipc_common.c b/src/ipc_common.c new file mode 100644 index 0000000..9943975 --- /dev/null +++ b/src/ipc_common.c @@ -0,0 +1,14 @@ +#include "ipc.h" + +#include +#include + +void imv_ipc_path(char *buf, size_t len, int pid) +{ + const char *base = getenv("XDG_RUNTIME_DIR"); + if (!base) { + base = "/tmp"; + } + snprintf(buf, len, "%s/imv-%d.sock", base, pid); +} + -- cgit v1.2.3