diff --git a/cr-dump.c b/cr-dump.c index f93cda787..45890b16c 100644 --- a/cr-dump.c +++ b/cr-dump.c @@ -158,7 +158,7 @@ int do_dump_gen_file(struct fd_parms *p, int lfd, pr_info("fdinfo: type: 0x%2x flags: 0x%4x pos: 0x%8lx fd: %d\n", ops->type, p->flags, p->pos, p->fd); - return pb_write(fdset_fd(cr_fdset, CR_FD_FDINFO), &e, fdinfo_entry); + return pb_write_one(fdset_fd(cr_fdset, CR_FD_FDINFO), &e, PB_FDINFO); } static int dump_task_exe_link(pid_t pid, MmEntry *mm) @@ -393,7 +393,7 @@ static int dump_task_fs(pid_t pid, struct cr_fdset *fdset) pr_info("Dumping task cwd id %#x root id %#x\n", fe.cwd_id, fe.root_id); - return pb_write(fdset_fd(fdset, CR_FD_FS), &fe, fs_entry); + return pb_write_one(fdset_fd(fdset, CR_FD_FS), &fe, PB_FS); } static int dump_filemap(pid_t pid, VmaEntry *vma, int file_fd, @@ -444,7 +444,7 @@ static int dump_task_mappings(pid_t pid, const struct list_head *vma_area_list, ret = 0; if (!ret) - ret = pb_write(fd, vma, vma_entry); + ret = pb_write_one(fd, vma, PB_VMAS); if (ret) goto err; } @@ -492,7 +492,7 @@ static int dump_task_creds(pid_t pid, const struct parasite_dump_misc *misc, ce.secbits = misc->secbits; - return pb_write(fdset_fd(fds, CR_FD_CREDS), &ce, creds_entry); + return pb_write_one(fdset_fd(fds, CR_FD_CREDS), &ce, PB_CREDS); } #define assign_reg(dst, src, e) dst->e = (__typeof__(dst->e))src.e @@ -559,7 +559,7 @@ static int dump_task_mm(pid_t pid, const struct proc_pid_stat *stat, if (dump_task_exe_link(pid, &mme)) goto out; - ret = pb_write(fdset_fd(fdset, CR_FD_MM), &mme, mm_entry); + ret = pb_write_one(fdset_fd(fdset, CR_FD_MM), &mme, PB_MM); xfree(mme.mm_saved_auxv); out: return ret; @@ -859,7 +859,7 @@ static int dump_task_core_all(pid_t pid, const struct proc_pid_stat *stat, core->tc->task_state = TASK_ALIVE; core->tc->exit_code = 0; - ret = pb_write(fd_core, core, core_entry); + ret = pb_write_one(fd_core, core, PB_CORE); if (ret < 0) { pr_info("ERROR\n"); goto err_free; @@ -1248,7 +1248,7 @@ static int dump_task_thread(struct parasite_ctl *parasite_ctl, struct pid *tid) if (fd_core < 0) goto err_free; - ret = pb_write(fd_core, core, core_entry); + ret = pb_write_one(fd_core, core, PB_CORE); close(fd_core); err_free: @@ -1275,7 +1275,7 @@ static int dump_one_zombie(const struct pstree_item *item, if (fd_core < 0) goto err_free; - ret = pb_write(fd_core, core, core_entry); + ret = pb_write_one(fd_core, core, PB_CORE); close(fd_core); err_free: core_entry_free(core); diff --git a/eventfd.c b/eventfd.c index 17be2c517..042ae1115 100644 --- a/eventfd.c +++ b/eventfd.c @@ -67,8 +67,8 @@ static int dump_eventfd_entry(union fdinfo_entries *e, void *arg) e->efd.fown = (FownEntry *)&da->p->fown; pr_info_eventfd("Dumping ", &e->efd); - return pb_write(fdset_fd(glob_fdset, CR_FD_EVENTFD), - &e->efd, eventfd_file_entry); + return pb_write_one(fdset_fd(glob_fdset, CR_FD_EVENTFD), + &e->efd, PB_EVENTFD); } static int dump_one_eventfd(int lfd, u32 id, const struct fd_parms *p) diff --git a/eventpoll.c b/eventpoll.c index 1d6f9183d..6ea5194b3 100644 --- a/eventpoll.c +++ b/eventpoll.c @@ -69,8 +69,8 @@ static int dump_eventpoll_entry(union fdinfo_entries *e, void *arg) efd->id = *(u32 *)arg; pr_info_eventpoll_tfd("Dumping: ", efd); - return pb_write(fdset_fd(glob_fdset, CR_FD_EVENTPOLL_TFD), - efd, eventpoll_tfd_entry); + return pb_write_one(fdset_fd(glob_fdset, CR_FD_EVENTPOLL_TFD), + efd, PB_EVENTPOLL_TFD); } static int dump_one_eventpoll(int lfd, u32 id, const struct fd_parms *p) @@ -82,8 +82,8 @@ static int dump_one_eventpoll(int lfd, u32 id, const struct fd_parms *p) e.fown = (FownEntry *)&p->fown; pr_info_eventpoll("Dumping ", &e); - if (pb_write(fdset_fd(glob_fdset, CR_FD_EVENTPOLL), - &e, eventpoll_file_entry)) + if (pb_write_one(fdset_fd(glob_fdset, CR_FD_EVENTPOLL), + &e, PB_EVENTPOLL)) return -1; return parse_fdinfo(lfd, FD_TYPES__EVENTPOLL, dump_eventpoll_entry, &id); diff --git a/fifo.c b/fifo.c index 03f10b3fb..7fdbb6a3c 100644 --- a/fifo.c +++ b/fifo.c @@ -57,7 +57,7 @@ static int dump_one_fifo(int lfd, u32 id, const struct fd_parms *p) e.id = id; e.pipe_id = pipe_id(p); - if (pb_write(img, &e, fifo_entry)) + if (pb_write_one(img, &e, PB_FIFO)) return -1; return dump_one_pipe_data(&pd_fifo, lfd, p); diff --git a/files-reg.c b/files-reg.c index 33e817c16..64f548a9b 100644 --- a/files-reg.c +++ b/files-reg.c @@ -198,7 +198,7 @@ static int dump_ghost_file(int _fd, u32 id, const struct stat *st) gfe.gid = st->st_gid; gfe.mode = st->st_mode; - if (pb_write(img, &gfe, ghost_file_entry)) + if (pb_write_one(img, &gfe, PB_GHOST_FILE)) return -1; if (S_ISREG(st->st_mode)) { @@ -254,8 +254,8 @@ dump_entry: rpe.orig_id = id; rpe.remap_id = gf->id | REMAP_GHOST; - return pb_write(fdset_fd(glob_fdset, CR_FD_REMAP_FPATH), - &rpe, remap_file_path_entry); + return pb_write_one(fdset_fd(glob_fdset, CR_FD_REMAP_FPATH), + &rpe, PB_REMAP_FPATH); } static int check_path_remap(char *rpath, const struct stat *ost, int lfd, u32 id) @@ -332,7 +332,7 @@ int dump_one_reg_file(int lfd, u32 id, const struct fd_parms *p) rfd = fdset_fd(glob_fdset, CR_FD_REG_FILES); - return pb_write(rfd, &rfe, reg_file_entry); + return pb_write_one(rfd, &rfe, PB_REG_FILES); } static const struct fdtype_ops regfile_ops = { diff --git a/image.c b/image.c index 725f33409..524c8c72a 100644 --- a/image.c +++ b/image.c @@ -52,7 +52,7 @@ int write_img_inventory(void) he.img_version = CRTOOLS_IMAGES_V1; - if (pb_write(fd, &he, inventory_entry) < 0) + if (pb_write_one(fd, &he, PB_INVENTORY) < 0) return -1; close(fd); diff --git a/include/protobuf.h b/include/protobuf.h index 5fa89baa0..42a912bdb 100644 --- a/include/protobuf.h +++ b/include/protobuf.h @@ -67,8 +67,6 @@ extern int pb_read_object_with_header(int fd, void **pobj, bool eof); #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; }) void cr_pb_init(void); @@ -81,14 +79,7 @@ void cr_pb_init(void); pb_read_object_with_header(__fd, (void **)__obj_pptr, \ PB_UNPACK_TYPECHECK(__obj_pptr, __proto_message_name), true) -extern int pb_write_object_with_header(int fd, void *obj, - pb_getpksize_t getpksize, - pb_pack_t pack); - -#define pb_write(__fd, __obj, __proto_message_name) \ - pb_write_object_with_header(__fd, __obj, \ - PB_GPS_TYPECHECK(__obj, __proto_message_name), \ - PB_PACK_TYPECHECK(__obj, __proto_message_name)) +extern int pb_write_one(int fd, void *obj, int type); #define pb_pksize(__obj, __proto_message_name) \ (__proto_message_name ##__get_packed_size(__obj) + sizeof(u32)) diff --git a/inotify.c b/inotify.c index 2b8839206..343c7effa 100644 --- a/inotify.c +++ b/inotify.c @@ -77,7 +77,7 @@ static int dump_inotify_entry(union fdinfo_entries *e, void *arg) pr_info("\t[fhandle] bytes 0x%08x type 0x%08x __handle 0x%016lx:0x%016lx\n", we->f_handle->bytes, we->f_handle->type, we->f_handle->handle[0], we->f_handle->handle[1]); - return pb_write(fdset_fd(glob_fdset, CR_FD_INOTIFY_WD), we, inotify_wd_entry); + return pb_write_one(fdset_fd(glob_fdset, CR_FD_INOTIFY_WD), we, PB_INOTIFY_WD); } static int dump_one_inotify(int lfd, u32 id, const struct fd_parms *p) @@ -89,7 +89,7 @@ static int dump_one_inotify(int lfd, u32 id, const struct fd_parms *p) ie.fown = (FownEntry *)&p->fown; pr_info("inotify: id 0x%08x flags 0x%08x\n", ie.id, ie.flags); - if (pb_write(fdset_fd(glob_fdset, CR_FD_INOTIFY), &ie, inotify_file_entry)) + if (pb_write_one(fdset_fd(glob_fdset, CR_FD_INOTIFY), &ie, PB_INOTIFY)) return -1; return parse_fdinfo(lfd, FD_TYPES__INOTIFY, dump_inotify_entry, &id); diff --git a/ipc_ns.c b/ipc_ns.c index 390ac8048..1211b2671 100644 --- a/ipc_ns.c +++ b/ipc_ns.c @@ -134,7 +134,7 @@ static int dump_ipc_sem_desc(int fd, int id, const struct semid_ds *ds) fill_ipc_desc(id, sem.desc, &ds->sem_perm); pr_info_ipc_sem_entry(&sem); - ret = pb_write(fd, &sem, ipc_sem_entry); + ret = pb_write_one(fd, &sem, PB_IPCNS_SEM); if (ret < 0) { pr_err("Failed to write IPC semaphores set\n"); return ret; @@ -222,7 +222,7 @@ static int dump_ipc_msg_queue_messages(int fd, const IpcMsgEntry *entry, size_t pr_info_ipc_msg(msg_nr, &msg); - ret = pb_write(fd, &msg, ipc_msg); + ret = pb_write_one(fd, &msg, PB_IPCNS_MSG); if (ret < 0) { pr_err("Failed to write IPC message header\n"); break; @@ -254,7 +254,7 @@ static int dump_ipc_msg_queue(int fd, int id, const struct msqid_ds *ds) pr_info_ipc_msg_entry(&msg); - ret = pb_write(fd, &msg, ipc_msg_entry); + ret = pb_write_one(fd, &msg, PB_IPCNS_MSG_ENT); if (ret < 0) { pr_err("Failed to write IPC message queue\n"); return ret; @@ -361,7 +361,7 @@ static int dump_ipc_shm_seg(int fd, int id, const struct shmid_ds *ds) fill_ipc_desc(id, shm.desc, &ds->shm_perm); pr_info_ipc_shm(&shm); - ret = pb_write(fd, &shm, ipc_shm_entry); + ret = pb_write_one(fd, &shm, PB_IPCNS_SHM); if (ret < 0) { pr_err("Failed to write IPC shared memory segment\n"); return ret; @@ -422,7 +422,7 @@ static int dump_ipc_var(int fd) goto err; } - ret = pb_write(fd, &var, ipc_var_entry); + ret = pb_write_one(fd, &var, PB_IPCNS_VAR); if (ret < 0) { pr_err("Failed to write IPC variables\n"); goto err; diff --git a/mount.c b/mount.c index 6d4bf91db..ca6d5f0ea 100644 --- a/mount.c +++ b/mount.c @@ -248,7 +248,7 @@ static int dump_one_mountpoint(struct mount_info *pm, int fd) return -1; } - if (pb_write(fd, &me, mnt_entry)) + if (pb_write_one(fd, &me, PB_MOUNTPOINTS)) return -1; return 0; diff --git a/net.c b/net.c index 5d55d487f..fe155eeef 100644 --- a/net.c +++ b/net.c @@ -42,7 +42,7 @@ static int dump_one_netdev(int type, struct nlmsghdr *h, struct ifinfomsg *ifi, netdev.flags = ifi->ifi_flags; netdev.name = RTA_DATA(tb[IFLA_IFNAME]); - return pb_write(fdset_fd(fds, CR_FD_NETDEV), &netdev, net_device_entry); + return pb_write_one(fdset_fd(fds, CR_FD_NETDEV), &netdev, PB_NETDEV); } static int dump_one_link(struct nlmsghdr *hdr, void *arg) diff --git a/parasite-syscall.c b/parasite-syscall.c index aeeddaa04..6fa4ffea5 100644 --- a/parasite-syscall.c +++ b/parasite-syscall.c @@ -470,7 +470,7 @@ int parasite_dump_sigacts_seized(struct parasite_ctl *ctl, struct cr_fdset *cr_f ASSIGN_TYPED(se.restorer, args.sas[i].rt_sa_restorer); ASSIGN_TYPED(se.mask, args.sas[i].rt_sa_mask.sig[0]); - if (pb_write(fd, &se, sa_entry) < 0) + if (pb_write_one(fd, &se, PB_SIGACT) < 0) return -1; } @@ -486,7 +486,7 @@ static int dump_one_timer(struct itimerval *v, int fd) ie.vsec = v->it_value.tv_sec; ie.vusec = v->it_value.tv_sec; - return pb_write(fd, &ie, itimer_entry); + return pb_write_one(fd, &ie, PB_ITIMERS); } int parasite_dump_itimers_seized(struct parasite_ctl *ctl, struct cr_fdset *cr_fdset) diff --git a/pipes.c b/pipes.c index 587fc4526..1fcdf07e4 100644 --- a/pipes.c +++ b/pipes.c @@ -397,7 +397,7 @@ int dump_one_pipe_data(struct pipe_data_dump *pd, int lfd, const struct fd_parms pde.pipe_id = pipe_id(p); pde.bytes = bytes; - if (pb_write(img, &pde, pipe_data_entry)) + if (pb_write_one(img, &pde, PB_PIPES_DATA)) goto err_close; wrote = splice(steal_pipe[0], NULL, img, NULL, bytes, 0); @@ -439,7 +439,7 @@ static int dump_one_pipe(int lfd, u32 id, const struct fd_parms *p) pe.flags = p->flags; pe.fown = (FownEntry *)&p->fown; - if (pb_write(fdset_fd(glob_fdset, CR_FD_PIPES), &pe, pipe_entry)) + if (pb_write_one(fdset_fd(glob_fdset, CR_FD_PIPES), &pe, PB_PIPES)) return -1; return dump_one_pipe_data(&pd_pipes, lfd, p); diff --git a/protobuf.c b/protobuf.c index 9f6f441ac..1117ef8c6 100644 --- a/protobuf.c +++ b/protobuf.c @@ -52,6 +52,9 @@ struct cr_pb_message_desc { const ProtobufCMessageDescriptor *pb_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; }) + /* * This should be explicitly "called" to do type-checking */ @@ -406,21 +409,26 @@ err: * 0 on success * -1 on error */ -int pb_write_object_with_header(int fd, void *obj, pb_getpksize_t getpksize, pb_pack_t pack) +int pb_write_one(int fd, void *obj, int type) { u8 local[PB_PKOBJ_LOCAL_SIZE]; void *buf = (void *)&local; u32 size, packed; int ret = -1; - size = getpksize(obj); + if (!cr_pb_descs[type].pb_desc) { + pr_err("Wron object requested %d\n", type); + return -1; + } + + size = cr_pb_descs[type].getpksize(obj); if (size > (u32)sizeof(local)) { buf = xmalloc(size); if (!buf) goto err; } - packed = pack(obj, buf); + packed = cr_pb_descs[type].pack(obj, buf); if (packed != size) { pr_err("Failed packing PB object %p\n", obj); goto err; diff --git a/pstree.c b/pstree.c index 95560fa80..940282e27 100644 --- a/pstree.c +++ b/pstree.c @@ -102,7 +102,7 @@ int dump_pstree(struct pstree_item *root_item) for (i = 0; i < item->nr_threads; i++) e.threads[i] = item->threads[i].virt; - ret = pb_write(pstree_fd, &e, pstree_entry); + ret = pb_write_one(pstree_fd, &e, PB_PSTREE); xfree(e.threads); if (ret) diff --git a/signalfd.c b/signalfd.c index 1018a4a68..e6f44bcee 100644 --- a/signalfd.c +++ b/signalfd.c @@ -50,8 +50,8 @@ static int dump_signalfd_entry(union fdinfo_entries *e, void *arg) e->sfd.flags = da->p->flags; e->sfd.fown = (FownEntry *)&da->p->fown; - return pb_write(fdset_fd(glob_fdset, CR_FD_SIGNALFD), - &e->sfd, signalfd_entry); + return pb_write_one(fdset_fd(glob_fdset, CR_FD_SIGNALFD), + &e->sfd, PB_SIGNALFD); } static int dump_one_signalfd(int lfd, u32 id, const struct fd_parms *p) diff --git a/sk-inet.c b/sk-inet.c index f779e2013..9d603f6a5 100644 --- a/sk-inet.c +++ b/sk-inet.c @@ -213,7 +213,7 @@ static int dump_one_inet_fd(int lfd, u32 id, const struct fd_parms *p) if (dump_socket_opts(lfd, &skopts)) goto err; - if (pb_write(fdset_fd(glob_fdset, CR_FD_INETSK), &ie, inet_sk_entry)) + if (pb_write_one(fdset_fd(glob_fdset, CR_FD_INETSK), &ie, PB_INETSK)) goto err; pr_info("Dumping inet socket at %d\n", p->fd); diff --git a/sk-queue.c b/sk-queue.c index 40b14388c..cbf54b804 100644 --- a/sk-queue.c +++ b/sk-queue.c @@ -142,7 +142,7 @@ int dump_sk_queue(int sock_fd, int sock_id) goto err_set_sock; } - ret = pb_write(fdset_fd(glob_fdset, CR_FD_SK_QUEUES), &pe, sk_packet_entry); + ret = pb_write_one(fdset_fd(glob_fdset, CR_FD_SK_QUEUES), &pe, PB_SK_QUEUES); if (ret < 0) { ret = -EIO; goto err_set_sock; diff --git a/sk-tcp.c b/sk-tcp.c index 19ef7ea77..4ec9f9e28 100644 --- a/sk-tcp.c +++ b/sk-tcp.c @@ -274,7 +274,7 @@ static int dump_tcp_conn_state(struct inet_sk_desc *sk) if (img_fd < 0) goto err_img; - ret = pb_write(img_fd, &tse, tcp_stream_entry); + ret = pb_write_one(img_fd, &tse, PB_TCP_STREAM); if (ret < 0) goto err_iw; diff --git a/sk-unix.c b/sk-unix.c index 5125a34e1..840dff774 100644 --- a/sk-unix.c +++ b/sk-unix.c @@ -200,7 +200,7 @@ static int dump_one_unix_fd(int lfd, u32 id, const struct fd_parms *p) if (dump_socket_opts(lfd, &skopts)) goto err; - if (pb_write(fdset_fd(glob_fdset, CR_FD_UNIXSK), &ue, unix_sk_entry)) + if (pb_write_one(fdset_fd(glob_fdset, CR_FD_UNIXSK), &ue, PB_UNIXSK)) goto err; if (sk->rqlen != 0 && !(sk->type == SOCK_STREAM && @@ -414,7 +414,7 @@ int fix_external_unix_sockets(void) show_one_unix("Dumping extern", sk); - if (pb_write(fdset_fd(glob_fdset, CR_FD_UNIXSK), &e, unix_sk_entry)) + if (pb_write_one(fdset_fd(glob_fdset, CR_FD_UNIXSK), &e, PB_UNIXSK)) goto err; show_one_unix_img("Dumped extern", &e); diff --git a/uts_ns.c b/uts_ns.c index c4ddf4072..434729b98 100644 --- a/uts_ns.c +++ b/uts_ns.c @@ -31,7 +31,7 @@ int dump_uts_ns(int ns_pid, struct cr_fdset *fdset) ue.nodename = ubuf.nodename; ue.domainname = ubuf.domainname; - return pb_write(fdset_fd(fdset, CR_FD_UTSNS), &ue, utsns_entry); + return pb_write_one(fdset_fd(fdset, CR_FD_UTSNS), &ue, PB_UTSNS); } int prepare_utsns(int pid)