This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.c
1202 lines (1005 loc) · 33.3 KB
/
ipc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ipc.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <yajl/yajl_gen.h>
#include <yajl/yajl_tree.h>
#include "util.h"
#include "yajl_dumps.h"
static struct sockaddr_un sockaddr;
static struct epoll_event sock_epoll_event;
static IPCClientList ipc_clients = NULL;
static int epoll_fd = -1;
static int sock_fd = -1;
static IPCCommand *ipc_commands;
static unsigned int ipc_commands_len;
// Max size is 1 MB
static const uint32_t MAX_MESSAGE_SIZE = 1000000;
static const int IPC_SOCKET_BACKLOG = 5;
/**
* Create IPC socket at specified path and return file descriptor to socket.
* This initializes the static variable sockaddr.
*/
static int
ipc_create_socket(const char *filename)
{
char *normal_filename;
char *parent;
const size_t addr_size = sizeof(struct sockaddr_un);
const int sock_type = SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC;
normalizepath(filename, &normal_filename);
// In case socket file exists
unlink(normal_filename);
// For portability clear the addr structure, since some implementations have
// nonstandard fields in the structure
memset(&sockaddr, 0, addr_size);
parentdir(normal_filename, &parent);
// Create parent directories
mkdirp(parent);
free(parent);
sockaddr.sun_family = AF_LOCAL;
strcpy(sockaddr.sun_path, normal_filename);
free(normal_filename);
sock_fd = socket(AF_LOCAL, sock_type, 0);
if (sock_fd == -1) {
fputs("Failed to create socket\n", stderr);
return -1;
}
DEBUG("Created socket at %s\n", sockaddr.sun_path);
if (bind(sock_fd, (const struct sockaddr *)&sockaddr, addr_size) == -1) {
fputs("Failed to bind socket\n", stderr);
return -1;
}
DEBUG("Socket binded\n");
if (listen(sock_fd, IPC_SOCKET_BACKLOG) < 0) {
fputs("Failed to listen for connections on socket\n", stderr);
return -1;
}
DEBUG("Now listening for connections on socket\n");
return sock_fd;
}
/**
* Internal function used to receive IPC messages from a given file descriptor.
*
* Returns -1 on error reading (could be EAGAIN or EINTR)
* Returns -2 if EOF before header could be read
* Returns -3 if invalid IPC header
* Returns -4 if message length exceeds MAX_MESSAGE_SIZE
*/
static int
ipc_recv_message(int fd, uint8_t *msg_type, uint32_t *reply_size,
uint8_t **reply)
{
uint32_t read_bytes = 0;
const int32_t to_read = sizeof(dwm_ipc_header_t);
char header[to_read];
char *walk = header;
// Try to read header
while (read_bytes < to_read) {
const ssize_t n = read(fd, header + read_bytes, to_read - read_bytes);
if (n == 0) {
if (read_bytes == 0) {
fprintf(stderr, "Unexpectedly reached EOF while reading header.");
fprintf(stderr,
"Read %" PRIu32 " bytes, expected %" PRIu32 " total bytes.\n",
read_bytes, to_read);
return -2;
} else {
fprintf(stderr, "Unexpectedly reached EOF while reading header.");
fprintf(stderr,
"Read %" PRIu32 " bytes, expected %" PRIu32 " total bytes.\n",
read_bytes, to_read);
return -3;
}
} else if (n == -1) {
// errno will still be set
return -1;
}
read_bytes += n;
}
// Check if magic string in header matches
if (memcmp(walk, IPC_MAGIC, IPC_MAGIC_LEN) != 0) {
fprintf(stderr, "Invalid magic string. Got '%.*s', expected '%s'\n",
IPC_MAGIC_LEN, walk, IPC_MAGIC);
return -3;
}
walk += IPC_MAGIC_LEN;
// Extract reply size
memcpy(reply_size, walk, sizeof(uint32_t));
walk += sizeof(uint32_t);
if (*reply_size > MAX_MESSAGE_SIZE) {
fprintf(stderr, "Message too long: %" PRIu32 " bytes. ", *reply_size);
fprintf(stderr, "Maximum message size is: %d\n", MAX_MESSAGE_SIZE);
return -4;
}
// Extract message type
memcpy(msg_type, walk, sizeof(uint8_t));
walk += sizeof(uint8_t);
if (*reply_size > 0)
(*reply) = malloc(*reply_size);
else
return 0;
read_bytes = 0;
while (read_bytes < *reply_size) {
const ssize_t n = read(fd, *reply + read_bytes, *reply_size - read_bytes);
if (n == 0) {
fprintf(stderr, "Unexpectedly reached EOF while reading payload.");
fprintf(stderr, "Read %" PRIu32 " bytes, expected %" PRIu32 " bytes.\n",
read_bytes, *reply_size);
free(*reply);
return -2;
} else if (n == -1) {
// TODO: Should we return and wait for another epoll event?
// This would require saving the partial read in some way.
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) continue;
free(*reply);
return -1;
}
read_bytes += n;
}
return 0;
}
/**
* Internal function used to write a buffer to a file descriptor
*
* Returns number of bytes written if successful write
* Returns 0 if no bytes were written due to EAGAIN or EWOULDBLOCK
* Returns -1 on unknown error trying to write, errno will carry over from
* write() call
*/
static ssize_t
ipc_write_message(int fd, const void *buf, size_t count)
{
size_t written = 0;
while (written < count) {
const ssize_t n = write(fd, (uint8_t *)buf + written, count - written);
if (n == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return written;
else if (errno == EINTR)
continue;
else
return n;
}
written += n;
DEBUG("Wrote %zu/%zu to client at fd %d\n", written, count, fd);
}
return written;
}
/**
* Initialization for generic event message. This is used to allocate the yajl
* handle, set yajl options, and in the future any other initialization that
* should occur for event messages.
*/
static void
ipc_event_init_message(yajl_gen *gen)
{
*gen = yajl_gen_alloc(NULL);
yajl_gen_config(*gen, yajl_gen_beautify, 1);
}
/**
* Prepares buffers of IPC subscribers of specified event using buffer from yajl
* handle.
*/
static void
ipc_event_prepare_send_message(yajl_gen gen, IPCEvent event)
{
const unsigned char *buffer;
size_t len = 0;
yajl_gen_get_buf(gen, &buffer, &len);
len++; // For null char
for (IPCClient *c = ipc_clients; c; c = c->next) {
if (c->subscriptions & event) {
DEBUG("Sending selected client change event to fd %d\n", c->fd);
ipc_prepare_send_message(c, IPC_TYPE_EVENT, len, (char *)buffer);
}
}
// Not documented, but this frees temp_buffer
yajl_gen_free(gen);
}
/**
* Initialization for generic reply message. This is used to allocate the yajl
* handle, set yajl options, and in the future any other initialization that
* should occur for reply messages.
*/
static void
ipc_reply_init_message(yajl_gen *gen)
{
*gen = yajl_gen_alloc(NULL);
yajl_gen_config(*gen, yajl_gen_beautify, 1);
}
/**
* Prepares the IPC client's buffer with a message using the buffer of the yajl
* handle.
*/
static void
ipc_reply_prepare_send_message(yajl_gen gen, IPCClient *c,
IPCMessageType msg_type)
{
const unsigned char *buffer;
size_t len = 0;
yajl_gen_get_buf(gen, &buffer, &len);
len++; // For null char
ipc_prepare_send_message(c, msg_type, len, (const char *)buffer);
// Not documented, but this frees temp_buffer
yajl_gen_free(gen);
}
/**
* Find the IPCCommand with the specified name
*
* Returns 0 if a command with the specified name was found
* Returns -1 if a command with the specified name could not be found
*/
static int
ipc_get_ipc_command(const char *name, IPCCommand *ipc_command)
{
for (int i = 0; i < ipc_commands_len; i++) {
if (strcmp(ipc_commands[i].name, name) == 0) {
*ipc_command = ipc_commands[i];
return 0;
}
}
return -1;
}
/**
* Parse a IPC_TYPE_RUN_COMMAND message from a client. This function extracts
* the arguments, argument count, argument types, and command name and returns
* the parsed information as an IPCParsedCommand. If this function returns
* successfully, the parsed_command must be freed using
* ipc_free_parsed_command_members.
*
* Returns 0 if the message was successfully parsed
* Returns -1 otherwise
*/
static int
ipc_parse_run_command(char *msg, IPCParsedCommand *parsed_command)
{
char error_buffer[1000];
yajl_val parent = yajl_tree_parse(msg, error_buffer, 1000);
if (parent == NULL) {
fputs("Failed to parse command from client\n", stderr);
fprintf(stderr, "%s\n", error_buffer);
fprintf(stderr, "Tried to parse: %s\n", msg);
return -1;
}
// Format:
// {
// "command": "<command name>"
// "args": [ "arg1", "arg2", ... ]
// }
const char *command_path[] = {"command", 0};
yajl_val command_val = yajl_tree_get(parent, command_path, yajl_t_string);
if (command_val == NULL) {
fputs("No command key found in client message\n", stderr);
yajl_tree_free(parent);
return -1;
}
const char *command_name = YAJL_GET_STRING(command_val);
size_t command_name_len = strlen(command_name);
parsed_command->name = (char *)malloc((command_name_len + 1) * sizeof(char));
strcpy(parsed_command->name, command_name);
DEBUG("Received command: %s\n", parsed_command->name);
const char *args_path[] = {"args", 0};
yajl_val args_val = yajl_tree_get(parent, args_path, yajl_t_array);
if (args_val == NULL) {
fputs("No args key found in client message\n", stderr);
yajl_tree_free(parent);
return -1;
}
unsigned int *argc = &parsed_command->argc;
Arg **args = &parsed_command->args;
ArgType **arg_types = &parsed_command->arg_types;
*argc = args_val->u.array.len;
// If no arguments are specified, make a dummy argument to pass to the
// function. This is just the way dwm's void(Arg*) functions are setup.
if (*argc == 0) {
*args = (Arg *)malloc(sizeof(Arg));
*arg_types = (ArgType *)malloc(sizeof(ArgType));
(*arg_types)[0] = ARG_TYPE_NONE;
(*args)[0].i = 0;
(*argc)++;
} else if (*argc > 0) {
*args = (Arg *)calloc(*argc, sizeof(Arg));
*arg_types = (ArgType *)malloc(*argc * sizeof(ArgType));
for (int i = 0; i < *argc; i++) {
yajl_val arg_val = args_val->u.array.values[i];
if (YAJL_IS_NUMBER(arg_val)) {
if (YAJL_IS_INTEGER(arg_val)) {
// Any values below 0 must be a signed int
if (YAJL_GET_INTEGER(arg_val) < 0) {
(*args)[i].i = YAJL_GET_INTEGER(arg_val);
(*arg_types)[i] = ARG_TYPE_SINT;
DEBUG("i=%ld\n", (*args)[i].i);
// Any values above 0 should be an unsigned int
} else if (YAJL_GET_INTEGER(arg_val) >= 0) {
(*args)[i].ui = YAJL_GET_INTEGER(arg_val);
(*arg_types)[i] = ARG_TYPE_UINT;
DEBUG("ui=%ld\n", (*args)[i].i);
}
// If the number is not an integer, it must be a float
} else {
(*args)[i].f = (float)YAJL_GET_DOUBLE(arg_val);
(*arg_types)[i] = ARG_TYPE_FLOAT;
DEBUG("f=%f\n", (*args)[i].f);
// If argument is not a number, it must be a string
}
} else if (YAJL_IS_STRING(arg_val)) {
char *arg_s = YAJL_GET_STRING(arg_val);
size_t arg_s_size = (strlen(arg_s) + 1) * sizeof(char);
(*args)[i].v = (char *)malloc(arg_s_size);
(*arg_types)[i] = ARG_TYPE_STR;
strcpy((char *)(*args)[i].v, arg_s);
}
}
}
yajl_tree_free(parent);
return 0;
}
/**
* Free the members of a IPCParsedCommand struct
*/
static void
ipc_free_parsed_command_members(IPCParsedCommand *command)
{
for (int i = 0; i < command->argc; i++) {
if (command->arg_types[i] == ARG_TYPE_STR) free((void *)command->args[i].v);
}
free(command->args);
free(command->arg_types);
free(command->name);
}
/**
* Check if the given arguments are the correct length and type. Also do any
* casting to correct the types.
*
* Returns 0 if the arguments were the correct length and types
* Returns -1 if the argument count doesn't match
* Returns -2 if the argument types don't match
*/
static int
ipc_validate_run_command(IPCParsedCommand *parsed, const IPCCommand actual)
{
if (actual.argc != parsed->argc) return -1;
for (int i = 0; i < parsed->argc; i++) {
ArgType ptype = parsed->arg_types[i];
ArgType atype = actual.arg_types[i];
if (ptype != atype) {
if (ptype == ARG_TYPE_UINT && atype == ARG_TYPE_PTR)
// If this argument is supposed to be a void pointer, cast it
parsed->args[i].v = (void *)parsed->args[i].ui;
else if (ptype == ARG_TYPE_UINT && atype == ARG_TYPE_SINT)
// If this argument is supposed to be a signed int, cast it
parsed->args[i].i = parsed->args[i].ui;
else
return -2;
}
}
return 0;
}
/**
* Convert event name to their IPCEvent equivalent enum value
*
* Returns 0 if a valid event name was given
* Returns -1 otherwise
*/
static int
ipc_event_stoi(const char *subscription, IPCEvent *event)
{
if (strcmp(subscription, "tag_change_event") == 0)
*event = IPC_EVENT_TAG_CHANGE;
else if (strcmp(subscription, "client_focus_change_event") == 0)
*event = IPC_EVENT_CLIENT_FOCUS_CHANGE;
else if (strcmp(subscription, "layout_change_event") == 0)
*event = IPC_EVENT_LAYOUT_CHANGE;
else if (strcmp(subscription, "monitor_focus_change_event") == 0)
*event = IPC_EVENT_MONITOR_FOCUS_CHANGE;
else if (strcmp(subscription, "focused_title_change_event") == 0)
*event = IPC_EVENT_FOCUSED_TITLE_CHANGE;
else if (strcmp(subscription, "focused_state_change_event") == 0)
*event = IPC_EVENT_FOCUSED_STATE_CHANGE;
else
return -1;
return 0;
}
/**
* Parse a IPC_TYPE_SUBSCRIBE message from a client. This function extracts the
* event name and the subscription action from the message.
*
* Returns 0 if message was successfully parsed
* Returns -1 otherwise
*/
static int
ipc_parse_subscribe(const char *msg, IPCSubscriptionAction *subscribe,
IPCEvent *event)
{
char error_buffer[100];
yajl_val parent = yajl_tree_parse((char *)msg, error_buffer, 100);
if (parent == NULL) {
fputs("Failed to parse command from client\n", stderr);
fprintf(stderr, "%s\n", error_buffer);
return -1;
}
// Format:
// {
// "event": "<event name>"
// "action": "<subscribe|unsubscribe>"
// }
const char *event_path[] = {"event", 0};
yajl_val event_val = yajl_tree_get(parent, event_path, yajl_t_string);
if (event_val == NULL) {
fputs("No 'event' key found in client message\n", stderr);
return -1;
}
const char *event_str = YAJL_GET_STRING(event_val);
DEBUG("Received event: %s\n", event_str);
if (ipc_event_stoi(event_str, event) < 0) return -1;
const char *action_path[] = {"action", 0};
yajl_val action_val = yajl_tree_get(parent, action_path, yajl_t_string);
if (action_val == NULL) {
fputs("No 'action' key found in client message\n", stderr);
return -1;
}
const char *action = YAJL_GET_STRING(action_val);
if (strcmp(action, "subscribe") == 0)
*subscribe = IPC_ACTION_SUBSCRIBE;
else if (strcmp(action, "unsubscribe") == 0)
*subscribe = IPC_ACTION_UNSUBSCRIBE;
else {
fputs("Invalid action specified for subscription\n", stderr);
return -1;
}
yajl_tree_free(parent);
return 0;
}
/**
* Parse an IPC_TYPE_GET_DWM_CLIENT message from a client. This function
* extracts the window id from the message.
*
* Returns 0 if message was successfully parsed
* Returns -1 otherwise
*/
static int
ipc_parse_get_dwm_client(const char *msg, Window *win)
{
char error_buffer[100];
yajl_val parent = yajl_tree_parse(msg, error_buffer, 100);
if (parent == NULL) {
fputs("Failed to parse message from client\n", stderr);
fprintf(stderr, "%s\n", error_buffer);
return -1;
}
// Format:
// {
// "client_window_id": <client window id>
// }
const char *win_path[] = {"client_window_id", 0};
yajl_val win_val = yajl_tree_get(parent, win_path, yajl_t_number);
if (win_val == NULL) {
fputs("No client window id found in client message\n", stderr);
return -1;
}
*win = YAJL_GET_INTEGER(win_val);
yajl_tree_free(parent);
return 0;
}
/**
* Called when an IPC_TYPE_RUN_COMMAND message is received from a client. This
* function parses, executes the given command, and prepares a reply message to
* the client indicating success/failure.
*
* NOTE: There is currently no check for argument validity beyond the number of
* arguments given and types of arguments. There is also no way to check if the
* function succeeded based on dwm's void(const Arg*) function types. Pointer
* arguments can cause crashes if they are not validated in the function itself.
*
* Returns 0 if message was successfully parsed
* Returns -1 on failure parsing message
*/
static int
ipc_run_command(IPCClient *ipc_client, char *msg)
{
IPCParsedCommand parsed_command;
IPCCommand ipc_command;
// Initialize struct
memset(&parsed_command, 0, sizeof(IPCParsedCommand));
if (ipc_parse_run_command(msg, &parsed_command) < 0) {
ipc_prepare_reply_failure(ipc_client, IPC_TYPE_RUN_COMMAND,
"Failed to parse run command");
return -1;
}
if (ipc_get_ipc_command(parsed_command.name, &ipc_command) < 0) {
ipc_prepare_reply_failure(ipc_client, IPC_TYPE_RUN_COMMAND,
"Command %s not found", parsed_command.name);
ipc_free_parsed_command_members(&parsed_command);
return -1;
}
int res = ipc_validate_run_command(&parsed_command, ipc_command);
if (res < 0) {
if (res == -1)
ipc_prepare_reply_failure(ipc_client, IPC_TYPE_RUN_COMMAND,
"%u arguments provided, %u expected",
parsed_command.argc, ipc_command.argc);
else if (res == -2)
ipc_prepare_reply_failure(ipc_client, IPC_TYPE_RUN_COMMAND,
"Type mismatch");
ipc_free_parsed_command_members(&parsed_command);
return -1;
}
if (parsed_command.argc == 1)
ipc_command.func.single_param(parsed_command.args);
else if (parsed_command.argc > 1)
ipc_command.func.array_param(parsed_command.args, parsed_command.argc);
DEBUG("Called function for command %s\n", parsed_command.name);
ipc_free_parsed_command_members(&parsed_command);
ipc_prepare_reply_success(ipc_client, IPC_TYPE_RUN_COMMAND);
return 0;
}
/**
* Called when an IPC_TYPE_GET_MONITORS message is received from a client. It
* prepares a reply with the properties of all of the monitors in JSON.
*/
static void
ipc_get_monitors(IPCClient *c, Monitor *mons, Monitor *selmon)
{
yajl_gen gen;
ipc_reply_init_message(&gen);
dump_monitors(gen, mons, selmon);
ipc_reply_prepare_send_message(gen, c, IPC_TYPE_GET_MONITORS);
}
/**
* Called when an IPC_TYPE_GET_TAGS message is received from a client. It
* prepares a reply with info about all the tags in JSON.
*/
static void
ipc_get_tags(IPCClient *c, const char *tags[], const int tags_len)
{
yajl_gen gen;
ipc_reply_init_message(&gen);
dump_tags(gen, tags, tags_len);
ipc_reply_prepare_send_message(gen, c, IPC_TYPE_GET_TAGS);
}
/**
* Called when an IPC_TYPE_GET_LAYOUTS message is received from a client. It
* prepares a reply with a JSON array of available layouts
*/
static void
ipc_get_layouts(IPCClient *c, const Layout layouts[], const int layouts_len)
{
yajl_gen gen;
ipc_reply_init_message(&gen);
dump_layouts(gen, layouts, layouts_len);
ipc_reply_prepare_send_message(gen, c, IPC_TYPE_GET_LAYOUTS);
}
/**
* Called when an IPC_TYPE_GET_DWM_CLIENT message is received from a client. It
* prepares a JSON reply with the properties of the client with the specified
* window XID.
*
* Returns 0 if the message was successfully parsed and if the client with the
* specified window XID was found
* Returns -1 if the message could not be parsed
*/
static int
ipc_get_dwm_client(IPCClient *ipc_client, const char *msg, const Monitor *mons)
{
Window win;
if (ipc_parse_get_dwm_client(msg, &win) < 0) return -1;
// Find client with specified window XID
for (const Monitor *m = mons; m; m = m->next)
for (Client *c = m->clients; c; c = c->next)
if (c->win == win) {
yajl_gen gen;
ipc_reply_init_message(&gen);
dump_client(gen, c);
ipc_reply_prepare_send_message(gen, ipc_client,
IPC_TYPE_GET_DWM_CLIENT);
return 0;
}
ipc_prepare_reply_failure(ipc_client, IPC_TYPE_GET_DWM_CLIENT,
"Client with window id %d not found", win);
return -1;
}
/**
* Called when an IPC_TYPE_SUBSCRIBE message is received from a client. It
* subscribes/unsubscribes the client from the specified event and replies with
* the result.
*
* Returns 0 if the message was successfully parsed.
* Returns -1 if the message could not be parsed
*/
static int
ipc_subscribe(IPCClient *c, const char *msg)
{
IPCSubscriptionAction action = IPC_ACTION_SUBSCRIBE;
IPCEvent event = 0;
if (ipc_parse_subscribe(msg, &action, &event)) {
ipc_prepare_reply_failure(c, IPC_TYPE_SUBSCRIBE, "Event does not exist");
return -1;
}
if (action == IPC_ACTION_SUBSCRIBE) {
DEBUG("Subscribing client on fd %d to %d\n", c->fd, event);
c->subscriptions |= event;
} else if (action == IPC_ACTION_UNSUBSCRIBE) {
DEBUG("Unsubscribing client on fd %d to %d\n", c->fd, event);
c->subscriptions ^= event;
} else {
ipc_prepare_reply_failure(c, IPC_TYPE_SUBSCRIBE,
"Invalid subscription action");
return -1;
}
ipc_prepare_reply_success(c, IPC_TYPE_SUBSCRIBE);
return 0;
}
int
ipc_init(const char *socket_path, const int p_epoll_fd, IPCCommand commands[],
const int commands_len)
{
// Initialize struct to 0
memset(&sock_epoll_event, 0, sizeof(sock_epoll_event));
int socket_fd = ipc_create_socket(socket_path);
if (socket_fd < 0) return -1;
ipc_commands = commands;
ipc_commands_len = commands_len;
epoll_fd = p_epoll_fd;
// Wake up to incoming connection requests
sock_epoll_event.data.fd = socket_fd;
sock_epoll_event.events = EPOLLIN;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &sock_epoll_event)) {
fputs("Failed to add sock file descriptor to epoll", stderr);
return -1;
}
return socket_fd;
}
void
ipc_cleanup()
{
IPCClient *c = ipc_clients;
// Free clients and their buffers
while (c) {
ipc_drop_client(c);
c = ipc_clients;
}
// Stop waking up for socket events
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sock_fd, &sock_epoll_event);
// Uninitialize all static variables
epoll_fd = -1;
sock_fd = -1;
ipc_commands = NULL;
ipc_commands_len = 0;
memset(&sock_epoll_event, 0, sizeof(struct epoll_event));
memset(&sockaddr, 0, sizeof(struct sockaddr_un));
// Delete socket
unlink(sockaddr.sun_path);
shutdown(sock_fd, SHUT_RDWR);
close(sock_fd);
}
int
ipc_get_sock_fd()
{
return sock_fd;
}
IPCClient *
ipc_get_client(int fd)
{
return ipc_list_get_client(ipc_clients, fd);
}
int
ipc_is_client_registered(int fd)
{
return (ipc_get_client(fd) != NULL);
}
int
ipc_accept_client()
{
int fd = -1;
struct sockaddr_un client_addr;
socklen_t len = 0;
// For portability clear the addr structure, since some implementations
// have nonstandard fields in the structure
memset(&client_addr, 0, sizeof(struct sockaddr_un));
fd = accept(sock_fd, (struct sockaddr *)&client_addr, &len);
if (fd < 0 && errno != EINTR) {
fputs("Failed to accept IPC connection from client", stderr);
return -1;
}
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
shutdown(fd, SHUT_RDWR);
close(fd);
fputs("Failed to set flags on new client fd", stderr);
}
IPCClient *nc = ipc_client_new(fd);
if (nc == NULL) return -1;
// Wake up to messages from this client
nc->event.data.fd = fd;
nc->event.events = EPOLLIN | EPOLLHUP;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &nc->event);
ipc_list_add_client(&ipc_clients, nc);
DEBUG("%s%d\n", "New client at fd: ", fd);
return fd;
}
int
ipc_drop_client(IPCClient *c)
{
int fd = c->fd;
shutdown(fd, SHUT_RDWR);
int res = close(fd);
if (res == 0) {
struct epoll_event ev;
// Stop waking up to messages from this client
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev);
ipc_list_remove_client(&ipc_clients, c);
free(c->buffer);
free(c);
DEBUG("Successfully removed client on fd %d\n", fd);
} else if (res < 0 && res != EINTR) {
fprintf(stderr, "Failed to close fd %d\n", fd);
}
return res;
}
int
ipc_read_client(IPCClient *c, IPCMessageType *msg_type, uint32_t *msg_size,
char **msg)
{
int fd = c->fd;
int ret =
ipc_recv_message(fd, (uint8_t *)msg_type, msg_size, (uint8_t **)msg);
if (ret < 0) {
// This will happen if these errors occur while reading header
if (ret == -1 &&
(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
return -2;
fprintf(stderr, "Error reading message: dropping client at fd %d\n", fd);
ipc_drop_client(c);
return -1;
}
// Make sure receive message is null terminated to avoid parsing issues
if (*msg_size > 0) {
size_t len = *msg_size;
nullterminate(msg, &len);
*msg_size = len;
}
DEBUG("[fd %d] ", fd);
if (*msg_size > 0)
DEBUG("Received message: '%.*s' ", *msg_size, *msg);
else
DEBUG("Received empty message ");
DEBUG("Message type: %" PRIu8 " ", (uint8_t)*msg_type);
DEBUG("Message size: %" PRIu32 "\n", *msg_size);
return 0;
}
ssize_t
ipc_write_client(IPCClient *c)
{
const ssize_t n = ipc_write_message(c->fd, c->buffer, c->buffer_size);
if (n < 0) return n;
// TODO: Deal with client timeouts
if (n == c->buffer_size) {
c->buffer_size = 0;
free(c->buffer);
// No dangling pointers!
c->buffer = NULL;
// Stop waking up when client is ready to receive messages
if (c->event.events & EPOLLOUT) {
c->event.events -= EPOLLOUT;
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, c->fd, &c->event);
}
return n;
}
// Shift unwritten buffer to beginning of buffer and reallocate
c->buffer_size -= n;
memmove(c->buffer, c->buffer + n, c->buffer_size);
c->buffer = (char *)realloc(c->buffer, c->buffer_size);
return n;
}
void
ipc_prepare_send_message(IPCClient *c, const IPCMessageType msg_type,
const uint32_t msg_size, const char *msg)
{
dwm_ipc_header_t header = {
.magic = IPC_MAGIC_ARR, .type = msg_type, .size = msg_size};
uint32_t header_size = sizeof(dwm_ipc_header_t);
uint32_t packet_size = header_size + msg_size;
if (c->buffer == NULL)
c->buffer = (char *)malloc(c->buffer_size + packet_size);
else
c->buffer = (char *)realloc(c->buffer, c->buffer_size + packet_size);
// Copy header to end of client buffer
memcpy(c->buffer + c->buffer_size, &header, header_size);
c->buffer_size += header_size;
// Copy message to end of client buffer
memcpy(c->buffer + c->buffer_size, msg, msg_size);
c->buffer_size += msg_size;
// Wake up when client is ready to receive messages
c->event.events |= EPOLLOUT;
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, c->fd, &c->event);
}
void
ipc_prepare_reply_failure(IPCClient *c, IPCMessageType msg_type,
const char *format, ...)
{
yajl_gen gen;
va_list args;
// Get output size
va_start(args, format);
size_t len = vsnprintf(NULL, 0, format, args);
va_end(args);