forked from checkpoint-restore/criu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc_ns.c
936 lines (788 loc) · 19.8 KB
/
ipc_ns.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
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include "util.h"
#include "cr_options.h"
#include "imgset.h"
#include "syscall.h"
#include "namespaces.h"
#include "sysctl.h"
#include "ipc_ns.h"
#include "protobuf.h"
#include "protobuf/ipc-var.pb-c.h"
#include "protobuf/ipc-shm.pb-c.h"
#include "protobuf/ipc-sem.pb-c.h"
#include "protobuf/ipc-msg.pb-c.h"
#if defined (__GLIBC__) && __GLIBC__ >= 2
#define KEY __key
#else
#define KEY key
#endif
#ifndef MSGMAX
#define MSGMAX 8192
#endif
#ifndef MSG_COPY
#define MSG_COPY 040000
#endif
static void pr_ipc_desc_entry(unsigned int loglevel, const IpcDescEntry *desc)
{
print_on_level(loglevel, "id: %-10d key: 0x%08x uid: %-10d gid: %-10d "
"cuid: %-10d cgid: %-10d mode: %-10o ",
desc->id, desc->key, desc->uid, desc->gid,
desc->cuid, desc->cgid, desc->mode);
}
static void fill_ipc_desc(int id, IpcDescEntry *desc, const struct ipc_perm *ipcp)
{
desc->id = id;
desc->key = ipcp->KEY;
desc->uid = userns_uid(ipcp->uid);
desc->gid = userns_gid(ipcp->gid);
desc->cuid = userns_uid(ipcp->cuid);
desc->cgid = userns_gid(ipcp->cgid);
desc->mode = ipcp->mode;
}
static void pr_ipc_sem_array(unsigned int loglevel, int nr, u16 *values)
{
while (nr--)
print_on_level(loglevel, " %-5d", values[nr]);
print_on_level(loglevel, "\n");
}
#define pr_info_ipc_sem_array(nr, values) pr_ipc_sem_array(LOG_INFO, nr, values)
#define pr_msg_ipc_sem_array(nr, values) pr_ipc_sem_array(LOG_MSG, nr, values)
static void pr_info_ipc_sem_entry(const IpcSemEntry *sem)
{
pr_ipc_desc_entry(LOG_INFO, sem->desc);
print_on_level(LOG_INFO, "nsems: %-10d\n", sem->nsems);
}
static int dump_ipc_sem_set(struct cr_img *img, const IpcSemEntry *sem)
{
size_t rounded;
int ret, size;
u16 *values;
size = sizeof(u16) * sem->nsems;
rounded = round_up(size, sizeof(u64));
values = xmalloc(rounded);
if (values == NULL) {
pr_err("Failed to allocate memory for semaphore set values\n");
ret = -ENOMEM;
goto out;
}
ret = semctl(sem->desc->id, 0, GETALL, values);
if (ret < 0) {
pr_perror("Failed to get semaphore set values");
ret = -errno;
goto out;
}
pr_info_ipc_sem_array(sem->nsems, values);
memzero((void *)values + size, rounded - size);
ret = write_img_buf(img, values, rounded);
if (ret < 0) {
pr_err("Failed to write IPC message data\n");
goto out;
}
out:
xfree(values);
return ret;
}
static int dump_ipc_sem_desc(struct cr_img *img, int id, const struct semid_ds *ds)
{
IpcSemEntry sem = IPC_SEM_ENTRY__INIT;
IpcDescEntry desc = IPC_DESC_ENTRY__INIT;
int ret;
sem.desc = &desc;
sem.nsems = ds->sem_nsems;
fill_ipc_desc(id, sem.desc, &ds->sem_perm);
pr_info_ipc_sem_entry(&sem);
ret = pb_write_one(img, &sem, PB_IPC_SEM);
if (ret < 0) {
pr_err("Failed to write IPC semaphores set\n");
return ret;
}
return dump_ipc_sem_set(img, &sem);
}
static int dump_ipc_sem(struct cr_img *img)
{
int i, maxid;
struct seminfo info;
int slot;
maxid = semctl(0, 0, SEM_INFO, &info);
if (maxid < 0) {
pr_perror("semctl failed");
return -errno;
}
pr_info("IPC semaphore sets: %d\n", info.semusz);
for (i = 0, slot = 0; i <= maxid; i++) {
struct semid_ds ds;
int id, ret;
id = semctl(i, 0, SEM_STAT, &ds);
if (id < 0) {
if (errno == EINVAL)
continue;
pr_perror("Failed to get stats for IPC semaphore set");
break;
}
ret = dump_ipc_sem_desc(img, id, &ds);
if (!ret)
slot++;
}
if (slot != info.semusz) {
pr_err("Failed to collect %d (only %d succeeded)\n", info.semusz, slot);
return -EFAULT;
}
return info.semusz;
}
static void pr_info_ipc_msg(int nr, const IpcMsg *msg)
{
print_on_level(LOG_INFO, " %-5d: type: %-20"PRId64" size: %-10d\n",
nr++, msg->mtype, msg->msize);
}
static void pr_info_ipc_msg_entry(const IpcMsgEntry *msg)
{
pr_ipc_desc_entry(LOG_INFO, msg->desc);
print_on_level(LOG_INFO, "qbytes: %-10d qnum: %-10d\n",
msg->qbytes, msg->qnum);
}
static int dump_ipc_msg_queue_messages(struct cr_img *img, const IpcMsgEntry *msq,
unsigned int msg_nr)
{
struct msgbuf *message = NULL;
unsigned int msgmax;
int ret, msg_cnt = 0;
struct sysctl_req req[] = {
{ "kernel/msgmax", &msgmax, CTL_U32 },
};
ret = sysctl_op(req, ARRAY_SIZE(req), CTL_READ, CLONE_NEWIPC);
if (ret < 0) {
pr_err("Failed to read max IPC message size\n");
goto err;
}
msgmax += sizeof(struct msgbuf);
message = xmalloc(round_up(msgmax, sizeof(u64)));
if (message == NULL) {
pr_err("Failed to allocate memory for IPC message\n");
return -ENOMEM;
}
for (msg_cnt = 0; msg_cnt < msg_nr; msg_cnt++) {
IpcMsg msg = IPC_MSG__INIT;
size_t rounded;
ret = msgrcv(msq->desc->id, message, msgmax, msg_cnt, IPC_NOWAIT | MSG_COPY);
if (ret < 0) {
pr_perror("Failed to copy IPC message");
goto err;
}
msg.msize = ret;
msg.mtype = message->mtype;
pr_info_ipc_msg(msg_cnt, &msg);
ret = pb_write_one(img, &msg, PB_IPCNS_MSG);
if (ret < 0) {
pr_err("Failed to write IPC message header\n");
break;
}
rounded = round_up(msg.msize, sizeof(u64));
memzero(((void *)message->mtext + msg.msize), rounded - msg.msize);
ret = write_img_buf(img, message->mtext, rounded);
if (ret < 0) {
pr_err("Failed to write IPC message data\n");
break;
}
}
ret = 0;
err:
xfree(message);
return ret;
}
static int dump_ipc_msg_queue(struct cr_img *img, int id, const struct msqid_ds *ds)
{
IpcMsgEntry msg = IPC_MSG_ENTRY__INIT;
IpcDescEntry desc = IPC_DESC_ENTRY__INIT;
int ret;
msg.desc = &desc;
fill_ipc_desc(id, msg.desc, &ds->msg_perm);
msg.qbytes = ds->msg_qbytes;
msg.qnum = ds->msg_qnum;
pr_info_ipc_msg_entry(&msg);
ret = pb_write_one(img, &msg, PB_IPCNS_MSG_ENT);
if (ret < 0) {
pr_err("Failed to write IPC message queue\n");
return ret;
}
return dump_ipc_msg_queue_messages(img, &msg, ds->msg_qnum);
}
static int dump_ipc_msg(struct cr_img *img)
{
int i, maxid;
struct msginfo info;
int slot;
maxid = msgctl(0, MSG_INFO, (struct msqid_ds *)&info);
if (maxid < 0) {
pr_perror("msgctl failed");
return -errno;
}
pr_info("IPC message queues: %d\n", info.msgpool);
for (i = 0, slot = 0; i <= maxid; i++) {
struct msqid_ds ds;
int id, ret;
id = msgctl(i, MSG_STAT, &ds);
if (id < 0) {
if (errno == EINVAL)
continue;
pr_perror("Failed to get stats for IPC message queue");
break;
}
ret = dump_ipc_msg_queue(img, id, &ds);
if (!ret)
slot++;
}
if (slot != info.msgpool) {
pr_err("Failed to collect %d message queues (only %d succeeded)\n", info.msgpool, slot);
return -EFAULT;
}
return info.msgpool;
}
static void pr_info_ipc_shm(const IpcShmEntry *shm)
{
pr_ipc_desc_entry(LOG_INFO, shm->desc);
print_on_level(LOG_INFO, "size: %-10"PRIu64"\n", shm->size);
}
static int ipc_sysctl_req(IpcVarEntry *e, int op)
{
struct sysctl_req req[] = {
{ "kernel/sem", e->sem_ctls, CTL_U32A(e->n_sem_ctls) },
{ "kernel/msgmax", &e->msg_ctlmax, CTL_U32 },
{ "kernel/msgmnb", &e->msg_ctlmnb, CTL_U32 },
{ "kernel/auto_msgmni", &e->auto_msgmni, CTL_U32 },
{ "kernel/msgmni", &e->msg_ctlmni, CTL_U32 },
{ "kernel/shmmax", &e->shm_ctlmax, CTL_U64 },
{ "kernel/shmall", &e->shm_ctlall, CTL_U64 },
{ "kernel/shmmni", &e->shm_ctlmni, CTL_U32 },
{ "kernel/shm_rmid_forced", &e->shm_rmid_forced, CTL_U32 },
};
struct sysctl_req req_mq[] = {
{ "fs/mqueue/queues_max", &e->mq_queues_max, CTL_U32 },
{ "fs/mqueue/msg_max", &e->mq_msg_max, CTL_U32 },
{ "fs/mqueue/msgsize_max", &e->mq_msgsize_max, CTL_U32 },
};
int ret;
ret = sysctl_op(req, ARRAY_SIZE(req), op, CLONE_NEWIPC);
if (ret)
return ret;
if (access("/proc/sys/fs/mqueue", X_OK)) {
pr_info("Mqueue sysctls are missing\n");
return 0;
}
return sysctl_op(req_mq, ARRAY_SIZE(req_mq), op, CLONE_NEWIPC);
}
/*
* TODO: Function below should be later improved to locate and dump only dirty
* pages via updated sys_mincore().
*/
static int dump_ipc_shm_pages(struct cr_img *img, const IpcShmEntry *shm)
{
void *data;
int ret;
data = shmat(shm->desc->id, NULL, SHM_RDONLY);
if (data == (void *)-1) {
pr_perror("Failed to attach IPC shared memory");
return -errno;
}
ret = write_img_buf(img, data, round_up(shm->size, sizeof(u32)));
if (ret < 0) {
pr_err("Failed to write IPC shared memory data\n");
return ret;
}
if (shmdt(data)) {
pr_perror("Failed to detach IPC shared memory");
return -errno;
}
return 0;
}
static int dump_ipc_shm_seg(struct cr_img *img, int id, const struct shmid_ds *ds)
{
IpcShmEntry shm = IPC_SHM_ENTRY__INIT;
IpcDescEntry desc = IPC_DESC_ENTRY__INIT;
int ret;
shm.desc = &desc;
shm.size = ds->shm_segsz;
fill_ipc_desc(id, shm.desc, &ds->shm_perm);
pr_info_ipc_shm(&shm);
ret = pb_write_one(img, &shm, PB_IPC_SHM);
if (ret < 0) {
pr_err("Failed to write IPC shared memory segment\n");
return ret;
}
return dump_ipc_shm_pages(img, &shm);
}
static int dump_ipc_shm(struct cr_img *img)
{
int i, maxid, slot;
struct shm_info info;
maxid = shmctl(0, SHM_INFO, (void *)&info);
if (maxid < 0) {
pr_perror("shmctl(SHM_INFO) failed");
return -errno;
}
pr_info("IPC shared memory segments: %d\n", info.used_ids);
for (i = 0, slot = 0; i <= maxid; i++) {
struct shmid_ds ds;
int id, ret;
id = shmctl(i, SHM_STAT, &ds);
if (id < 0) {
if (errno == EINVAL)
continue;
pr_perror("Failed to get stats for IPC shared memory");
break;
}
ret = dump_ipc_shm_seg(img, id, &ds);
if (ret < 0)
return ret;
slot++;
}
if (slot != info.used_ids) {
pr_err("Failed to collect %d (only %d succeeded)\n",
info.used_ids, slot);
return -EFAULT;
}
return 0;
}
static int dump_ipc_var(struct cr_img *img)
{
IpcVarEntry var = IPC_VAR_ENTRY__INIT;
int ret = -1;
var.n_sem_ctls = 4;
var.sem_ctls = xmalloc(pb_repeated_size(&var, sem_ctls));
if (!var.sem_ctls)
goto err;
ret = ipc_sysctl_req(&var, CTL_READ);
if (ret < 0) {
pr_err("Failed to read IPC variables\n");
goto err;
}
ret = pb_write_one(img, &var, PB_IPC_VAR);
if (ret < 0) {
pr_err("Failed to write IPC variables\n");
goto err;
}
err:
xfree(var.sem_ctls);
return ret;
}
static int dump_ipc_data(const struct cr_imgset *imgset)
{
int ret;
ret = dump_ipc_var(img_from_set(imgset, CR_FD_IPC_VAR));
if (ret < 0)
return ret;
ret = dump_ipc_shm(img_from_set(imgset, CR_FD_IPCNS_SHM));
if (ret < 0)
return ret;
ret = dump_ipc_msg(img_from_set(imgset, CR_FD_IPCNS_MSG));
if (ret < 0)
return ret;
ret = dump_ipc_sem(img_from_set(imgset, CR_FD_IPCNS_SEM));
if (ret < 0)
return ret;
return 0;
}
int dump_ipc_ns(int ns_id)
{
int ret;
struct cr_imgset *imgset;
imgset = cr_imgset_open(ns_id, IPCNS, O_DUMP);
if (imgset == NULL)
return -1;
ret = dump_ipc_data(imgset);
if (ret < 0) {
pr_err("Failed to write IPC namespace data\n");
goto err;
}
err:
close_cr_imgset(&imgset);
return ret < 0 ? -1 : 0;
}
void ipc_sem_handler(struct cr_img *img, void *obj)
{
IpcSemEntry *e = obj;
u16 *values;
int size;
pr_msg("\n");
size = round_up(sizeof(u16) * e->nsems, sizeof(u64));
values = xmalloc(size);
if (values == NULL)
return;
if (read_img_buf(img, values, size) <= 0) {
xfree(values);
return;
}
pr_msg_ipc_sem_array(e->nsems, values);
xfree(values);
}
static void ipc_msg_data_handler(struct cr_img *img, void *obj)
{
IpcMsg *e = obj;
print_image_data(img, round_up(e->msize, sizeof(u64)), opts.show_pages_content);
}
void ipc_msg_handler(struct cr_img *img, void *obj)
{
IpcMsgEntry *e = obj;
int msg_nr = 0;
pr_msg("\n");
while (msg_nr++ < e->qnum)
pb_show_plain_payload(img, PB_IPCNS_MSG, ipc_msg_data_handler);
}
void ipc_shm_handler(struct cr_img *img, void *obj)
{
IpcShmEntry *e = obj;
print_image_data(img, round_up(e->size, sizeof(u32)), opts.show_pages_content);
}
static int prepare_ipc_sem_values(struct cr_img *img, const IpcSemEntry *sem)
{
int ret, size;
u16 *values;
size = round_up(sizeof(u16) * sem->nsems, sizeof(u64));
values = xmalloc(size);
if (values == NULL) {
pr_err("Failed to allocate memory for semaphores set values\n");
ret = -ENOMEM;
goto out;
}
ret = read_img_buf(img, values, size);
if (ret < 0) {
pr_err("Failed to allocate memory for semaphores set values\n");
ret = -ENOMEM;
goto out;
}
pr_info_ipc_sem_array(sem->nsems, values);
ret = semctl(sem->desc->id, 0, SETALL, values);
if (ret < 0) {
pr_perror("Failed to set semaphores set values");
ret = -errno;
}
out:
xfree(values);
return ret;
}
static int prepare_ipc_sem_desc(struct cr_img *img, const IpcSemEntry *sem)
{
int ret, id;
struct sysctl_req req[] = {
{ "kernel/sem_next_id", &sem->desc->id, CTL_U32 },
};
struct semid_ds semid;
ret = sysctl_op(req, ARRAY_SIZE(req), CTL_WRITE, CLONE_NEWIPC);
if (ret < 0) {
pr_err("Failed to set desired IPC sem ID\n");
return ret;
}
id = semget(sem->desc->key, sem->nsems,
sem->desc->mode | IPC_CREAT | IPC_EXCL);
if (id == -1) {
pr_perror("Failed to create sem set");
return -errno;
}
if (id != sem->desc->id) {
pr_err("Failed to restore sem id (%d instead of %d)\n",
id, sem->desc->id);
return -EFAULT;
}
ret = semctl(id, sem->nsems, IPC_STAT, &semid);
if (ret == -1) {
pr_err("Failed to get sem stat structure\n");
return -EFAULT;
}
semid.sem_perm.uid = sem->desc->uid;
semid.sem_perm.gid = sem->desc->gid;
ret = semctl(id, sem->nsems, IPC_SET, &semid);
if (ret == -1) {
pr_err("Failed to set sem uid and gid\n");
return -EFAULT;
}
ret = prepare_ipc_sem_values(img, sem);
if (ret < 0) {
pr_err("Failed to update sem pages\n");
return ret;
}
return 0;
}
static int prepare_ipc_sem(int pid)
{
int ret;
struct cr_img *img;
pr_info("Restoring IPC semaphores sets\n");
img = open_image(CR_FD_IPCNS_SEM, O_RSTR, pid);
if (!img)
return -1;
while (1) {
IpcSemEntry *sem;
ret = pb_read_one_eof(img, &sem, PB_IPC_SEM);
if (ret < 0) {
ret = -EIO;
goto err;
}
if (ret == 0)
break;
pr_info_ipc_sem_entry(sem);
ret = prepare_ipc_sem_desc(img, sem);
ipc_sem_entry__free_unpacked(sem, NULL);
if (ret < 0) {
pr_err("Failed to prepare semaphores set\n");
goto err;
}
}
close_image(img);
return 0;
err:
close_image(img);
return ret;
}
static int prepare_ipc_msg_queue_messages(struct cr_img *img, const IpcMsgEntry *msq)
{
IpcMsg *msg = NULL;
int msg_nr = 0;
int ret = 0;
while (msg_nr < msq->qnum) {
struct msgbuf {
long mtype;
char mtext[MSGMAX];
} data;
ret = pb_read_one(img, &msg, PB_IPCNS_MSG);
if (ret <= 0)
return -EIO;
pr_info_ipc_msg(msg_nr, msg);
if (msg->msize > MSGMAX) {
ret = -1;
pr_err("Unsupported message size: %d (MAX: %d)\n",
msg->msize, MSGMAX);
break;
}
ret = read_img_buf(img, data.mtext, round_up(msg->msize, sizeof(u64)));
if (ret < 0) {
pr_err("Failed to read IPC message data\n");
break;
}
data.mtype = msg->mtype;
ret = msgsnd(msq->desc->id, &data, msg->msize, IPC_NOWAIT);
if (ret < 0) {
pr_perror("Failed to send IPC message");
ret = -errno;
break;
}
msg_nr++;
}
if (msg)
ipc_msg__free_unpacked(msg, NULL);
return ret;
}
static int prepare_ipc_msg_queue(struct cr_img *img, const IpcMsgEntry *msq)
{
int ret, id;
struct sysctl_req req[] = {
{ "kernel/msg_next_id", &msq->desc->id, CTL_U32 },
};
struct msqid_ds msqid;
ret = sysctl_op(req, ARRAY_SIZE(req), CTL_WRITE, CLONE_NEWIPC);
if (ret < 0) {
pr_err("Failed to set desired IPC msg ID\n");
return ret;
}
id = msgget(msq->desc->key, msq->desc->mode | IPC_CREAT | IPC_EXCL);
if (id == -1) {
pr_perror("Failed to create msg set");
return -errno;
}
if (id != msq->desc->id) {
pr_err("Failed to restore msg id (%d instead of %d)\n",
id, msq->desc->id);
return -EFAULT;
}
ret = msgctl(id, IPC_STAT, &msqid);
if (ret == -1) {
pr_err("Failed to get msq stat structure\n");
return -EFAULT;
}
msqid.msg_perm.uid = msq->desc->uid;
msqid.msg_perm.gid = msq->desc->gid;
ret = msgctl(id, IPC_SET, &msqid);
if (ret == -1) {
pr_err("Failed to set msq queue uid and gid\n");
return -EFAULT;
}
ret = prepare_ipc_msg_queue_messages(img, msq);
if (ret < 0) {
pr_err("Failed to update message queue messages\n");
return ret;
}
return 0;
}
static int prepare_ipc_msg(int pid)
{
int ret;
struct cr_img *img;
pr_info("Restoring IPC message queues\n");
img = open_image(CR_FD_IPCNS_MSG, O_RSTR, pid);
if (!img)
return -1;
while (1) {
IpcMsgEntry *msq;
ret = pb_read_one_eof(img, &msq, PB_IPCNS_MSG_ENT);
if (ret < 0) {
pr_err("Failed to read IPC messages queue\n");
ret = -EIO;
goto err;
}
if (ret == 0)
break;
pr_info_ipc_msg_entry(msq);
ret = prepare_ipc_msg_queue(img, msq);
ipc_msg_entry__free_unpacked(msq, NULL);
if (ret < 0) {
pr_err("Failed to prepare messages queue\n");
goto err;
}
}
close_image(img);
return 0;
err:
close_image(img);
return ret;
}
static int prepare_ipc_shm_pages(struct cr_img *img, const IpcShmEntry *shm)
{
int ret;
void *data;
data = shmat(shm->desc->id, NULL, 0);
if (data == (void *)-1) {
pr_perror("Failed to attach IPC shared memory");
return -errno;
}
ret = read_img_buf(img, data, round_up(shm->size, sizeof(u32)));
if (ret < 0) {
pr_err("Failed to read IPC shared memory data\n");
return ret;
}
if (shmdt(data)) {
pr_perror("Failed to detach IPC shared memory");
return -errno;
}
return 0;
}
static int prepare_ipc_shm_seg(struct cr_img *img, const IpcShmEntry *shm)
{
int ret, id;
struct sysctl_req req[] = {
{ "kernel/shm_next_id", &shm->desc->id, CTL_U32 },
};
struct shmid_ds shmid;
ret = sysctl_op(req, ARRAY_SIZE(req), CTL_WRITE, CLONE_NEWIPC);
if (ret < 0) {
pr_err("Failed to set desired IPC shm ID\n");
return ret;
}
id = shmget(shm->desc->key, shm->size,
shm->desc->mode | IPC_CREAT | IPC_EXCL);
if (id == -1) {
pr_perror("Failed to create shm set");
return -errno;
}
if (id != shm->desc->id) {
pr_err("Failed to restore shm id (%d instead of %d)\n",
id, shm->desc->id);
return -EFAULT;
}
ret = shmctl(id, IPC_STAT, &shmid);
if (ret == -1) {
pr_err("Failed to get shm stat structure\n");
return -EFAULT;
}
shmid.shm_perm.uid = shm->desc->uid;
shmid.shm_perm.gid = shm->desc->gid;
ret = shmctl(id, IPC_SET, &shmid);
if (ret == -1) {
pr_err("Failed to set shm uid and gid\n");
return -EFAULT;
}
ret = prepare_ipc_shm_pages(img, shm);
if (ret < 0) {
pr_err("Failed to update shm pages\n");
return ret;
}
return 0;
}
static int prepare_ipc_shm(int pid)
{
int ret;
struct cr_img *img;
pr_info("Restoring IPC shared memory\n");
img = open_image(CR_FD_IPCNS_SHM, O_RSTR, pid);
if (!img)
return -1;
while (1) {
IpcShmEntry *shm;
ret = pb_read_one_eof(img, &shm, PB_IPC_SHM);
if (ret < 0) {
pr_err("Failed to read IPC shared memory segment\n");
ret = -EIO;
goto err;
}
if (ret == 0)
break;
pr_info_ipc_shm(shm);
ret = prepare_ipc_shm_seg(img, shm);
ipc_shm_entry__free_unpacked(shm, NULL);
if (ret < 0) {
pr_err("Failed to prepare shm segment\n");
goto err;
}
}
close_image(img);
return 0;
err:
close_image(img);
return ret;
}
static int prepare_ipc_var(int pid)
{
int ret;
struct cr_img *img;
IpcVarEntry *var;
pr_info("Restoring IPC variables\n");
img = open_image(CR_FD_IPC_VAR, O_RSTR, pid);
if (!img)
return -1;
ret = pb_read_one(img, &var, PB_IPC_VAR);
close_image(img);
if (ret <= 0) {
pr_err("Failed to read IPC namespace variables\n");
return -EFAULT;
}
ret = ipc_sysctl_req(var, CTL_WRITE);
ipc_var_entry__free_unpacked(var, NULL);
if (ret < 0) {
pr_err("Failed to prepare IPC namespace variables\n");
return -EFAULT;
}
return 0;
}
int prepare_ipc_ns(int pid)
{
int ret;
pr_info("Restoring IPC namespace\n");
ret = prepare_ipc_var(pid);
if (ret < 0)
return ret;
ret = prepare_ipc_shm(pid);
if (ret < 0)
return ret;
ret = prepare_ipc_msg(pid);
if (ret < 0)
return ret;
ret = prepare_ipc_sem(pid);
if (ret < 0)
return ret;
return 0;
}
struct ns_desc ipc_ns_desc = NS_DESC_ENTRY(CLONE_NEWIPC, "ipc");