2012-01-31 22:28:55 +04:00
|
|
|
#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>
|
2012-02-02 19:55:48 +04:00
|
|
|
|
2012-01-31 22:28:55 +04:00
|
|
|
#include "util.h"
|
|
|
|
#include "crtools.h"
|
|
|
|
#include "syscall.h"
|
|
|
|
#include "namespaces.h"
|
2012-02-02 19:55:48 +04:00
|
|
|
#include "sysctl.h"
|
2012-01-31 22:28:55 +04:00
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
static int ipc_sysctl_req(struct ipc_var_entry *e, int op)
|
2012-02-02 19:55:48 +04:00
|
|
|
{
|
|
|
|
struct sysctl_req req[] = {
|
|
|
|
{ "kernel/sem", e->sem_ctls, CTL_U32A(4) },
|
|
|
|
{ "kernel/msgmax", &e->msg_ctlmax, CTL_U32 },
|
|
|
|
{ "kernel/msgmnb", &e->msg_ctlmnb, CTL_U32 },
|
|
|
|
{ "kernel/msgmni", &e->msg_ctlmni, CTL_U32 },
|
|
|
|
{ "kernel/auto_msgmni", &e->auto_msgmni, 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 },
|
|
|
|
{ "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 },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
return sysctl_op(req, op);
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
static int dump_ipc_msg(void *data)
|
2012-01-31 22:28:55 +04:00
|
|
|
{
|
|
|
|
struct msginfo info;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
ret = msgctl(0, MSG_INFO, (struct msqid_ds *)&info);
|
|
|
|
if (ret < 0) {
|
2012-02-01 02:03:41 +04:00
|
|
|
pr_perror("msgctl failed");
|
2012-01-31 22:28:55 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
pr_err("IPC messages migration is not supported yet\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
static int dump_ipc_sem(void *data)
|
2012-01-31 22:28:55 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct seminfo info;
|
|
|
|
|
|
|
|
ret = semctl(0, 0, SEM_INFO, &info);
|
|
|
|
if (ret < 0)
|
2012-02-01 02:03:41 +04:00
|
|
|
pr_perror("semctl failed");
|
2012-01-31 22:28:55 +04:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
pr_err("IPC semaphores migration is not supported yet\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
static int dump_ipc_shm(void *data)
|
2012-01-31 22:28:55 +04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
struct shmid_ds shmid;
|
|
|
|
|
|
|
|
ret = shmctl(0, IPC_INFO, &shmid);
|
|
|
|
if (ret < 0)
|
2012-02-01 02:03:41 +04:00
|
|
|
pr_perror("semctl failed");
|
2012-01-31 22:28:55 +04:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
pr_err("IPC shared memory migration is not supported yet\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
static int dump_ipc_var(int fd)
|
2012-01-31 22:28:55 +04:00
|
|
|
{
|
2012-02-08 12:13:38 +03:00
|
|
|
int ret;
|
|
|
|
struct ipc_var_entry var;
|
|
|
|
|
|
|
|
ret = ipc_sysctl_req(&var, CTL_READ);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to read IPC variables\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write_img(fd, &var);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to write IPC variables\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-01-31 22:28:55 +04:00
|
|
|
}
|
|
|
|
|
2012-02-08 14:47:18 +03:00
|
|
|
static int dump_ipc_data(const struct cr_fdset *fdset)
|
2012-01-31 22:28:55 +04:00
|
|
|
{
|
2012-02-08 12:13:38 +03:00
|
|
|
int ret;
|
2012-01-31 22:28:55 +04:00
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
ret = dump_ipc_var(fdset->fds[CR_FD_IPCNS_VAR]);
|
2012-01-31 22:28:55 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-02-08 12:13:38 +03:00
|
|
|
ret = dump_ipc_shm(0);
|
2012-01-31 22:28:55 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-02-08 12:13:38 +03:00
|
|
|
ret = dump_ipc_msg(0);
|
2012-01-31 22:28:55 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-02-08 12:13:38 +03:00
|
|
|
ret = dump_ipc_sem(0);
|
2012-01-31 22:28:55 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-08 14:47:18 +03:00
|
|
|
int dump_ipc_ns(int ns_pid, const struct cr_fdset *fdset)
|
2012-01-31 22:28:55 +04:00
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
|
2012-02-03 16:48:44 +03:00
|
|
|
ret = switch_ns(ns_pid, CLONE_NEWIPC, "ipc");
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
ret = dump_ipc_data(fdset);
|
2012-01-31 22:28:55 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to write IPC namespace data\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-31 22:29:11 +04:00
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
static void show_var_entry(struct ipc_var_entry *entry)
|
2012-01-31 22:29:11 +04:00
|
|
|
{
|
2012-02-02 19:55:48 +04:00
|
|
|
ipc_sysctl_req(entry, CTL_PRINT);
|
2012-01-31 22:29:11 +04:00
|
|
|
}
|
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
static void show_ipc_var_entry(int fd)
|
2012-01-31 22:29:11 +04:00
|
|
|
{
|
|
|
|
int ret;
|
2012-02-08 12:13:38 +03:00
|
|
|
struct ipc_var_entry var;
|
2012-01-31 22:29:11 +04:00
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
ret = read_img_eof(fd, &var);
|
2012-01-31 22:29:11 +04:00
|
|
|
if (ret <= 0)
|
|
|
|
return;
|
2012-02-08 12:13:38 +03:00
|
|
|
show_var_entry(&var);
|
2012-01-31 22:29:11 +04:00
|
|
|
}
|
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
void show_ipc_var(int fd)
|
2012-01-31 22:29:11 +04:00
|
|
|
{
|
|
|
|
pr_img_head(CR_FD_IPCNS);
|
2012-02-08 12:13:38 +03:00
|
|
|
show_ipc_var_entry(fd);
|
2012-01-31 22:29:11 +04:00
|
|
|
pr_img_tail(CR_FD_IPCNS);
|
|
|
|
}
|
2012-01-31 22:29:22 +04:00
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
static int prepare_ipc_var(int pid)
|
2012-01-31 22:29:22 +04:00
|
|
|
{
|
2012-02-08 12:13:38 +03:00
|
|
|
int fd, ret;
|
|
|
|
struct ipc_var_entry var;
|
2012-01-31 22:29:22 +04:00
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
pr_info("Restoring IPC variables\n");
|
|
|
|
fd = open_image_ro(CR_FD_IPCNS_VAR, pid);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
2012-01-31 22:29:22 +04:00
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
ret = read_img(fd, &var);
|
2012-01-31 22:29:22 +04:00
|
|
|
if (ret <= 0)
|
|
|
|
return -EFAULT;
|
2012-02-08 12:13:38 +03:00
|
|
|
|
|
|
|
show_var_entry(&var);
|
|
|
|
|
|
|
|
return ipc_sysctl_req(&var, CTL_WRITE);
|
2012-01-31 22:29:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int prepare_ipc_ns(int pid)
|
|
|
|
{
|
2012-02-08 12:13:38 +03:00
|
|
|
int ret;
|
2012-01-31 22:29:22 +04:00
|
|
|
|
2012-02-08 12:13:38 +03:00
|
|
|
pr_info("Restoring IPC namespace\n");
|
|
|
|
ret = prepare_ipc_var(pid);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return 0;
|
2012-01-31 22:29:22 +04:00
|
|
|
}
|