forked from checkpoint-restore/criu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cr-restore.c
3006 lines (2410 loc) · 65.5 KB
/
cr-restore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/vfs.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <sys/shm.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sched.h>
#include <sys/sendfile.h>
#include "ptrace.h"
#include "compiler.h"
#include "asm/types.h"
#include "asm/restorer.h"
#include "cr_options.h"
#include "servicefd.h"
#include "image.h"
#include "util.h"
#include "util-pie.h"
#include "log.h"
#include "syscall.h"
#include "restorer.h"
#include "sockets.h"
#include "sk-packet.h"
#include "lock.h"
#include "files.h"
#include "files-reg.h"
#include "pipes.h"
#include "fifo.h"
#include "sk-inet.h"
#include "eventfd.h"
#include "eventpoll.h"
#include "signalfd.h"
#include "proc_parse.h"
#include "restorer-blob.h"
#include "crtools.h"
#include "namespaces.h"
#include "mem.h"
#include "mount.h"
#include "fsnotify.h"
#include "pstree.h"
#include "net.h"
#include "tty.h"
#include "cpu.h"
#include "file-lock.h"
#include "page-read.h"
#include "vdso.h"
#include "stats.h"
#include "tun.h"
#include "vma.h"
#include "kerndat.h"
#include "rst-malloc.h"
#include "plugin.h"
#include "cgroup.h"
#include "timerfd.h"
#include "file-lock.h"
#include "action-scripts.h"
#include "aio.h"
#include "security.h"
#include "parasite-syscall.h"
#include "protobuf.h"
#include "protobuf/sa.pb-c.h"
#include "protobuf/timer.pb-c.h"
#include "protobuf/vma.pb-c.h"
#include "protobuf/rlimit.pb-c.h"
#include "protobuf/pagemap.pb-c.h"
#include "protobuf/siginfo.pb-c.h"
#include "asm/restore.h"
#include "asm/atomic.h"
#include "cr-errno.h"
static struct pstree_item *current;
static int restore_task_with_children(void *);
static int sigreturn_restore(pid_t pid, CoreEntry *core);
static int prepare_restorer_blob(void);
static int prepare_rlimits(int pid, CoreEntry *core);
static int prepare_posix_timers(int pid, CoreEntry *core);
static int prepare_signals(int pid, CoreEntry *core);
static int root_as_sibling;
static pid_t *helpers = NULL;
static unsigned long helpers_pos = 0;
static int n_helpers = 0;
static int crtools_prepare_shared(void)
{
if (prepare_shared_fdinfo())
return -1;
/* Connections are unlocked from criu */
if (collect_inet_sockets())
return -1;
if (tty_prep_fds())
return -1;
if (prepare_cgroup())
return -1;
return 0;
}
/*
* Collect order information:
* - reg_file should be before remap, as the latter needs
* to find file_desc objects
* - per-pid collects (mm and fd) should be after remap and
* reg_file since both per-pid ones need to get fdesc-s
* and bump counters on remaps if they exist
*/
static struct collect_image_info *cinfos[] = {
®_file_cinfo,
&remap_cinfo,
&nsfile_cinfo,
&pipe_cinfo,
&fifo_cinfo,
&unix_sk_cinfo,
&packet_sk_cinfo,
&netlink_sk_cinfo,
&eventfd_cinfo,
&epoll_tfd_cinfo,
&epoll_cinfo,
&signalfd_cinfo,
&inotify_cinfo,
&inotify_mark_cinfo,
&fanotify_cinfo,
&fanotify_mark_cinfo,
&tty_info_cinfo,
&tty_cinfo,
&tunfile_cinfo,
&ext_file_cinfo,
&timerfd_cinfo,
&file_locks_cinfo,
};
static int root_prepare_shared(void)
{
int ret = 0, i;
struct pstree_item *pi;
pr_info("Preparing info about shared resources\n");
if (prepare_shared_tty())
return -1;
if (prepare_shared_reg_files())
return -1;
for (i = 0; i < ARRAY_SIZE(cinfos); i++) {
ret = collect_image(cinfos[i]);
if (ret)
return -1;
}
if (collect_pipes())
return -1;
if (collect_fifo())
return -1;
if (collect_unix_sockets())
return -1;
if (tty_verify_active_pairs())
return -1;
for_each_pstree_item(pi) {
if (pi->state == TASK_HELPER)
continue;
ret = prepare_mm_pid(pi);
if (ret < 0)
break;
ret = prepare_fd_pid(pi);
if (ret < 0)
break;
ret = prepare_fs_pid(pi);
if (ret < 0)
break;
}
if (ret < 0)
goto err;
mark_pipe_master();
ret = tty_setup_slavery();
if (ret)
goto err;
ret = resolve_unix_peers();
if (ret)
goto err;
ret = prepare_restorer_blob();
if (ret)
goto err;
show_saved_shmems();
show_saved_files();
err:
return ret;
}
/* Map a private vma, if it is not mapped by a parent yet */
static int map_private_vma(pid_t pid, struct vma_area *vma, void **tgt_addr,
struct vma_area **pvma, struct list_head *pvma_list)
{
int ret;
void *addr, *paddr = NULL;
unsigned long nr_pages, size;
struct vma_area *p = *pvma;
if (vma_area_is(vma, VMA_FILE_PRIVATE)) {
ret = get_filemap_fd(vma);
if (ret < 0) {
pr_err("Can't fixup VMA's fd\n");
return -1;
}
vma->e->fd = ret;
}
nr_pages = vma_entry_len(vma->e) / PAGE_SIZE;
vma->page_bitmap = xzalloc(BITS_TO_LONGS(nr_pages) * sizeof(long));
if (vma->page_bitmap == NULL)
return -1;
list_for_each_entry_from(p, pvma_list, list) {
if (p->e->start > vma->e->start)
break;
if (!vma_priv(p->e))
continue;
if (p->e->end != vma->e->end ||
p->e->start != vma->e->start)
continue;
/* Check flags, which must be identical for both vma-s */
if ((vma->e->flags ^ p->e->flags) & (MAP_GROWSDOWN | MAP_ANONYMOUS))
break;
if (!(vma->e->flags & MAP_ANONYMOUS) &&
vma->e->shmid != p->e->shmid)
break;
pr_info("COW 0x%016"PRIx64"-0x%016"PRIx64" 0x%016"PRIx64" vma\n",
vma->e->start, vma->e->end, vma->e->pgoff);
paddr = decode_pointer(p->premmaped_addr);
break;
}
/*
* A grow-down VMA has a guard page, which protect a VMA below it.
* So one more page is mapped here to restore content of the first page
*/
if (vma->e->flags & MAP_GROWSDOWN) {
vma->e->start -= PAGE_SIZE;
if (paddr)
paddr -= PAGE_SIZE;
}
size = vma_entry_len(vma->e);
if (paddr == NULL) {
/*
* The respective memory area was NOT found in the parent.
* Map a new one.
*/
pr_info("Map 0x%016"PRIx64"-0x%016"PRIx64" 0x%016"PRIx64" vma\n",
vma->e->start, vma->e->end, vma->e->pgoff);
addr = mmap(*tgt_addr, size,
vma->e->prot | PROT_WRITE,
vma->e->flags | MAP_FIXED,
vma->e->fd, vma->e->pgoff);
if (addr == MAP_FAILED) {
pr_perror("Unable to map ANON_VMA");
return -1;
}
*pvma = p;
} else {
/*
* This region was found in parent -- remap it to inherit physical
* pages (if any) from it (and COW them later if required).
*/
vma->ppage_bitmap = p->page_bitmap;
addr = mremap(paddr, size, size,
MREMAP_FIXED | MREMAP_MAYMOVE, *tgt_addr);
if (addr != *tgt_addr) {
pr_perror("Unable to remap a private vma");
return -1;
}
*pvma = list_entry(p->list.next, struct vma_area, list);
}
vma->premmaped_addr = (unsigned long) addr;
pr_debug("\tpremap 0x%016"PRIx64"-0x%016"PRIx64" -> %016lx\n",
vma->e->start, vma->e->end, (unsigned long)addr);
if (vma->e->flags & MAP_GROWSDOWN) { /* Skip gurad page */
vma->e->start += PAGE_SIZE;
vma->premmaped_addr += PAGE_SIZE;
}
if (vma_area_is(vma, VMA_FILE_PRIVATE))
close(vma->e->fd);
*tgt_addr += size;
return 0;
}
static int premap_priv_vmas(pid_t pid, struct vm_area_list *vmas, void *at)
{
struct list_head *parent_vmas;
struct vma_area *pvma, *vma;
unsigned long pstart = 0;
int ret = 0;
LIST_HEAD(empty);
/*
* Keep parent vmas at hands to check whether we can "inherit" them.
* See comments in map_private_vma.
*/
if (current->parent)
parent_vmas = &rsti(current->parent)->vmas.h;
else
parent_vmas = ∅
pvma = list_first_entry(parent_vmas, struct vma_area, list);
list_for_each_entry(vma, &vmas->h, list) {
if (pstart > vma->e->start) {
ret = -1;
pr_err("VMA-s are not sorted in the image file\n");
break;
}
pstart = vma->e->start;
if (!vma_priv(vma->e))
continue;
ret = map_private_vma(pid, vma, &at, &pvma, parent_vmas);
if (ret < 0)
break;
}
return ret;
}
static int restore_priv_vma_content(pid_t pid)
{
struct vma_area *vma;
int ret = 0;
struct list_head *vmas = &rsti(current)->vmas.h;
unsigned int nr_restored = 0;
unsigned int nr_shared = 0;
unsigned int nr_droped = 0;
unsigned int nr_compared = 0;
unsigned long va;
struct page_read pr;
vma = list_first_entry(vmas, struct vma_area, list);
ret = open_page_read(pid, &pr, PR_TASK);
if (ret)
return -1;
/*
* Read page contents.
*/
while (1) {
unsigned long off, i, nr_pages;;
struct iovec iov;
ret = pr.get_pagemap(&pr, &iov);
if (ret <= 0)
break;
va = (unsigned long)iov.iov_base;
nr_pages = iov.iov_len / PAGE_SIZE;
for (i = 0; i < nr_pages; i++) {
unsigned char buf[PAGE_SIZE];
void *p;
/*
* The lookup is over *all* possible VMAs
* read from image file.
*/
while (va >= vma->e->end) {
if (vma->list.next == vmas)
goto err_addr;
vma = list_entry(vma->list.next, struct vma_area, list);
}
/*
* Make sure the page address is inside existing VMA
* and the VMA it refers to still private one, since
* there is no guarantee that the data from pagemap is
* valid.
*/
if (va < vma->e->start)
goto err_addr;
else if (unlikely(!vma_priv(vma->e))) {
pr_err("Trying to restore page for non-private VMA\n");
goto err_addr;
}
off = (va - vma->e->start) / PAGE_SIZE;
p = decode_pointer((off) * PAGE_SIZE +
vma->premmaped_addr);
set_bit(off, vma->page_bitmap);
if (vma->ppage_bitmap) { /* inherited vma */
clear_bit(off, vma->ppage_bitmap);
ret = pr.read_page(&pr, va, buf);
if (ret < 0)
goto err_read;
va += PAGE_SIZE;
nr_compared++;
if (memcmp(p, buf, PAGE_SIZE) == 0) {
nr_shared++; /* the page is cowed */
continue;
}
memcpy(p, buf, PAGE_SIZE);
} else {
ret = pr.read_page(&pr, va, p);
if (ret < 0)
goto err_read;
va += PAGE_SIZE;
}
nr_restored++;
}
if (pr.put_pagemap)
pr.put_pagemap(&pr);
}
err_read:
pr.close(&pr);
if (ret < 0)
return ret;
/* Remove pages, which were not shared with a child */
list_for_each_entry(vma, vmas, list) {
unsigned long size, i = 0;
void *addr = decode_pointer(vma->premmaped_addr);
if (vma->ppage_bitmap == NULL)
continue;
size = vma_entry_len(vma->e) / PAGE_SIZE;
while (1) {
/* Find all pages, which are not shared with this child */
i = find_next_bit(vma->ppage_bitmap, size, i);
if ( i >= size)
break;
ret = madvise(addr + PAGE_SIZE * i,
PAGE_SIZE, MADV_DONTNEED);
if (ret < 0) {
pr_perror("madvise failed");
return -1;
}
i++;
nr_droped++;
}
}
cnt_add(CNT_PAGES_COMPARED, nr_compared);
cnt_add(CNT_PAGES_SKIPPED_COW, nr_shared);
cnt_add(CNT_PAGES_RESTORED, nr_restored);
pr_info("nr_restored_pages: %d\n", nr_restored);
pr_info("nr_shared_pages: %d\n", nr_shared);
pr_info("nr_droped_pages: %d\n", nr_droped);
return 0;
err_addr:
pr_err("Page entry address %lx outside of VMA %lx-%lx\n",
va, (long)vma->e->start, (long)vma->e->end);
return -1;
}
static int prepare_mappings(int pid)
{
int ret = 0;
void *addr;
struct vm_area_list *vmas;
void *old_premmapped_addr = NULL;
unsigned long old_premmapped_len;
vmas = &rsti(current)->vmas;
if (vmas->nr == 0) /* Zombie */
goto out;
/* Reserve a place for mapping private vma-s one by one */
addr = mmap(NULL, vmas->priv_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
if (addr == MAP_FAILED) {
ret = -1;
pr_perror("Unable to reserve memory (%lu bytes)", vmas->priv_size);
goto out;
}
old_premmapped_addr = rsti(current)->premmapped_addr;
old_premmapped_len = rsti(current)->premmapped_len;
rsti(current)->premmapped_addr = addr;
rsti(current)->premmapped_len = vmas->priv_size;
ret = premap_priv_vmas(pid, vmas, addr);
if (ret < 0)
goto out;
ret = restore_priv_vma_content(pid);
if (ret < 0)
goto out;
if (old_premmapped_addr) {
ret = munmap(old_premmapped_addr, old_premmapped_len);
if (ret < 0)
pr_perror("Unable to unmap %p(%lx)",
old_premmapped_addr, old_premmapped_len);
}
out:
return ret;
}
/*
* A gard page must be unmapped after restoring content and
* forking children to restore COW memory.
*/
static int unmap_guard_pages()
{
struct vma_area *vma;
struct list_head *vmas = &rsti(current)->vmas.h;
list_for_each_entry(vma, vmas, list) {
if (!vma_priv(vma->e))
continue;
if (vma->e->flags & MAP_GROWSDOWN) {
void *addr = decode_pointer(vma->premmaped_addr);
if (munmap(addr - PAGE_SIZE, PAGE_SIZE)) {
pr_perror("Can't unmap guard page");
return -1;
}
}
}
return 0;
}
static int open_vmas(int pid)
{
struct vma_area *vma;
int ret = 0;
struct list_head *vmas = &rsti(current)->vmas.h;
list_for_each_entry(vma, vmas, list) {
if (!(vma_area_is(vma, VMA_AREA_REGULAR)))
continue;
pr_info("Opening 0x%016"PRIx64"-0x%016"PRIx64" 0x%016"PRIx64" (%x) vma\n",
vma->e->start, vma->e->end,
vma->e->pgoff, vma->e->status);
if (vma_area_is(vma, VMA_AREA_SYSVIPC))
ret = vma->e->shmid;
else if (vma_area_is(vma, VMA_ANON_SHARED))
ret = get_shmem_fd(pid, vma->e);
else if (vma_area_is(vma, VMA_FILE_SHARED))
ret = get_filemap_fd(vma);
else if (vma_area_is(vma, VMA_AREA_SOCKET))
ret = get_socket_fd(pid, vma->e);
else
continue;
if (ret < 0) {
pr_err("Can't fixup fd\n");
break;
}
pr_info("\t`- setting %d as mapping fd\n", ret);
vma->e->fd = ret;
}
return ret < 0 ? -1 : 0;
}
static rt_sigaction_t sigchld_act;
static rt_sigaction_t parent_act[SIGMAX];
static bool sa_inherited(int sig, rt_sigaction_t *sa)
{
rt_sigaction_t *pa;
if (current == root_item)
return false; /* XXX -- inherit from CRIU? */
pa = &parent_act[sig];
return pa->rt_sa_handler == sa->rt_sa_handler &&
pa->rt_sa_flags == sa->rt_sa_flags &&
pa->rt_sa_restorer == sa->rt_sa_restorer &&
pa->rt_sa_mask.sig[0] == sa->rt_sa_mask.sig[0];
}
static int prepare_sigactions(void)
{
int pid = current->pid.virt;
rt_sigaction_t act;
struct cr_img *img;
SaEntry *e;
int sig, rst = 0;
int ret = 0;
if (!task_alive(current))
return 0;
pr_info("Restore sigacts for %d\n", pid);
img = open_image(CR_FD_SIGACT, O_RSTR, pid);
if (!img)
return -1;
for (sig = 1; sig <= SIGMAX; sig++) {
if (sig == SIGKILL || sig == SIGSTOP)
continue;
ret = pb_read_one_eof(img, &e, PB_SIGACT);
if (ret == 0) {
if (sig != SIGMAX_OLD + 1) { /* backward compatibility */
pr_err("Unexpected EOF %d\n", sig);
ret = -1;
break;
}
pr_warn("This format of sigacts-%d.img is deprecated\n", pid);
break;
}
if (ret < 0)
break;
ASSIGN_TYPED(act.rt_sa_handler, decode_pointer(e->sigaction));
ASSIGN_TYPED(act.rt_sa_flags, e->flags);
ASSIGN_TYPED(act.rt_sa_restorer, decode_pointer(e->restorer));
ASSIGN_TYPED(act.rt_sa_mask.sig[0], e->mask);
sa_entry__free_unpacked(e, NULL);
if (sig == SIGCHLD) {
sigchld_act = act;
continue;
}
if (sa_inherited(sig - 1, &act))
continue;
/*
* A pure syscall is used, because glibc
* sigaction overwrites se_restorer.
*/
ret = sys_sigaction(sig, &act, NULL, sizeof(k_rtsigset_t));
if (ret == -1) {
pr_err("%d: Can't restore sigaction: %m\n", pid);
goto err;
}
parent_act[sig - 1] = act;
rst++;
}
pr_info("Restored %d/%d sigacts\n", rst,
SIGMAX - 3 /* KILL, STOP and CHLD */);
err:
close_image(img);
return ret;
}
static int collect_helper_pids()
{
struct pstree_item *pi;
list_for_each_entry(pi, ¤t->children, sibling) {
if (pi->state != TASK_HELPER)
continue;
if (helpers) {
void *m;
m = rst_mem_alloc(sizeof(*helpers) * ++n_helpers, RM_PRIVATE);
if (!m)
return -1;
} else {
helpers_pos = rst_mem_cpos(RM_PRIVATE);
helpers = rst_mem_alloc(sizeof(*helpers), RM_PRIVATE);
if (!helpers)
return -1;
n_helpers = 1;
}
helpers[n_helpers - 1] = pi->pid.virt;
}
return 0;
}
static int open_cores(int pid, CoreEntry *leader_core)
{
int i, tpid;
CoreEntry **cores = NULL;
cores = xmalloc(sizeof(*cores)*current->nr_threads);
if (!cores)
goto err;
for (i = 0; i < current->nr_threads; i++) {
tpid = current->threads[i].virt;
if (tpid == pid)
cores[i] = leader_core;
else {
struct cr_img *img;
img = open_image(CR_FD_CORE, O_RSTR, tpid);
if (!img) {
pr_err("Can't open core data for thread %d\n", tpid);
goto err;
}
if (pb_read_one(img, &cores[i], PB_CORE) <= 0) {
close_image(img);
goto err;
}
close_image(img);
}
}
current->core = cores;
return 0;
err:
xfree(cores);
return -1;
}
static int restore_one_alive_task(int pid, CoreEntry *core)
{
pr_info("Restoring resources\n");
rst_mem_switch_to_private();
if (prepare_fds(current))
return -1;
if (prepare_file_locks(pid))
return -1;
if (open_vmas(pid))
return -1;
if (open_cores(pid, core))
return -1;
if (prepare_signals(pid, core))
return -1;
if (prepare_posix_timers(pid, core))
return -1;
if (prepare_rlimits(pid, core) < 0)
return -1;
if (collect_helper_pids() < 0)
return -1;
if (inherit_fd_fini() < 0)
return -1;
return sigreturn_restore(pid, core);
}
static void zombie_prepare_signals(void)
{
sigset_t blockmask;
int sig;
struct sigaction act;
sigfillset(&blockmask);
sigprocmask(SIG_UNBLOCK, &blockmask, NULL);
memset(&act, 0, sizeof(act));
act.sa_handler = SIG_DFL;
for (sig = 1; sig <= SIGMAX; sig++)
sigaction(sig, &act, NULL);
}
#define SIG_FATAL_MASK ( \
(1 << SIGHUP) |\
(1 << SIGINT) |\
(1 << SIGQUIT) |\
(1 << SIGILL) |\
(1 << SIGTRAP) |\
(1 << SIGABRT) |\
(1 << SIGIOT) |\
(1 << SIGBUS) |\
(1 << SIGFPE) |\
(1 << SIGKILL) |\
(1 << SIGUSR1) |\
(1 << SIGSEGV) |\
(1 << SIGUSR2) |\
(1 << SIGPIPE) |\
(1 << SIGALRM) |\
(1 << SIGTERM) |\
(1 << SIGXCPU) |\
(1 << SIGXFSZ) |\
(1 << SIGVTALRM)|\
(1 << SIGPROF) |\
(1 << SIGPOLL) |\
(1 << SIGIO) |\
(1 << SIGSYS) |\
(1 << SIGUNUSED)|\
(1 << SIGSTKFLT)|\
(1 << SIGPWR) \
)
static inline int sig_fatal(int sig)
{
return (sig > 0) && (sig < SIGMAX) && (SIG_FATAL_MASK & (1UL << sig));
}
struct task_entries *task_entries;
static unsigned long task_entries_pos;
static int restore_one_zombie(int pid, CoreEntry *core)
{
int exit_code = core->tc->exit_code;
pr_info("Restoring zombie with %d code\n", exit_code);
if (inherit_fd_fini() < 0)
return -1;
sys_prctl(PR_SET_NAME, (long)(void *)core->tc->comm, 0, 0, 0);
if (task_entries != NULL) {
restore_finish_stage(CR_STATE_RESTORE);
zombie_prepare_signals();
mutex_lock(&task_entries->zombie_lock);
}
if (exit_code & 0x7f) {
int signr;
/* prevent generating core files */
if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0))
pr_perror("Can't drop the dumpable flag");
signr = exit_code & 0x7F;
if (!sig_fatal(signr)) {
pr_warn("Exit with non fatal signal ignored\n");
signr = SIGABRT;
}
if (kill(pid, signr) < 0)
pr_perror("Can't kill myself, will just exit");
exit_code = 0;
}
exit((exit_code >> 8) & 0x7f);
/* never reached */
BUG_ON(1);
return -1;
}
static int check_core(CoreEntry *core, struct pstree_item *me)
{
int ret = -1;
if (core->mtype != CORE_ENTRY__MARCH) {
pr_err("Core march mismatch %d\n", (int)core->mtype);
goto out;
}
if (!core->tc) {
pr_err("Core task state data missed\n");
goto out;
}
if (core->tc->task_state != TASK_DEAD) {
if (!core->ids && !me->ids) {
pr_err("Core IDS data missed for non-zombie\n");
goto out;
}
if (!CORE_THREAD_ARCH_INFO(core)) {
pr_err("Core info data missed for non-zombie\n");
goto out;
}
}
ret = 0;
out:
return ret;
}
static int restore_one_task(int pid, CoreEntry *core)
{
int ret;
/* No more fork()-s => no more per-pid logs */
if (task_alive(current))
ret = restore_one_alive_task(pid, core);
else if (current->state == TASK_DEAD)
ret = restore_one_zombie(pid, core);
else if (current->state == TASK_HELPER) {
restore_finish_stage(CR_STATE_RESTORE);
ret = 0;
} else {
pr_err("Unknown state in code %d\n", (int)core->tc->task_state);
ret = -1;
}
if (core)
core_entry__free_unpacked(core, NULL);
return ret;
}
/* All arguments should be above stack, because it grows down */
struct cr_clone_arg {
/*
* Reserve some space for clone() to locate arguments
* and retcode in this place
*/
char stack[128] __attribute__((aligned (8)));
char stack_ptr[0];
struct pstree_item *item;
unsigned long clone_flags;
int fd;
CoreEntry *core;
};
static void maybe_clone_parent(struct pstree_item *item,
struct cr_clone_arg *ca)
{
/*
* zdtm runs in kernel 3.11, which has the problem described below. We
* avoid this by including the pdeath_sig test. Once users/zdtm migrate
* off of 3.11, this condition can be simplified to just test the
* options and not have the pdeath_sig test.
*/
if (opts.restore_sibling) {
/*
* This means we're called from lib's criu_restore_child().
* In that case create the root task as the child one to+
* the caller. This is the only way to correctly restore the