aboutsummaryrefslogtreecommitdiffstats
path: root/src/runesc.c
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2016-05-02 04:21:26 -0400
committerJesse Luehrs <doy@tozt.net>2016-05-02 04:37:17 -0400
commita513d9943fb71607ac8c0f2f3695dec9706f1ebb (patch)
treed55dacb37503fd6749009d61fa84c8866d668f5f /src/runesc.c
parent957ef4d81438a74d530f2a3890bfcd0145c11fd9 (diff)
downloadrunes-a513d9943fb71607ac8c0f2f3695dec9706f1ebb.tar.gz
runes-a513d9943fb71607ac8c0f2f3695dec9706f1ebb.zip
start working on a client/server model similar to urxvtd/urxvtc
the code is still kind of a mess, and it doesn't quite work properly yet, but it's close enough to be a start, i think
Diffstat (limited to 'src/runesc.c')
-rw-r--r--src/runesc.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/runesc.c b/src/runesc.c
new file mode 100644
index 0000000..04fc26b
--- /dev/null
+++ b/src/runesc.c
@@ -0,0 +1,66 @@
+#include <arpa/inet.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+
+#include "runes.h"
+#include "socket.h"
+
+static int runes_socket_open_client(char *name);
+
+int main (int argc, char *argv[])
+{
+ char *name, *buf;
+ int s, i;
+ size_t len, offset;
+ uint32_t argc32 = argc, argv_len;
+
+ name = runes_get_socket_name();
+ s = runes_socket_open_client(name);
+
+ len = sizeof(argc32) + sizeof(argv_len);
+ for (i = 0; i < argc; ++i) {
+ len += strlen(argv[i]) + 1;
+ }
+
+ buf = malloc(len);
+ ((uint32_t*)buf)[0] = htonl(argc32);
+ offset = sizeof(argc32) + sizeof(argv_len);
+ ((uint32_t*)buf)[1] = htonl(len - offset);
+
+ for (i = 0; i < argc; ++i) {
+ size_t arg_len = strlen(argv[i]) + 1;
+ memcpy(buf + offset, argv[i], arg_len);
+ offset += arg_len;
+ }
+
+ send(s, buf, offset, 0);
+ free(buf);
+ shutdown(s, SHUT_RDWR);
+}
+
+static int runes_socket_open_client(char *name)
+{
+ int s;
+ struct sockaddr_un client;
+
+ if (strlen(name) + 1 > MAX_SOCKET_PATH_LEN) {
+ runes_die("socket path %s is too long\n", name);
+ }
+
+ s = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (s < 0) {
+ runes_die("couldn't create socket: %s\n", strerror(errno));
+ }
+
+ client.sun_family = AF_UNIX;
+ strcpy(client.sun_path, name);
+ if (connect(s, (struct sockaddr*)(&client), sizeof(struct sockaddr_un))) {
+ runes_die("couldn't connect to socket at %s: %s\n", name,
+ strerror(errno));
+ }
+
+ return s;
+}