-
Notifications
You must be signed in to change notification settings - Fork 2
/
quark.c
2007 lines (1745 loc) · 45 KB
/
quark.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: Apache-2.0
/* Copyright (c) 2024 Elastic NV */
#include <sys/epoll.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include "quark.h"
#define AGE(_ts, _now) ((_ts) > (_now) ? 0 : (_now) - (_ts))
static int raw_event_by_time_cmp(struct raw_event *, struct raw_event *);
static int raw_event_by_pidtime_cmp(struct raw_event *, struct raw_event *);
static int process_by_pid_cmp(struct quark_process *, struct quark_process *);
/* For debugging */
int quark_verbose;
RB_PROTOTYPE(process_by_pid, quark_process,
entry_by_pid, process_by_pid_cmp);
RB_GENERATE(process_by_pid, quark_process,
entry_by_pid, process_by_pid_cmp);
RB_PROTOTYPE(raw_event_by_time, raw_event,
entry_by_time, raw_event_by_time_cmp);
RB_GENERATE(raw_event_by_time, raw_event,
entry_by_time, raw_event_by_time_cmp);
RB_PROTOTYPE(raw_event_by_pidtime, raw_event,
entry_by_pidtime, raw_event_by_pidtime_cmp);
RB_GENERATE(raw_event_by_pidtime, raw_event,
entry_by_pidtime, raw_event_by_pidtime_cmp);
struct quark {
unsigned int hz;
u64 boottime;
} quark;
struct raw_event *
raw_event_alloc(int type)
{
struct raw_event *raw;
if ((raw = calloc(1, sizeof(*raw))) == NULL)
return (NULL);
raw->type = type;
TAILQ_INIT(&raw->agg_queue);
switch (raw->type) {
case RAW_WAKE_UP_NEW_TASK: /* FALLTHROUGH */
case RAW_EXIT_THREAD:
raw->task.exit_code = -1;
qstr_init(&raw->task.cwd);
break;
case RAW_EXEC:
qstr_init(&raw->exec.filename);
qstr_init(&raw->exec.ext.args);
qstr_init(&raw->exec.ext.task.cwd);
break;
case RAW_EXEC_CONNECTOR:
qstr_init(&raw->exec_connector.args);
qstr_init(&raw->exec_connector.task.cwd);
break;
case RAW_COMM: /* nada */
break;
default:
qwarnx("unhandled raw_type %d", raw->type);
free(raw);
return (NULL);
}
return (raw);
}
void
raw_event_free(struct raw_event *raw)
{
struct raw_event *aux;
switch (raw->type) {
case RAW_WAKE_UP_NEW_TASK:
case RAW_EXIT_THREAD:
qstr_free(&raw->task.cwd);
break;
case RAW_EXEC:
qstr_free(&raw->exec.filename);
qstr_free(&raw->exec.ext.task.cwd);
qstr_free(&raw->exec.ext.args);
break;
case RAW_EXEC_CONNECTOR:
qstr_free(&raw->exec_connector.args);
qstr_free(&raw->exec_connector.task.cwd);
break;
case RAW_COMM: /* nada */
break;
default:
qwarnx("unhandled raw_type %d", raw->type);
break;
}
while ((aux = TAILQ_FIRST(&raw->agg_queue)) != NULL) {
TAILQ_REMOVE(&raw->agg_queue, aux, agg_entry);
raw_event_free(aux);
}
free(raw);
}
static int
raw_event_by_time_cmp(struct raw_event *a, struct raw_event *b)
{
if (a->time < b->time)
return (-1);
else
return (a->time > b->time);
}
static int
raw_event_by_pidtime_cmp(struct raw_event *a, struct raw_event *b)
{
if (a->pid < b->pid)
return (-1);
else if (a->pid > b->pid)
return (1);
if (a->time < b->time)
return (-1);
else
return (a->time > b->time);
}
static inline u64
now64(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
err(1, "clock_gettime");
return ((u64)ts.tv_sec * (u64)NS_PER_S + (u64)ts.tv_nsec);
}
static inline u64
raw_event_age(struct raw_event *raw, u64 now)
{
return AGE(raw->time, now);
}
/*
* Target age is the duration in ns of how long should we hold the event in the
* tree before processing it. It's a function of the number of items in the tree
* and its maximum capacity:
* from [0; 10%] -> 1000ms
* from [90%; 100%] -> 0ms
* from (10%; 90%) -> linear from 1000ms -> 100ms
*/
static u64
raw_event_target_age(struct quark_queue *qq)
{
int v;
if (qq->length < (qq->max_length / 10))
v = qq->hold_time;
else if (qq->length < ((qq->max_length / 10) * 9)) {
v = qq->hold_time -
(qq->length / (qq->max_length / qq->hold_time)) + 1;
} else
v = 0;
return ((u64)MS_TO_NS(v));
}
static inline int
raw_event_expired(struct quark_queue *qq, struct raw_event *raw, u64 now)
{
u64 target;
target = raw_event_target_age(qq);
return (raw_event_age(raw, now) >= target);
}
/*
* Insert without a colision, cheat on the timestamp in case we do. NOTE: since
* we bump "time" here, we shouldn't copy "time" before it sits in the tree.
*/
void
raw_event_insert(struct quark_queue *qq, struct raw_event *raw)
{
struct raw_event *col;
int attempts = 10;
/*
* Link it first by time
*/
do {
col = RB_INSERT(raw_event_by_time, &qq->raw_event_by_time, raw);
if (likely(col == NULL))
break;
/*
* We managed to get a collision on the TSC, this happens!
* We just bump time by one until we can insert it.
*/
raw->time++;
qwarnx("raw_event_by_time collision");
} while (--attempts > 0);
if (unlikely(col != NULL))
err(1, "we got consecutive collisions, this is a bug");
/*
* Link it in the combined tree, we accept no collisions here as the
* above case already saves us, but trust nothing.
*/
/* XXX this should be by tid, but we're not there yet XXX */
col = RB_INSERT(raw_event_by_pidtime, &qq->raw_event_by_pidtime, raw);
if (unlikely(col != NULL))
err(1, "collision on pidtime tree, this is a bug");
/* if (qq->min == NULL || raw_event_by_time_cmp(raw, qq->min) == -1) */
/* qq->min = raw; */
qq->length++;
qq->stats.insertions++;
}
static void
raw_event_remove(struct quark_queue *qq, struct raw_event *raw)
{
RB_REMOVE(raw_event_by_time, &qq->raw_event_by_time, raw);
RB_REMOVE(raw_event_by_pidtime, &qq->raw_event_by_pidtime, raw);
/* if (qq->min == raw) qq->min = NULL */
qq->length--;
qq->stats.removals++;
}
static int
tty_type(int major, int minor)
{
if (major >= 136 && major <= 143)
return (QUARK_TTY_PTS);
if (major == 4) {
if (minor <= 63)
return (QUARK_TTY_CONSOLE);
else if (minor <= 255)
return (QUARK_TTY_TTY);
}
return (QUARK_TTY_UNKNOWN);
}
#if 0
static void
event_copy_fields(struct quark_process *dst, struct quark_process *src)
{
char *p;
u_int i;
#define MCPY(_f, _l) \
do { \
memcpy(dst->_f, src->_f, src->_l); \
for (i = 0; i < src->_l; i++) { \
p = (char *)dst->_f + i; \
if (*p == 0) \
continue; \
if (!isprint(*p)) \
*p = '?'; \
} \
} while (0)
#define STCPY(_f) \
do { \
strlcpy(dst->_f, src->_f, sizeof(dst->_f)); \
for (p = dst->_f; *p != 0; p++) { \
if (!isprint(*p)) \
*p = '?'; \
} \
} while (0)
#define CPY(_f) dst->_f = src->_f
CPY(flags);
if (src->flags & QUARK_F_PROC) {
CPY(proc_cap_inheritable);
CPY(proc_cap_permitted);
CPY(proc_cap_effective);
CPY(proc_cap_bset);
CPY(proc_cap_ambient);
CPY(proc_time_boot);
CPY(proc_ppid);
CPY(proc_uid);
CPY(proc_gid);
CPY(proc_suid);
CPY(proc_sgid);
CPY(proc_euid);
CPY(proc_egid);
CPY(proc_pgid);
CPY(proc_sid);
CPY(proc_tty_major);
CPY(proc_tty_minor);
CPY(proc_entry_leader_type);
CPY(proc_entry_leader);
}
if (src->flags & QUARK_F_EXIT) {
CPY(exit_code);
CPY(exit_time_event);
}
if (src->flags & QUARK_F_COMM)
STCPY(comm);
if (src->flags & QUARK_F_FILENAME)
STCPY(filename);
if (src->flags & QUARK_F_CMDLINE) {
CPY(cmdline_len);
MCPY(cmdline, cmdline_len);
}
if (src->flags & QUARK_F_CWD)
STCPY(cwd);
#undef CPY
#undef STCPY
#undef MCPY
}
static void
event_copy_out(struct quark_process *dst, struct quark_process *src, u64 events)
{
bzero(&dst->quark_process_zero_start,
(char *)&dst->quark_process_zero_end - (char *)&dst->quark_process_zero_start);
dst->pid = src->pid;
event_copy_fields(dst, src);
}
#endif
static struct quark_process *
process_cache_get(struct quark_queue *qq, int pid, int alloc)
{
struct quark_process key;
struct quark_process *qp;
key.pid = pid;
qp = RB_FIND(process_by_pid, &qq->process_by_pid, &key);
if (qp != NULL)
return (qp);
if (!alloc) {
errno = ESRCH;
return (NULL);
}
qp = calloc(1, sizeof(*qp));
if (qp == NULL)
return (NULL);
qp->pid = pid;
if (RB_INSERT(process_by_pid, &qq->process_by_pid, qp) != NULL) {
qwarnx("collision, this is a bug");
free(qp);
return (NULL);
}
return (qp);
}
static void
process_cache_inherit(struct quark_queue *qq, struct quark_process *qp, int ppid)
{
struct quark_process *parent;
if ((parent = process_cache_get(qq, ppid, 0)) == NULL)
return;
/* Ignore QUARK_F_PROC? as we always have it all on fork */
if (parent->flags & QUARK_F_COMM) {
qp->flags |= QUARK_F_COMM;
strlcpy(qp->comm, parent->comm, sizeof(qp->comm));
}
if (parent->flags & QUARK_F_FILENAME) {
qp->flags |= QUARK_F_FILENAME;
strlcpy(qp->filename, parent->filename, sizeof(qp->filename));
}
/* Do we really want CMDLINE? */
if (parent->flags & QUARK_F_CMDLINE) {
qp->flags |= QUARK_F_CMDLINE;
qp->cmdline_len = parent->cmdline_len;
memcpy(qp->cmdline, parent->cmdline, parent->cmdline_len);
}
}
static void
process_cache_delete(struct quark_queue *qq, struct quark_process *qp)
{
RB_REMOVE(process_by_pid, &qq->process_by_pid, qp);
if (qp->gc_time)
TAILQ_REMOVE(&qq->event_gc, qp, entry_gc);
free(qp);
}
static int
process_cache_gc(struct quark_queue *qq)
{
struct quark_process *qp;
u64 now;
int n;
now = now64();
n = 0;
while ((qp = TAILQ_FIRST(&qq->event_gc)) != NULL) {
if (AGE(qp->gc_time, now) < qq->cache_grace_time)
break;
process_cache_delete(qq, qp);
n++;
}
return (n);
}
static int
process_by_pid_cmp(struct quark_process *a, struct quark_process *b)
{
if (a->pid < b->pid)
return (-1);
else if (a->pid > b->pid)
return (1);
return (0);
}
static const char *
event_flag_str(u64 flag)
{
switch (flag) {
case QUARK_F_PROC:
return "PROC";
case QUARK_F_EXIT:
return "EXIT";
case QUARK_F_COMM:
return "COMM";
case QUARK_F_FILENAME:
return "FILENAME";
case QUARK_F_CMDLINE:
return "CMDLINE";
case QUARK_F_CWD:
return "CWD";
default:
return "?";
}
}
static const char *
event_type_str(u64 event)
{
switch (event) {
case QUARK_EV_FORK:
return "FORK";
case QUARK_EV_EXEC:
return "EXEC";
case QUARK_EV_EXIT:
return "EXIT";
case QUARK_EV_SETPROCTITLE:
return "SETPROCTITLE";
case QUARK_EV_SNAPSHOT:
return "SNAPSHOT";
default:
return "?";
}
}
static int
events_type_str(u64 events, char *buf, size_t len)
{
int i, n;
u64 ev;
if (len == 0)
return (-1);
for (i = 0, n = 0, *buf = 0; i < 64; i++) {
ev = (u64)1 << i;
if ((events & ev) == 0)
continue;
if (n > 0)
if (strlcat(buf, "+", len) >= len)
return (-1);
if (strlcat(buf, event_type_str(ev), len) >= len)
return (-1);
n++;
}
return (0);
}
static int
entry_leader_compute(struct quark_queue *qq, struct quark_process *qp)
{
struct quark_process *parent;
char *basename, *p_basename;
int tty;
int is_ses_leader;
if ((qq->flags & QQ_ENTRY_LEADER) == 0)
return (0);
/*
* Init
*/
if (qp->pid == 1) {
qp->proc_entry_leader_type = QUARK_ELT_INIT;
qp->proc_entry_leader = 1;
return (0);
}
is_ses_leader = qp->pid == qp->proc_sid;
/*
* All kthreads are QUARK_ELT_KTHREAD;
*/
if (qp->pid == 2 || qp->proc_ppid == 2) {
qp->proc_entry_leader_type = QUARK_ELT_KTHREAD;
qp->proc_entry_leader = is_ses_leader ? qp->pid : 2;
return (0);
}
tty = tty_type(qp->proc_tty_major, qp->proc_tty_minor);
basename = strrchr(qp->filename, '/');
if (basename == NULL)
basename = "";
else
basename++;
/*
* CONSOLE only considers the login process, keep the same behaviour
* from other elastic products.
*/
if (is_ses_leader) {
if (tty == QUARK_TTY_TTY) {
qp->proc_entry_leader_type = QUARK_ELT_TERM;
qp->proc_entry_leader = qp->pid;
return (0);
}
if (tty == QUARK_TTY_CONSOLE && !strcmp(basename, "login")) {
qp->proc_entry_leader_type = QUARK_ELT_TERM;
qp->proc_entry_leader = qp->pid;
return (0);
}
}
/*
* Fetch the parent
*/
parent = process_cache_get(qq, qp->proc_ppid, 0);
if (parent == NULL || parent->proc_entry_leader_type == QUARK_ELT_UNKNOWN)
return (-1);
/*
* Since we didn't hit anything, inherit from parent. Non leaders are
* done.
*/
qp->proc_entry_leader_type = parent->proc_entry_leader_type;
qp->proc_entry_leader = parent->proc_entry_leader;
if (!is_ses_leader)
return (0);
#define STARTS_WITH(_x, _y) (!strncmp(_x, _y, strlen(_y)))
/*
* Filter these out, keep same behaviour of other elastic products.
*/
if (STARTS_WITH(basename, "runc") ||
STARTS_WITH(basename, "containerd-shim") ||
STARTS_WITH(basename, "calico-node") ||
STARTS_WITH(basename, "check-status") ||
STARTS_WITH(basename, "pause") ||
STARTS_WITH(basename, "conmon"))
return (0);
p_basename = strrchr(parent->filename, '/');
if (p_basename == NULL)
p_basename = "";
else
p_basename++;
/*
* SSM.
*/
if (tty == QUARK_TTY_PTS &&
!strcmp(p_basename, "ssm-session-worker")) {
qp->proc_entry_leader_type = QUARK_ELT_SSM;
qp->proc_entry_leader = qp->pid;
return (0);
}
/*
* SSHD. If we're a direct descendant of sshd, but we're not sshd
* ourselves: we're an entry group leader for sshd.
*/
if (!strcmp(p_basename, "sshd") && strcmp(basename, "sshd")) {
qp->proc_entry_leader_type = QUARK_ELT_SSHD;
qp->proc_entry_leader = qp->pid;
return (0);
}
/*
* Container. Similar dance to sshd but more names, cloud-defend ignores
* basename here.
*/
if (STARTS_WITH(p_basename, "containerd-shim") ||
STARTS_WITH(p_basename, "runc") ||
STARTS_WITH(p_basename, "conmon")) {
qp->proc_entry_leader_type = QUARK_ELT_CONTAINER;
qp->proc_entry_leader = qp->pid;
return (0);
}
#undef STARTS_WITH
if (qp->proc_entry_leader == QUARK_ELT_UNKNOWN)
qwarnx("%d (%s) is UNKNOWN (tty=%d)",
qp->pid, qp->filename, tty);
return (0);
}
struct proc_node {
u32 pid;
TAILQ_ENTRY(proc_node) entry;
};
TAILQ_HEAD(proc_node_list, proc_node);
static int
entry_leader_build_walklist(struct quark_queue *qq, struct proc_node_list *list)
{
struct proc_node *node, *new_node;
struct quark_process *qp;
TAILQ_INIT(list);
/*
* Look for the root nodes, this is init(pid = 1) and kthread(pid = 2),
* but maybe there's something else in the future or in the past so
* don't hardcode.
*/
RB_FOREACH(qp, process_by_pid, &qq->process_by_pid) {
if (qp->proc_ppid != 0)
continue;
new_node = calloc(1, sizeof(*new_node));
if (new_node == NULL)
goto fail;
new_node->pid = qp->pid;
TAILQ_INSERT_TAIL(list, new_node, entry);
}
/*
* Now do the "recursion"
*/
TAILQ_FOREACH(node, list, entry) {
RB_FOREACH(qp, process_by_pid, &qq->process_by_pid) {
if (qp->proc_ppid != node->pid)
continue;
new_node = calloc(1, sizeof(*new_node));
if (new_node == NULL)
goto fail;
new_node->pid = qp->pid;
TAILQ_INSERT_TAIL(list, new_node, entry);
}
}
return (0);
fail:
while ((node = TAILQ_FIRST(list)) != NULL) {
TAILQ_REMOVE(list, node, entry);
free(node);
}
return (-1);
}
static int
entry_leaders_build(struct quark_queue *qq)
{
struct quark_process *qp;
struct proc_node *node;
struct proc_node_list list;
if ((qq->flags & QQ_ENTRY_LEADER) == 0)
return (0);
if (entry_leader_build_walklist(qq, &list) == -1)
return (-1);
while ((node = TAILQ_FIRST(&list)) != NULL) {
qp = process_cache_get(qq, node->pid, 0);
if (qp == NULL)
goto fail;
if (entry_leader_compute(qq, qp) == -1)
qwarnx("unknown entry_leader for pid %d", qp->pid);
TAILQ_REMOVE(&list, node, entry);
free(node);
}
return (0);
fail:
while ((node = TAILQ_FIRST(&list)) != NULL) {
TAILQ_REMOVE(&list, node, entry);
free(node);
}
return (-1);
}
static const char *
entry_leader_type_str(u32 entry_leader_type)
{
switch (entry_leader_type) {
case QUARK_ELT_UNKNOWN:
return "UNKNOWN";
case QUARK_ELT_INIT:
return "INIT";
case QUARK_ELT_KTHREAD:
return "KTHREAD";
case QUARK_ELT_SSHD:
return "SSHD";
case QUARK_ELT_SSM:
return "SSM";
case QUARK_ELT_CONTAINER:
return "CONTAINER";
case QUARK_ELT_TERM:
return "TERM";
case QUARK_ELT_CONSOLE:
return "CONSOLE";
default:
return "?";
}
}
#define P(...) \
do { \
if (fprintf(f, __VA_ARGS__) < 0) \
return (-1); \
} while(0)
int
quark_event_dump(const struct quark_event *qev, FILE *f)
{
const char *flagname;
char events[1024];
const struct quark_process *qp;
qp = qev->process;
if (qp == NULL)
return (-1);
/* TODO: add tid */
events_type_str(qev->events, events, sizeof(events));
P("->%d (%s)\n", qp->pid, events);
if (qp->flags & QUARK_F_COMM) {
flagname = event_flag_str(QUARK_F_COMM);
P(" %.4s\tcomm=%s\n", flagname, qp->comm);
}
if (qp->flags & QUARK_F_CMDLINE) {
int i;
struct args *args;
flagname = event_flag_str(QUARK_F_CMDLINE);
P(" %.4s\tcmdline=", flagname);
P("[ ");
args = args_make(qp);
if (args == NULL)
P("(%s)", strerror(errno));
else {
for (i = 0; i < args->argc; i++) {
if (i > 0)
P(", ");
P("%s", args->argv[i]);
}
args_free(args);
}
P(" ]\n");
}
if (qp->flags & QUARK_F_PROC) {
flagname = event_flag_str(QUARK_F_PROC);
P(" %.4s\tppid=%d\n", flagname, qp->proc_ppid);
P(" %.4s\tuid=%d gid=%d suid=%d sgid=%d "
"euid=%d egid=%d pgid=%d sid=%d\n",
flagname, qp->proc_uid, qp->proc_gid, qp->proc_suid,
qp->proc_sgid, qp->proc_euid, qp->proc_egid,
qp->proc_pgid, qp->proc_sid);
P(" %.4s\tcap_inheritable=0x%llx cap_permitted=0x%llx "
"cap_effective=0x%llx\n",
flagname, qp->proc_cap_inheritable,
qp->proc_cap_permitted, qp->proc_cap_effective);
P(" %.4s\tcap_bset=0x%llx cap_ambient=0x%llx\n",
flagname, qp->proc_cap_bset, qp->proc_cap_ambient);
P(" %.4s\ttime_boot=%llu tty_major=%d tty_minor=%d\n",
flagname, qp->proc_time_boot,
qp->proc_tty_major, qp->proc_tty_minor);
P(" %.4s\tuts_inonum=%u ipc_inonum=%u\n",
flagname, qp->proc_uts_inonum, qp->proc_ipc_inonum);
P(" %.4s\tmnt_inonum=%u net_inonum=%u\n",
flagname, qp->proc_mnt_inonum, qp->proc_net_inonum);
P(" %.4s\tentry_leader_type=%s entry_leader=%d\n", flagname,
entry_leader_type_str(qp->proc_entry_leader_type),
qp->proc_entry_leader);
}
if (qp->flags & QUARK_F_CWD) {
flagname = event_flag_str(QUARK_F_CWD);
P(" %.4s\tcwd=%s\n", flagname, qp->cwd);
}
if (qp->flags & QUARK_F_FILENAME) {
flagname = event_flag_str(QUARK_F_FILENAME);
P(" %.4s\tfilename=%s\n", flagname, qp->filename);
}
if (qp->flags & QUARK_F_EXIT) {
flagname = event_flag_str(QUARK_F_EXIT);
P(" %.4s\texit_code=%d exit_time=%llu\n", flagname,
qp->exit_code, qp->exit_time_event);
}
fflush(f);
return (0);
}
#undef P
/* User facing version of process_cache_lookup() */
const struct quark_process *
quark_process_lookup(struct quark_queue *qq, int pid)
{
return (process_cache_get(qq, pid, 0));
}
void
quark_process_iter_init(struct quark_process_iter *qi, struct quark_queue *qq)
{
qi->qq = qq;
qi->qp = RB_MIN(process_by_pid, &qq->process_by_pid);
}
const struct quark_process *
quark_process_iter_next(struct quark_process_iter *qi)
{
const struct quark_process *qp;
qp = qi->qp;
if (qi->qp != NULL)
qi->qp = RB_NEXT(process_by_pid, &qq->process_by_pid, qi->qp);
return (qp);
}
static struct quark_event *
raw_event_process(struct quark_queue *qq, struct raw_event *src)
{
struct quark_process *qp;
struct quark_event *dst;
struct raw_event *agg;
struct raw_task *raw_fork, *raw_exit, *raw_task;
struct raw_comm *raw_comm;
struct raw_exec *raw_exec;
struct raw_exec_connector *raw_exec_connector;
char *comm;
char *cwd;
char *args;
size_t args_len;
u64 events;
dst = &qq->event_storage;
raw_fork = NULL;
raw_exit = NULL;
raw_task = NULL;
raw_comm = NULL;
raw_exec = NULL;
raw_exec_connector = NULL;
comm = NULL;
cwd = NULL;
args = NULL;
args_len = 0;
/* XXX pass if this is a fork down, so we can evict the old one XXX */
qp = process_cache_get(qq, src->pid, 1);
if (qp == NULL)
return (NULL);
events = 0;
switch (src->type) {
case RAW_WAKE_UP_NEW_TASK:
events |= QUARK_EV_FORK;
raw_fork = &src->task;
break;
case RAW_EXEC:
events |= QUARK_EV_EXEC;
raw_exec = &src->exec;
break;
case RAW_EXIT_THREAD:
events |= QUARK_EV_EXIT;
raw_exit = &src->task;
break;
case RAW_COMM:
events |= QUARK_EV_SETPROCTITLE;
raw_comm = &src->comm;
break;
case RAW_EXEC_CONNECTOR:
events |= QUARK_EV_EXEC;
raw_exec_connector = &src->exec_connector;
break;
default:
return (NULL);
break; /* NOTREACHED */
};
TAILQ_FOREACH(agg, &src->agg_queue, agg_entry) {
switch (agg->type) {
case RAW_WAKE_UP_NEW_TASK:
raw_fork = &agg->task;
events |= QUARK_EV_FORK;
break;
case RAW_EXEC:
events |= QUARK_EV_EXEC;
raw_exec = &agg->exec;
break;
case RAW_EXIT_THREAD:
events |= QUARK_EV_EXIT;
raw_exit = &agg->task;
break;
case RAW_COMM:
events |= QUARK_EV_SETPROCTITLE;
raw_comm = &agg->comm;
break;
case RAW_EXEC_CONNECTOR:
events |= QUARK_EV_EXEC;
raw_exec_connector = &agg->exec_connector;
break;
default:
break;
}
}
/* QUARK_F_PROC */
if (raw_fork != NULL) {
process_cache_inherit(qq, qp, raw_fork->ppid);
raw_task = raw_fork;
cwd = raw_task->cwd.p;
}
if (raw_exit != NULL) {
qp->flags |= QUARK_F_EXIT;
qp->exit_code = raw_exit->exit_code;
if (raw_exit->exit_time_event)
qp->exit_time_event = quark.boottime + raw_exit->exit_time_event;
raw_task = raw_exit;
/* cwd is invalid, don't collect */
/* NOTE: maybe there are more things we _don't_ want from exit */
}
if (raw_exec != NULL) {
qp->flags |= QUARK_F_FILENAME;
strlcpy(qp->filename, raw_exec->filename.p, sizeof(qp->filename));
if (raw_exec->flags & RAW_EXEC_F_EXT) {
args = raw_exec->ext.args.p;
args_len = raw_exec->ext.args_len;
raw_task = &raw_exec->ext.task;
cwd = raw_task->cwd.p;
}
}
if (raw_exec_connector != NULL) {
args = raw_exec_connector->args.p;
args_len = raw_exec_connector->args_len;
raw_task = &raw_exec_connector->task;
cwd = raw_task->cwd.p;
}
if (raw_task != NULL) {
qp->flags |= QUARK_F_PROC;