-
Notifications
You must be signed in to change notification settings - Fork 4
/
netdumpd.c
1323 lines (1174 loc) · 33.2 KB
/
netdumpd.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
/*-
* SDPX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2005-2011 Sandvine Incorporated. All rights reserved.
* Copyright (c) 2016-2018 Dell EMC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/capsicum.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <sys/event.h>
#include <sys/kerneldump.h>
#include <sys/nv.h>
#include <sys/procdesc.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <assert.h>
#include <capsicum_helpers.h>
#include <err.h>
#include <fcntl.h>
#include <libgen.h>
#include <netdb.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <libcasper.h>
#include <libutil.h>
#include "cap_dns.h"
#include "netdumpd.h"
#include "kerneldump_compat.h"
#define MAX_DUMPS 1024 /* Maximum saved dumps per remote host. */
#define CLIENT_TIMEOUT 600 /* Netdump timeout period, in seconds. */
#define CLIENT_TPASS 10 /* Scan for timed-out clients every 10s. */
#define LOGERR(m, ...) \
(*g_phook)(LOG_ERR | LOG_DAEMON, (m), ## __VA_ARGS__)
#define LOGERR_PERROR(m) \
(*g_phook)(LOG_ERR | LOG_DAEMON, "%s: %s\n", m, strerror(errno))
#define LOGINFO(m, ...) \
(*g_phook)(LOG_INFO | LOG_DAEMON, (m), ## __VA_ARGS__)
#define LOGWARN(m, ...) \
(*g_phook)(LOG_WARNING | LOG_DAEMON, (m), ## __VA_ARGS__)
#define client_ntoa(cl) \
inet_ntoa((cl)->ip)
#define client_pinfo(cl, f, ...) \
fprintf((cl)->infofile, (f), ## __VA_ARGS__)
#define VMCORE_BUFSZ (128 * 1024)
struct netdump_client {
LIST_ENTRY(netdump_client) iter;
char *path;
char infofilename[MAXPATHLEN];
char corefilename[MAXPATHLEN];
char hostname[NI_MAXHOST];
time_t last_msg;
struct in_addr ip;
FILE *infofile;
int corefd;
int keyfilefd;
int sock;
int index;
bool any_data_rcvd;
ssize_t vmcorebufoff;
off_t vmcoreoff;
uint8_t vmcorebuf[VMCORE_BUFSZ];
};
/* Clients list. */
static LIST_HEAD(, netdump_client) g_clients = LIST_HEAD_INITIALIZER(g_clients);
/* Capabilities. */
static cap_channel_t *g_capdns, *g_caphandler, *g_capherald;
/* Program arguments handlers. */
static char g_dumpdir[MAXPATHLEN];
static int g_dumpdir_fd = -1;
static char g_defpath[MAXPATHLEN];
static char *g_handler_script;
static struct in_addr g_bindip;
/* Miscellaneous handlers. */
static struct pidfh *g_pfh;
static time_t g_now;
static time_t g_last_timeout_check;
static int g_kq;
static int g_sock = -1;
static bool g_debug = false;
/* Daemon print functions hook. */
static void (*g_phook)(int, const char *, ...);
static struct netdump_client *alloc_client(int sd, struct sockaddr_in *saddr,
char *path);
static void client_event(struct netdump_client *client);
static int eventloop(void);
static void exec_handler(struct netdump_client *client, const char *reason);
static void free_client(struct netdump_client *client);
static void handle_finish(struct netdump_client *client,
struct netdump_pkt *pkt);
static void handle_kdh(struct netdump_client *client,
struct netdump_pkt *pkt);
static void handle_timeout(struct netdump_client *client);
static void handle_vmcore(struct netdump_client *client,
struct netdump_pkt *pkt);
static void phook_printf(int priority, const char *message, ...)
__printflike(2, 3);
static void send_ack(struct netdump_client *client, uint32_t seqno);
static void server_event(void);
static void timeout_clients(void);
static void usage(void);
static void
usage(void)
{
fprintf(stderr,
"usage: %s [-D] [-a <bind_addr>] [-d <dumpdir>] [-i <script>] [-P <pidfile>]\n"
"\t\t[-p <default path>]\n",
getprogname());
}
static void
phook_printf(int priority, const char *message, ...)
{
va_list ap;
va_start(ap, message);
if ((priority & LOG_INFO) != 0)
vprintf(message, ap);
else
vfprintf(stderr, message, ap);
va_end(ap);
}
/*
* Attempt to read the bounds file for the specified client. Return the saved
* index if possible, else return -1.
*/
static int
read_index(struct netdump_client *client, const char *dir)
{
char bounds[MAXPATHLEN], buf[16];
FILE *fp;
size_t len;
u_long bound;
int fd;
len = snprintf(bounds, sizeof(bounds), "%s/bounds.%s", dir,
client->hostname);
if (len >= sizeof(bounds)) {
LOGERR("Truncated bounds file path: '%s'\n", bounds);
return (-1);
}
fd = openat(g_dumpdir_fd, bounds,
O_RDONLY | O_CREAT | O_CLOEXEC, 0600);
if (fd < 0) {
LOGERR("openat(%s): %s\n", bounds, strerror(errno));
return (-1);
}
fp = fdopen(fd, "r");
if (fp == NULL) {
LOGERR_PERROR("fdopen()");
(void)close(fd);
return (-1);
}
if (fgets(buf, sizeof(buf), fp) == NULL) {
if (feof(fp))
LOGINFO("Empty bounds file for client %s [%s]\n",
client->hostname, client_ntoa(client));
else
LOGERR("Failed to read bound for client %s [%s]: %s\n",
client->hostname, client_ntoa(client),
strerror(errno));
(void)fclose(fp);
return (-1);
}
(void)fclose(fp);
errno = 0;
bound = strtoul(buf, NULL, 10);
if ((bound == 0 && errno == EINVAL) ||
(bound == ULONG_MAX && errno == ERANGE) ||
bound > INT_MAX) {
LOGERR("Invalid bound for client %s [%s]: '%s'\n",
client->hostname, client_ntoa(client), buf);
return (-1);
}
return ((int)bound);
}
static int
write_index(struct netdump_client *client)
{
char bounds[MAXPATHLEN];
FILE *fp;
size_t len;
int fd;
len = snprintf(bounds, sizeof(bounds), "%s/bounds.%s", client->path,
client->hostname);
if (len >= sizeof(bounds)) {
LOGERR("Truncated bounds file path: '%s'\n", bounds);
return (-1);
}
fd = openat(g_dumpdir_fd, bounds, O_WRONLY | O_CLOEXEC);
if (fd < 0) {
LOGERR("openat(%s): %s\n", bounds, strerror(errno));
return (-1);
}
fp = fdopen(fd, "w");
if (fp == NULL) {
LOGERR_PERROR("fdopen()");
(void)close(fd);
return (-1);
}
(void)fprintf(fp, "%d\n", client->index + 1);
(void)fclose(fp);
return (0);
}
static int
open_info_file(struct netdump_client *client, const char *dir, int idx)
{
size_t len;
int fd;
len = snprintf(client->infofilename, sizeof(client->infofilename),
"%s/info.%s.%d", dir, client->hostname, idx);
if (len >= sizeof(client->infofilename)) {
LOGERR("Truncated info file path: '%s'\n",
client->infofilename);
return (-1);
}
fd = openat(g_dumpdir_fd, client->infofilename,
O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
if (fd == -1 && errno != EEXIST)
LOGERR("openat(\"%s\"): %s\n",
client->infofilename, strerror(errno));
return (fd);
}
static int
open_client_files(struct netdump_client *client, const char *dir)
{
size_t len;
int error, fd, idx;
fd = -1;
idx = read_index(client, dir);
if (idx >= 0) {
fd = open_info_file(client, dir, idx);
if (fd < 0) {
LOGERR("Incorrect bounds file for client %s [%s]\n",
client->hostname, client_ntoa(client));
idx = -1;
}
}
if (idx == -1) {
for (idx = 0; idx < MAX_DUMPS; idx++) {
fd = open_info_file(client, dir, idx);
if (fd >= 0)
break;
}
if (idx == MAX_DUMPS) {
LOGERR("No free index for client %s [%s]\n",
client->hostname, client_ntoa(client));
return (-1);
}
}
client->index = idx;
client->infofile = fdopen(fd, "a");
if (client->infofile == NULL) {
LOGERR_PERROR("fdopen()");
(void)close(fd);
(void)unlinkat(g_dumpdir_fd, client->infofilename, 0);
return (-1);
}
len = snprintf(client->corefilename, sizeof(client->corefilename),
"%s/vmcore.%s.%d", dir, client->hostname, client->index);
if (len >= sizeof(client->corefilename)) {
LOGERR("Truncated core file path: '%s'\n",
client->corefilename);
return (-1);
}
fd = openat(g_dumpdir_fd, client->corefilename,
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
if (fd == -1) {
error = errno;
/* Failed. Keep the numbers in sync. */
(void)fclose(client->infofile);
(void)unlinkat(g_dumpdir_fd, client->infofilename, 0);
client->infofile = NULL;
LOGERR("openat(%s): %s\n",
client->corefilename, strerror(error));
return (-1);
}
client->corefd = fd;
return (0);
}
/*
* Allocate a bookkeeping structure for a new client. The client may, in its
* herald message, specify a path relative to the dumpdir in which to store the
* dump.
*/
static struct netdump_client *
alloc_client(int sd, struct sockaddr_in *saddr, char *path)
{
struct kevent event;
struct netdump_client *client;
char *firstdot, *origpath;
int error, bufsz;
client = calloc(1, sizeof(*client));
if (client == NULL) {
LOGERR_PERROR("calloc()");
goto error_out;
}
client->corefd = client->keyfilefd = -1;
client->index = -1;
client->sock = sd;
client->last_msg = g_now;
client->ip = saddr->sin_addr;
error = cap_getnameinfo(g_capdns, (struct sockaddr *)saddr,
saddr->sin_len, client->hostname, sizeof(client->hostname),
NULL, 0, NI_NAMEREQD);
if (error != 0) {
/* Can't resolve, try with a numeric IP. */
error = cap_getnameinfo(g_capdns, (struct sockaddr *)saddr,
saddr->sin_len, client->hostname, sizeof(client->hostname),
NULL, 0, 0);
if (error != 0) {
LOGERR("cap_getnameinfo(): %s\n", gai_strerror(error));
goto error_out;
}
} else {
/* Strip off the domain name. */
firstdot = strchr(client->hostname, '.');
if (firstdot)
*firstdot = '\0';
}
/* It should be enough to hold approximatively twice the chunk size. */
bufsz = 128 * 1024;
if (setsockopt(client->sock, SOL_SOCKET, SO_RCVBUF, &bufsz,
sizeof(bufsz))) {
LOGERR_PERROR("setsockopt()");
LOGWARN(
"May drop packets from %s due to small receive buffer\n",
client->hostname);
}
origpath = path;
if (path == NULL)
/* g_defpath defaults to "." */
path = strdup(g_defpath);
error = open_client_files(client, path);
if (error != 0) {
if (origpath != NULL) {
LOGWARN(
"Can't create output files in path for client %s [%s], retrying with default\n",
client->hostname, client_ntoa(client));
free(origpath);
path = strdup(g_defpath);
error = open_client_files(client, path);
}
if (error != 0) {
LOGERR(
"Can't create output files for new client %s [%s]\n",
client->hostname, client_ntoa(client));
goto error_out;
}
}
client->path = path;
EV_SET(&event, client->sock, EVFILT_READ, EV_ADD, 0, 0, client);
if (kevent(g_kq, &event, 1, NULL, 0, NULL) != 0) {
LOGERR_PERROR("kevent(EV_ADD)");
goto error_out;
}
(void)write_index(client);
LIST_INSERT_HEAD(&g_clients, client, iter);
return (client);
error_out:
free(path);
if (client != NULL) {
if (client->infofile != NULL)
(void)fclose(client->infofile);
if (client->corefd != -1)
(void)close(client->corefd);
if (client->sock != -1)
(void)close(client->sock);
free(client);
}
return (NULL);
}
static void
free_client(struct netdump_client *client)
{
struct kevent event;
EV_SET(&event, client->sock, EVFILT_READ, EV_DELETE, 0, 0, NULL);
if (kevent(g_kq, &event, 1, NULL, 0, NULL) != 0)
LOGERR_PERROR("kevent(EV_DELETE)");
/* Remove from the list. Ignore errors from close() routines. */
LIST_REMOVE(client, iter);
if (client->keyfilefd != -1)
(void)close(client->keyfilefd);
(void)fclose(client->infofile);
(void)close(client->corefd);
(void)close(client->sock);
free(client->path);
free(client);
}
static void
exec_handler(struct netdump_client *client, const char *reason)
{
struct kevent ev;
int pd;
if (g_caphandler == NULL)
return;
pd = netdump_cap_handler(g_caphandler, reason, client_ntoa(client),
client->hostname, client->infofilename, client->corefilename);
if (pd < 0)
LOGERR("netdump_cap_handler(): %m");
EV_SET(&ev, pd, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
if (kevent(g_kq, &ev, 1, NULL, 0, NULL) != 0) {
LOGERR_PERROR("kevent(procdesc)");
/* There is not much we can do aside from kill the process. */
(void)close(pd);
}
}
static void
handle_timeout(struct netdump_client *client)
{
assert(client != NULL);
LOGINFO("Client %s timed out\n", client_ntoa(client));
client_pinfo(client, "Dump incomplete: client timed out\n");
exec_handler(client, "timeout");
free_client(client);
}
static int
vmcore_flush(struct netdump_client *client)
{
ssize_t len, n;
off_t off;
int error;
len = client->vmcorebufoff;
off = 0;
while (len > 0) {
n = pwrite(client->corefd, client->vmcorebuf + off, len,
client->vmcoreoff + off);
if (n < 0) {
error = errno;
LOGERR("pwrite (for client %s [%s]): %s\n",
client->hostname, client_ntoa(client),
strerror(errno));
if (error == EINTR)
continue;
client_pinfo(client,
"Dump unsuccessful: write error @ offset %08jx: %s\n",
(uintmax_t)off, strerror(errno));
exec_handler(client, "error");
free_client(client);
return (1);
}
len -= n;
off += n;
}
client->vmcorebufoff = 0;
return (0);
}
static void
timeout_clients(void)
{
struct netdump_client *client, *tmp;
/* Only time out clients every 10 seconds. */
if (g_now - g_last_timeout_check < CLIENT_TPASS)
return;
g_last_timeout_check = g_now;
/* Traverse the list looking for stale clients. */
LIST_FOREACH_SAFE(client, &g_clients, iter, tmp) {
if (client->last_msg + CLIENT_TIMEOUT < g_now)
handle_timeout(client);
}
}
static void
send_ack(struct netdump_client *client, uint32_t seqno)
{
struct netdump_ack ack;
memset(&ack, 0, sizeof(ack));
ack.na_seqno = htonl(seqno);
if (send(client->sock, &ack, sizeof(ack), 0) == -1)
LOGERR_PERROR("send()");
/*
* XXX: On EAGAIN, we should probably queue the packet
* to be sent when the socket is writable but
* that is too much effort, since it is mostly
* harmless to wait for the client to retransmit.
*/
}
static void
handle_kdh(struct netdump_client *client, struct netdump_pkt *pkt)
{
#if KERNELDUMPVERSION >= 3
static const char *const compalgos[] = {
[KERNELDUMP_COMP_NONE] = "none",
[KERNELDUMP_COMP_GZIP] = "gzip",
#ifdef KERNELDUMP_COMP_ZSTD
[KERNELDUMP_COMP_ZSTD] = "zstd",
#endif
NULL,
};
static const char *const suffixes[] = {
[KERNELDUMP_COMP_GZIP] = ".gz",
#ifdef KERNELDUMP_COMP_ZSTD
[KERNELDUMP_COMP_ZSTD] = ".zst",
#endif
};
const char *compalgo;
#endif /* KERNELDUMPVERSION >= 3 */
#if KERNELDUMPVERSION >= 2
char newpath[MAXPATHLEN];
char *p;
int limit;
#endif
struct kerneldumpheader *kdh;
uint64_t dumplen;
time_t t;
int parity_check;
client->any_data_rcvd = true;
LOGINFO("(KDH from %s [%s])\n", client->hostname, client_ntoa(client));
send_ack(client, pkt->hdr.mh_seqno);
if (pkt->hdr.mh_len < sizeof(struct kerneldumpheader)) {
LOGERR("Bad KDH from %s [%s]: packet too small\n",
client->hostname, client_ntoa(client));
client_pinfo(client, "Bad KDH: packet too small\n");
return;
}
kdh = (struct kerneldumpheader *)(void *)pkt->data;
parity_check = kerneldump_parity(kdh);
/* Make sure all the strings are null-terminated. */
kdh->architecture[sizeof(kdh->architecture) - 1] = '\0';
kdh->hostname[sizeof(kdh->hostname) - 1] = '\0';
kdh->versionstring[sizeof(kdh->versionstring) - 1] = '\0';
kdh->panicstring[sizeof(kdh->panicstring) - 1] = '\0';
client_pinfo(client, " Architecture: %s\n", kdh->architecture);
client_pinfo(client, " Architecture Version: %d\n",
dtoh32(kdh->architectureversion));
dumplen = dtoh64(kdh->dumplength);
client_pinfo(client, " Dump Length: %lldB (%lld MB)\n",
(long long)dumplen, (long long)(dumplen >> 20));
client_pinfo(client, " Blocksize: %d\n", dtoh32(kdh->blocksize));
t = dtoh64(kdh->dumptime);
#if KERNELDUMPVERSION >= 3
if (kdh->version >= 3) {
if (kdh->compression < nitems(compalgos))
compalgo = compalgos[kdh->compression];
else
compalgo = "???";
client_pinfo(client, " Compression: %s\n", compalgo);
}
#endif
client_pinfo(client, " Dumptime: %s", ctime(&t));
client_pinfo(client, " Hostname: %s\n", kdh->hostname);
client_pinfo(client, " Version String: %s", kdh->versionstring);
client_pinfo(client, " Panic String: %s\n", kdh->panicstring);
client_pinfo(client, " Header Parity Check: %s\n",
parity_check ? "Fail" : "Pass");
fflush(client->infofile);
#if KERNELDUMPVERSION >= 2
if (kdh->version >= 2 && kdh->dumpkeysize > 0) {
(void)strlcpy(newpath, client->corefilename, sizeof(newpath));
p = strrchr(newpath, '/');
if (p == NULL ||
strncmp(p + 1, "vmcore.", 7) != 0)
LOGWARN("Corrupted core file path '%s'\n",
client->corefilename);
else {
p++;
limit = sizeof(newpath) - (p - &newpath[0]);
if (snprintf(p, limit, "vmcore_encrypted.%s.%d",
client->hostname, client->index) < limit) {
renameat(g_dumpdir_fd, client->corefilename,
g_dumpdir_fd, newpath);
(void)strlcpy(client->corefilename, newpath,
sizeof(client->corefilename));
} else
LOGWARN(
"Couldn't append encryption suffix to '%s'\n",
client->corefilename);
}
}
#endif
#if KERNELDUMPVERSION >= 3
/*
* Rename the dump file so that it has the correct suffix, and truncate
* it to the correct size. The kernel dump code always writes in
* multiples of the block size, so the resulting file size will be
* rounded up to the next multiple. The trailing bytes confuse
* decompressors, so chop them off now. This is a bit of a hack, but so
* it goes.
*/
if (kdh->version >= 3 && kdh->compression < nitems(compalgos) &&
kdh->compression != KERNELDUMP_COMP_NONE) {
(void)strlcpy(newpath, client->corefilename, sizeof(newpath));
if (strlcat(newpath, suffixes[kdh->compression],
sizeof(newpath)) < sizeof(newpath)) {
renameat(g_dumpdir_fd, client->corefilename,
g_dumpdir_fd, newpath);
(void)strlcpy(client->corefilename, newpath,
sizeof(client->corefilename));
} else
LOGWARN("Couldn't append compression suffix to '%s'\n",
client->corefilename);
if (vmcore_flush(client) == 0 &&
ftruncate(client->corefd, dumplen) != 0)
/* Not fatal. */
LOGERR_PERROR("ftruncate()");
}
#endif
}
static void
handle_ekcd_key(struct netdump_client *client, struct netdump_pkt *pkt)
{
char *data;
ssize_t n;
uint32_t bytes, offset;
int fd;
if (client->keyfilefd == -1) {
char keyfile[MAXPATHLEN];
size_t len;
len = snprintf(keyfile, sizeof(keyfile), "%s/key.%s.%d",
client->path, client->hostname, client->index);
if (len >= sizeof(keyfile)) {
LOGERR(
"Truncated key file path for client %s [%s]: %s\n",
client->hostname, client_ntoa(client), keyfile);
return;
}
fd = openat(g_dumpdir_fd, keyfile,
O_WRONLY | O_CREAT | O_CLOEXEC, 0400);
if (fd < 0) {
LOGERR_PERROR("openat()");
return;
}
client->keyfilefd = fd;
LOGINFO("(EKCD key from %s [%s])\n",
client->hostname, client_ntoa(client));
} else
fd = client->keyfilefd;
bytes = pkt->hdr.mh_len;
data = pkt->data;
offset = pkt->hdr.mh_offset;
do {
n = pwrite(fd, data, bytes, offset);
if (n < 0) {
LOGERR_PERROR("write()");
return;
}
bytes -= n;
data += n;
offset += n;
} while (bytes > 0);
send_ack(client, pkt->hdr.mh_seqno);
}
static void
handle_vmcore(struct netdump_client *client, struct netdump_pkt *pkt)
{
client->any_data_rcvd = true;
if (pkt->hdr.mh_seqno % (16 * 1024 * 1024 / 1456) == 0) {
/* Approximately every 16MB with MTU of 1500 */
LOGINFO(".");
}
/*
* Flush the vmcore buffer if it's full, or if the received segment
* isn't contiguous with respect to any already-buffered data.
*/
if (client->vmcorebufoff + NETDUMP_DATASIZE > VMCORE_BUFSZ ||
(client->vmcorebufoff > 0 &&
client->vmcoreoff + client->vmcorebufoff !=
(off_t)pkt->hdr.mh_offset))
if (vmcore_flush(client) != 0)
return;
/*
* Buffer vmcore contents. This greatly improves throughput over
* simply writing each packet's contents directly.
*/
memcpy(client->vmcorebuf + client->vmcorebufoff, pkt->data,
pkt->hdr.mh_len);
if (client->vmcorebufoff == 0)
client->vmcoreoff = pkt->hdr.mh_offset;
client->vmcorebufoff += pkt->hdr.mh_len;
send_ack(client, pkt->hdr.mh_seqno);
}
static void
symlink_client_file(const char *path, const char *hostname, const char *file,
const char *target)
{
char symlinkpath[MAXPATHLEN], *symlinktarget;
size_t len;
len = snprintf(symlinkpath, sizeof(symlinkpath), "%s/%s.%s.last",
path, file, hostname);
if (len >= sizeof(symlinkpath)) {
LOGWARN("client %s file path is too long");
return;
}
if (unlinkat(g_dumpdir_fd, symlinkpath, 0) != 0 && errno != ENOENT) {
LOGERR_PERROR("unlinkat()");
return;
}
symlinktarget = strdup(target);
if (symlinkat(basename(symlinktarget), g_dumpdir_fd, symlinkpath) != 0)
LOGERR_PERROR("symlink()");
free(symlinktarget);
}
static void
handle_finish(struct netdump_client *client, struct netdump_pkt *pkt)
{
/* Make sure we commit any buffered vmcore data. */
if (vmcore_flush(client) != 0)
return;
if (fsync(client->corefd) != 0)
/* Not fatal. */
LOGERR_PERROR("fsync()");
/* Create symlinks to the new vmcore and info files. */
symlink_client_file(client->path, client->hostname, "vmcore",
client->corefilename);
symlink_client_file(client->path, client->hostname, "info",
client->infofilename);
LOGINFO("Completed dump from client %s [%s]\n", client->hostname,
client_ntoa(client));
client_pinfo(client, "Dump complete\n");
send_ack(client, pkt->hdr.mh_seqno);
exec_handler(client, "success");
free_client(client);
}
/* Handle a read event on the server socket. */
static void
server_event(void)
{
char *path;
struct sockaddr_in saddr;
struct netdump_client *client;
uint32_t seqno;
int error, sd;
error = netdump_cap_herald(g_capherald, &sd, &saddr, &seqno, &path);
if (error != 0) {
LOGERR("netdump_cap_herald(): %s\n", strerror(error));
return;
}
LIST_FOREACH(client, &g_clients, iter) {
if (client->ip.s_addr == saddr.sin_addr.s_addr)
break;
}
if (client != NULL) {
if (!client->any_data_rcvd) {
/* retransmit of the herald packet */
send_ack(client, seqno);
return;
}
handle_timeout(client);
}
/* path is always consumed or freed by alloc_client(). */
client = alloc_client(sd, &saddr, path);
path = NULL;
if (client == NULL) {
LOGERR(
"server_event(): new client allocation failure\n");
return;
}
client_pinfo(client, "Dump from %s [%s]\n", client->hostname,
client_ntoa(client));
LOGINFO("New dump from client %s [%s] (to %s)\n", client->hostname,
client_ntoa(client), client->corefilename);
send_ack(client, seqno);
}
/* Handle a read event on a client socket. */
static void
client_event(struct netdump_client *client)
{
struct netdump_pkt pkt;
ssize_t len;
if ((len = recv(client->sock, &pkt, sizeof(pkt), 0)) < 0) {
if (errno != EAGAIN && errno != EINTR) {
LOGERR_PERROR("recv()");
handle_timeout(client);
}
return;
}
if ((size_t)len < sizeof(struct netdump_msg_hdr)) {
LOGERR("Ignoring runt packet from %s (got %zu)\n",
client_ntoa(client), (size_t)len);
return;
}
ndtoh(&pkt.hdr);
if ((size_t)len - sizeof(struct netdump_msg_hdr) != pkt.hdr.mh_len) {
LOGERR("Bad packet size from %s\n", client_ntoa(client));
return;
}
client->last_msg = time(NULL);
switch (pkt.hdr.mh_type) {
case NETDUMP_KDH:
handle_kdh(client, &pkt);
break;
case NETDUMP_EKCD_KEY:
handle_ekcd_key(client, &pkt);
break;
case NETDUMP_VMCORE:
handle_vmcore(client, &pkt);
break;
case NETDUMP_FINISHED:
handle_finish(client, &pkt);
break;
default:
LOGERR("Received unexpected message type %d from %s\n",
pkt.hdr.mh_type, client_ntoa(client));
break;
}
}
static int
handle_event(struct kevent *ev)
{
pid_t pid;
int pd, status;
switch (ev->filter) {
case EVFILT_SIGNAL:
/* We received SIGINT or SIGTERM. */
return (-1);
case EVFILT_READ:
if ((int)ev->ident == g_sock)
server_event();
else
client_event((struct netdump_client *)ev->udata);
break;
case EVFILT_PROCDESC:
pd = (int)ev->ident;
if (pdgetpid(pd, &pid) != 0) {
LOGERR_PERROR("pdgetpid()");
break;
}
status = ev->data;
if (status == 0)
LOGINFO("Process %d exited with status 0\n", pid);
else
LOGWARN("Process %d exited with status %d\n", pid,
status);
(void)close(pd);
break;
default:
LOGERR("unexpected event %d", ev->filter);
break;
}
return (0);
}
static int
eventloop(void)
{
struct kevent events[8];
struct timespec ts;
int ev, rc;
LOGINFO("Waiting for clients.\n");
/* We check for timed-out clients regularly. */
ts.tv_sec = CLIENT_TPASS;
ts.tv_nsec = 0;
for (;;) {
rc = kevent(g_kq, NULL, 0, events, nitems(events), &ts);
if (rc < 0) {
if (errno == EINTR)
continue;
LOGERR_PERROR("kevent()");
return (1);
}
g_now = time(NULL);
for (ev = 0; ev < rc; ev++)
if (handle_event(&events[ev]) != 0)
goto out;
timeout_clients();
}
out:
LOGINFO("Shutting down...");
/*