aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-08-12 20:10:48 +0100
committerHarry Jeffery <harry@exec64.co.uk>2019-08-12 20:10:48 +0100
commitfcb2dbd80c8adc5581b4e0e92046d8f5b11d1147 (patch)
tree0265921f78272730c2038bdcaad9682862892bc5 /src
parentc7cbdf0139cae2445b62df2af5f7a64b4ab925dc (diff)
downloadimv-fcb2dbd80c8adc5581b4e0e92046d8f5b11d1147.tar.gz
Add imv-msg command
Diffstat (limited to 'src')
-rw-r--r--src/imv_msg.c42
-rw-r--r--src/ipc.c17
-rw-r--r--src/ipc.h4
-rw-r--r--src/ipc_common.c14
4 files changed, 64 insertions, 13 deletions
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 <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "ipc.h"
+
+int main(int argc, char **argv)
+{
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s <pid> <command>\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 <unistd.h>
+
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 <stdio.h>
+#include <stdlib.h>
+
+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);
+}
+