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-07-19 11:31:39 +04:00
|
|
|
#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"
|
|
|
|
|
2012-02-10 13:05:06 +03:00
|
|
|
#if defined (__GLIBC__) && __GLIBC__ >= 2
|
|
|
|
#define KEY __key
|
|
|
|
#else
|
|
|
|
#define KEY key
|
|
|
|
#endif
|
|
|
|
|
2012-02-09 12:09:55 +03:00
|
|
|
#ifndef IPC_PRESET
|
|
|
|
#define IPC_PRESET 00040000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SHM_SET
|
|
|
|
#define SHM_SET 15
|
|
|
|
#endif
|
|
|
|
|
2012-02-13 20:27:35 +03:00
|
|
|
#ifndef MSGMAX
|
|
|
|
#define MSGMAX 8192
|
|
|
|
#endif
|
|
|
|
|
2012-09-19 20:09:59 +04:00
|
|
|
#ifndef MSG_COPY
|
|
|
|
#define MSG_COPY 040000
|
2012-02-13 20:27:49 +03:00
|
|
|
#endif
|
2012-02-13 20:27:35 +03:00
|
|
|
|
2012-02-13 20:27:49 +03:00
|
|
|
#ifndef MSG_SET
|
|
|
|
#define MSG_SET 13
|
2012-02-13 20:27:35 +03:00
|
|
|
#endif
|
|
|
|
|
2012-09-19 20:09:59 +04:00
|
|
|
#ifndef MSG_SET_COPY
|
|
|
|
#define MSG_SET_COPY 14
|
|
|
|
#endif
|
|
|
|
|
2012-02-14 19:54:20 +03:00
|
|
|
#ifndef SEM_SET
|
|
|
|
#define SEM_SET 20
|
|
|
|
#endif
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static void pr_ipc_desc_entry(unsigned int loglevel, const IpcDescEntry *desc)
|
2012-02-09 12:09:41 +03:00
|
|
|
{
|
2012-03-02 00:59:59 +04:00
|
|
|
print_on_level(loglevel, "id: %-10d key: 0x%08x ", desc->id, desc->key);
|
|
|
|
print_on_level(loglevel, "uid: %-10d gid: %-10d ", desc->uid, desc->gid);
|
|
|
|
print_on_level(loglevel, "cuid: %-10d cgid: %-10d ", desc->cuid, desc->cgid);
|
|
|
|
print_on_level(loglevel, "mode: %-10o ", desc->mode);
|
2012-02-09 12:09:41 +03:00
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static void fill_ipc_desc(int id, IpcDescEntry *desc, const struct ipc_perm *ipcp)
|
2012-02-09 12:09:41 +03:00
|
|
|
{
|
2012-02-13 18:19:15 +03:00
|
|
|
desc->id = id;
|
|
|
|
desc->key = ipcp->KEY;
|
|
|
|
desc->uid = ipcp->uid;
|
|
|
|
desc->gid = ipcp->gid;
|
|
|
|
desc->cuid = ipcp->cuid;
|
|
|
|
desc->cgid = ipcp->cgid;
|
|
|
|
desc->mode = ipcp->mode;
|
2012-02-09 12:09:41 +03:00
|
|
|
}
|
|
|
|
|
2012-03-02 00:59:59 +04:00
|
|
|
static void pr_ipc_sem_array(unsigned int loglevel, int nr, u16 *values)
|
2012-02-14 19:54:06 +03:00
|
|
|
{
|
2012-08-11 21:39:36 +04:00
|
|
|
while (nr--)
|
2012-03-02 00:59:59 +04:00
|
|
|
print_on_level(loglevel, " %-5d", values[nr]);
|
|
|
|
print_on_level(loglevel, "\n");
|
2012-02-14 19:54:06 +03:00
|
|
|
}
|
|
|
|
|
2012-03-02 00:59:59 +04:00
|
|
|
#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)
|
|
|
|
|
2012-07-26 12:44:00 +04:00
|
|
|
static void pr_info_ipc_sem_entry(const IpcSemEntry *sem)
|
2012-02-14 19:54:06 +03:00
|
|
|
{
|
2012-07-26 12:44:00 +04:00
|
|
|
pr_ipc_desc_entry(LOG_INFO, sem->desc);
|
|
|
|
print_on_level(LOG_INFO, "nsems: %-10d\n", sem->nsems);
|
2012-02-14 19:54:06 +03:00
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static int dump_ipc_sem_set(int fd, const IpcSemEntry *entry)
|
2012-02-14 19:54:06 +03:00
|
|
|
{
|
|
|
|
int ret, size;
|
|
|
|
u16 *values;
|
|
|
|
|
|
|
|
size = sizeof(u16) * entry->nsems;
|
|
|
|
values = xmalloc(size);
|
|
|
|
if (values == NULL) {
|
|
|
|
pr_err("Failed to allocate memory for semaphore set values\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-07-19 11:31:39 +04:00
|
|
|
ret = semctl(entry->desc->id, 0, GETALL, values);
|
2012-02-14 19:54:06 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Failed to get semaphore set values");
|
|
|
|
ret = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-03-02 00:59:59 +04:00
|
|
|
pr_info_ipc_sem_array(entry->nsems, values);
|
2012-02-14 19:54:06 +03:00
|
|
|
|
|
|
|
ret = write_img_buf(fd, values, round_up(size, sizeof(u64)));
|
|
|
|
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(int fd, int id, const struct semid_ds *ds)
|
|
|
|
{
|
2012-07-19 11:31:39 +04:00
|
|
|
IpcSemEntry sem = IPC_SEM_ENTRY__INIT;
|
|
|
|
IpcDescEntry desc = IPC_DESC_ENTRY__INIT;
|
2012-02-14 19:54:06 +03:00
|
|
|
int ret;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
sem.desc = &desc;
|
2012-02-14 19:54:06 +03:00
|
|
|
sem.nsems = ds->sem_nsems;
|
2012-07-19 11:31:39 +04:00
|
|
|
|
|
|
|
fill_ipc_desc(id, sem.desc, &ds->sem_perm);
|
2012-03-02 00:59:59 +04:00
|
|
|
pr_info_ipc_sem_entry(&sem);
|
2012-02-14 19:54:06 +03:00
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
ret = pb_write_one(fd, &sem, PB_IPCNS_SEM);
|
2012-02-14 19:54:06 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to write IPC semaphores set\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return dump_ipc_sem_set(fd, &sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_ipc_sem(int fd)
|
|
|
|
{
|
|
|
|
int i, maxid;
|
|
|
|
struct seminfo info;
|
2012-05-18 15:39:00 +04:00
|
|
|
int slot;
|
2012-02-14 19:54:06 +03:00
|
|
|
|
|
|
|
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(fd, 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;
|
|
|
|
}
|
|
|
|
|
2012-07-28 09:04:51 +04:00
|
|
|
static void pr_info_ipc_msg(int nr, const IpcMsg *msg)
|
2012-02-13 20:27:35 +03:00
|
|
|
{
|
2012-07-28 09:04:51 +04:00
|
|
|
print_on_level(LOG_INFO, " %-5d: type: %-20ld size: %-10d\n",
|
2012-03-02 00:59:59 +04:00
|
|
|
nr++, msg->mtype, msg->msize);
|
2012-02-13 20:27:35 +03:00
|
|
|
}
|
|
|
|
|
2012-07-28 09:04:51 +04:00
|
|
|
static void pr_info_ipc_msg_entry(const IpcMsgEntry *msg)
|
2012-02-13 20:27:35 +03:00
|
|
|
{
|
2012-07-28 09:04:51 +04:00
|
|
|
pr_ipc_desc_entry(LOG_INFO, msg->desc);
|
|
|
|
print_on_level(LOG_INFO, "qbytes: %-10d qnum: %-10d\n",
|
2012-03-02 00:59:59 +04:00
|
|
|
msg->qbytes, msg->qnum);
|
2012-02-13 20:27:35 +03:00
|
|
|
}
|
|
|
|
|
2012-09-19 20:09:59 +04:00
|
|
|
static int dump_ipc_msg_queue_messages(int fd, const IpcMsgEntry *entry,
|
|
|
|
unsigned int msg_nr)
|
2012-02-13 20:27:35 +03:00
|
|
|
{
|
2012-09-19 20:32:30 +04:00
|
|
|
struct msgbuf *message = NULL;
|
2012-09-19 20:09:59 +04:00
|
|
|
unsigned int msgmax;
|
|
|
|
int ret, msg_cnt = 0;
|
|
|
|
struct sysctl_req req[] = {
|
|
|
|
{ "kernel/msgmax", &msgmax, CTL_U32 },
|
|
|
|
{ },
|
|
|
|
};
|
2012-02-13 20:27:35 +03:00
|
|
|
|
2012-09-19 20:09:59 +04:00
|
|
|
ret = sysctl_op(req, CTL_READ);
|
2012-02-13 20:27:35 +03:00
|
|
|
if (ret < 0) {
|
2012-09-19 20:09:59 +04:00
|
|
|
pr_err("Failed to read max IPC message size\n");
|
2012-02-13 20:27:35 +03:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-09-19 20:09:59 +04:00
|
|
|
msgmax += sizeof(struct msgbuf);
|
|
|
|
message = xmalloc(msgmax);
|
|
|
|
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++) {
|
2012-07-19 11:31:39 +04:00
|
|
|
IpcMsg msg = IPC_MSG__INIT;
|
2012-02-13 20:27:35 +03:00
|
|
|
|
2012-09-19 20:09:59 +04:00
|
|
|
ret = msgrcv(entry->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;
|
2012-02-13 20:27:35 +03:00
|
|
|
|
2012-09-19 20:09:59 +04:00
|
|
|
pr_info_ipc_msg(msg_cnt, &msg);
|
2012-02-13 20:27:35 +03:00
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
ret = pb_write_one(fd, &msg, PB_IPCNS_MSG);
|
2012-02-13 20:27:35 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to write IPC message header\n");
|
|
|
|
break;
|
|
|
|
}
|
2012-09-19 20:09:59 +04:00
|
|
|
ret = write_img_buf(fd, message->mtext, round_up(msg.msize, sizeof(u64)));
|
2012-02-13 20:27:35 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to write IPC message data\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
err:
|
2012-09-19 20:09:59 +04:00
|
|
|
xfree(message);
|
2012-02-13 20:27:35 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_ipc_msg_queue(int fd, int id, const struct msqid_ds *ds)
|
|
|
|
{
|
2012-07-19 11:31:39 +04:00
|
|
|
IpcMsgEntry msg = IPC_MSG_ENTRY__INIT;
|
|
|
|
IpcDescEntry desc = IPC_DESC_ENTRY__INIT;
|
2012-02-13 20:27:35 +03:00
|
|
|
int ret;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
msg.desc = &desc;
|
|
|
|
fill_ipc_desc(id, msg.desc, &ds->msg_perm);
|
2012-02-13 20:27:35 +03:00
|
|
|
msg.qbytes = ds->msg_qbytes;
|
|
|
|
msg.qnum = ds->msg_qnum;
|
2012-07-19 11:31:39 +04:00
|
|
|
|
2012-03-02 00:59:59 +04:00
|
|
|
pr_info_ipc_msg_entry(&msg);
|
2012-02-13 20:27:35 +03:00
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
ret = pb_write_one(fd, &msg, PB_IPCNS_MSG_ENT);
|
2012-02-13 20:27:35 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to write IPC message queue\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2012-09-19 20:09:59 +04:00
|
|
|
return dump_ipc_msg_queue_messages(fd, &msg, ds->msg_qnum);
|
2012-02-13 20:27:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_ipc_msg(int fd)
|
|
|
|
{
|
|
|
|
int i, maxid;
|
|
|
|
struct msginfo info;
|
2012-05-18 15:39:00 +04:00
|
|
|
int slot;
|
2012-02-13 20:27:35 +03:00
|
|
|
|
|
|
|
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(fd, id, &ds);
|
|
|
|
if (!ret)
|
|
|
|
slot++;
|
|
|
|
}
|
|
|
|
if (slot != info.msgpool) {
|
2012-09-19 20:09:59 +04:00
|
|
|
pr_err("Failed to collect %d message queues (only %d succeeded)\n", info.msgpool, slot);
|
2012-02-13 20:27:35 +03:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
return info.msgpool;
|
|
|
|
}
|
|
|
|
|
2012-07-28 09:04:58 +04:00
|
|
|
static void pr_info_ipc_shm(const IpcShmEntry *shm)
|
2012-02-09 12:09:41 +03:00
|
|
|
{
|
2012-07-28 09:04:58 +04:00
|
|
|
pr_ipc_desc_entry(LOG_INFO, shm->desc);
|
|
|
|
print_on_level(LOG_INFO, "size: %-10lu\n", shm->size);
|
2012-02-09 12:09:41 +03:00
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static int ipc_sysctl_req(IpcVarEntry *e, int op)
|
2012-02-02 19:55:48 +04:00
|
|
|
{
|
|
|
|
struct sysctl_req req[] = {
|
2012-07-19 11:31:39 +04:00
|
|
|
{ "kernel/sem", e->sem_ctls, CTL_U32A(e->n_sem_ctls) },
|
2012-02-02 19:55:48 +04:00
|
|
|
{ "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-09 12:09:41 +03:00
|
|
|
/*
|
|
|
|
* TODO: Function below should be later improved to locate and dump only dirty
|
|
|
|
* pages via updated sys_mincore().
|
|
|
|
*/
|
2012-07-19 11:31:39 +04:00
|
|
|
static int dump_ipc_shm_pages(int fd, const IpcShmEntry *shm)
|
2012-01-31 22:28:55 +04:00
|
|
|
{
|
2012-02-09 12:09:41 +03:00
|
|
|
void *data;
|
2012-01-31 22:28:55 +04:00
|
|
|
int ret;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
data = shmat(shm->desc->id, NULL, SHM_RDONLY);
|
2012-02-09 12:09:41 +03:00
|
|
|
if (data == (void *)-1) {
|
|
|
|
pr_perror("Failed to attach IPC shared memory");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
ret = write_img_buf(fd, 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;
|
|
|
|
}
|
2012-01-31 22:28:55 +04:00
|
|
|
|
2012-02-09 12:09:41 +03:00
|
|
|
static int dump_ipc_shm_seg(int fd, int id, const struct shmid_ds *ds)
|
|
|
|
{
|
2012-07-19 11:31:39 +04:00
|
|
|
IpcShmEntry shm = IPC_SHM_ENTRY__INIT;
|
|
|
|
IpcDescEntry desc = IPC_DESC_ENTRY__INIT;
|
2012-02-09 12:09:41 +03:00
|
|
|
int ret;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
shm.desc = &desc;
|
2012-02-09 12:09:41 +03:00
|
|
|
shm.size = ds->shm_segsz;
|
2012-07-19 11:31:39 +04:00
|
|
|
fill_ipc_desc(id, shm.desc, &ds->shm_perm);
|
2012-03-02 00:59:59 +04:00
|
|
|
pr_info_ipc_shm(&shm);
|
2012-02-09 12:09:41 +03:00
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
ret = pb_write_one(fd, &shm, PB_IPCNS_SHM);
|
2012-02-09 12:09:41 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to write IPC shared memory segment\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return dump_ipc_shm_pages(fd, &shm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dump_ipc_shm(int fd)
|
|
|
|
{
|
|
|
|
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;
|
2012-01-31 22:28:55 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 12:09:41 +03:00
|
|
|
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(fd, 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;
|
|
|
|
}
|
2012-01-31 22:28:55 +04:00
|
|
|
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-07-19 11:31:39 +04:00
|
|
|
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;
|
2012-02-08 12:13:38 +03:00
|
|
|
|
|
|
|
ret = ipc_sysctl_req(&var, CTL_READ);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to read IPC variables\n");
|
2012-07-19 11:31:39 +04:00
|
|
|
goto err;
|
2012-02-08 12:13:38 +03:00
|
|
|
}
|
|
|
|
|
2012-08-07 02:26:50 +04:00
|
|
|
ret = pb_write_one(fd, &var, PB_IPCNS_VAR);
|
2012-02-08 12:13:38 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to write IPC variables\n");
|
2012-07-19 11:31:39 +04:00
|
|
|
goto err;
|
2012-02-08 12:13:38 +03:00
|
|
|
}
|
2012-07-19 11:31:39 +04:00
|
|
|
|
|
|
|
err:
|
|
|
|
xfree(var.sem_ctls);
|
|
|
|
return ret;
|
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-03-26 17:43:29 +04:00
|
|
|
ret = dump_ipc_var(fdset_fd(fdset, CR_FD_IPCNS_VAR));
|
2012-01-31 22:28:55 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-03-26 17:43:29 +04:00
|
|
|
ret = dump_ipc_shm(fdset_fd(fdset, CR_FD_IPCNS_SHM));
|
2012-01-31 22:28:55 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-03-26 17:43:29 +04:00
|
|
|
ret = dump_ipc_msg(fdset_fd(fdset, CR_FD_IPCNS_MSG));
|
2012-01-31 22:28:55 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-03-26 17:43:29 +04:00
|
|
|
ret = dump_ipc_sem(fdset_fd(fdset, CR_FD_IPCNS_SEM));
|
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
|
|
|
{
|
2012-05-18 15:39:00 +04:00
|
|
|
int ret;
|
2012-01-31 22:28:55 +04:00
|
|
|
|
2012-08-02 07:55:05 +04:00
|
|
|
ret = switch_ns(ns_pid, CLONE_NEWIPC, "ipc", NULL);
|
2012-02-03 16:48:44 +03:00
|
|
|
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-07-26 12:44:00 +04:00
|
|
|
static void ipc_sem_handler(int fd, void *obj, int show_pages_content)
|
2012-02-14 19:54:13 +03:00
|
|
|
{
|
2012-07-26 12:44:00 +04:00
|
|
|
IpcSemEntry *e = obj;
|
2012-07-19 11:31:39 +04:00
|
|
|
u16 *values;
|
2012-07-26 12:44:00 +04:00
|
|
|
int size;
|
2012-07-19 11:31:39 +04:00
|
|
|
|
2012-07-26 12:44:00 +04:00
|
|
|
pr_msg("\n");
|
|
|
|
size = sizeof(u16) * e->nsems;
|
|
|
|
values = xmalloc(size);
|
|
|
|
if (values == NULL)
|
|
|
|
return;
|
|
|
|
if (read_img_buf(fd, values, round_up(size, sizeof(u64))) <= 0)
|
|
|
|
return;
|
|
|
|
pr_msg_ipc_sem_array(e->nsems, values);
|
2012-02-14 19:54:13 +03:00
|
|
|
}
|
|
|
|
|
2012-03-27 12:01:14 +04:00
|
|
|
void show_ipc_sem(int fd, struct cr_options *o)
|
2012-02-14 19:54:13 +03:00
|
|
|
{
|
2012-08-07 02:51:34 +04:00
|
|
|
pb_show_plain_payload(fd, PB_IPCNS_SEM, ipc_sem_handler, 0);
|
2012-02-14 19:54:13 +03:00
|
|
|
}
|
|
|
|
|
2012-07-28 09:04:51 +04:00
|
|
|
static void ipc_msg_data_handler(int fd, void *obj, int show_pages_content)
|
2012-02-13 20:27:42 +03:00
|
|
|
{
|
2012-07-28 09:04:51 +04:00
|
|
|
IpcMsg *e = obj;
|
2012-02-13 20:27:42 +03:00
|
|
|
|
2012-07-28 09:04:51 +04:00
|
|
|
if (show_pages_content) {
|
|
|
|
pr_msg("\n");
|
|
|
|
print_image_data(fd, round_up(e->msize, sizeof(u64)));
|
|
|
|
} else
|
|
|
|
lseek(fd, round_up(e->msize, sizeof(u64)), SEEK_CUR);
|
|
|
|
}
|
2012-02-13 20:27:42 +03:00
|
|
|
|
2012-07-28 09:04:51 +04:00
|
|
|
static void ipc_msg_handler(int fd, void *obj, int show_pages_content)
|
|
|
|
{
|
|
|
|
IpcMsgEntry *e = obj;
|
|
|
|
int msg_nr = 0;
|
2012-07-19 11:31:39 +04:00
|
|
|
|
2012-07-28 09:04:51 +04:00
|
|
|
pr_msg("\n");
|
|
|
|
while (msg_nr++ < e->qnum)
|
2012-08-07 02:51:34 +04:00
|
|
|
pb_show_plain_payload(fd, PB_IPCNS_MSG, ipc_msg_data_handler,
|
2012-07-28 09:04:51 +04:00
|
|
|
show_pages_content);
|
2012-07-19 11:31:39 +04:00
|
|
|
|
2012-02-13 20:27:42 +03:00
|
|
|
}
|
|
|
|
|
2012-03-27 12:01:14 +04:00
|
|
|
void show_ipc_msg(int fd, struct cr_options *o)
|
2012-02-13 20:27:42 +03:00
|
|
|
{
|
2012-08-07 02:51:34 +04:00
|
|
|
pb_show_plain_payload(fd, PB_IPCNS_MSG_ENT, ipc_msg_handler, o->show_pages_content);
|
2012-02-13 20:27:42 +03:00
|
|
|
}
|
|
|
|
|
2012-07-28 09:04:58 +04:00
|
|
|
static void ipc_shm_handler(int fd, void *obj, int show_pages_content)
|
2012-02-09 12:09:48 +03:00
|
|
|
{
|
2012-07-28 09:04:58 +04:00
|
|
|
IpcShmEntry *e = obj;
|
2012-02-09 12:09:48 +03:00
|
|
|
|
2012-07-28 09:04:58 +04:00
|
|
|
if (show_pages_content) {
|
|
|
|
pr_msg("\n");
|
|
|
|
print_image_data(fd, round_up(e->size, sizeof(u64)));
|
|
|
|
} else
|
|
|
|
lseek(fd, round_up(e->size, sizeof(u32)), SEEK_CUR);
|
2012-02-09 12:09:48 +03:00
|
|
|
}
|
|
|
|
|
2012-03-27 12:01:14 +04:00
|
|
|
void show_ipc_shm(int fd, struct cr_options *o)
|
2012-02-09 12:09:48 +03:00
|
|
|
{
|
2012-08-07 02:51:34 +04:00
|
|
|
pb_show_plain_payload(fd, PB_IPCNS_SHM, ipc_shm_handler,
|
2012-07-28 09:04:58 +04:00
|
|
|
o->show_pages_content);
|
2012-02-09 12:09:48 +03:00
|
|
|
}
|
|
|
|
|
2012-03-27 12:01:14 +04:00
|
|
|
void show_ipc_var(int fd, struct cr_options *o)
|
2012-01-31 22:29:11 +04:00
|
|
|
{
|
2012-08-07 02:51:34 +04:00
|
|
|
pb_show_vertical(fd, PB_IPCNS_VAR);
|
2012-01-31 22:29:11 +04:00
|
|
|
}
|
2012-01-31 22:29:22 +04:00
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static int prepare_ipc_sem_values(int fd, const IpcSemEntry *entry)
|
2012-02-14 19:54:20 +03:00
|
|
|
{
|
|
|
|
int ret, size;
|
|
|
|
u16 *values;
|
|
|
|
|
|
|
|
size = sizeof(u16) * entry->nsems;
|
|
|
|
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(fd, values, round_up(size, sizeof(u64)));
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to allocate memory for semaphores set values\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-03-02 00:59:59 +04:00
|
|
|
pr_info_ipc_sem_array(entry->nsems, values);
|
2012-02-14 19:54:20 +03:00
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
ret = semctl(entry->desc->id, 0, SETALL, values);
|
2012-02-14 19:54:20 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Failed to set semaphores set values");
|
|
|
|
ret = -errno;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
xfree(values);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static int prepare_ipc_sem_desc(int fd, const IpcSemEntry *entry)
|
2012-02-14 19:54:20 +03:00
|
|
|
{
|
|
|
|
int ret, id;
|
|
|
|
struct semid_ds ds;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
id = semget(entry->desc->id, entry->nsems,
|
|
|
|
entry->desc->mode | IPC_CREAT | IPC_EXCL | IPC_PRESET);
|
2012-02-14 19:54:20 +03:00
|
|
|
if (id == -1) {
|
|
|
|
pr_perror("Failed to create sem set");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
if (id != entry->desc->id) {
|
2012-02-14 19:54:20 +03:00
|
|
|
pr_err("Failed to preset id (%d instead of %d)\n",
|
2012-07-19 11:31:39 +04:00
|
|
|
id, entry->desc->id);
|
2012-02-14 19:54:20 +03:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = semctl(id, 0, SEM_STAT, &ds);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Failed to stat sem set");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
ds.sem_perm.KEY = entry->desc->key;
|
2012-02-14 19:54:20 +03:00
|
|
|
ret = semctl(id, 0, SEM_SET, &ds);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Failed to update sem key");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
ret = prepare_ipc_sem_values(fd, entry);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to update sem pages\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prepare_ipc_sem(int pid)
|
|
|
|
{
|
2012-10-24 16:51:50 +04:00
|
|
|
int fd, ret;
|
2012-02-14 19:54:20 +03:00
|
|
|
|
|
|
|
pr_info("Restoring IPC semaphores sets\n");
|
|
|
|
fd = open_image_ro(CR_FD_IPCNS_SEM, pid);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (1) {
|
2012-07-19 11:31:39 +04:00
|
|
|
IpcSemEntry *entry;
|
2012-02-14 19:54:20 +03:00
|
|
|
|
2012-08-07 02:42:58 +04:00
|
|
|
ret = pb_read_one_eof(fd, &entry, PB_IPCNS_SEM);
|
2012-10-24 16:51:50 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto err;
|
|
|
|
}
|
2012-02-14 19:54:20 +03:00
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
pr_info_ipc_sem_entry(entry);
|
|
|
|
|
|
|
|
ret = prepare_ipc_sem_desc(fd, entry);
|
|
|
|
ipc_sem_entry__free_unpacked(entry, NULL);
|
2012-02-14 19:54:20 +03:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to prepare semaphores set\n");
|
2012-10-24 16:51:50 +04:00
|
|
|
goto err;
|
2012-02-14 19:54:20 +03:00
|
|
|
}
|
|
|
|
}
|
2012-10-24 16:51:50 +04:00
|
|
|
|
2012-02-17 12:02:49 +03:00
|
|
|
return close_safe(&fd);
|
2012-10-24 16:51:50 +04:00
|
|
|
err:
|
|
|
|
close_safe(&fd);
|
|
|
|
return ret;
|
2012-02-14 19:54:20 +03:00
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static int prepare_ipc_msg_queue_messages(int fd, const IpcMsgEntry *entry)
|
2012-02-13 20:27:49 +03:00
|
|
|
{
|
2012-07-19 11:31:39 +04:00
|
|
|
IpcMsg *msg = NULL;
|
2012-02-13 20:27:49 +03:00
|
|
|
int msg_nr = 0;
|
2012-07-19 11:31:39 +04:00
|
|
|
int ret = 0;
|
2012-02-13 20:27:49 +03:00
|
|
|
|
|
|
|
while (msg_nr < entry->qnum) {
|
|
|
|
struct msgbuf {
|
|
|
|
long mtype;
|
|
|
|
char mtext[MSGMAX];
|
|
|
|
} data;
|
|
|
|
|
2012-08-07 02:42:58 +04:00
|
|
|
ret = pb_read_one(fd, &msg, PB_IPCNS_MSG);
|
2012-02-13 20:27:49 +03:00
|
|
|
if (ret <= 0)
|
|
|
|
return -EIO;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
pr_info_ipc_msg(msg_nr, msg);
|
2012-02-13 20:27:49 +03:00
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
if (msg->msize > MSGMAX) {
|
|
|
|
ret = -1;
|
2012-02-13 20:27:49 +03:00
|
|
|
pr_err("Unsupported message size: %d (MAX: %d)\n",
|
2012-07-19 11:31:39 +04:00
|
|
|
msg->msize, MSGMAX);
|
|
|
|
break;
|
2012-02-13 20:27:49 +03:00
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
ret = read_img_buf(fd, data.mtext, round_up(msg->msize, sizeof(u64)));
|
2012-02-13 20:27:49 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to read IPC message data\n");
|
2012-07-19 11:31:39 +04:00
|
|
|
break;
|
2012-02-13 20:27:49 +03:00
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
data.mtype = msg->mtype;
|
|
|
|
ret = msgsnd(entry->desc->id, &data, msg->msize, IPC_NOWAIT);
|
2012-02-13 20:27:49 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Failed to send IPC message");
|
2012-07-19 11:31:39 +04:00
|
|
|
ret = -errno;
|
|
|
|
break;
|
2012-02-13 20:27:49 +03:00
|
|
|
}
|
|
|
|
msg_nr++;
|
|
|
|
}
|
2012-07-19 11:31:39 +04:00
|
|
|
|
|
|
|
if (msg)
|
|
|
|
ipc_msg__free_unpacked(msg, NULL);
|
|
|
|
return ret;
|
2012-02-13 20:27:49 +03:00
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static int prepare_ipc_msg_queue(int fd, const IpcMsgEntry *entry)
|
2012-02-13 20:27:49 +03:00
|
|
|
{
|
|
|
|
int ret, id;
|
|
|
|
struct msqid_ds ds;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
id = msgget(entry->desc->id,
|
|
|
|
entry->desc->mode | IPC_CREAT | IPC_EXCL | IPC_PRESET);
|
2012-02-13 20:27:49 +03:00
|
|
|
if (id == -1) {
|
|
|
|
pr_perror("Failed to create message queue");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
if (id != entry->desc->id) {
|
2012-02-13 20:27:49 +03:00
|
|
|
pr_err("Failed to preset id (%d instead of %d)\n",
|
2012-07-19 11:31:39 +04:00
|
|
|
id, entry->desc->id);
|
2012-02-13 20:27:49 +03:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = msgctl(id, MSG_STAT, &ds);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Failed to stat message queue");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
ds.msg_perm.KEY = entry->desc->key;
|
2012-02-13 20:27:49 +03:00
|
|
|
ds.msg_qbytes = entry->qbytes;
|
|
|
|
ret = msgctl(id, MSG_SET, &ds);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Failed to update message key");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
ret = prepare_ipc_msg_queue_messages(fd, entry);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to update message queue messages\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prepare_ipc_msg(int pid)
|
|
|
|
{
|
2012-10-24 16:51:50 +04:00
|
|
|
int fd, ret;
|
2012-02-13 20:27:49 +03:00
|
|
|
|
|
|
|
pr_info("Restoring IPC message queues\n");
|
|
|
|
fd = open_image_ro(CR_FD_IPCNS_MSG, pid);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (1) {
|
2012-07-19 11:31:39 +04:00
|
|
|
IpcMsgEntry *entry;
|
2012-02-13 20:27:49 +03:00
|
|
|
|
2012-08-07 02:42:58 +04:00
|
|
|
ret = pb_read_one_eof(fd, &entry, PB_IPCNS_MSG_ENT);
|
2012-02-13 20:27:49 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to read IPC messages queue\n");
|
2012-10-24 16:51:50 +04:00
|
|
|
ret = -EIO;
|
|
|
|
goto err;
|
2012-02-13 20:27:49 +03:00
|
|
|
}
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
pr_info_ipc_msg_entry(entry);
|
|
|
|
|
|
|
|
ret = prepare_ipc_msg_queue(fd, entry);
|
|
|
|
ipc_msg_entry__free_unpacked(entry, NULL);
|
2012-02-13 20:27:49 +03:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to prepare messages queue\n");
|
2012-10-24 16:51:50 +04:00
|
|
|
goto err;
|
2012-02-13 20:27:49 +03:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 12:02:49 +03:00
|
|
|
return close_safe(&fd);
|
2012-10-24 16:51:50 +04:00
|
|
|
err:
|
|
|
|
close_safe(&fd);
|
|
|
|
return ret;
|
2012-02-13 20:27:49 +03:00
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static int prepare_ipc_shm_pages(int fd, const IpcShmEntry *shm)
|
2012-02-09 12:09:55 +03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
void *data;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
data = shmat(shm->desc->id, NULL, 0);
|
2012-02-09 12:09:55 +03:00
|
|
|
if (data == (void *)-1) {
|
|
|
|
pr_perror("Failed to attach IPC shared memory");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
ret = read_img_buf(fd, 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;
|
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
static int prepare_ipc_shm_seg(int fd, const IpcShmEntry *shm)
|
2012-02-09 12:09:55 +03:00
|
|
|
{
|
|
|
|
int ret, id;
|
|
|
|
struct shmid_ds ds;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
id = shmget(shm->desc->id, shm->size,
|
|
|
|
shm->desc->mode | IPC_CREAT | IPC_EXCL | IPC_PRESET);
|
2012-02-09 12:09:55 +03:00
|
|
|
if (id == -1) {
|
|
|
|
pr_perror("Failed to create shm segment");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
if (id != shm->desc->id) {
|
2012-02-09 12:09:55 +03:00
|
|
|
pr_err("Failed to preset id (%d instead of %d)\n",
|
2012-07-19 11:31:39 +04:00
|
|
|
id, shm->desc->id);
|
2012-02-09 12:09:55 +03:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = shmctl(id, SHM_STAT, &ds);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Failed to stat shm segment");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
ds.shm_perm.KEY = shm->desc->key;
|
2012-02-09 12:09:55 +03:00
|
|
|
ret = shmctl(id, SHM_SET, &ds);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Failed to update shm key");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
ret = prepare_ipc_shm_pages(fd, shm);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to update shm pages\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prepare_ipc_shm(int pid)
|
|
|
|
{
|
2012-10-24 16:51:50 +04:00
|
|
|
int fd, ret;
|
2012-02-09 12:09:55 +03:00
|
|
|
|
|
|
|
pr_info("Restoring IPC shared memory\n");
|
|
|
|
fd = open_image_ro(CR_FD_IPCNS_SHM, pid);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (1) {
|
2012-07-19 11:31:39 +04:00
|
|
|
IpcShmEntry *shm;
|
2012-02-09 12:09:55 +03:00
|
|
|
|
2012-08-07 02:42:58 +04:00
|
|
|
ret = pb_read_one_eof(fd, &shm, PB_IPCNS_SHM);
|
2012-02-10 13:16:49 +03:00
|
|
|
if (ret < 0) {
|
2012-02-13 18:19:15 +03:00
|
|
|
pr_err("Failed to read IPC shared memory segment\n");
|
2012-10-24 16:51:50 +04:00
|
|
|
ret = -EIO;
|
|
|
|
goto err;
|
2012-02-10 13:16:49 +03:00
|
|
|
}
|
2012-02-09 12:09:55 +03:00
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
pr_info_ipc_shm(shm);
|
|
|
|
|
|
|
|
ret = prepare_ipc_shm_seg(fd, shm);
|
|
|
|
ipc_shm_entry__free_unpacked(shm, NULL);
|
2012-02-09 12:09:55 +03:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to prepare shm segment\n");
|
2012-10-24 16:51:50 +04:00
|
|
|
goto err;
|
2012-02-09 12:09:55 +03:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 12:02:49 +03:00
|
|
|
return close_safe(&fd);
|
2012-10-24 16:51:50 +04:00
|
|
|
err:
|
|
|
|
close_safe(&fd);
|
|
|
|
return ret;
|
2012-02-09 12:09:55 +03: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;
|
2012-07-19 11:31:39 +04:00
|
|
|
IpcVarEntry *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-08-07 02:42:58 +04:00
|
|
|
ret = pb_read_one(fd, &var, PB_IPCNS_VAR);
|
2012-10-26 00:08:47 +04:00
|
|
|
close_safe(&fd);
|
2012-02-10 13:42:50 +03:00
|
|
|
if (ret <= 0) {
|
|
|
|
pr_err("Failed to read IPC namespace variables\n");
|
2012-10-26 00:08:47 +04:00
|
|
|
return -EFAULT;
|
2012-02-10 13:42:50 +03:00
|
|
|
}
|
2012-02-08 12:13:38 +03:00
|
|
|
|
2012-07-19 11:31:39 +04:00
|
|
|
ipc_sysctl_req(var, CTL_PRINT);
|
|
|
|
|
|
|
|
ret = ipc_sysctl_req(var, CTL_WRITE);
|
|
|
|
ipc_var_entry__free_unpacked(var, NULL);
|
2012-02-08 12:13:38 +03:00
|
|
|
|
2012-02-17 12:02:49 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Failed to prepare IPC namespace variables\n");
|
2012-10-26 00:08:47 +04:00
|
|
|
return -EFAULT;
|
2012-02-17 12:02:49 +03:00
|
|
|
}
|
2012-10-26 00:08:47 +04:00
|
|
|
|
|
|
|
return 0;
|
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);
|
2012-02-09 12:09:55 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = prepare_ipc_shm(pid);
|
2012-02-13 20:27:49 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = prepare_ipc_msg(pid);
|
2012-02-14 19:54:20 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = prepare_ipc_sem(pid);
|
2012-02-08 12:13:38 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return 0;
|
2012-01-31 22:29:22 +04:00
|
|
|
}
|