diff --git a/Makefile b/Makefile index 673f17e24..9905c3d33 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include Makefile.inc CFLAGS += -I./include CFLAGS += -O0 -ggdb3 -LIBS += -lrt -lpthread +LIBS += -lrt -lpthread -lprotobuf-c DEFINES += -D_FILE_OFFSET_BITS=64 DEFINES += -D_GNU_SOURCE @@ -61,6 +61,8 @@ OBJS += inotify.o OBJS += pstree.o OBJS += protobuf.o +PROTOBUF-LIB := protobuf/protobuf-lib.o + DEPS := $(patsubst %.o,%.d,$(OBJS)) MAKEFLAGS += --no-print-directory @@ -69,13 +71,16 @@ include Makefile.syscall include Makefile.pie .PHONY: all test-legacy zdtm test rebuild clean distclean tags cscope \ - docs help pie + docs help pie protobuf -all: pie +all: protobuf pie $(Q) $(MAKE) $(PROGRAM) pie: $(PIE-GEN) +protobuf: + $(Q) $(MAKE) -C protobuf/ all + %.o: %.c $(E) " CC " $@ $(Q) $(CC) -c $(CFLAGS) $< -o $@ @@ -92,7 +97,7 @@ pie: $(PIE-GEN) $(E) " DEP " $@ $(Q) $(CC) -M -MT $@ -MT $(patsubst %.d,%.o,$@) $(CFLAGS) $< -o $@ -$(PROGRAM): $(OBJS) $(SYS-OBJ) +$(PROGRAM): $(OBJS) $(SYS-OBJ) $(PROTOBUF-LIB) $(E) " LINK " $@ $(Q) $(CC) $(CFLAGS) $^ $(LIBS) -o $@ @@ -121,6 +126,7 @@ clean: cleanpie cleansyscall $(Q) $(RM) -f ./*.bin $(Q) $(RM) -f ./$(PROGRAM) $(Q) $(RM) -rf ./test/dump/ + $(Q) $(MAKE) -C protobuf/ clean $(Q) $(MAKE) -C test/legacy clean $(Q) $(MAKE) -C test/zdtm cleandep $(Q) $(MAKE) -C test/zdtm clean diff --git a/cr-dump.c b/cr-dump.c index 9ee3463b1..93514a0b4 100644 --- a/cr-dump.c +++ b/cr-dump.c @@ -45,6 +45,9 @@ #include "inotify.h" #include "pstree.h" +#include "protobuf.h" +#include "protobuf/fdinfo.pb-c.h" + #ifndef CONFIG_X86_64 # error No x86-32 support yet #endif @@ -130,7 +133,7 @@ u32 make_gen_id(const struct fd_parms *p) int do_dump_gen_file(struct fd_parms *p, int lfd, const struct fdtype_ops *ops, const struct cr_fdset *cr_fdset) { - struct fdinfo_entry e; + FdinfoEntry e = FDINFO_ENTRY__INIT; int ret = -1; e.type = ops->type; @@ -143,17 +146,12 @@ int do_dump_gen_file(struct fd_parms *p, int lfd, ret = ops->dump(lfd, e.id, p); if (ret < 0) - goto err; + return -1; pr_info("fdinfo: type: 0x%2x flags: 0x%4x pos: 0x%8lx fd: %d\n", ops->type, p->flags, p->pos, p->fd); - if (write_img(fdset_fd(cr_fdset, CR_FD_FDINFO), &e)) - goto err; - - ret = 0; -err: - return ret; + return pb_write(fdset_fd(cr_fdset, CR_FD_FDINFO), &e, fdinfo_entry); } static int dump_task_exe_link(pid_t pid, struct mm_entry *mm) diff --git a/cr-show.c b/cr-show.c index e99fa01e3..6099f559d 100644 --- a/cr-show.c +++ b/cr-show.c @@ -24,6 +24,9 @@ #include "ipc_ns.h" #include "pstree.h" +#include "protobuf.h" +#include "protobuf/fdinfo.pb-c.h" + #define DEF_PAGES_PER_LINE 6 #ifndef CONFIG_X86_64 @@ -77,21 +80,16 @@ static char *fdtype2s(u8 type) void show_files(int fd_files, struct cr_options *o) { - struct fdinfo_entry e; - pr_img_head(CR_FD_FDINFO); while (1) { - int ret; - - ret = read_img_eof(fd_files, &e); + FdinfoEntry *e; + int ret = pb_read_eof(fd_files, &e, fdinfo_entry); if (ret <= 0) goto out; - - pr_msg("type: %-5s fd: %-5d id: %#x flags %#x", - fdtype2s(e.type), e.fd, e.id, e.flags); - - pr_msg("\n"); + pr_msg("type: %-5s fd: %-5d id: %#x flags %#x\n", + fdtype2s(e->type), e->fd, e->id, e->flags); + fdinfo_entry__free_unpacked(e, NULL); } out: diff --git a/file-ids.c b/file-ids.c index 31cd215eb..690713e15 100644 --- a/file-ids.c +++ b/file-ids.c @@ -30,7 +30,7 @@ u32 fd_id_generate_special(void) return fd_tree.subid++; } -int fd_id_generate(pid_t pid, struct fdinfo_entry *fe) +int fd_id_generate(pid_t pid, FdinfoEntry *fe) { u32 id; struct kid_elem e; diff --git a/files.c b/files.c index 3facd5f67..a44b76cb0 100644 --- a/files.c +++ b/files.c @@ -24,6 +24,8 @@ #include "sockets.h" #include "pstree.h" +#include "protobuf.h" + static struct fdinfo_list_entry *fdinfo_list; static int nr_fdinfo_list; @@ -71,7 +73,7 @@ struct file_desc *find_file_desc_raw(int type, u32 id) return NULL; } -static inline struct file_desc *find_file_desc(struct fdinfo_entry *fe) +static inline struct file_desc *find_file_desc(FdinfoEntry *fe) { return find_file_desc_raw(fe->type, fe->id); } @@ -152,7 +154,7 @@ int rst_file_params(int fd, fown_t *fown, int flags) return 0; } -static int collect_fd(int pid, struct fdinfo_entry *e, struct rst_info *rst_info) +static int collect_fd(int pid, FdinfoEntry *e, struct rst_info *rst_info) { struct fdinfo_list_entry *l, *le = &fdinfo_list[nr_fdinfo_list]; struct file_desc *fdesc; @@ -206,19 +208,17 @@ int prepare_fd_pid(int pid, struct rst_info *rst_info) } while (1) { - struct fdinfo_entry *e = xmalloc(sizeof(*e)); - if (!e) { - ret = -1; - break; - } + FdinfoEntry *e; - ret = read_img_eof(fdinfo_fd, e); + ret = pb_read_eof(fdinfo_fd, &e, fdinfo_entry); if (ret <= 0) break; ret = collect_fd(pid, e, rst_info); - if (ret < 0) + if (ret < 0) { + fdinfo_entry__free_unpacked(e, NULL); break; + } } close(fdinfo_fd); @@ -255,7 +255,7 @@ static void transport_name_gen(struct sockaddr_un *addr, int *len, *addr->sun_path = '\0'; } -static int should_open_transport(struct fdinfo_entry *fe, struct file_desc *fd) +static int should_open_transport(FdinfoEntry *fe, struct file_desc *fd) { if (fd->ops->want_transport) return fd->ops->want_transport(fe, fd); @@ -326,7 +326,7 @@ int send_fd_to_peer(int fd, struct fdinfo_list_entry *fle, int tsk) return send_fd(tsk, &saddr, len, fd); } -static int open_fd(int pid, struct fdinfo_entry *fe, struct file_desc *d) +static int open_fd(int pid, FdinfoEntry *fe, struct file_desc *d) { int tmp; int sock; @@ -383,7 +383,7 @@ static int open_fd(int pid, struct fdinfo_entry *fe, struct file_desc *d) return 0; } -static int receive_fd(int pid, struct fdinfo_entry *fe, struct file_desc *d) +static int receive_fd(int pid, FdinfoEntry *fe, struct file_desc *d) { int tmp; struct fdinfo_list_entry *fle; diff --git a/include/file-ids.h b/include/file-ids.h index d71f553e3..ab054b502 100644 --- a/include/file-ids.h +++ b/include/file-ids.h @@ -5,6 +5,8 @@ #include "types.h" #include "rbtree.h" +#include "../protobuf/fdinfo.pb-c.h" + #define FD_PID_INVALID (-2U) #define FD_DESC_INVALID (-3U) @@ -12,7 +14,7 @@ (((u32)(dev) ^ (u32)(ino) ^ (u32)(pos))) struct fdinfo_entry; -extern int fd_id_generate(pid_t pid, struct fdinfo_entry *fe); +extern int fd_id_generate(pid_t pid, FdinfoEntry *fe); extern u32 fd_id_generate_special(void); extern void fd_id_show_tree(void); diff --git a/include/files.h b/include/files.h index 099340c03..bf44a666e 100644 --- a/include/files.h +++ b/include/files.h @@ -7,6 +7,8 @@ #include "list.h" #include "image.h" +#include "../protobuf/fdinfo.pb-c.h" + struct pstree_item; struct file_desc; struct cr_fdset; @@ -38,13 +40,13 @@ struct fdinfo_list_entry { struct list_head ps_list; int pid; futex_t real_pid; - struct fdinfo_entry *fe; + FdinfoEntry *fe; }; struct file_desc_ops { unsigned int type; int (*open)(struct file_desc *d); - int (*want_transport)(struct fdinfo_entry *fe, struct file_desc *d); + int (*want_transport)(FdinfoEntry *fe, struct file_desc *d); }; struct file_desc { diff --git a/include/image.h b/include/image.h index 334ad020d..12ec93837 100644 --- a/include/image.h +++ b/include/image.h @@ -131,13 +131,6 @@ struct inotify_file_entry { fown_t fown; } __packed; -struct fdinfo_entry { - u32 fd; - u8 type; - u8 flags; - u32 id; -} __packed; - struct fs_entry { u32 cwd_id; u32 root_id; diff --git a/include/sockets.h b/include/sockets.h index 267e7e26d..9979a76a7 100644 --- a/include/sockets.h +++ b/include/sockets.h @@ -9,7 +9,6 @@ struct fdinfo_list_entry; struct sk_opts_entry; -struct fdinfo_entry; struct cr_options; struct file_desc; struct fd_parms; diff --git a/pipes.c b/pipes.c index 12c973205..48ced6c31 100644 --- a/pipes.c +++ b/pipes.c @@ -294,7 +294,7 @@ static int open_pipe(struct file_desc *d) return tmp; } -static int want_transport(struct fdinfo_entry *fe, struct file_desc *d) +static int want_transport(FdinfoEntry *fe, struct file_desc *d) { struct pipe_info *pi; diff --git a/protobuf/Makefile b/protobuf/Makefile new file mode 100644 index 000000000..324a9621c --- /dev/null +++ b/protobuf/Makefile @@ -0,0 +1,51 @@ +-include ../Makefile.inc + +CFLAGS += -I./include +CFLAGS += -O0 -ggdb3 + +DEFINES += -D_FILE_OFFSET_BITS=64 +DEFINES += -D_GNU_SOURCE + +ifneq ($(WERROR),0) + WARNINGS += -Werror +endif + +ifeq ($(DEBUG),1) + DEFINES += -DCR_DEBUG +endif + +WARNINGS += -Wall +CFLAGS += $(WARNINGS) $(DEFINES) + +LIBRARY := protobuf-lib.o + +PROTO_FILES += fdinfo.proto + +HDRS := $(patsubst %.proto,%.pb-c.h,$(PROTO_FILES)) +SRCS := $(patsubst %.proto,%.pb-c.c,$(PROTO_FILES)) +OBJS := $(patsubst %.c,%.o,$(SRCS)) + +.DEFAULT_GOAL := all + +%.pb-c.c: %.proto + $(E) " PROTOBUF "$@ + $(Q) protoc-c --c_out=./ $< + +%.o: %.c + $(E) " CC "$@ + $(Q) $(CC) -c $(CFLAGS) $< -o $@ + +.SECONDARY: + +$(LIBRARY): $(OBJS) + $(E) " LINK "$@ + $(Q) ld -r -o $@ $(OBJS) + +.PHONY: all clean + +all: $(LIBRARY) + +clean: + $(E) " CLEAN PROTOBUF" + $(Q) rm -f $(SRCS) $(HDRS) $(OBJS) $(LIBRARY) + diff --git a/protobuf/fdinfo.proto b/protobuf/fdinfo.proto new file mode 100644 index 000000000..3b4a57ca4 --- /dev/null +++ b/protobuf/fdinfo.proto @@ -0,0 +1,6 @@ +message fdinfo_entry { + required uint32 id = 1; + required uint32 flags = 2; + required uint32 type = 3; + required uint32 fd = 4; +} diff --git a/sk-unix.c b/sk-unix.c index 40c5f708c..927329381 100644 --- a/sk-unix.c +++ b/sk-unix.c @@ -573,7 +573,7 @@ done: return 0; } -static int unixsk_should_open_transport(struct fdinfo_entry *fe, +static int unixsk_should_open_transport(FdinfoEntry *fe, struct file_desc *d) { struct unix_sk_info *ui;