-
Notifications
You must be signed in to change notification settings - Fork 551
/
cr-service.c
1546 lines (1267 loc) · 33.3 KB
/
cr-service.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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sched.h>
#include <sys/prctl.h>
#include "version.h"
#include "crtools.h"
#include "cr_options.h"
#include "external.h"
#include "util.h"
#include "criu-log.h"
#include "cpu.h"
#include "files.h"
#include "pstree.h"
#include "cr-service.h"
#include "cr-service-const.h"
#include "page-xfer.h"
#include "protobuf.h"
#include "net.h"
#include "mount.h"
#include "filesystems.h"
#include "cgroup.h"
#include "cgroup-props.h"
#include "action-scripts.h"
#include "sockets.h"
#include "irmap.h"
#include "kerndat.h"
#include "proc_parse.h"
#include "common/scm.h"
#include "uffd.h"
#include "pidfd-store.h"
#include "setproctitle.h"
#include "cr-errno.h"
#include "namespaces.h"
unsigned int service_sk_ino = -1;
static int recv_criu_msg(int socket_fd, CriuReq **req)
{
u8 local[PB_PKOBJ_LOCAL_SIZE];
void *buf = (void *)&local;
int len, exit_code = -1;
len = recv(socket_fd, NULL, 0, MSG_TRUNC | MSG_PEEK);
if (len == -1) {
pr_perror("Can't read request");
goto err;
}
if (len > sizeof(local)) {
buf = xmalloc(len);
if (!buf)
return -ENOMEM;
}
len = recv(socket_fd, buf, len, MSG_TRUNC);
if (len == -1) {
pr_perror("Can't read request");
goto err;
}
if (len == 0) {
pr_info("Client exited unexpectedly\n");
errno = ECONNRESET;
goto err;
}
*req = criu_req__unpack(NULL, len, buf);
if (!*req) {
pr_perror("Failed unpacking request");
goto err;
}
exit_code = 0;
err:
if (buf != (void *)&local)
xfree(buf);
return exit_code;
}
static int send_criu_msg_with_fd(int socket_fd, CriuResp *msg, int fd)
{
u8 local[PB_PKOBJ_LOCAL_SIZE];
void *buf = (void *)&local;
int len, exit_code = -1;
len = criu_resp__get_packed_size(msg);
if (len > sizeof(local)) {
buf = xmalloc(len);
if (!buf)
return -ENOMEM;
}
if (criu_resp__pack(msg, buf) != len) {
pr_perror("Failed packing response");
goto err;
}
if (fd >= 0)
exit_code = send_fds(socket_fd, NULL, 0, &fd, 1, buf, len);
else
exit_code = write(socket_fd, buf, len);
if (exit_code < 0) {
pr_perror("Can't send response");
goto err;
}
exit_code = 0;
err:
if (buf != (void *)&local)
xfree(buf);
return exit_code;
}
static int send_criu_msg(int socket_fd, CriuResp *msg)
{
return send_criu_msg_with_fd(socket_fd, msg, -1);
}
static void set_resp_err(CriuResp *resp)
{
resp->cr_errno = get_cr_errno();
resp->has_cr_errno = resp->cr_errno ? true : false;
resp->cr_errmsg = log_first_err();
}
static void send_criu_err(int sk, char *msg)
{
CriuResp resp = CRIU_RESP__INIT;
pr_perror("RPC error: %s", msg);
resp.type = CRIU_REQ_TYPE__EMPTY;
resp.success = false;
set_resp_err(&resp);
send_criu_msg(sk, &resp);
}
int send_criu_dump_resp(int socket_fd, bool success, bool restored)
{
CriuResp msg = CRIU_RESP__INIT;
CriuDumpResp resp = CRIU_DUMP_RESP__INIT;
msg.type = CRIU_REQ_TYPE__DUMP;
msg.success = success;
set_resp_err(&msg);
msg.dump = &resp;
resp.has_restored = true;
resp.restored = restored;
return send_criu_msg(socket_fd, &msg);
}
static int send_criu_pre_dump_resp(int socket_fd, bool success, bool single)
{
CriuResp msg = CRIU_RESP__INIT;
msg.type = single ? CRIU_REQ_TYPE__SINGLE_PRE_DUMP : CRIU_REQ_TYPE__PRE_DUMP;
msg.success = success;
set_resp_err(&msg);
return send_criu_msg(socket_fd, &msg);
}
int send_criu_restore_resp(int socket_fd, bool success, int pid)
{
CriuResp msg = CRIU_RESP__INIT;
CriuRestoreResp resp = CRIU_RESTORE_RESP__INIT;
msg.type = CRIU_REQ_TYPE__RESTORE;
msg.success = success;
set_resp_err(&msg);
msg.restore = &resp;
resp.pid = pid;
return send_criu_msg(socket_fd, &msg);
}
int send_criu_rpc_script(enum script_actions act, char *name, int sk, int fd)
{
int ret;
CriuResp msg = CRIU_RESP__INIT;
CriuReq *req;
CriuNotify cn = CRIU_NOTIFY__INIT;
msg.type = CRIU_REQ_TYPE__NOTIFY;
msg.success = true;
msg.notify = &cn;
cn.script = name;
switch (act) {
case ACT_SETUP_NS:
case ACT_POST_RESTORE:
/*
* FIXME pid is required only once on
* restore. Need some more sane way of
* checking this.
*/
cn.has_pid = true;
cn.pid = root_item->pid->real;
break;
default:
break;
}
ret = send_criu_msg_with_fd(sk, &msg, fd);
if (ret < 0)
return ret;
ret = recv_criu_msg(sk, &req);
if (ret < 0)
return ret;
if (req->type != CRIU_REQ_TYPE__NOTIFY || !req->notify_success) {
pr_err("RPC client reported script error\n");
return -1;
}
criu_req__free_unpacked(req, NULL);
return 0;
}
int exec_rpc_query_external_files(char *name, int sk)
{
int i, ret;
CriuNotify cn = CRIU_NOTIFY__INIT;
CriuResp msg = CRIU_RESP__INIT;
CriuReq *req;
cn.script = name;
msg.type = CRIU_REQ_TYPE__NOTIFY;
msg.success = true;
msg.notify = &cn;
ret = send_criu_msg_with_fd(sk, &msg, -1);
if (ret < 0)
return ret;
ret = recv_criu_msg(sk, &req);
if (ret < 0)
return ret;
if (req->type != CRIU_REQ_TYPE__NOTIFY || !req->notify_success) {
pr_err("RPC client reported script error\n");
return -1;
}
ret = 0;
if (req->opts)
for (i = 0; i < req->opts->n_external; i++) {
char *key = req->opts->external[i];
pr_info("Adding external object: %s\n", key);
if (add_external(key)) {
pr_err("Failed to add external object: %s\n", key);
ret = -1;
}
}
else
pr_info("RPC NOTIFY %s: no `opts` returned.\n", name);
criu_req__free_unpacked(req, NULL);
return ret;
}
static char images_dir[PATH_MAX];
static int setup_opts_from_req(int sk, CriuOpts *req)
{
struct ucred ids;
struct stat st;
socklen_t ids_len = sizeof(struct ucred);
char images_dir_path[PATH_MAX];
char work_dir_path[PATH_MAX];
char status_fd[PATH_MAX];
bool output_changed_by_rpc_conf = false;
bool work_changed_by_rpc_conf = false;
bool imgs_changed_by_rpc_conf = false;
int i;
bool dummy = false;
if (getsockopt(sk, SOL_SOCKET, SO_PEERCRED, &ids, &ids_len)) {
pr_perror("Can't get socket options");
goto err;
}
if (fstat(sk, &st)) {
pr_perror("Can't get socket stat");
goto err;
}
BUG_ON(st.st_ino == -1);
service_sk_ino = st.st_ino;
/*
* Evaluate an additional configuration file if specified.
* This needs to happen twice, because it is needed early to detect
* things like work_dir, imgs_dir and logfile. The second parsing
* of the optional RPC configuration file happens at the end and
* overwrites all options set via RPC.
*/
if (req->config_file) {
char *tmp_output = opts.output;
char *tmp_work = opts.work_dir;
char *tmp_imgs = opts.imgs_dir;
opts.output = NULL;
opts.work_dir = NULL;
opts.imgs_dir = NULL;
rpc_cfg_file = req->config_file;
i = parse_options(0, NULL, &dummy, &dummy, PARSING_RPC_CONF);
if (i) {
xfree(tmp_output);
xfree(tmp_work);
xfree(tmp_imgs);
goto err;
}
/* If this is non-NULL, the RPC configuration file had a value, use it.*/
if (opts.output)
output_changed_by_rpc_conf = true;
/* If this is NULL, use the old value if it was set. */
if (!opts.output && tmp_output) {
opts.output = tmp_output;
tmp_output = NULL;
}
if (opts.work_dir)
work_changed_by_rpc_conf = true;
if (!opts.work_dir && tmp_work) {
opts.work_dir = tmp_work;
tmp_work = NULL;
}
if (opts.imgs_dir)
imgs_changed_by_rpc_conf = true;
/*
* As the images directory is a required RPC setting, it is not
* necessary to use the value from other configuration files.
* Either it is set in the RPC configuration file or it is set
* via RPC.
*/
xfree(tmp_output);
xfree(tmp_work);
xfree(tmp_imgs);
}
/*
* open images_dir - images_dir_fd is a required RPC parameter
*
* This assumes that if opts.imgs_dir is set we have a value
* from the configuration file parser. The test to see that
* imgs_changed_by_rpc_conf is true is used to make sure the value
* is from the RPC configuration file.
* The idea is that only the RPC configuration file is able to
* overwrite RPC settings:
* * apply_config(global_conf)
* * apply_config(user_conf)
* * apply_config(environment variable)
* * apply_rpc_options()
* * apply_config(rpc_conf)
*/
if (imgs_changed_by_rpc_conf)
strncpy(images_dir_path, opts.imgs_dir, PATH_MAX - 1);
else if (req->images_dir_fd != -1)
sprintf(images_dir_path, "/proc/%d/fd/%d", ids.pid, req->images_dir_fd);
else if (req->images_dir)
strncpy(images_dir_path, req->images_dir, PATH_MAX - 1);
else {
pr_err("Neither images_dir_fd nor images_dir was passed by RPC client.\n");
goto err;
}
if (req->parent_img)
SET_CHAR_OPTS(img_parent, req->parent_img);
/*
* Image streaming is not supported with CRIU's service feature as
* the streamer must be started for each dump/restore operation.
* It is unclear how to do that with RPC, so we punt for now.
* This explains why we provide the argument mode=-1 instead of
* O_RSTR or O_DUMP.
*/
if (open_image_dir(images_dir_path, -1) < 0) {
pr_perror("Can't open images directory");
goto err;
}
/* get full path to images_dir to use in process title */
if (readlink(images_dir_path, images_dir, PATH_MAX) == -1) {
pr_perror("Can't readlink %s", images_dir_path);
goto err;
}
/* chdir to work dir */
if (work_changed_by_rpc_conf)
/* Use the value from the RPC configuration file first. */
strncpy(work_dir_path, opts.work_dir, PATH_MAX - 1);
else if (req->has_work_dir_fd)
/* Use the value set via RPC. */
sprintf(work_dir_path, "/proc/%d/fd/%d", ids.pid, req->work_dir_fd);
else if (opts.work_dir)
/* Use the value from one of the other configuration files. */
strncpy(work_dir_path, opts.work_dir, PATH_MAX - 1);
else
/* Use the images directory a work directory. */
strcpy(work_dir_path, images_dir_path);
if (chdir(work_dir_path)) {
pr_perror("Can't chdir to work_dir");
goto err;
}
/* initiate log file in work dir */
if (req->log_file && !output_changed_by_rpc_conf) {
/*
* If RPC sets a log file and if there nothing from the
* RPC configuration file, use the RPC value.
*/
if (strchr(req->log_file, '/')) {
pr_perror("No subdirs are allowed in log_file name");
goto err;
}
SET_CHAR_OPTS(output, req->log_file);
} else if (req->has_log_to_stderr && req->log_to_stderr && !output_changed_by_rpc_conf) {
xfree(opts.output);
opts.output = NULL;
} else if (!opts.output) {
SET_CHAR_OPTS(output, DEFAULT_LOG_FILENAME);
}
/* This is needed later to correctly set the log_level */
opts.log_level = req->log_level;
log_set_loglevel(req->log_level);
if (log_init(opts.output) == -1) {
pr_perror("Can't initiate log");
goto err;
}
if (req->config_file) {
pr_debug("Would overwrite RPC settings with values from %s\n", req->config_file);
}
if (req->has_unprivileged)
opts.unprivileged = req->unprivileged;
if (check_caps())
return 1;
if (kerndat_init())
return 1;
if (log_keep_err()) {
pr_perror("Can't tune log");
goto err;
}
/* checking flags from client */
if (req->has_leave_running && req->leave_running)
opts.final_state = TASK_ALIVE;
if (req->has_leave_stopped && req->leave_stopped)
opts.final_state = TASK_STOPPED;
if (!req->has_pid) {
req->has_pid = true;
req->pid = ids.pid;
}
if (req->has_ext_unix_sk) {
opts.ext_unix_sk = req->ext_unix_sk;
for (i = 0; i < req->n_unix_sk_ino; i++) {
if (unix_sk_id_add((unsigned int)req->unix_sk_ino[i]->inode) < 0)
goto err;
}
}
if (req->root)
SET_CHAR_OPTS(root, req->root);
if (req->has_rst_sibling) {
if (!opts.swrk_restore) {
pr_err("rst_sibling is not allowed in standalone service\n");
goto err;
}
opts.restore_sibling = req->rst_sibling;
}
if (req->has_tcp_established)
opts.tcp_established_ok = req->tcp_established;
if (req->has_tcp_skip_in_flight)
opts.tcp_skip_in_flight = req->tcp_skip_in_flight;
if (req->has_tcp_close)
opts.tcp_close = req->tcp_close;
if (req->has_weak_sysctls)
opts.weak_sysctls = req->weak_sysctls;
if (req->has_evasive_devices)
opts.evasive_devices = req->evasive_devices;
if (req->has_shell_job)
opts.shell_job = req->shell_job;
if (req->has_skip_file_rwx_check)
opts.skip_file_rwx_check = req->skip_file_rwx_check;
if (req->has_file_locks)
opts.handle_file_locks = req->file_locks;
if (req->has_track_mem)
opts.track_mem = req->track_mem;
if (req->has_link_remap)
opts.link_remap_ok = req->link_remap;
if (req->has_auto_dedup)
opts.auto_dedup = req->auto_dedup;
if (req->has_force_irmap)
opts.force_irmap = req->force_irmap;
if (req->n_exec_cmd > 0) {
opts.exec_cmd = xmalloc((req->n_exec_cmd + 1) * sizeof(char *));
memcpy(opts.exec_cmd, req->exec_cmd, req->n_exec_cmd * sizeof(char *));
opts.exec_cmd[req->n_exec_cmd] = NULL;
}
if (req->has_lazy_pages) {
opts.lazy_pages = req->lazy_pages;
}
if (req->has_pre_dump_mode) {
switch (req->pre_dump_mode) {
case CRIU_PRE_DUMP_MODE__SPLICE:
opts.pre_dump_mode = PRE_DUMP_SPLICE;
break;
case CRIU_PRE_DUMP_MODE__VM_READ:
opts.pre_dump_mode = PRE_DUMP_READ;
break;
default:
goto err;
}
}
if (req->has_network_lock) {
switch (req->network_lock) {
case CRIU_NETWORK_LOCK_METHOD__IPTABLES:
opts.network_lock_method = NETWORK_LOCK_IPTABLES;
break;
case CRIU_NETWORK_LOCK_METHOD__NFTABLES:
opts.network_lock_method = NETWORK_LOCK_NFTABLES;
break;
case CRIU_NETWORK_LOCK_METHOD__SKIP:
opts.network_lock_method = NETWORK_LOCK_SKIP;
break;
default:
goto err;
}
}
if (req->ps) {
opts.port = (short)req->ps->port;
if (!opts.lazy_pages) {
opts.use_page_server = true;
if (req->ps->address)
SET_CHAR_OPTS(addr, req->ps->address);
else
opts.addr = NULL;
if (req->ps->has_fd) {
if (!opts.swrk_restore)
goto err;
opts.ps_socket = req->ps->fd;
}
}
}
if (req->notify_scripts && add_rpc_notify(sk))
goto err;
for (i = 0; i < req->n_veths; i++) {
if (veth_pair_add(req->veths[i]->if_in, req->veths[i]->if_out))
goto err;
}
for (i = 0; i < req->n_ext_mnt; i++) {
if (ext_mount_add(req->ext_mnt[i]->key, req->ext_mnt[i]->val))
goto err;
}
for (i = 0; i < req->n_join_ns; i++) {
if (join_ns_add(req->join_ns[i]->ns, req->join_ns[i]->ns_file, req->join_ns[i]->extra_opt))
goto err;
}
if (req->n_inherit_fd && !opts.swrk_restore) {
pr_err("inherit_fd is not allowed in standalone service\n");
goto err;
}
for (i = 0; i < req->n_inherit_fd; i++) {
if (inherit_fd_add(req->inherit_fd[i]->fd, req->inherit_fd[i]->key))
goto err;
}
for (i = 0; i < req->n_external; i++)
if (add_external(req->external[i]))
goto err;
for (i = 0; i < req->n_cg_root; i++) {
if (new_cg_root_add(req->cg_root[i]->ctrl, req->cg_root[i]->path))
goto err;
}
for (i = 0; i < req->n_enable_fs; i++) {
if (!add_fsname_auto(req->enable_fs[i]))
goto err;
}
for (i = 0; i < req->n_skip_mnt; i++) {
if (!add_skip_mount(req->skip_mnt[i]))
goto err;
}
if (req->has_cpu_cap) {
opts.cpu_cap = req->cpu_cap;
opts.cpu_cap |= CPU_CAP_IMAGE;
}
/*
* FIXME: For backward compatibility we setup
* soft mode here, need to enhance to support
* other modes as well via separate option
* probably.
*/
if (req->has_manage_cgroups)
opts.manage_cgroups = req->manage_cgroups ? CG_MODE_SOFT : CG_MODE_IGNORE;
/* Override the manage_cgroup if mode is set explicitly */
if (req->has_manage_cgroups_mode) {
unsigned int mode;
switch (req->manage_cgroups_mode) {
case CRIU_CG_MODE__IGNORE:
mode = CG_MODE_IGNORE;
break;
case CRIU_CG_MODE__CG_NONE:
mode = CG_MODE_NONE;
break;
case CRIU_CG_MODE__PROPS:
mode = CG_MODE_PROPS;
break;
case CRIU_CG_MODE__SOFT:
mode = CG_MODE_SOFT;
break;
case CRIU_CG_MODE__FULL:
mode = CG_MODE_FULL;
break;
case CRIU_CG_MODE__STRICT:
mode = CG_MODE_STRICT;
break;
case CRIU_CG_MODE__DEFAULT:
mode = CG_MODE_DEFAULT;
break;
default:
goto err;
}
opts.manage_cgroups = mode;
}
if (req->freeze_cgroup)
SET_CHAR_OPTS(freeze_cgroup, req->freeze_cgroup);
if (req->lsm_profile) {
opts.lsm_supplied = true;
SET_CHAR_OPTS(lsm_profile, req->lsm_profile);
}
if (req->lsm_mount_context)
SET_CHAR_OPTS(lsm_mount_context, req->lsm_mount_context);
if (req->has_timeout)
opts.timeout = req->timeout;
if (req->cgroup_props)
SET_CHAR_OPTS(cgroup_props, req->cgroup_props);
if (req->cgroup_props_file)
SET_CHAR_OPTS(cgroup_props_file, req->cgroup_props_file);
for (i = 0; i < req->n_cgroup_dump_controller; i++) {
if (!cgp_add_dump_controller(req->cgroup_dump_controller[i]))
goto err;
}
if (req->cgroup_yard)
SET_CHAR_OPTS(cgroup_yard, req->cgroup_yard);
if (req->tls_cacert)
SET_CHAR_OPTS(tls_cacert, req->tls_cacert);
if (req->tls_cacrl)
SET_CHAR_OPTS(tls_cacrl, req->tls_cacrl);
if (req->tls_cert)
SET_CHAR_OPTS(tls_cert, req->tls_cert);
if (req->tls_key)
SET_CHAR_OPTS(tls_key, req->tls_key);
if (req->tls)
opts.tls = req->tls;
if (req->tls_no_cn_verify)
opts.tls_no_cn_verify = req->tls_no_cn_verify;
if (req->has_auto_ext_mnt)
opts.autodetect_ext_mounts = req->auto_ext_mnt;
if (req->has_ext_sharing)
opts.enable_external_sharing = req->ext_sharing;
if (req->has_ext_masters)
opts.enable_external_masters = req->ext_masters;
if (req->has_ghost_limit)
opts.ghost_limit = req->ghost_limit;
if (req->has_empty_ns) {
opts.empty_ns = req->empty_ns;
if (req->empty_ns & ~(CLONE_NEWNET))
goto err;
}
if (req->n_irmap_scan_paths) {
for (i = 0; i < req->n_irmap_scan_paths; i++) {
if (irmap_scan_path_add(req->irmap_scan_paths[i]))
goto err;
}
}
if (req->has_status_fd) {
pr_warn("status_fd is obsoleted; use status-ready notification instead\n");
sprintf(status_fd, "/proc/%d/fd/%d", ids.pid, req->status_fd);
opts.status_fd = open(status_fd, O_WRONLY);
if (opts.status_fd < 0) {
pr_perror("Can't reopen status fd %s", status_fd);
goto err;
}
}
if (req->has_pidfd_store_sk && init_pidfd_store_sk(ids.pid, req->pidfd_store_sk))
goto err;
if (req->orphan_pts_master)
opts.orphan_pts_master = true;
if (req->has_display_stats)
opts.display_stats = req->display_stats;
/* Evaluate additional configuration file a second time to overwrite
* all RPC settings. */
if (req->config_file) {
rpc_cfg_file = req->config_file;
i = parse_options(0, NULL, &dummy, &dummy, PARSING_RPC_CONF);
if (i)
goto err;
}
if (req->mntns_compat_mode)
opts.mntns_compat_mode = true;
log_set_loglevel(opts.log_level);
if (check_options())
goto err;
return 0;
err:
set_cr_errno(EBADRQC);
return -1;
}
static int dump_using_req(int sk, CriuOpts *req)
{
bool success = false;
bool self_dump = !req->pid;
opts.mode = CR_DUMP;
if (setup_opts_from_req(sk, req))
goto exit;
__setproctitle("dump --rpc -t %d -D %s", req->pid, images_dir);
if (init_pidfd_store_hash())
goto pidfd_store_err;
/*
* FIXME -- cr_dump_tasks() may return code from custom
* scripts, that can be positive. However, right now we
* don't have ability to push scripts via RPC, so positive
* ret values are impossible here.
*/
if (cr_dump_tasks(req->pid))
goto exit;
success = true;
exit:
free_pidfd_store();
pidfd_store_err:
if (req->leave_running || !self_dump || !success) {
if (send_criu_dump_resp(sk, success, false) == -1) {
pr_perror("Can't send response");
success = false;
}
}
return success ? 0 : 1;
}
static int restore_using_req(int sk, CriuOpts *req)
{
bool success = false;
/*
* We can't restore processes under arbitrary task yet.
* Thus for now we force the detached restore under the
* cr service task.
*/
opts.restore_detach = true;
opts.mode = CR_RESTORE;
if (setup_opts_from_req(sk, req))
goto exit;
__setproctitle("restore --rpc -D %s", images_dir);
if (cr_restore_tasks())
goto exit;
success = true;
exit:
if (send_criu_restore_resp(sk, success, root_item ? root_item->pid->real : -1) == -1) {
pr_perror("Can't send response");
success = false;
}
if (success && opts.exec_cmd) {
int logfd;
logfd = log_get_fd();
if (dup2(logfd, STDOUT_FILENO) == -1 || dup2(logfd, STDERR_FILENO) == -1) {
pr_perror("Failed to redirect stdout and stderr to the logfile");
return 1;
}
close_pid_proc();
close(sk);
execvp(opts.exec_cmd[0], opts.exec_cmd);
pr_perror("Failed to exec cmd %s", opts.exec_cmd[0]);
success = false;
}
return success ? 0 : 1;
}
static int check(int sk, CriuOpts *req)
{
int pid, status;
CriuResp resp = CRIU_RESP__INIT;
resp.type = CRIU_REQ_TYPE__CHECK;
pid = fork();
if (pid < 0) {
pr_perror("Can't fork");
goto out;
}
if (pid == 0) {
__setproctitle("check --rpc");
opts.mode = CR_CHECK;
if (setup_opts_from_req(sk, req))
exit(1);
exit(!!cr_check());
}
if (waitpid(pid, &status, 0) != pid) {
pr_perror("Unable to wait %d", pid);
goto out;
}
if (status)
goto out;
resp.success = true;
out:
return send_criu_msg(sk, &resp);
}
static int pre_dump_using_req(int sk, CriuOpts *req, bool single)
{
int pid, status;
bool success = false;
pid = fork();
if (pid < 0) {
pr_perror("Can't fork");
goto out;
}
if (pid == 0) {
int ret = 1;
opts.mode = CR_PRE_DUMP;
if (setup_opts_from_req(sk, req))
goto cout;
__setproctitle("pre-dump --rpc -t %d -D %s", req->pid, images_dir);
if (init_pidfd_store_hash())
goto pidfd_store_err;
if (cr_pre_dump_tasks(req->pid))
goto cout;
ret = 0;
cout:
free_pidfd_store();
pidfd_store_err:
exit(ret);
}
if (waitpid(pid, &status, 0) != pid) {
pr_perror("Unable to wait %d", pid);
goto out;
}
if (status != 0)
goto out;
success = true;
out:
if (send_criu_pre_dump_resp(sk, success, single) == -1) {
pr_perror("Can't send pre-dump resp");
success = false;
}
return success ? 0 : -1;
}
static int pre_dump_loop(int sk, CriuReq *msg)
{
int ret;
do {
ret = pre_dump_using_req(sk, msg->opts, false);
if (ret < 0)
return ret;
criu_req__free_unpacked(msg, NULL);
if (recv_criu_msg(sk, &msg) == -1) {
pr_perror("Can't recv request");
return -1;
}
} while (msg->type == CRIU_REQ_TYPE__PRE_DUMP);
if (msg->type != CRIU_REQ_TYPE__DUMP) {
send_criu_err(sk, "Bad req seq");
return -1;
}
return dump_using_req(sk, msg->opts);
}
static int start_page_server_req(int sk, CriuOpts *req, bool daemon_mode)
{