-
Notifications
You must be signed in to change notification settings - Fork 7
/
memcr.c
3031 lines (2453 loc) · 70.9 KB
/
memcr.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
/*
* Copyright (C) 2022 Liberty Global Service B.V.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2
* of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdarg.h>
#include <dirent.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <linux/ptrace.h>
#include <sys/user.h>
#include <sys/param.h> /* MIN(), MAX() */
#include <sys/mman.h>
#ifdef COMPRESS_LZ4
#include <lz4.h>
#endif
#ifdef CHECKSUM_MD5
#include <openssl/opensslv.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/evp.h>
#else
#include <openssl/md5.h>
#endif
#endif
#include "memcr.h"
#include "arch/cpu.h"
#include "arch/enter.h"
#include "parasite-blob.h"
#ifndef ARCH_NAME
#define ARCH_NAME "unknown"
#endif
#define NT_PRSTATUS 1
#define __round_mask(x, y) ((__typeof__(x))((y)-1))
#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
#define round_down(x, y) ((x) & ~__round_mask(x, y))
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#ifndef SI_FROMUSER
#define SI_FROMUSER(siptr) ((siptr)->si_code <= 0)
#endif
#define PARASITE_CMD_ADDR(start) (((char *)start) + parasite_blob_offset__parasite_cmd)
#define PARASITE_ARGS_ADDR(start) (((char *)start) + parasite_blob_offset__parasite_args)
#define __DEBUG__ fprintf(stderr, "%s: %s() +%d\n", __FILE__, __func__, __LINE__);
#define PROT_NONE 0x0
#define PROT_READ 0x1
#define PROT_WRITE 0x2
#define PROT_EXEC 0x4
#define FLAG_NONE 0x0
#define FLAG_STACK 0x1 /* stack */
#define FLAG_HEAP 0x2 /* heap */
#define FLAG_ANON 0x3 /* anonymous mapping */
#define FLAG_FILE 0x4 /* file mapped area */
static char *flag_desc[5] = {
[FLAG_NONE] = "none",
[FLAG_STACK] = "stck",
[FLAG_HEAP] = "heap",
[FLAG_ANON] = "anon",
[FLAG_FILE] = "file",
};
#define DEBUG_SIGSET 0
struct vm_area {
unsigned long start;
unsigned long end;
unsigned long prot;
unsigned long flags;
};
static char *dump_dir;
static char *parasite_socket_dir;
static int parasite_socket_use_netns;
static int no_wait;
static int proc_mem;
static int rss_file;
static int compress;
static int checksum;
static int service;
static unsigned int timeout;
#define BIT(x) (1ULL << x)
#define PM_PAGE_FRAME_NUMBER_MASK 0x007fffffffffffff
#define PM_PAGE_FILE_OR_SHARED_ANON 61
#define PM_PAGE_SWAPPED 62
#define PM_PAGE_PRESENT 63
#define KPF_UNEVICTABLE 18 /* (since Linux 2.6.31) */
static int kpageflags_fd;
#define PATH_MAX 4096
#define MAX_THREADS 1024
static pid_t tids[MAX_THREADS];
static int nr_threads;
#define SERVICE_MODE_SELECT_TIMEOUT_MS 100
#define MAX_VMAS (3*4096)
static struct vm_area vmas[MAX_VMAS];
static int nr_vmas;
#define MAX_VM_REGION_SIZE (256 * PAGE_SIZE)
#ifdef COMPRESS_LZ4
#define MAX_LZ4_DST_SIZE LZ4_compressBound(MAX_VM_REGION_SIZE)
#endif
static pid_t parasite_pid;
static pid_t parasite_pid_clone;
static struct target_context ctx;
static sig_atomic_t interrupted;
static struct {
pthread_t thread_id;
pthread_mutex_t lock;
pthread_cond_t cond;
int changed;
int status;
} parasite_watch = {
.lock = PTHREAD_MUTEX_INITIALIZER,
.cond = PTHREAD_COND_INITIALIZER,
};
/*
* man sigaction: For a ptrace(2) event, si_code will contain SIG‐TRAP and have the ptrace event in the high byte:
* (SIGTRAP | PTRACE_EVENT_foo << 8).
*/
#define SI_EVENT(si_code) (((si_code) & 0xFF00) >> 8)
/*
* These functions are used for interacting with a dump file and are declared
* as weak symbols so that a shared library can provide them.
*/
int __attribute__((weak)) lib__open(const char *pathname, int flags, mode_t mode);
int __attribute__((weak)) lib__close(int fd);
int __attribute__((weak)) lib__read(int fd, void *buf, size_t count);
int __attribute__((weak)) lib__write(int fd, const void *buf, size_t count);
int __attribute__((weak)) lib__init(int enable, const char *arg);
int __attribute__((weak)) lib__fini(void);
#define CHECKPOINTED_PIDS_LIMIT 48
#define PID_INVALID 0
#define STATE_RESTORED 0
#define STATE_CHECKPOINTING 1
#define STATE_CHECKPOINTED 2
static pthread_mutex_t checkpoint_service_data_lock = PTHREAD_MUTEX_INITIALIZER;
static struct {
pid_t pid;
pid_t worker;
int state;
int checkpoint_abort;
int checkpoint_cmd_sd;
} checkpoint_service_data[CHECKPOINTED_PIDS_LIMIT];
#define SOCKET_INVALID (-1)
static int checkpoint_service_socket = SOCKET_INVALID;
#define TRUE 1
#define FALSE 0
#define MAX_CLIENT_CONNECTIONS 8
struct service_command_ctx {
struct service_command svc_cmd;
int cd;
};
static struct {
pthread_mutex_t lock;
pthread_cond_t cond;
struct service_command_ctx svc_ctxs[MAX_CLIENT_CONNECTIONS];
int front_idx;
int back_idx;
size_t size;
int interrupt;
} service_cmds_ctx = { .lock = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER };
static int service_cmds_push_back(struct service_command_ctx *ctx)
{
int ret = 0;
pthread_mutex_lock(&service_cmds_ctx.lock);
if (service_cmds_ctx.size >= MAX_CLIENT_CONNECTIONS) {
fprintf(stderr, "[-] %s: Commands queue full\n", __func__);
ret = 1;
goto err;
}
service_cmds_ctx.svc_ctxs[service_cmds_ctx.back_idx] = *ctx;
service_cmds_ctx.back_idx++;
service_cmds_ctx.size++;
if(service_cmds_ctx.back_idx >= MAX_CLIENT_CONNECTIONS)
service_cmds_ctx.back_idx = 0;
pthread_cond_signal(&service_cmds_ctx.cond);
err:
pthread_mutex_unlock(&service_cmds_ctx.lock);
return ret;
}
static int service_cmds_wait_and_pop_front(struct service_command_ctx *ctx)
{
int ret = 0;
pthread_mutex_lock(&service_cmds_ctx.lock);
while (service_cmds_ctx.size == 0 && service_cmds_ctx.interrupt == FALSE && ret == 0)
ret = pthread_cond_wait(&service_cmds_ctx.cond, &service_cmds_ctx.lock);
if (!ret && service_cmds_ctx.size > 0 && service_cmds_ctx.interrupt == FALSE) {
*ctx = service_cmds_ctx.svc_ctxs[service_cmds_ctx.front_idx];
service_cmds_ctx.front_idx++;
service_cmds_ctx.size--;
if(service_cmds_ctx.front_idx >= MAX_CLIENT_CONNECTIONS)
service_cmds_ctx.front_idx = 0;
} else if (service_cmds_ctx.interrupt == TRUE)
ret = 1;
else
fprintf(stderr, "[-] %s: pthread_cond_wait() failed: %d\n", __func__, ret);
pthread_mutex_unlock(&service_cmds_ctx.lock);
return ret;
}
static void service_cmds_interrupt(void)
{
pthread_mutex_lock(&service_cmds_ctx.lock);
service_cmds_ctx.interrupt = TRUE;
pthread_cond_signal(&service_cmds_ctx.cond);
pthread_mutex_unlock(&service_cmds_ctx.lock);
}
#ifdef CHECKSUM_MD5
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#define MD5_DIGEST_SIZE EVP_MAX_MD_SIZE
static EVP_MD_CTX *md5_checkpoint_ctx;
static EVP_MD_CTX *md5_restore_ctx;
#else
#define MD5_DIGEST_SIZE MD5_DIGEST_LENGTH
static MD5_CTX md5_checkpoint_ctx;
static MD5_CTX md5_restore_ctx;
#endif
static unsigned char md5_checkpoint_digest[MD5_DIGEST_SIZE];
static unsigned int md5_checkpoint_digest_len;
static unsigned char md5_restore_digest[MD5_DIGEST_SIZE];
static unsigned int md5_restore_digest_len;
static void md5_init(void *ctx)
{
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
const EVP_MD *md5_md = EVP_md5();
EVP_MD_CTX **ctx_ptr = (EVP_MD_CTX **)ctx;
*ctx_ptr = EVP_MD_CTX_new();
if (!EVP_DigestInit_ex2(*ctx_ptr, md5_md, NULL)) {
fprintf(stdout, "[-] MD5 digest initialization failed.\n");
EVP_MD_CTX_free(*ctx_ptr);
*ctx_ptr = NULL;
}
#else
MD5_Init(ctx);
#endif
}
static void md5_update(void *ctx, const void *data, size_t len)
{
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
EVP_MD_CTX **ctx_ptr = (EVP_MD_CTX **)ctx;
if (*ctx_ptr == NULL)
return;
if (!EVP_DigestUpdate(*ctx_ptr, data, len)) {
fprintf(stdout, "[-] Message digest update failed.\n");
EVP_MD_CTX_free(*ctx_ptr);
*ctx_ptr = NULL;
}
#else
MD5_Update(ctx, data, len);
#endif
}
static void md5_final(unsigned char *md, unsigned int *len, void *ctx)
{
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
EVP_MD_CTX **ctx_ptr = (EVP_MD_CTX **)ctx;
*len = 0;
if (*ctx_ptr == NULL)
return;
if (!EVP_DigestFinal_ex(*ctx_ptr, md, len)) {
fprintf(stdout, "[-] Message digest finalization failed.\n");
}
EVP_MD_CTX_free(*ctx_ptr);
*ctx_ptr = NULL;
#else
*len = MD5_DIGEST_SIZE;
MD5_Final(md, ctx);
#endif
}
#endif
static void parasite_status_signal(pid_t pid, int status)
{
pthread_mutex_lock(¶site_watch.lock);
parasite_watch.changed = 1;
parasite_watch.status = status;
pthread_cond_signal(¶site_watch.cond);
pthread_mutex_unlock(¶site_watch.lock);
if (WIFEXITED(status))
; /* normal exit */
else if (WIFSIGNALED(status))
if (WTERMSIG(status) == SIGKILL)
printf("[-] parasite killed by SIGKILL\n");
else
printf("[i] parasite terminated by signal %d%s\n", WTERMSIG(status), WCOREDUMP(status) ? " (code dumped)" : " ");
else
printf("[-] unhandled parasite status %x\n", status);
}
static int parasite_status_wait(int *status)
{
int ret = 0;
struct timespec ts;
while (1) {
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 1;
pthread_mutex_lock(¶site_watch.lock);
while (!parasite_watch.changed && (ret == 0 || ret == ETIMEDOUT))
ret = pthread_cond_timedwait(¶site_watch.cond, ¶site_watch.lock, &ts);
if (!ret)
*status = parasite_watch.status;
pthread_mutex_unlock(¶site_watch.lock);
if (ret != ETIMEDOUT)
break;
fprintf(stdout, "[i] waiting for parasite status change\n");
}
if (ret)
fprintf(stderr, "[-] parasite status cond timedwait failed: %d\n", ret);
pthread_join(parasite_watch.thread_id, NULL);
return ret;
}
static int parasite_status_ok(void)
{
int ret;
pthread_mutex_lock(¶site_watch.lock);
ret = !parasite_watch.changed;
pthread_mutex_unlock(¶site_watch.lock);
return ret;
}
static void parasite_socket_init(struct sockaddr_un *addr, pid_t pid)
{
memset(addr, 0x00, sizeof(struct sockaddr_un));
addr->sun_family = AF_UNIX;
if (parasite_socket_dir)
snprintf(addr->sun_path, sizeof(addr->sun_path), "%s/memcr%u", parasite_socket_dir, pid);
else {
snprintf(addr->sun_path, sizeof(addr->sun_path), "#memcr%u", pid);
addr->sun_path[0] = '\0';
}
}
static void cleanup_pid(pid_t pid)
{
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/pages-%d.img", dump_dir, pid);
unlink(path);
if (!parasite_socket_dir)
return;
snprintf(path, sizeof(path), "%s/memcr%u", parasite_socket_dir, pid);
unlink(path);
}
static int iterate_pstree(pid_t pid, int skip_self, int max_threads, int (*callback)(pid_t pid))
{
int ret;
char path[PATH_MAX];
DIR *task_dir;
struct dirent *ent;
int nr_threads = 0;
snprintf(path, sizeof(path), "/proc/%d/task", pid);
task_dir = opendir(path);
if (!task_dir) {
fprintf(stderr, "opendir() %s: %m\n", path);
return -errno;
}
while ((ent = readdir(task_dir))) {
pid_t tid;
char *eptr;
tid = strtoul(ent->d_name, &eptr, 0);
if (*eptr != '\0')
continue;
if (nr_threads >= max_threads) {
fprintf(stderr, "too many threads\n");
return -EINVAL;
}
if (skip_self && tid == pid) {
printf("skip tid %d == pid %d\n", tid, pid);
continue;
}
ret = callback(tid);
if (ret)
break;
}
closedir(task_dir);
return ret;
}
static int seize_pid(pid_t pid)
{
int ret;
int status;
siginfo_t si;
ret = ptrace(PTRACE_SEIZE, pid, NULL, 0);
if (ret) {
if (errno == ESRCH) {
fprintf(stderr, "ptrace(PTRACE_SEIZE) pid %d: %m, ignoring\n", pid);
return 0;
}
fprintf(stderr, "ptrace(PTRACE_SEIZE) %d pid %d: %m\n", errno, pid);
return 1;
}
try_again:
ret = ptrace(PTRACE_INTERRUPT, pid, NULL, NULL);
if (ret) {
fprintf(stderr, "ptrace(PTRACE_INTERRUPT) pid %d: %m\n", pid);
return 1;
}
ret = wait4(pid, &status, __WALL, NULL);
if (ret < 0) {
fprintf(stderr, "wait4() pid %d: %m\n", pid);
return 1;
}
if (ret != pid) {
fprintf(stderr, "wrong pid attached ret %d != pid %d\n", ret, pid);
return 1;
}
if (!WIFSTOPPED(status)) {
fprintf(stderr, "pid %d not stopped after seize, status %x\n", pid, status);
return 1;
}
ret = ptrace(PTRACE_GETSIGINFO, pid, NULL, &si);
if (ret) {
fprintf(stderr, "ptrace(PTRACE_GETSIGINFO) pid %d, %m\n", pid);
return 1;
}
if (SI_EVENT(si.si_code) != PTRACE_EVENT_STOP) {
/*
* Kernel notifies us about the task being seized received some
* event other than the STOP, i.e. -- a signal. Let the task
* handle one and repeat.
*/
ret = ptrace(PTRACE_CONT, pid, NULL, (void *)(unsigned long)si.si_signo);
if (ret) {
fprintf(stderr, "can't continue signal handling: %m\n");
return 1;
}
goto try_again;
}
ret = ptrace(PTRACE_SETOPTIONS, pid, NULL, (void *)(unsigned long)PTRACE_O_TRACEEXIT);
if (ret) {
fprintf(stderr, "ptrace(PTRACE_SETOPTIONS) pid %d: %m\n", pid);
return 1;
}
tids[nr_threads++] = pid;
return 0;
}
static int seize_target(pid_t pid)
{
int ret;
char path[PATH_MAX];
snprintf(path, sizeof(path), "/proc/%d", pid);
ret = access(path, F_OK);
if (ret) {
fprintf(stderr, "%d: No such process\n", pid);
return 1;
}
printf("[+] seizing target pid %d\n", pid);
ret = iterate_pstree(pid, 0, MAX_THREADS, seize_pid);
if (ret)
return ret;
printf("[i] %d %s\n", nr_threads, nr_threads == 1 ? "thread" : "threads");
return 0;
}
static int unseize_pid(pid_t pid)
{
return ptrace(PTRACE_DETACH, pid, NULL, 0);
}
static int unseize_target(void)
{
int ret = 0;
int i;
printf("[+] unseizing target\n");
for (i = 0; i < nr_threads; i++)
ret |= unseize_pid(tids[i]);
nr_threads = 0;
return ret;
}
static int parasite_socket_create(pid_t pid)
{
int pid_netns = -1;
int cur_netns = -1;
char netns_path[64];
int cd;
if (parasite_socket_use_netns) {
/* get both current and parasite network namespaces */
snprintf(netns_path, sizeof(netns_path), "/proc/%d/ns/net", pid);
pid_netns = open(netns_path, O_CLOEXEC | O_RDONLY);
if (pid_netns < 0) {
fprintf(stderr, "open('%s', ) failed: %m\n", netns_path);
} else {
cur_netns = open("/proc/self/ns/net", O_CLOEXEC | O_RDONLY);
if (cur_netns < 0) {
fprintf(stderr, "open('/proc/self/ns/net', ) failed: %m\n");
close(pid_netns);
pid_netns = -1;
}
}
/* switch to network namespace of parasite if available */
if (pid_netns >= 0) {
if (setns(pid_netns, CLONE_NEWNET) != 0) {
fprintf(stderr, "setns() failed: %m\n");
}
close(pid_netns);
}
}
cd = socket(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (cd < 0) {
fprintf(stderr, "socket() failed: %m\n");
}
/* restore original network namespace if available */
if (cur_netns >= 0) {
if (setns(cur_netns, CLONE_NEWNET) != 0) {
fprintf(stderr, "setns() failed: %m\n");
}
close(cur_netns);
}
return cd;
}
static int parasite_connect(pid_t pid)
{
int cd;
struct sockaddr_un addr;
int ret;
int cnt = 0;
cd = parasite_socket_create(pid);
if (cd < 0) {
return -1;
}
parasite_socket_init(&addr, pid);
/* parasite needs some time to start listening on a socket */
retry:
ret = connect(cd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un));
if (ret < 0) {
if (cnt++ < 100) {
usleep(1*1000);
goto retry;
} else {
fprintf(stderr, "connect() to %s failed: %m\n", addr.sun_path + 1);
close(cd);
return -1;
}
}
return cd;
}
static int __read(int fd, void *buf, size_t count, int (*check_peer_ok)(void), int silent)
{
int ret;
int off = 0;
assert(count != 0);
while (1) {
ret = read(fd, buf + off, count - off);
if (ret == 0)
break;
if (ret < 0) {
if (errno == EAGAIN && check_peer_ok) {
if (check_peer_ok())
continue;
break;
} else if (errno == EINTR) {
continue;
}
if (silent == FALSE)
fprintf(stderr, "[-] %s() failed: %m\n", __func__);
break;
}
if (ret < count - off) {
off += ret;
continue;
}
return count;
}
return ret;
}
static int __write(int fd, const void *buf, size_t count, int (*check_peer_ok)(void))
{
int ret;
int off = 0;
assert(count != 0);
while (1) {
ret = write(fd, buf + off, count - off);
if (ret < 0) {
if (errno == EAGAIN && check_peer_ok)
if (check_peer_ok())
continue;
fprintf(stderr, "[-] %s() failed: %m\n", __func__);
break;
}
if (ret < count - off) {
off += ret;
continue;
}
return count;
}
return ret;
}
static int parasite_read(int fd, void *buf, size_t count)
{
return __read(fd, buf, count, parasite_status_ok, FALSE);
}
static int parasite_write(int fd, const void *buf, size_t count)
{
return __write(fd, buf, count, parasite_status_ok);
}
static int _read(int fd, void *buf, size_t count)
{
return __read(fd, buf, count, NULL, FALSE);
}
static int _read_silent(int fd, void *buf, size_t count)
{
return __read(fd, buf, count, NULL, TRUE);
}
static int _write(int fd, const void *buf, size_t count)
{
return __write(fd, buf, count, NULL);
}
static int dump_open(const char *pathname, int flags, mode_t mode)
{
if (lib__open)
return lib__open(pathname, flags, mode);
return open(pathname, flags, mode);
}
static int dump_close(int fd)
{
if (lib__close)
return lib__close(fd);
return close(fd);
}
static int dump_read(int fd, void *buf, size_t count)
{
int ret;
if (lib__read)
ret = lib__read(fd, buf, count);
else
ret = __read(fd, buf, count, NULL, FALSE);
#ifdef CHECKSUM_MD5
if (checksum && ret > 0)
md5_update(&md5_restore_ctx, buf, count);
#endif
return ret;
}
static int dump_write(int fd, const void *buf, size_t count)
{
int ret;
if (lib__write)
ret = lib__write(fd, buf, count);
else
ret = __write(fd, buf, count, NULL);
#ifdef CHECKSUM_MD5
if (checksum && ret > 0)
md5_update(&md5_checkpoint_ctx, buf, count);
#endif
return ret;
}
static void init_pid_checkpoint_data(pid_t pid)
{
pthread_mutex_lock(&checkpoint_service_data_lock);
for (int i=0; i<CHECKPOINTED_PIDS_LIMIT; ++i) {
if (checkpoint_service_data[i].pid == PID_INVALID) {
checkpoint_service_data[i].pid = pid;
checkpoint_service_data[i].worker = PID_INVALID;
checkpoint_service_data[i].checkpoint_cmd_sd = SOCKET_INVALID;
checkpoint_service_data[i].state = STATE_RESTORED;
pthread_mutex_unlock(&checkpoint_service_data_lock);
return;
}
}
pthread_mutex_unlock(&checkpoint_service_data_lock);
fprintf(stderr, "%s: Checkpoint data PIDs limit exceeded!\n", __func__);
}
static void cleanup_checkpointed_pids(void)
{
fprintf(stdout, "[i] Terminating checkpointed processes\n");
pthread_mutex_lock(&checkpoint_service_data_lock);
for (int i=0; i<CHECKPOINTED_PIDS_LIMIT; ++i) {
if (checkpoint_service_data[i].pid != PID_INVALID) {
fprintf(stdout, "[i] Killing PID %d\n", checkpoint_service_data[i].pid);
kill(checkpoint_service_data[i].pid, SIGKILL);
cleanup_pid(checkpoint_service_data[i].pid);
checkpoint_service_data[i].pid = PID_INVALID;
checkpoint_service_data[i].worker = PID_INVALID;
checkpoint_service_data[i].state = STATE_RESTORED;
checkpoint_service_data[i].checkpoint_cmd_sd = SOCKET_INVALID;
}
}
pthread_mutex_unlock(&checkpoint_service_data_lock);
}
static void send_checkpoint_abort(int sd)
{
/* Send MEMCR_RESTORE cmd to worker to abort checkpoint */
struct service_command cmd;
cmd.cmd = MEMCR_RESTORE;
int ret = _write(sd, &cmd, sizeof(cmd));
if (ret != sizeof(cmd)) {
fprintf(stdout, "[-] Command abort checkpoint write failed\n");
return;
}
}
static void set_pid_checkpointing(pid_t pid, int cmd_sd)
{
pthread_mutex_lock(&checkpoint_service_data_lock);
for (int i=0; i<CHECKPOINTED_PIDS_LIMIT; ++i) {
if (checkpoint_service_data[i].pid == pid
&& checkpoint_service_data[i].state == STATE_RESTORED) {
checkpoint_service_data[i].state = STATE_CHECKPOINTING;
checkpoint_service_data[i].checkpoint_cmd_sd = cmd_sd;
if (checkpoint_service_data[i].checkpoint_abort)
send_checkpoint_abort(cmd_sd);
pthread_mutex_unlock(&checkpoint_service_data_lock);
return;
}
}
pthread_mutex_unlock(&checkpoint_service_data_lock);
fprintf(stderr, "%s: PID not found!\n", __func__);
}
static void set_pid_checkpointed(pid_t pid, pid_t worker)
{
pthread_mutex_lock(&checkpoint_service_data_lock);
for (int i=0; i<CHECKPOINTED_PIDS_LIMIT; ++i) {
if (checkpoint_service_data[i].pid == pid
&& checkpoint_service_data[i].state == STATE_CHECKPOINTING) {
checkpoint_service_data[i].state = STATE_CHECKPOINTED;
checkpoint_service_data[i].checkpoint_cmd_sd = SOCKET_INVALID;
checkpoint_service_data[i].worker = worker;
pthread_mutex_unlock(&checkpoint_service_data_lock);
return;
}
}
pthread_mutex_unlock(&checkpoint_service_data_lock);
fprintf(stderr, "%s: PID not found!\n", __func__);
}
static void clear_pid_checkpoint_data(pid_t pid)
{
pthread_mutex_lock(&checkpoint_service_data_lock);
for (int i=0; i<CHECKPOINTED_PIDS_LIMIT; ++i) {
if (checkpoint_service_data[i].pid == pid) {
checkpoint_service_data[i].pid = PID_INVALID;
checkpoint_service_data[i].worker = PID_INVALID;
checkpoint_service_data[i].checkpoint_cmd_sd = SOCKET_INVALID;
checkpoint_service_data[i].state = STATE_RESTORED;
}
}
pthread_mutex_unlock(&checkpoint_service_data_lock);
}
static void clear_pid_on_worker_exit_non_blocking(pid_t worker)
{
for (int i=0; i<CHECKPOINTED_PIDS_LIMIT; ++i) {
if (checkpoint_service_data[i].worker == worker) {
fprintf(stdout, "[+] Clearing pid: %d with worker: %d on worker exit ...\n",
checkpoint_service_data[i].pid, worker);
cleanup_pid(checkpoint_service_data[i].pid);
checkpoint_service_data[i].pid = PID_INVALID;
checkpoint_service_data[i].worker = PID_INVALID;
checkpoint_service_data[i].checkpoint_cmd_sd = SOCKET_INVALID;
checkpoint_service_data[i].state = STATE_RESTORED;
}
}
}
static int get_pid_worker(pid_t pid)
{
int worker = PID_INVALID;
pthread_mutex_lock(&checkpoint_service_data_lock);
for (int i=0; i<CHECKPOINTED_PIDS_LIMIT; ++i) {
if (checkpoint_service_data[i].pid == pid) {
worker = checkpoint_service_data[i].worker;
break;
}
}
pthread_mutex_unlock(&checkpoint_service_data_lock);
return worker;
}
static int can_checkpoint_pid(pid_t pid)
{
pthread_mutex_lock(&checkpoint_service_data_lock);
for (int i=0; i<CHECKPOINTED_PIDS_LIMIT; ++i) {
/* Check if pid is already in use */
if (checkpoint_service_data[i].pid == pid) {
pthread_mutex_unlock(&checkpoint_service_data_lock);
return 0;
}
}
pthread_mutex_unlock(&checkpoint_service_data_lock);
return 1;
}
static int can_restore_pid(pid_t pid)
{
pthread_mutex_lock(&checkpoint_service_data_lock);
for (int i=0; i<CHECKPOINTED_PIDS_LIMIT; ++i) {
/* Check if pid is already in use */
if (checkpoint_service_data[i].pid == pid) {
pthread_mutex_unlock(&checkpoint_service_data_lock);
return 1;
}
}
pthread_mutex_unlock(&checkpoint_service_data_lock);
return 0;
}
static void register_socket_for_checkpoint_service_cmds(int sd)
{
int flags = fcntl(sd, F_GETFL);
fcntl(sd, F_SETFL, flags | O_NONBLOCK);
checkpoint_service_socket = sd;
}
static void clear_socket_for_checkpoint_service_cmds(void)
{
checkpoint_service_socket = SOCKET_INVALID;
}
static int is_checkpoint_aborted(void)
{
if (interrupted)
return 1;
if (service) {
struct service_command svc_cmd;
if (checkpoint_service_socket == SOCKET_INVALID)
return 0;
int ret = _read_silent(checkpoint_service_socket, &svc_cmd, sizeof(svc_cmd));
if (ret == sizeof(svc_cmd) && svc_cmd.cmd == MEMCR_RESTORE)