-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltx.c
1322 lines (1058 loc) · 26.2 KB
/
ltx.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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2022 Richard Palethorpe <[email protected]>
* Copyright (c) 2023 Andrea Cervesato <[email protected]>
*/
#define _GNU_SOURCE /* CLOCK_MONOTONIC */
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/epoll.h>
#include <sys/signalfd.h>
#include <sys/statfs.h>
#include <linux/magic.h>
#include <linux/limits.h> /* PATH_MAX */
#include "ltx.h"
#include "msgpack/msgpack.h"
/* maximum number of epoll events */
#define MAX_EVENTS 128
/* maximum number of msgpack messages per ltx message */
#define MAX_MESSAGES 5
/* set when no slots are assigned */
#define SLOT_NONE 0xffff
/* maximum number of slots to execute commands */
#define MAX_SLOTS 128
/* used when we want to assign same evn/cwd to all slots */
#define ALL_SLOTS MAX_SLOTS
/* maximum number of environment variables */
#define MAX_ENVS 16
/* number of bytes for read(..) */
#define READ_BUFFER_SIZE 1024
/* maximum number char per string */
#define MAX_STRING_LEN 4096
/* Macro used to print messages */
#ifdef DEBUG
#define LTX_PRINT_MESSAGE(x) mp_message_print(x, session->debug_fd)
#else
#define LTX_PRINT_MESSAGE(x) {}
#endif
struct ltx_message
{
/* type of ltx message (LTX_PING, LTX_ERROR, etc. )*/
uint32_t type;
/* current unpacking data position */
size_t curr;
/* expected number of msgpack messages */
size_t length;
/* messages which are building the ltx message */
struct mp_message data[MAX_MESSAGES];
};
struct ltx_env
{
/* environment key */
char key[MAX_STRING_LEN];
/* environment value */
char value[MAX_STRING_LEN];
};
enum ltx_event_type
{
/* received on stdin event */
LTX_EVT_STDIN = 0xa0,
/* received on stdout event */
LTX_EVT_STDOUT,
};
struct ltx_event
{
/* LTX event type */
enum ltx_event_type type;
/* slot id of the relative event */
uint64_t slot_id;
/* file descriptor associated with event */
int fd;
};
struct ltx_slot
{
/* used if 1, 0 otherwise */
int used;
/* process id */
pid_t pid;
/* environment variables */
struct ltx_env env[MAX_ENVS];
/* current working directory */
char cwd[PATH_MAX];
/* stdin buffer */
uint8_t buffer[READ_BUFFER_SIZE];
/* ltx event associated with this object */
struct ltx_event event;
};
struct ltx_table
{
/* executions slots */
struct ltx_slot slots[MAX_SLOTS];
/* global environment variables */
struct ltx_env env[MAX_ENVS];
};
struct ltx_session
{
/* current application PID */
pid_t pid;
/* stdin file descriptor */
int stdin_fd;
/* stdout file descriptor */
int stdout_fd;
/* debug file descriptor */
int debug_fd;
/* epoll file descriptor */
int epoll_fd;
/* application stdin buffer */
uint8_t stdin_buffer[READ_BUFFER_SIZE];
/* msgpack messages unpacker */
struct mp_unpacker msg_unpacker;
/* current ltx unpacking message */
struct ltx_message ltx_message;
/* current ltx execution table */
struct ltx_table table;
};
/* stores information about error position inside the code */
struct ltx_error_pos
{
const char *const file;
const char *const func;
const int line;
};
#define LTX_HANDLE_ERROR(session, str, show_errno) \
ltx_handle_error( \
((struct ltx_error_pos){ __FILE__, __func__, __LINE__ }), \
session, \
str, \
show_errno)
/* it's 1 when we want to stop the event loop. zero otherwise */
static volatile int stop_loop = 0;
/* it's 1 when stdin/stdout pipes are broken. zero otherwise */
static volatile int broken_pipe = 0;
static void ltx_message_reserve_next(struct ltx_session *session)
{
++(session->ltx_message.curr);
assert(session->ltx_message.curr < MAX_MESSAGES);
mp_unpacker_reserve(
&session->msg_unpacker,
session->ltx_message.data + session->ltx_message.curr);
}
static void ltx_message_reset(struct ltx_session *session)
{
session->ltx_message.type = LTX_NONE;
session->ltx_message.curr = 0;
session->ltx_message.length = 0;
mp_unpacker_reserve(
&session->msg_unpacker,
session->ltx_message.data);
}
static void ltx_send_message(
struct ltx_session *session,
struct mp_message *msg)
{
LTX_PRINT_MESSAGE(msg);
ssize_t pos = 0, ret;
do {
ret = write(session->stdout_fd, msg->data, msg->length);
if (ret == -1)
break;
pos += ret;
} while (pos < (ssize_t)msg->length);
}
static void ltx_send_messages(
struct ltx_session *session,
struct mp_message *const msgs,
const int count)
{
struct mp_message msg;
mp_message_array(&msg, count);
ltx_send_message(session, &msg);
for (int i = 0; i < count; i++)
ltx_send_message(session, msgs + i);
}
static void ltx_echo(struct ltx_session *session)
{
ltx_send_messages(
session,
session->ltx_message.data,
session->ltx_message.curr + 1);
ltx_message_reset(session);
}
static void ltx_handle_error(
struct ltx_error_pos pos,
struct ltx_session *session,
const char *const str,
const int show_errno)
{
assert(str);
struct mp_message msgs[2];
mp_message_uint(&msgs[0], LTX_ERROR);
char* msg;
int ret;
if (show_errno) {
ret = asprintf(&msg, "%s:%s:%d %s (%s)",
pos.file,
pos.func,
pos.line,
str,
strerror(errno));
assert(ret >= 0);
} else {
ret = asprintf(&msg, "%s:%s:%d %s",
pos.file,
pos.func,
pos.line,
str);
assert(ret >= 0);
}
mp_message_str(&msgs[1], msg, (size_t)ret);
ltx_send_messages(session, msgs, 2);
ltx_message_reset(session);
if (msg)
free(msg);
}
static void ltx_read_string(
struct ltx_session *session,
struct mp_message *msg,
char *str)
{
size_t size;
char *ptr;
ptr = mp_message_read_str(msg, &size);
if (size > MAX_STRING_LEN) {
LTX_HANDLE_ERROR(session, "Maximum string length is 4096", 0);
return;
}
strncpy(str, ptr, size);
str[size] = '\0';
}
static void ltx_handle_version(struct ltx_session *session)
{
struct mp_message msgs[2];
mp_message_uint(&msgs[0], LTX_VERSION);
mp_message_str(&msgs[1], VERSION, strlen(VERSION));
ltx_send_messages(session, msgs, 2);
ltx_message_reset(session);
}
static uint64_t ltx_gettime(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000000 + ts.tv_nsec;
}
static void ltx_handle_ping(struct ltx_session *session)
{
ltx_echo(session);
struct mp_message msgs[2];
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
mp_message_uint(&msgs[0], LTX_PONG);
mp_message_uint(&msgs[1], ltx_gettime());
ltx_send_messages(session, msgs, 2);
ltx_message_reset(session);
}
static int ltx_file_from_proc(struct ltx_session *session, const int fd)
{
struct statfs fs;
if (fstatfs(fd, &fs) == -1) {
LTX_HANDLE_ERROR(session, "fstatfs() error", 1);
return -1;
}
int is_proc = (fs.f_type == PROC_SUPER_MAGIC) ? 1 : 0;
return is_proc;
}
static void ltx_handle_get_file(struct ltx_session *session)
{
char path[MAX_STRING_LEN];
ltx_read_string(session, session->ltx_message.data + 1, path);
if (path[0] == '\0') {
LTX_HANDLE_ERROR(session, "Empty given path", 0);
return;
}
int fd = open(path, O_RDONLY);
if (fd == -1) {
LTX_HANDLE_ERROR(session, "open() error", 1);
return;
}
int from_proc = ltx_file_from_proc(session, fd);
if (from_proc == -1)
return;
struct stat st;
if (fstat(fd, &st) == -1) {
LTX_HANDLE_ERROR(session, "fstat() error", 1);
return;
}
if (!S_ISREG(st.st_mode)) {
LTX_HANDLE_ERROR(session, "Given path is not a file", 0);
return;
}
if (st.st_size >= 0x7ffff000) {
LTX_HANDLE_ERROR(session, "File is too large", 0);
return;
}
struct mp_message msgs[2];
size_t nread;
if (from_proc) {
/* read /proc files has zero length. We need to use getline() */
FILE *stream = fdopen(fd, "r");
if (!stream) {
LTX_HANDLE_ERROR(session, "fdopen() error", 1);
return;
};
char *line = NULL;
while ((nread = (size_t)getline(&line, &nread, stream)) != (size_t)-1) {
mp_message_uint(&msgs[0], LTX_DATA);
mp_message_bin(&msgs[1], (uint8_t *)line, nread);
ltx_send_messages(session, msgs, 2);
}
if (line)
free(line);
if (!feof(stream)) {
LTX_HANDLE_ERROR(session, "getline() error", 1);
return;
}
} else {
/* regular files can use read() */
uint8_t data[READ_BUFFER_SIZE];
ssize_t pos = 0, nread;
do {
nread = read(fd, data, READ_BUFFER_SIZE);
if (nread == -1) {
LTX_HANDLE_ERROR(session, "read() error", 1);
return;
}
pos += nread;
mp_message_uint(&msgs[0], LTX_DATA);
mp_message_bin(&msgs[1], data, nread);
ltx_send_messages(session, msgs, 2);
} while (pos < st.st_size);
}
if (close(fd) == -1) {
LTX_HANDLE_ERROR(session, "close() error", 1);
return;
}
mp_message_uint(&msgs[0], LTX_GET_FILE);
mp_message_str(&msgs[1], path, strlen(path));
ltx_send_messages(session, msgs, 2);
ltx_message_reset(session);
}
static void ltx_handle_set_file(struct ltx_session *session)
{
char path[MAX_STRING_LEN];
ltx_read_string(session, session->ltx_message.data + 1, path);
size_t size;
void *data = mp_message_read_bin(session->ltx_message.data + 2, &size);
if (path[0] == '\0') {
LTX_HANDLE_ERROR(session, "Empty given path", 0);
return;
}
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd == -1) {
LTX_HANDLE_ERROR(session, "open() error", 1);
return;
}
ssize_t pos = 0, ret;
do {
ret = write(fd, data, size);
if (ret == -1) {
LTX_HANDLE_ERROR(session, "write() error", 1);
return;
}
pos += ret;
} while (pos < (ssize_t)size);
close(fd);
struct mp_message msgs[2];
mp_message_uint(&msgs[0], LTX_SET_FILE);
mp_message_str(&msgs[1], path, strlen(path));
ltx_send_messages(session, msgs, 2);
ltx_message_reset(session);
}
static int ltx_env_set(
struct ltx_session *session,
struct ltx_env *exec_env,
const char *key,
const char *value)
{
assert(exec_env);
assert(key);
assert(value);
struct ltx_env *env;
unsigned i = 0;
if (strlen(value) > 0) {
for (i = 0; i < MAX_ENVS; i++) {
env = exec_env + i;
if (!strcmp(env->key, key) || !strlen(env->key))
break;
}
if (i >= MAX_ENVS) {
LTX_HANDLE_ERROR(
session,
"Set too many environment variables", 0);
return 1;
}
} else {
for (i = 0; i < MAX_ENVS; i++) {
env = exec_env + i;
if (!strcmp(env->key, key))
break;
}
if (i >= MAX_ENVS)
env = NULL;
}
if (env) {
strcpy(env->key, key);
strcpy(env->value, value);
}
return 0;
}
static void ltx_handle_env(struct ltx_session *session)
{
uint64_t slot_id = mp_message_read_uint(session->ltx_message.data + 1);
if (slot_id > MAX_SLOTS) {
LTX_HANDLE_ERROR(session, "Out of bound slot ID", 0);
return;
}
char key[MAX_STRING_LEN];
ltx_read_string(session, session->ltx_message.data + 2, key);
char val[MAX_STRING_LEN];
ltx_read_string(session, session->ltx_message.data + 3, val);
struct ltx_slot *exec_slot;
int error;
if (slot_id == ALL_SLOTS) {
for (unsigned i = 0; i < MAX_SLOTS; i++) {
exec_slot = session->table.slots + i;
error = ltx_env_set(
session,
exec_slot->env,
key,
val);
}
} else {
exec_slot = session->table.slots + slot_id;
error = ltx_env_set(
session,
exec_slot->env,
key,
val);
}
if (!error)
ltx_echo(session);
}
static void ltx_handle_cwd(struct ltx_session *session)
{
uint64_t slot_id = mp_message_read_uint(session->ltx_message.data + 1);
if (slot_id > MAX_SLOTS) {
LTX_HANDLE_ERROR(session, "Out of bound slot ID", 0);
return;
}
char path[MAX_STRING_LEN];
ltx_read_string(session, session->ltx_message.data + 2, path);
struct stat sb;
if (stat(path, &sb) || !S_ISDIR(sb.st_mode)) {
LTX_HANDLE_ERROR(session, "CWD directory does not exist", 0);
return;
}
struct ltx_slot *exec_slot;
if (slot_id == ALL_SLOTS) {
for (unsigned i = 0; i < MAX_SLOTS; i++) {
exec_slot = session->table.slots + i;
strcpy(exec_slot->cwd, path);
}
} else {
exec_slot = session->table.slots + slot_id;
strcpy(exec_slot->cwd, path);
}
ltx_echo(session);
}
static struct ltx_slot *ltx_slot_reserve(
struct ltx_session *session,
const uint64_t slot_id)
{
assert(slot_id < MAX_SLOTS);
struct ltx_slot *exec_slot = session->table.slots + slot_id;
if (exec_slot->used) {
LTX_HANDLE_ERROR(session, "Execution slot is reserved", 0);
return NULL;
}
exec_slot->used = 1;
return exec_slot;
}
static void ltx_slot_free(struct ltx_session *session, const uint64_t slot_id)
{
assert(slot_id < MAX_SLOTS);
struct ltx_slot *exec_slot;
exec_slot = session->table.slots + slot_id;
close(exec_slot->event.fd);
memset(exec_slot, 0, sizeof(struct ltx_slot));
exec_slot->pid = -1;
}
static int ltx_epoll_add(
struct ltx_session *session,
struct ltx_event *evt,
const uint32_t events)
{
struct epoll_event in_evt = {
.events = events,
.data.ptr = evt,
};
int ret = epoll_ctl(
session->epoll_fd,
EPOLL_CTL_ADD,
evt->fd,
&in_evt);
if (ret == -1) {
LTX_HANDLE_ERROR(session, "epoll_ctl() error", 1);
return 1;
}
return 0;
}
static void ltx_run_command(struct ltx_session *session, const char *cmd)
{
int ret;
/* run with shell first */
ret = execlp("sh", "sh", "-c", cmd, (char *) NULL);
if (ret == -1 && errno != ENOENT) {
LTX_HANDLE_ERROR(session, "execlp() error", 1);
return;
}
/* if no shell is available, run without it */
ret = execlp(cmd, cmd, (char *) NULL);
if (ret == -1) {
LTX_HANDLE_ERROR(session, "execlp() error", 1);
return;
}
_exit(0);
}
static void ltx_handle_exec(struct ltx_session *session)
{
/* read execution message */
uint64_t slot_id = mp_message_read_uint(session->ltx_message.data + 1);
if (slot_id >= MAX_SLOTS) {
LTX_HANDLE_ERROR(session, "Out of bound slot ID", 0);
return;
}
char cmd[MAX_STRING_LEN];
ltx_read_string(session, session->ltx_message.data + 2, cmd);
/* echo back the command */
ltx_echo(session);
/* reserve execution slot */
struct ltx_slot *exec_slot = ltx_slot_reserve(
session,
slot_id);
if (!exec_slot)
return;
/* create stdout pipe */
int pipefd[2];
if (pipe2(pipefd, O_CLOEXEC) == -1) {
LTX_HANDLE_ERROR(session, "pipe2() error", 1);
return;
}
/* setup event */
exec_slot->event.type = LTX_EVT_STDOUT;
exec_slot->event.slot_id = slot_id;
exec_slot->event.fd = pipefd[0];
assert(!ltx_epoll_add(session, &exec_slot->event, EPOLLIN));
/* run the command */
pid_t pid = fork();
if (pid == -1) {
LTX_HANDLE_ERROR(session, "fork() error", 1);
return;
}
/* setup parent */
if (pid) {
close(pipefd[1]);
exec_slot->pid = pid;
return;
}
/* redirect stdout to pipe */
if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
LTX_HANDLE_ERROR(session, "dup2() stdout", 1);
exit(1);
}
if (dup2(pipefd[1], STDERR_FILENO) == -1) {
LTX_HANDLE_ERROR(session, "dup2() stderr", 1);
exit(1);
}
close(pipefd[0]);
close(pipefd[1]);
/* set global environment vars */
for (unsigned i = 0; i < MAX_ENVS; i++) {
struct ltx_env env = session->table.env[i];
if (!strlen(env.key))
continue;
if (setenv(env.key, env.value, 1) == -1) {
LTX_HANDLE_ERROR(
session,
"global setenv()",
1);
exit(1);
}
}
/* set local environment vars */
for (unsigned i = 0; i < MAX_ENVS; i++) {
struct ltx_env env = exec_slot->env[i];
if (!strlen(env.key))
continue;
if (setenv(env.key, env.value, 1) == -1) {
LTX_HANDLE_ERROR(
session,
"local setenv()",
1);
exit(1);
}
}
/* change directory */
if (strlen(exec_slot->cwd) && chdir(exec_slot->cwd) == -1) {
LTX_HANDLE_ERROR(session, "chdir() error", 1);
exit(1);
}
/* execute the command */
ltx_run_command(session, cmd);
}
static void ltx_handle_result(
struct ltx_session *session,
uint64_t slot_id,
int ssi_code,
int ssi_status)
{
assert(slot_id < MAX_SLOTS);
struct mp_message msgs[5];
mp_message_uint(&msgs[0], LTX_RESULT);
mp_message_uint(&msgs[1], slot_id);
mp_message_uint(&msgs[2], ltx_gettime());
mp_message_uint(&msgs[3], (uint64_t)ssi_code);
mp_message_uint(&msgs[4], (uint64_t)ssi_status);
ltx_send_messages(session, msgs, 5);
ltx_message_reset(session);
}
static int ltx_check_stdout(struct ltx_session *session, const int slot_id)
{
if (session->pid != getpid())
return 0;
assert(slot_id < MAX_SLOTS);
struct ltx_slot *slot = session->table.slots + slot_id;
ssize_t ret = read(slot->event.fd, slot->buffer, READ_BUFFER_SIZE);
if (ret == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
LTX_HANDLE_ERROR(session, "read() log", 1);
return 0;
}
if (!ret)
return 0;
struct mp_message msgs[4];
mp_message_uint(&msgs[0], LTX_LOG);
mp_message_uint(&msgs[1], slot_id);
mp_message_uint(&msgs[2], ltx_gettime());
mp_message_str(&msgs[3], (char *)slot->buffer, (size_t)ret);
ltx_send_messages(session, msgs, 4);
ltx_message_reset(session);
return 1;
}
static void ltx_slots_waitpid(struct ltx_session *session)
{
struct ltx_slot *slot;
uint64_t slot_id;
int ret;
int status;
int ssi_code;
int ssi_status;
for (slot_id = 0; slot_id < MAX_SLOTS; slot_id++) {
slot = session->table.slots + slot_id;
if (slot->pid == -1)
continue;
ret = waitpid(slot->pid, &status, WNOHANG);
if (ret == -1) {
LTX_HANDLE_ERROR(session, "waitpid() error", 1);
return;
}
if (ret != slot->pid)
continue;
/* drain the child stdout before sending RESULT reply */
while (ltx_check_stdout(session, slot_id)) {}
if (WIFSIGNALED(status)) {
ssi_code = CLD_KILLED;
ssi_status = WTERMSIG(status);
} else if (WIFSTOPPED(status)) {
ssi_code = CLD_STOPPED;
ssi_status = WSTOPSIG(status);
} else {
ssi_code = CLD_EXITED;
ssi_status = WEXITSTATUS(status);
}
ltx_handle_result(
session,
slot_id,
ssi_code,
ssi_status);
ltx_slot_free(session, slot_id);
}
}
static void ltx_handle_log(struct ltx_session *session, struct ltx_event *evt)
{
if (session->pid != getpid())
return;
ltx_check_stdout(session, evt->slot_id);
}
static void ltx_handle_kill(struct ltx_session *session)
{
/* read message */
uint64_t slot_id = mp_message_read_uint(session->ltx_message.data + 1);
if (slot_id >= MAX_SLOTS) {
LTX_HANDLE_ERROR(session, "Out of bound slot ID", 0);
return;
}
struct ltx_slot *exec_slot = session->table.slots + slot_id;
if (exec_slot->pid == -1)
return;
int ret = kill(exec_slot->pid, SIGKILL);
if (ret == -1 && errno != ESRCH) {
LTX_HANDLE_ERROR(session, "kill() error", 1);
return;
}
/* echo back the command */
ltx_echo(session);
}
static void ltx_process_msg(struct ltx_session *session)
{
struct ltx_message *msg = &session->ltx_message;
int reserve_next = 1;
LTX_PRINT_MESSAGE(msg->data + msg->curr);
if (msg->type == LTX_NONE) {
if (!msg->length) {
if (mp_message_type(msg->data) != MP_ARRAY) {
LTX_HANDLE_ERROR(
session,
"Messages must be packed inside array",
0);
return;
}
/* read number messages inside the array */
msg->length = mp_message_read_array_length(msg->data);
/* array message is not carrying request information */
reserve_next = 0;
} else {
/* handle requests composed by a single message */
if (mp_message_type(msg->data) != MP_NUMERIC) {
LTX_HANDLE_ERROR(
session,
"Message type must be a numeric",
0);
return;
}
/* numeric types are used to store commands type.
* we save received command type and wait for next messages.
*/
msg->type = (uint32_t)mp_message_read_uint(msg->data);
switch (msg->type) {
/* handle commands with single message request */
case LTX_VERSION:
reserve_next = 0;
ltx_handle_version(session);
break;
case LTX_PING:
reserve_next = 0;
ltx_handle_ping(session);
break;
/* handle commands which should never be sent */
case LTX_PONG:
reserve_next = 0;
LTX_HANDLE_ERROR(
session,
"PONG should not be received",
0);
break;
case LTX_ERROR:
reserve_next = 0;
LTX_HANDLE_ERROR(
session,
"ERROR should not be received",
0);
break;
case LTX_DATA:
reserve_next = 0;
LTX_HANDLE_ERROR(
session,
"DATA should not be received",
0);
break;
case LTX_RESULT:
reserve_next = 0;
LTX_HANDLE_ERROR(
session,
"RESULT should not be received",
0);
break;
case LTX_LOG:
reserve_next = 0;
LTX_HANDLE_ERROR(
session,
"LOG should not be received",
0);