From b1b0a39a58c8948b4f44af068c3a6fbb1eb4635c Mon Sep 17 00:00:00 2001 From: Pavel Emelyanov Date: Tue, 7 Aug 2012 02:42:58 +0400 Subject: [PATCH] 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 --- cr-restore.c | 18 +++++++++--------- cr-show.c | 6 +++--- eventfd.c | 2 +- eventpoll.c | 4 ++-- fifo.c | 2 +- files-reg.c | 6 +++--- files.c | 4 ++-- image.c | 4 ++-- include/protobuf.h | 16 +++------------- inotify.c | 4 ++-- ipc_ns.c | 10 +++++----- mount.c | 2 +- net.c | 2 +- pipes.c | 4 ++-- protobuf.c | 15 ++++++++++++++- pstree.c | 2 +- shmem.c | 2 +- signalfd.c | 2 +- sk-inet.c | 4 ++-- sk-queue.c | 2 +- sk-tcp.c | 4 ++-- sk-unix.c | 4 ++-- uts_ns.c | 2 +- 23 files changed, 62 insertions(+), 59 deletions(-) diff --git a/cr-restore.c b/cr-restore.c index 008ffafa2..ba49f6b7e 100644 --- a/cr-restore.c +++ b/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) { diff --git a/cr-show.c b/cr-show.c index 9e1db5af1..232741d3c 100644 --- a/cr-show.c +++ b/cr-show.c @@ -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); diff --git a/eventfd.c b/eventfd.c index 042ae1115..14b3c5951 100644 --- a/eventfd.c +++ b/eventfd.c @@ -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) diff --git a/eventpoll.c b/eventpoll.c index 6ea5194b3..8b298f1a1 100644 --- a/eventpoll.c +++ b/eventpoll.c @@ -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) diff --git a/fifo.c b/fifo.c index 7fdbb6a3c..00d8f0d17 100644 --- a/fifo.c +++ b/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; diff --git a/files-reg.c b/files-reg.c index 64f548a9b..77428a47d 100644 --- a/files-reg.c +++ b/files-reg.c @@ -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; diff --git a/files.c b/files.c index 1e88149e8..a30e86cce 100644 --- a/files.c +++ b/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); diff --git a/image.c b/image.c index 524c8c72a..91f440f93 100644 --- a/image.c +++ b/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); diff --git a/include/protobuf.h b/include/protobuf.h index 42a912bdb..6c7e4ca5d 100644 --- a/include/protobuf.h +++ b/include/protobuf.h @@ -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); diff --git a/inotify.c b/inotify.c index 343c7effa..0d1262d73 100644 --- a/inotify.c +++ b/inotify.c @@ -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) diff --git a/ipc_ns.c b/ipc_ns.c index 1211b2671..78c3fc499 100644 --- a/ipc_ns.c +++ b/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; diff --git a/mount.c b/mount.c index ca6d5f0ea..152839749 100644 --- a/mount.c +++ b/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; diff --git a/net.c b/net.c index fe155eeef..b28677ab9 100644 --- a/net.c +++ b/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; diff --git a/pipes.c b/pipes.c index 1fcdf07e4..5bdcfee42 100644 --- a/pipes.c +++ b/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; diff --git a/protobuf.c b/protobuf.c index 1117ef8c6..e4dd61cf9 100644 --- a/protobuf.c +++ b/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; diff --git a/pstree.c b/pstree.c index 940282e27..aa882966f 100644 --- a/pstree.c +++ b/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; diff --git a/shmem.c b/shmem.c index cc1dcdb7a..921889369 100644 --- a/shmem.c +++ b/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; diff --git a/signalfd.c b/signalfd.c index e6f44bcee..b1021bb9b 100644 --- a/signalfd.c +++ b/signalfd.c @@ -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; diff --git a/sk-inet.c b/sk-inet.c index 9d603f6a5..46402a1d0 100644 --- a/sk-inet.c +++ b/sk-inet.c @@ -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] = ""; char dst_addr[INET_ADDR_LEN] = ""; - ret = pb_read_eof(fd, &ie, inet_sk_entry); + ret = pb_read_one_eof(fd, &ie, PB_INETSK); if (ret <= 0) goto out; diff --git a/sk-queue.c b/sk-queue.c index cbf54b804..5e3de0cbf 100644 --- a/sk-queue.c +++ b/sk-queue.c @@ -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; diff --git a/sk-tcp.c b/sk-tcp.c index 4ec9f9e28..fa31556db 100644 --- a/sk-tcp.c +++ b/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); diff --git a/sk-unix.c b/sk-unix.c index 840dff774..188dac7fe 100644 --- a/sk-unix.c +++ b/sk-unix.c @@ -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; diff --git a/uts_ns.c b/uts_ns.c index 434729b98..24fdfe10b 100644 --- a/uts_ns.c +++ b/uts_ns.c @@ -48,7 +48,7 @@ int prepare_utsns(int pid) if (fd < 0) return -1; - ret = pb_read(fd, &ue, utsns_entry); + ret = pb_read_one(fd, &ue, PB_UTSNS); if (ret < 0) goto out;