mirror of
https://github.com/checkpoint-restore/criu
synced 2025-08-29 13:28:27 +00:00
pb: Rewrite object reading to use pb-descs
The pb_read thing is no longer a macros. This will allow to factor out objects collecting on restore. Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
This commit is contained in:
parent
2398c55e41
commit
b1b0a39a58
18
cr-restore.c
18
cr-restore.c
@ -158,7 +158,7 @@ static int read_and_open_vmas(int pid, struct list_head *vmas, int *nr_vmas)
|
||||
|
||||
(*nr_vmas)++;
|
||||
list_add_tail(&vma->list, vmas);
|
||||
ret = pb_read_eof(fd, &e, vma_entry);
|
||||
ret = pb_read_one_eof(fd, &e, PB_VMAS);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
@ -222,7 +222,7 @@ static int prepare_sigactions(int pid)
|
||||
if (sig == SIGKILL || sig == SIGSTOP)
|
||||
continue;
|
||||
|
||||
ret = pb_read(fd_sigact, &e, sa_entry);
|
||||
ret = pb_read_one(fd_sigact, &e, PB_SIGACT);
|
||||
if (ret < 0)
|
||||
break;
|
||||
|
||||
@ -439,7 +439,7 @@ static int restore_one_task(int pid)
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
ret = pb_read(fd, &core, core_entry);
|
||||
ret = pb_read_one(fd, &core, PB_CORE);
|
||||
close(fd);
|
||||
|
||||
if (ret < 0)
|
||||
@ -1021,7 +1021,7 @@ static int prepare_itimers(int pid, struct task_restore_core_args *args)
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
ret = pb_read(fd, &ie, itimer_entry);
|
||||
ret = pb_read_one(fd, &ie, PB_ITIMERS);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
ret = itimer_restore_and_fix("real", ie, &args->itimers[0]);
|
||||
@ -1029,7 +1029,7 @@ static int prepare_itimers(int pid, struct task_restore_core_args *args)
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
|
||||
ret = pb_read(fd, &ie, itimer_entry);
|
||||
ret = pb_read_one(fd, &ie, PB_ITIMERS);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
ret = itimer_restore_and_fix("virt", ie, &args->itimers[1]);
|
||||
@ -1037,7 +1037,7 @@ static int prepare_itimers(int pid, struct task_restore_core_args *args)
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
|
||||
ret = pb_read(fd, &ie, itimer_entry);
|
||||
ret = pb_read_one(fd, &ie, PB_ITIMERS);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
ret = itimer_restore_and_fix("prof", ie, &args->itimers[2]);
|
||||
@ -1064,7 +1064,7 @@ static int prepare_creds(int pid, struct task_restore_core_args *args)
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
ret = pb_read(fd, &ce, creds_entry);
|
||||
ret = pb_read_one(fd, &ce, PB_CREDS);
|
||||
close_safe(&fd);
|
||||
|
||||
if (ret < 0)
|
||||
@ -1121,7 +1121,7 @@ static int prepare_mm(pid_t pid, struct task_restore_core_args *args)
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
if (pb_read(fd, &mm, mm_entry) < 0)
|
||||
if (pb_read_one(fd, &mm, PB_MM) < 0)
|
||||
return -1;
|
||||
|
||||
args->mm = *mm;
|
||||
@ -1360,7 +1360,7 @@ static int sigreturn_restore(pid_t pid, CoreEntry *core, struct list_head *tgt_v
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = pb_read(fd_core, &core, core_entry);
|
||||
ret = pb_read_one(fd_core, &core, PB_CORE);
|
||||
close(fd_core);
|
||||
|
||||
if (core->tc || core->ids) {
|
||||
|
@ -232,19 +232,19 @@ void show_itimers(int fd, struct cr_options *o)
|
||||
|
||||
pr_img_head(CR_FD_ITIMERS);
|
||||
|
||||
ret = pb_read(fd, &ie, itimer_entry);
|
||||
ret = pb_read_one(fd, &ie, PB_ITIMERS);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
show_itimer("real", ie);
|
||||
itimer_entry__free_unpacked(ie, NULL);
|
||||
|
||||
ret = pb_read(fd, &ie, itimer_entry);
|
||||
ret = pb_read_one(fd, &ie, PB_ITIMERS);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
show_itimer("virt", ie);
|
||||
itimer_entry__free_unpacked(ie, NULL);
|
||||
|
||||
ret = pb_read(fd, &ie, itimer_entry);
|
||||
ret = pb_read_one(fd, &ie, PB_ITIMERS);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
show_itimer("prof", ie);
|
||||
|
@ -136,7 +136,7 @@ int collect_eventfd(void)
|
||||
if (!info)
|
||||
break;
|
||||
|
||||
ret = pb_read_eof(image_fd, &info->efe, eventfd_file_entry);
|
||||
ret = pb_read_one_eof(image_fd, &info->efe, PB_EVENTFD);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
else if (!ret)
|
||||
|
@ -164,7 +164,7 @@ int collect_eventpoll(void)
|
||||
if (!info)
|
||||
goto err;
|
||||
|
||||
ret = pb_read_eof(image_fd, &info->tdefe, eventpoll_tfd_entry);
|
||||
ret = pb_read_one_eof(image_fd, &info->tdefe, PB_EVENTPOLL_TFD);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
else if (!ret)
|
||||
@ -190,7 +190,7 @@ int collect_eventpoll(void)
|
||||
if (!info)
|
||||
goto err;
|
||||
|
||||
ret = pb_read_eof(image_fd, &info->efe, eventpoll_file_entry);
|
||||
ret = pb_read_one_eof(image_fd, &info->efe, PB_EVENTPOLL);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
else if (!ret)
|
||||
|
2
fifo.c
2
fifo.c
@ -138,7 +138,7 @@ int collect_fifo(void)
|
||||
if (!info)
|
||||
break;
|
||||
|
||||
ret = pb_read_eof(img, &info->fe, fifo_entry);
|
||||
ret = pb_read_one_eof(img, &info->fe, PB_FIFO);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
@ -88,7 +88,7 @@ static int open_remap_ghost(struct reg_file_info *rfi,
|
||||
if (ifd < 0)
|
||||
goto err;
|
||||
|
||||
if (pb_read(ifd, &gfe, ghost_file_entry) < 0)
|
||||
if (pb_read_one(ifd, &gfe, PB_GHOST_FILE) < 0)
|
||||
goto err;
|
||||
|
||||
snprintf(gf->path, PATH_MAX, "%s.cr.%x.ghost", rfi->path, rfe->remap_id);
|
||||
@ -149,7 +149,7 @@ static int collect_remaps(void)
|
||||
struct file_desc *fdesc;
|
||||
struct reg_file_info *rfi;
|
||||
|
||||
ret = pb_read_eof(fd, &rfe, remap_file_path_entry);
|
||||
ret = pb_read_one_eof(fd, &rfe, PB_REMAP_FPATH);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
@ -442,7 +442,7 @@ int collect_reg_files(void)
|
||||
|
||||
rfi->path = NULL;
|
||||
|
||||
ret = pb_read_eof(fd, &rfe, reg_file_entry);
|
||||
ret = pb_read_one_eof(fd, &rfe, PB_REG_FILES);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
4
files.c
4
files.c
@ -215,7 +215,7 @@ int prepare_fd_pid(int pid, struct rst_info *rst_info)
|
||||
while (1) {
|
||||
FdinfoEntry *e;
|
||||
|
||||
ret = pb_read_eof(fdinfo_fd, &e, fdinfo_entry);
|
||||
ret = pb_read_one_eof(fdinfo_fd, &e, PB_FDINFO);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
@ -505,7 +505,7 @@ int prepare_fs(int pid)
|
||||
if (ifd < 0)
|
||||
return -1;
|
||||
|
||||
if (pb_read(ifd, &fe, fs_entry) < 0)
|
||||
if (pb_read_one(ifd, &fe, PB_FS) < 0)
|
||||
return -1;
|
||||
|
||||
cwd = open_reg_by_id(fe->cwd_id);
|
||||
|
4
image.c
4
image.c
@ -23,7 +23,7 @@ int check_img_inventory(void)
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
ret = pb_read(fd, &he, inventory_entry);
|
||||
ret = pb_read_one(fd, &he, PB_INVENTORY);
|
||||
close(fd);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
@ -63,7 +63,7 @@ static void show_inventory(int fd, struct cr_options *o)
|
||||
{
|
||||
InventoryEntry *he;
|
||||
|
||||
if (pb_read(fd, &he, inventory_entry) < 0)
|
||||
if (pb_read_one(fd, &he, PB_INVENTORY) < 0)
|
||||
return;
|
||||
|
||||
pr_msg("Version: %u\n", he->img_version);
|
||||
|
@ -62,22 +62,12 @@ typedef size_t (*pb_pack_t)(void *obj, void *where);
|
||||
typedef void *(*pb_unpack_t)(void *allocator, size_t size, void *from);
|
||||
typedef void (*pb_free_t)(void *obj, void *allocator);
|
||||
|
||||
extern int pb_read_object_with_header(int fd, void **pobj,
|
||||
pb_unpack_t unpack,
|
||||
bool eof);
|
||||
|
||||
#define PB_UNPACK_TYPECHECK(__op, __fn) ({ if (0) *__op = __fn##__unpack(NULL, 0, NULL); (pb_unpack_t)&__fn##__unpack; })
|
||||
#define PB_FREE_TYPECHECK(__o, __fn) ({ if (0) __fn##__free_unpacked(__o, NULL); (pb_free_t)&__fn##__free_unpacked; })
|
||||
|
||||
void cr_pb_init(void);
|
||||
|
||||
#define pb_read(__fd, __obj_pptr, __proto_message_name) \
|
||||
pb_read_object_with_header(__fd, (void **)__obj_pptr, \
|
||||
PB_UNPACK_TYPECHECK(__obj_pptr, __proto_message_name), false)
|
||||
extern int do_pb_read_one(int fd, void **objp, int type, bool eof);
|
||||
|
||||
#define pb_read_eof(__fd, __obj_pptr, __proto_message_name) \
|
||||
pb_read_object_with_header(__fd, (void **)__obj_pptr, \
|
||||
PB_UNPACK_TYPECHECK(__obj_pptr, __proto_message_name), true)
|
||||
#define pb_read_one(fd, objp, type) do_pb_read_one(fd, (void **)objp, type, false)
|
||||
#define pb_read_one_eof(fd, objp, type) do_pb_read_one(fd, (void **)objp, type, true)
|
||||
|
||||
extern int pb_write_one(int fd, void *obj, int type);
|
||||
|
||||
|
@ -228,7 +228,7 @@ int collect_inotify(void)
|
||||
if (!info)
|
||||
return -1;
|
||||
|
||||
ret = pb_read_eof(image_fd, &info->ife, inotify_file_entry);
|
||||
ret = pb_read_one_eof(image_fd, &info->ife, PB_INOTIFY);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
else if (!ret)
|
||||
@ -254,7 +254,7 @@ int collect_inotify(void)
|
||||
if (!mark)
|
||||
goto err;
|
||||
|
||||
ret = pb_read_eof(image_fd, &mark->iwe, inotify_wd_entry);
|
||||
ret = pb_read_one_eof(image_fd, &mark->iwe, PB_INOTIFY_WD);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
else if (!ret)
|
||||
|
10
ipc_ns.c
10
ipc_ns.c
@ -622,7 +622,7 @@ static int prepare_ipc_sem(int pid)
|
||||
int ret;
|
||||
IpcSemEntry *entry;
|
||||
|
||||
ret = pb_read_eof(fd, &entry, ipc_sem_entry);
|
||||
ret = pb_read_one_eof(fd, &entry, PB_IPCNS_SEM);
|
||||
if (ret < 0)
|
||||
return -EIO;
|
||||
if (ret == 0)
|
||||
@ -653,7 +653,7 @@ static int prepare_ipc_msg_queue_messages(int fd, const IpcMsgEntry *entry)
|
||||
char mtext[MSGMAX];
|
||||
} data;
|
||||
|
||||
ret = pb_read(fd, &msg, ipc_msg);
|
||||
ret = pb_read_one(fd, &msg, PB_IPCNS_MSG);
|
||||
if (ret <= 0)
|
||||
return -EIO;
|
||||
|
||||
@ -739,7 +739,7 @@ static int prepare_ipc_msg(int pid)
|
||||
int ret;
|
||||
IpcMsgEntry *entry;
|
||||
|
||||
ret = pb_read_eof(fd, &entry, ipc_msg_entry);
|
||||
ret = pb_read_one_eof(fd, &entry, PB_IPCNS_MSG_ENT);
|
||||
if (ret < 0) {
|
||||
pr_err("Failed to read IPC messages queue\n");
|
||||
return -EIO;
|
||||
@ -833,7 +833,7 @@ static int prepare_ipc_shm(int pid)
|
||||
int ret;
|
||||
IpcShmEntry *shm;
|
||||
|
||||
ret = pb_read_eof(fd, &shm, ipc_shm_entry);
|
||||
ret = pb_read_one_eof(fd, &shm, PB_IPCNS_SHM);
|
||||
if (ret < 0) {
|
||||
pr_err("Failed to read IPC shared memory segment\n");
|
||||
return -EIO;
|
||||
@ -864,7 +864,7 @@ static int prepare_ipc_var(int pid)
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
ret = pb_read(fd, &var, ipc_var_entry);
|
||||
ret = pb_read_one(fd, &var, PB_IPCNS_VAR);
|
||||
if (ret <= 0) {
|
||||
pr_err("Failed to read IPC namespace variables\n");
|
||||
return -EFAULT;
|
||||
|
2
mount.c
2
mount.c
@ -464,7 +464,7 @@ static int populate_mnt_ns(int ns_pid)
|
||||
while (1) {
|
||||
struct mount_info *pm;
|
||||
|
||||
ret = pb_read_eof(img, &me, mnt_entry);
|
||||
ret = pb_read_one_eof(img, &me, PB_MOUNTPOINTS);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
2
net.c
2
net.c
@ -155,7 +155,7 @@ static int restore_links(int pid)
|
||||
}
|
||||
|
||||
while (1) {
|
||||
ret = pb_read_eof(fd, &nde, net_device_entry);
|
||||
ret = pb_read_one_eof(fd, &nde, PB_NETDEV);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
4
pipes.c
4
pipes.c
@ -78,7 +78,7 @@ int collect_pipe_data(int img_type, struct pipe_data_rst **hash)
|
||||
if (!r)
|
||||
break;
|
||||
|
||||
ret = pb_read_eof(fd, &r->pde, pipe_data_entry);
|
||||
ret = pb_read_one_eof(fd, &r->pde, PB_PIPES_DATA);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
@ -324,7 +324,7 @@ int collect_pipes(void)
|
||||
break;
|
||||
|
||||
pi->create = 0;
|
||||
ret = pb_read_eof(fd, &pi->pe, pipe_entry);
|
||||
ret = pb_read_one_eof(fd, &pi->pe, PB_PIPES);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
15
protobuf.c
15
protobuf.c
@ -54,6 +54,8 @@ struct cr_pb_message_desc {
|
||||
|
||||
#define PB_PACK_TYPECHECK(__o, __fn) ({ if (0) __fn##__pack(__o, NULL); (pb_pack_t)&__fn##__pack; })
|
||||
#define PB_GPS_TYPECHECK(__o, __fn) ({ if (0) __fn##__get_packed_size(__o); (pb_getpksize_t)&__fn##__get_packed_size; })
|
||||
#define PB_UNPACK_TYPECHECK(__op, __fn) ({ if (0) *__op = __fn##__unpack(NULL, 0, NULL); (pb_unpack_t)&__fn##__unpack; })
|
||||
#define PB_FREE_TYPECHECK(__o, __fn) ({ if (0) __fn##__free_unpacked(__o, NULL); (pb_free_t)&__fn##__free_unpacked; })
|
||||
|
||||
/*
|
||||
* This should be explicitly "called" to do type-checking
|
||||
@ -309,6 +311,17 @@ static void pb_show_msg(const void *msg, pb_pr_ctl_t *ctl)
|
||||
}
|
||||
}
|
||||
|
||||
static int pb_read_object_with_header(int fd, void **pobj, pb_unpack_t unpack, bool eof);
|
||||
int do_pb_read_one(int fd, void **pobj, int type, bool eof)
|
||||
{
|
||||
if (!cr_pb_descs[type].pb_desc) {
|
||||
pr_err("Wrong object requested %d\n", type);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return pb_read_object_with_header(fd, pobj, cr_pb_descs[type].unpack, eof);
|
||||
}
|
||||
|
||||
static inline void pb_no_payload(int fd, void *obj, int flags) { }
|
||||
|
||||
void do_pb_show_plain(int fd, const ProtobufCMessageDescriptor *md,
|
||||
@ -347,7 +360,7 @@ void do_pb_show_plain(int fd, const ProtobufCMessageDescriptor *md,
|
||||
*
|
||||
* Don't forget to free memory granted to unpacked object in calling code if needed
|
||||
*/
|
||||
int pb_read_object_with_header(int fd, void **pobj, pb_unpack_t unpack, bool eof)
|
||||
static int pb_read_object_with_header(int fd, void **pobj, pb_unpack_t unpack, bool eof)
|
||||
{
|
||||
u8 local[PB_PKOBJ_LOCAL_SIZE];
|
||||
void *buf = (void *)&local;
|
||||
|
2
pstree.c
2
pstree.c
@ -131,7 +131,7 @@ int prepare_pstree(void)
|
||||
while (1) {
|
||||
PstreeEntry *e;
|
||||
|
||||
ret = pb_read_eof(ps_fd, &e, pstree_entry);
|
||||
ret = pb_read_one_eof(ps_fd, &e, PB_PSTREE);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
2
shmem.c
2
shmem.c
@ -90,7 +90,7 @@ int prepare_shmem_pid(int pid)
|
||||
}
|
||||
|
||||
while (1) {
|
||||
ret = pb_read_eof(fd, &vi, vma_entry);
|
||||
ret = pb_read_one_eof(fd, &vi, PB_VMAS);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
@ -133,7 +133,7 @@ int collect_signalfd(void)
|
||||
if (!info)
|
||||
break;
|
||||
|
||||
ret = pb_read_eof(image_fd, &info->sfe, signalfd_entry);
|
||||
ret = pb_read_one_eof(image_fd, &info->sfe, PB_SIGNALFD);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
@ -308,7 +308,7 @@ int collect_inet_sockets(void)
|
||||
if (!ii)
|
||||
break;
|
||||
|
||||
ret = pb_read_eof(fd, &ii->ie, inet_sk_entry);
|
||||
ret = pb_read_one_eof(fd, &ii->ie, PB_INETSK);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
@ -482,7 +482,7 @@ void show_inetsk(int fd, struct cr_options *o)
|
||||
char src_addr[INET_ADDR_LEN] = "<unknown>";
|
||||
char dst_addr[INET_ADDR_LEN] = "<unknown>";
|
||||
|
||||
ret = pb_read_eof(fd, &ie, inet_sk_entry);
|
||||
ret = pb_read_one_eof(fd, &ie, PB_INETSK);
|
||||
if (ret <= 0)
|
||||
goto out;
|
||||
|
||||
|
@ -48,7 +48,7 @@ int read_sk_queues(void)
|
||||
pr_err("Failed to allocate packet header\n");
|
||||
break;
|
||||
}
|
||||
ret = pb_read_eof(fd, &pkt->entry, sk_packet_entry);
|
||||
ret = pb_read_one_eof(fd, &pkt->entry, PB_SK_QUEUES);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
4
sk-tcp.c
4
sk-tcp.c
@ -445,7 +445,7 @@ static int restore_tcp_conn_state(int sk, struct inet_sk_info *ii)
|
||||
if (ifd < 0)
|
||||
goto err;
|
||||
|
||||
if (pb_read(ifd, &tse, tcp_stream_entry) < 0)
|
||||
if (pb_read_one(ifd, &tse, PB_TCP_STREAM) < 0)
|
||||
goto err_c;
|
||||
|
||||
if (restore_tcp_seqs(sk, tse))
|
||||
@ -506,7 +506,7 @@ void show_tcp_stream(int fd, struct cr_options *opt)
|
||||
TcpStreamEntry *tse;
|
||||
pr_img_head(CR_FD_TCP_STREAM);
|
||||
|
||||
if (pb_read_eof(fd, &tse, tcp_stream_entry) > 0) {
|
||||
if (pb_read_one_eof(fd, &tse, PB_TCP_STREAM) > 0) {
|
||||
pr_msg("IN: seq %10u len %10u\n", tse->inq_seq, tse->inq_len);
|
||||
pr_msg("OUT: seq %10u len %10u\n", tse->outq_seq, tse->outq_len);
|
||||
pr_msg("OPTS: %#x\n", (int)tse->opt_mask);
|
||||
|
@ -457,7 +457,7 @@ void show_unixsk(int fd, struct cr_options *o)
|
||||
pr_img_head(CR_FD_UNIXSK);
|
||||
|
||||
while (1) {
|
||||
ret = pb_read_eof(fd, &ue, unix_sk_entry);
|
||||
ret = pb_read_one_eof(fd, &ue, PB_UNIXSK);
|
||||
if (ret <= 0)
|
||||
goto out;
|
||||
|
||||
@ -740,7 +740,7 @@ int collect_unix_sockets(void)
|
||||
if (ui == NULL)
|
||||
break;
|
||||
|
||||
ret = pb_read_eof(fd, &ui->ue, unix_sk_entry);
|
||||
ret = pb_read_one_eof(fd, &ui->ue, PB_UNIXSK);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user