diff --git a/cr-show.c b/cr-show.c index 1aff59d5a..9632ee26c 100644 --- a/cr-show.c +++ b/cr-show.c @@ -67,21 +67,11 @@ #n2, s.n2, \ #n3, s.n3) -static char local_buf[PAGE_SIZE]; static LIST_HEAD(pstree_list); void show_files(int fd_files, struct cr_options *o) { - pr_img_head(CR_FD_FDINFO); - while (1) { - FdinfoEntry *e; - int ret = pb_read_eof(fd_files, &e, fdinfo_entry); - if (ret <= 0) - break; - pb_show_msg(e, &fdinfo_entry__descriptor); - fdinfo_entry__free_unpacked(e, NULL); - } - pr_img_tail(CR_FD_FDINFO); + pb_show_plain(fd_files, fdinfo_entry); } void show_fown_cont(fown_t *fown) @@ -100,20 +90,7 @@ void pb_show_fown_cont(void *p) void show_reg_files(int fd_reg_files, struct cr_options *o) { - local_buf[0] = 0; - - pr_img_head(CR_FD_REG_FILES); - while (1) { - RegFileEntry *rfe; - int ret; - - ret = pb_read_eof(fd_reg_files, &rfe, reg_file_entry); - if (ret <= 0) - break; - pb_show_msg(rfe, ®_file_entry__descriptor); - reg_file_entry__free_unpacked(rfe, NULL); - } - pr_img_tail(CR_FD_REG_FILES); + pb_show_plain(fd_reg_files, reg_file_entry); } static inline char *remap_id_type(u32 id) @@ -126,19 +103,7 @@ static inline char *remap_id_type(u32 id) void show_remap_files(int fd, struct cr_options *o) { - RemapFilePathEntry *rfe; - - pr_img_head(CR_FD_REMAP_FPATH); - while (1) { - int ret; - - ret = pb_read_eof(fd, &rfe, remap_file_path_entry); - if (ret <= 0) - break; - pb_show_msg(rfe, &remap_file_path_entry__descriptor); - remap_file_path_entry__free_unpacked(rfe, NULL); - } - pr_img_tail(CR_FD_REMAP_FPATH); + pb_show_plain(fd, remap_file_path_entry); } void show_ghost_file(int fd, struct cr_options *o) @@ -175,18 +140,7 @@ void show_pipes_data(int fd_pipes, struct cr_options *o) void show_pipes(int fd_pipes, struct cr_options *o) { - PipeEntry *e; - int ret; - - pr_img_head(CR_FD_PIPES); - while (1) { - ret = pb_read_eof(fd_pipes, &e, pipe_entry); - if (ret <= 0) - break; - pb_show_msg(e, &pipe_entry__descriptor); - pipe_entry__free_unpacked(e, NULL); - } - pr_img_tail(CR_FD_PIPES); + pb_show_plain(fd_pipes, pipe_entry); } void show_fifo_data(int fd, struct cr_options *o) @@ -198,16 +152,7 @@ void show_fifo_data(int fd, struct cr_options *o) void show_fifo(int fd, struct cr_options *o) { - FifoEntry *e; - - pr_img_head(CR_FD_FIFO); - while (1) { - if (pb_read_eof(fd, &e, fifo_entry) <= 0) - break; - pb_show_msg(e, &fifo_entry__descriptor); - fifo_entry__free_unpacked(e, NULL); - } - pr_img_tail(CR_FD_FIFO); + pb_show_plain(fd, fifo_entry); } void show_fs(int fd_fs, struct cr_options *o) diff --git a/eventfd.c b/eventfd.c index 525acd055..79d28a547 100644 --- a/eventfd.c +++ b/eventfd.c @@ -43,22 +43,7 @@ static void pr_info_eventfd(char *action, EventfdFileEntry *efe) void show_eventfds(int fd, struct cr_options *o) { - EventfdFileEntry *efe; - - pr_img_head(CR_FD_EVENTFD); - - while (1) { - int ret; - - ret = pb_read_eof(fd, &efe, eventfd_file_entry); - if (ret <= 0) - goto out; - pb_show_msg(efe, &eventfd_file_entry__descriptor); - eventfd_file_entry__free_unpacked(efe, NULL); - } - -out: - pr_img_tail(CR_FD_EVENTFD); + pb_show_plain(fd, eventfd_file_entry); } struct eventfd_dump_arg { diff --git a/eventpoll.c b/eventpoll.c index 7cc4ade9a..75a29b1f7 100644 --- a/eventpoll.c +++ b/eventpoll.c @@ -55,41 +55,12 @@ static void pr_info_eventpoll(char *action, EventpollFileEntry *e) void show_eventpoll_tfd(int fd, struct cr_options *o) { - EventpollTfdEntry *e; - - pr_img_head(CR_FD_EVENTPOLL_TFD); - - while (1) { - int ret; - - ret = pb_read_eof(fd, &e, eventpoll_tfd_entry); - if (ret <= 0) - goto out; - pb_show_msg(e, &eventpoll_tfd_entry__descriptor); - eventpoll_tfd_entry__free_unpacked(e, NULL); - } - -out: - pr_img_tail(CR_FD_EVENTPOLL_TFD); + pb_show_plain(fd, eventpoll_tfd_entry); } void show_eventpoll(int fd, struct cr_options *o) { - EventpollFileEntry *e; - - pr_img_head(CR_FD_EVENTPOLL); - - while (1) { - int ret; - - ret = pb_read_eof(fd, &e, eventpoll_file_entry); - if (ret <= 0) - break; - pb_show_msg(e, &eventpoll_file_entry__descriptor); - eventpoll_file_entry__free_unpacked(e, NULL); - } - - pr_img_tail(CR_FD_EVENTPOLL); + pb_show_plain(fd, eventpoll_file_entry); } static int dump_eventpoll_entry(union fdinfo_entries *e, void *arg) diff --git a/include/protobuf.h b/include/protobuf.h index 1e631b0a5..cf9c44948 100644 --- a/include/protobuf.h +++ b/include/protobuf.h @@ -22,6 +22,7 @@ typedef size_t (*pb_getpksize_t)(void *obj); 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, @@ -30,6 +31,7 @@ extern int pb_read_object_with_header(int fd, void **pobj, #define PB_UNPACK_TYPECHECK(__op, __fn) ({ if (0) *__op = __fn##__unpack(NULL, 0, NULL); (pb_unpack_t)&__fn##__unpack; }) #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_FREE_TYPECHECK(__o, __fn) ({ if (0) __fn##__free_unpacked(__o, NULL); (pb_free_t)&__fn##__free_unpacked; }) #define pb_read(__fd, __obj_pptr, __proto_message_name) \ pb_read_object_with_header(__fd, (void **)__obj_pptr, \ @@ -54,6 +56,16 @@ extern int pb_write_object_with_header(int fd, void *obj, #define pb_repeated_size(__obj, __member) \ (sizeof(*(__obj)->__member) * (__obj)->n_ ##__member) +#include + extern void pb_show_msg(const void *msg, const void *msg_desc); +extern void do_pb_show_plain(int fd, const ProtobufCMessageDescriptor *d, + pb_unpack_t unpack, pb_free_t free); + +/* Don't have objects at hands to also do typechecking here */ +#define pb_show_plain(__fd, __proto_message_name) \ + do_pb_show_plain(__fd, &__proto_message_name##__descriptor, \ + (pb_unpack_t)__proto_message_name##__unpack, \ + (pb_free_t)__proto_message_name##__free_unpacked) #endif /* PROTOBUF_H__ */ diff --git a/inotify.c b/inotify.c index 9419f1d15..e1b9aeee0 100644 --- a/inotify.c +++ b/inotify.c @@ -59,43 +59,12 @@ int is_inotify_link(int lfd) void show_inotify_wd(int fd_inotify_wd, struct cr_options *o) { - InotifyWdEntry *e; - - pr_img_head(CR_FD_INOTIFY_WD); - while (1) { - int ret; - - ret = pb_read_eof(fd_inotify_wd, &e, inotify_wd_entry); - if (ret <= 0) - goto out; - - if (e->f_handle->n_handle < 2) { - pr_err("Corrupted image n_handle = %d while %d expected\n", - (int)e->f_handle->n_handle, FH_ENTRY_SIZES__min_entries); - goto out; - } - pb_show_msg(e, &inotify_wd_entry__descriptor); - inotify_wd_entry__free_unpacked(e, NULL); - } -out: - pr_img_tail(CR_FD_INOTIFY_WD); + pb_show_plain(fd_inotify_wd, inotify_wd_entry); } void show_inotify(int fd_inotify, struct cr_options *o) { - InotifyFileEntry *e; - - pr_img_head(CR_FD_INOTIFY); - while (1) { - int ret; - - ret = pb_read_eof(fd_inotify, &e, inotify_file_entry); - if (ret <= 0) - break; - pb_show_msg(e, &inotify_file_entry__descriptor); - inotify_file_entry__free_unpacked(e, NULL); - } - pr_img_tail(CR_FD_INOTIFY); + pb_show_plain(fd_inotify, inotify_file_entry); } static int dump_inotify_entry(union fdinfo_entries *e, void *arg) diff --git a/mount.c b/mount.c index 32d08d491..1c601de7f 100644 --- a/mount.c +++ b/mount.c @@ -497,18 +497,5 @@ int prepare_mnt_ns(int ns_pid) void show_mountpoints(int fd, struct cr_options *o) { - pr_img_head(CR_FD_MOUNTPOINTS); - - while (1) { - MntEntry *me; - int ret; - - ret = pb_read_eof(fd, &me, mnt_entry); - if (ret <= 0) - break; - pb_show_msg(me, &mnt_entry__descriptor); - mnt_entry__free_unpacked(me, NULL); - } - - pr_img_tail(CR_FD_MOUNTPOINTS); + pb_show_plain(fd, mnt_entry); } diff --git a/protobuf.c b/protobuf.c index 504b46d03..7dbe243c6 100644 --- a/protobuf.c +++ b/protobuf.c @@ -143,6 +143,20 @@ void pb_show_msg(const void *msg, const void *msg_desc) pr_msg("\n"); } +void do_pb_show_plain(int fd, const ProtobufCMessageDescriptor *md, + pb_unpack_t unpack, pb_free_t free) +{ + while (1) { + void *obj; + + if (pb_read_object_with_header(fd, &obj, unpack, true) <= 0) + break; + + pb_show_msg(obj, md); + free(obj, NULL); + } +} + /* * Reads PB record (header + packed object) from file @fd and unpack * it with @unpack procedure to the pointer @pobj