forked from checkpoint-restore/criu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cr-restore.c
3046 lines (2431 loc) · 66.9 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 "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 "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 "shmem.h"
#include "aio.h"
#include "lsm.h"
#include "seccomp.h"
#include "fault-injection.h"
#include "sk-queue.h"
#include "parasite-syscall.h"
#include "files-reg.h"
#include "protobuf.h"
#include "images/sa.pb-c.h"
#include "images/timer.pb-c.h"
#include "images/vma.pb-c.h"
#include "images/rlimit.pb-c.h"
#include "images/pagemap.pb-c.h"
#include "images/siginfo.pb-c.h"
#include "asm/restore.h"
#include "asm/atomic.h"
#include "asm/bitops.h"
#include "cr-errno.h"
#include "pie/pie-relocs.h"
#ifndef arch_export_restore_thread
#define arch_export_restore_thread __export_restore_thread
#endif
#ifndef arch_export_restore_task
#define arch_export_restore_task __export_restore_task
#endif
#ifndef arch_export_unmap
#define arch_export_unmap __export_unmap
#endif
struct pstree_item *current;
static int restore_task_with_children(void *);
static int sigreturn_restore(pid_t pid, struct task_restore_args *ta, unsigned long alen, CoreEntry *core);
static int prepare_restorer_blob(void);
static int prepare_rlimits(int pid, struct task_restore_args *, CoreEntry *core);
static int prepare_posix_timers(int pid, struct task_restore_args *ta, CoreEntry *core);
static int prepare_signals(int pid, struct task_restore_args *, CoreEntry *core);
static int crtools_prepare_shared(void)
{
if (prepare_shared_fdinfo())
return -1;
/* We might want to remove ghost files on failed restore */
if (collect_remaps_and_regfiles())
return -1;
/* dead pid remap needs to allocate task helpers which all tasks need
* to see */
if (prepare_procfs_remaps())
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[] = {
&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,
&tty_cdata,
&tunfile_cinfo,
&ext_file_cinfo,
&timerfd_cinfo,
&file_locks_cinfo,
&pipe_data_cinfo,
&fifo_data_cinfo,
&sk_queues_cinfo,
};
struct post_prepare_cb {
struct list_head list;
int (*actor)(void *data);
void *data;
};
static struct list_head post_prepare_cbs = LIST_HEAD_INIT(post_prepare_cbs);
int add_post_prepare_cb(int (*actor)(void *data), void *data)
{
struct post_prepare_cb *cb;
cb = xmalloc(sizeof(*cb));
if (!cb)
return -1;
cb->actor = actor;
cb->data = data;
list_add(&cb->list, &post_prepare_cbs);
return 0;
}
static int run_post_prepare(void)
{
struct post_prepare_cb *o;
list_for_each_entry(o, &post_prepare_cbs, list) {
if (o->actor(o->data))
return -1;
}
return 0;
}
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;
if (prepare_remaps())
return -1;
if (prepare_seccomp_filters())
return -1;
for (i = 0; i < ARRAY_SIZE(cinfos); i++) {
ret = collect_image(cinfos[i]);
if (ret)
return -1;
}
for_each_pstree_item(pi) {
if (pi->pid.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;
ret = prepare_restorer_blob();
if (ret)
goto err;
ret = run_post_prepare();
if (ret)
goto err;
ret = open_transport_socket();
if (ret)
goto err;
show_saved_files();
err:
return ret;
}
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;
int i;
if (current == root_item)
return false; /* XXX -- inherit from CRIU? */
pa = &parent_act[sig];
for (i = 0; i < _KNSIG_WORDS; i++)
if (pa->rt_sa_mask.sig[i] != sa->rt_sa_mask.sig[i])
return false;
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;
}
/* Returns number of restored signals, -1 or negative errno on fail */
static int restore_one_sigaction(int sig, struct cr_img *img, int pid)
{
rt_sigaction_t act;
SaEntry *e;
int ret = 0;
BUG_ON(sig == SIGKILL || sig == SIGSTOP);
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);
return -1;
}
pr_warn("This format of sigacts-%d.img is deprecated\n", pid);
return -1;
}
if (ret < 0)
return ret;
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));
BUILD_BUG_ON(sizeof(e->mask) != sizeof(act.rt_sa_mask.sig));
memcpy(act.rt_sa_mask.sig, &e->mask, sizeof(act.rt_sa_mask.sig));
sa_entry__free_unpacked(e, NULL);
if (sig == SIGCHLD) {
sigchld_act = act;
return 0;
}
if (sa_inherited(sig - 1, &act))
return 1;
/*
* A pure syscall is used, because glibc
* sigaction overwrites se_restorer.
*/
ret = syscall(SYS_rt_sigaction, sig, &act, NULL, sizeof(k_rtsigset_t));
if (ret < 0) {
pr_perror("Can't restore sigaction");
return ret;
}
parent_act[sig - 1] = act;
return 1;
}
static int prepare_sigactions(void)
{
int pid = current->pid.virt;
struct cr_img *img;
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 = restore_one_sigaction(sig, img, pid);
if (ret < 0)
break;
if (ret)
rst++;
}
pr_info("Restored %d/%d sigacts\n", rst,
SIGMAX - 3 /* KILL, STOP and CHLD */);
close_image(img);
return ret;
}
static int __collect_child_pids(struct pstree_item *p, int state, unsigned int *n)
{
struct pstree_item *pi;
list_for_each_entry(pi, &p->children, sibling) {
pid_t *child;
if (pi->pid.state != state)
continue;
child = rst_mem_alloc(sizeof(*child), RM_PRIVATE);
if (!child)
return -1;
(*n)++;
*child = pi->pid.virt;
}
return 0;
}
static int collect_child_pids(int state, unsigned int *n)
{
struct pstree_item *pi;
*n = 0;
/*
* All children of helpers and zombies will be reparented to the init
* process and they have to be collected too.
*/
if (current == root_item) {
for_each_pstree_item(pi) {
if (pi->pid.state != TASK_HELPER &&
pi->pid.state != TASK_DEAD)
continue;
if (__collect_child_pids(pi, state, n))
return -1;
}
}
return __collect_child_pids(current, state, n);
}
static int collect_helper_pids(struct task_restore_args *ta)
{
ta->helpers = (pid_t *)rst_mem_align_cpos(RM_PRIVATE);
return collect_child_pids(TASK_HELPER, &ta->helpers_n);
}
static int collect_zombie_pids(struct task_restore_args *ta)
{
ta->zombies = (pid_t *)rst_mem_align_cpos(RM_PRIVATE);
return collect_child_pids(TASK_DEAD, &ta->zombies_n);
}
static int open_core(int pid, CoreEntry **pcore)
{
int ret;
struct cr_img *img;
img = open_image(CR_FD_CORE, O_RSTR, pid);
if (!img) {
pr_err("Can't open core data for %d", pid);
return -1;
}
ret = pb_read_one(img, pcore, PB_CORE);
close_image(img);
return ret <= 0 ? -1 : 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 if (open_core(tpid, &cores[i]))
goto err;
}
current->core = cores;
return 0;
err:
xfree(cores);
return -1;
}
static int prepare_oom_score_adj(int value)
{
int fd, ret = 0;
char buf[11];
fd = open_proc_rw(PROC_SELF, "oom_score_adj");
if (fd < 0)
return -1;
snprintf(buf, 11, "%d", value);
if (write(fd, buf, 11) < 0) {
pr_perror("Write %s to /proc/self/oom_score_adj failed", buf);
ret = -1;
}
close(fd);
return ret;
}
static int prepare_proc_misc(pid_t pid, TaskCoreEntry *tc)
{
int ret;
/* loginuid value is critical to restore */
if (kdat.has_loginuid && tc->has_loginuid &&
tc->loginuid != INVALID_UID) {
ret = prepare_loginuid(tc->loginuid, LOG_ERROR);
if (ret < 0)
return ret;
}
/* oom_score_adj is not critical: only log errors */
if (tc->has_oom_score_adj && tc->oom_score_adj != 0)
prepare_oom_score_adj(tc->oom_score_adj);
return 0;
}
static int prepare_itimers(int pid, struct task_restore_args *args, CoreEntry *core);
static int prepare_mm(pid_t pid, struct task_restore_args *args);
static int restore_one_alive_task(int pid, CoreEntry *core)
{
unsigned args_len;
struct task_restore_args *ta;
pr_info("Restoring resources\n");
rst_mem_switch_to_private();
args_len = round_up(sizeof(*ta) + sizeof(struct thread_restore_args) *
current->nr_threads, page_size());
ta = mmap(NULL, args_len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
if (!ta)
return -1;
memzero(ta, args_len);
if (prepare_fds(current))
return -1;
if (prepare_file_locks(pid))
return -1;
if (open_vmas(current))
return -1;
if (prepare_aios(current, ta))
return -1;
if (fixup_sysv_shmems())
return -1;
if (open_cores(pid, core))
return -1;
if (prepare_signals(pid, ta, core))
return -1;
if (prepare_posix_timers(pid, ta, core))
return -1;
if (prepare_rlimits(pid, ta, core) < 0)
return -1;
if (collect_helper_pids(ta) < 0)
return -1;
if (collect_zombie_pids(ta) < 0)
return -1;
if (inherit_fd_fini() < 0)
return -1;
if (prepare_proc_misc(pid, core->tc))
return -1;
/*
* Get all the tcp sockets fds into rst memory -- restorer
* will turn repair off before going sigreturn
*/
if (prepare_tcp_socks(ta))
return -1;
/*
* Copy timerfd params for restorer args, we need to proceed
* timer setting at the very late.
*/
if (prepare_timerfds(ta))
return -1;
if (seccomp_filters_get_rst_pos(core, ta) < 0)
return -1;
if (prepare_itimers(pid, ta, core) < 0)
return -1;
if (prepare_mm(pid, ta))
return -1;
if (prepare_vmas(current, ta))
return -1;
close_service_fd(TRANSPORT_FD_OFF);
return sigreturn_restore(pid, ta, args_len, 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 wait_on_helpers_zombies(void)
{
struct pstree_item *pi;
list_for_each_entry(pi, ¤t->children, sibling) {
pid_t pid = pi->pid.virt;
int status;
switch (pi->pid.state) {
case TASK_DEAD:
if (waitid(P_PID, pid, NULL, WNOWAIT | WEXITED) < 0) {
pr_perror("Wait on %d zombie failed", pid);
return -1;
}
break;
case TASK_HELPER:
if (waitpid(pid, &status, 0) != pid) {
pr_perror("waitpid for helper %d failed", pid);
return -1;
}
break;
}
}
return 0;
}
static int restore_one_zombie(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;
prctl(PR_SET_NAME, (long)(void *)core->tc->comm, 0, 0, 0);
if (task_entries != NULL) {
restore_finish_stage(task_entries, CR_STATE_RESTORE);
zombie_prepare_signals();
}
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(current->pid.virt, 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->pid.state == TASK_DEAD)
ret = restore_one_zombie(core);
else if (current->pid.state == TASK_HELPER) {
sigset_t blockmask, oldmask;
sigemptyset(&blockmask);
sigaddset(&blockmask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &blockmask, &oldmask) == -1) {
pr_perror("Can not set mask of blocked signals");
return -1;
}
restore_finish_stage(task_entries, CR_STATE_RESTORE);
if (wait_on_helpers_zombies()) {
pr_err("failed to wait on helpers and zombies\n");
ret = -1;
} else {
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] __stack_aligned__;
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
* pdeath_sig of the root task. But also looks nice.
*
* Alternatively, if we are --restore-detached, a similar trick is
* needed to correctly restore pdeath_sig and prevent processes from
* dying once restored.
*
* There were a problem in kernel 3.11 -- CLONE_PARENT can't be
* set together with CLONE_NEWPID, which has been solved in further
* versions of the kernels, but we treat 3.11 as a base, so at
* least warn a user about potential problems.
*/
rsti(item)->clone_flags |= CLONE_PARENT;
if (rsti(item)->clone_flags & CLONE_NEWPID)
pr_warn("Set CLONE_PARENT | CLONE_NEWPID but it might cause restore problem,"
"because not all kernels support such clone flags combinations!\n");
} else if (opts.restore_detach) {
if (ca->core->thread_core->pdeath_sig)
pr_warn("Root task has pdeath_sig configured, so it will receive one _right_"
"after restore on CRIU exit\n");
}
}
static inline int fork_with_pid(struct pstree_item *item)
{
struct cr_clone_arg ca;
int ret = -1;
pid_t pid = item->pid.virt;
if (item->pid.state != TASK_HELPER) {
if (open_core(pid, &ca.core))
return -1;
if (check_core(ca.core, item))
return -1;
item->pid.state = ca.core->tc->task_state;
rsti(item)->cg_set = ca.core->tc->cg_set;
rsti(item)->has_seccomp = ca.core->tc->seccomp_mode != SECCOMP_MODE_DISABLED;
if (item->pid.state != TASK_DEAD && !task_alive(item)) {
pr_err("Unknown task state %d\n", item->pid.state);
return -1;
}
if (unlikely(item == root_item))
maybe_clone_parent(item, &ca);
} else {
/*
* Helper entry will not get moved around and thus
* will live in the parent's cgset.
*/
rsti(item)->cg_set = rsti(item->parent)->cg_set;
ca.core = NULL;
}
ret = -1;
ca.item = item;
ca.clone_flags = rsti(item)->clone_flags;
BUG_ON(ca.clone_flags & CLONE_VM);
pr_info("Forking task with %d pid (flags 0x%lx)\n", pid, ca.clone_flags);
if (!(ca.clone_flags & CLONE_NEWPID)) {
char buf[32];
int len;
ca.fd = open_proc_rw(PROC_GEN, LAST_PID_PATH);
if (ca.fd < 0) {
pr_perror("%d: Can't open %s", pid, LAST_PID_PATH);
goto err;
}
if (flock(ca.fd, LOCK_EX)) {
close(ca.fd);
pr_perror("%d: Can't lock %s", pid, LAST_PID_PATH);
goto err;
}
len = snprintf(buf, sizeof(buf), "%d", pid - 1);
if (write(ca.fd, buf, len) != len) {
pr_perror("%d: Write %s to %s", pid, buf, LAST_PID_PATH);
goto err_unlock;
}
} else {
ca.fd = -1;
BUG_ON(pid != INIT_PID);
}
/*
* Some kernel modules, such as netwrok packet generator
* run kernel thread upon net-namespace creattion taking
* the @pid we've been requeting via LAST_PID_PATH interface
* so that we can't restore a take with pid needed.
*
* Here is an idea -- unhare net namespace in callee instead.
*/
/*
* The cgroup namespace is also unshared explicitly in the
* move_in_cgroup(), so drop this flag here as well.
*/
ret = clone(restore_task_with_children, ca.stack_ptr,
(ca.clone_flags & ~(CLONE_NEWNET | CLONE_NEWCGROUP)) | SIGCHLD, &ca);
if (ret < 0) {
pr_perror("Can't fork for %d", pid);
goto err_unlock;
}
if (item == root_item) {
item->pid.real = ret;
pr_debug("PID: real %d virt %d\n",
item->pid.real, item->pid.virt);
}
err_unlock:
if (ca.fd >= 0) {
if (flock(ca.fd, LOCK_UN))
pr_perror("%d: Can't unlock %s", pid, LAST_PID_PATH);
close(ca.fd);
}
err:
if (ca.core)
core_entry__free_unpacked(ca.core, NULL);
return ret;
}
static void sigchld_handler(int signal, siginfo_t *siginfo, void *data)
{
struct pstree_item *pi;
pid_t pid = siginfo->si_pid;
int status;
int exit;
exit = (siginfo->si_code == CLD_EXITED);
status = siginfo->si_status;
/* skip scripts */
if (!current && root_item->pid.real != pid) {
pid = waitpid(root_item->pid.real, &status, WNOHANG);
if (pid <= 0)
return;
exit = WIFEXITED(status);
status = exit ? WEXITSTATUS(status) : WTERMSIG(status);
}
if (!current && siginfo->si_code == CLD_TRAPPED &&
siginfo->si_status == SIGCHLD) {
/* The root task is ptraced. Allow it to handle SIGCHLD */
ptrace(PTRACE_CONT, siginfo->si_pid, 0, SIGCHLD);
return;
}
if (!current || status)
goto err;
while (pid) {
pid = waitpid(-1, &status, WNOHANG);
if (pid <= 0)
return;
exit = WIFEXITED(status);