Skip to content

Commit

Permalink
img: Introduce generic routine for collecting image entries
Browse files Browse the repository at this point in the history
On restore we typically read an image and put the entries into
some hash/list/whatever to work on them later. It's handy to have
a generic routine for doing so.

Signed-off-by: Pavel Emelyanov <[email protected]>
  • Loading branch information
xemul committed Aug 9, 2012
1 parent 00a1a4d commit b914457
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 308 deletions.
37 changes: 11 additions & 26 deletions eventfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,34 +120,19 @@ static struct file_desc_ops eventfd_desc_ops = {
.open = eventfd_open,
};

int collect_eventfd(void)
static int collect_one_efd(void *obj, ProtobufCMessage *msg)
{
struct eventfd_file_info *info = NULL;
int ret, image_fd;

image_fd = open_image_ro(CR_FD_EVENTFD);
if (image_fd < 0)
return -1;

while (1) {
ret = -1;
struct eventfd_file_info *info = obj;

info = xmalloc(sizeof(*info));
if (!info)
break;
info->efe = pb_msg(msg, EventfdFileEntry);
file_desc_add(&info->d, info->efe->id, &eventfd_desc_ops);
pr_info_eventfd("Collected ", info->efe);

ret = pb_read_one_eof(image_fd, &info->efe, PB_EVENTFD);
if (ret < 0)
goto err;
else if (!ret)
break;
pr_info_eventfd("Collected ", info->efe);
file_desc_add(&info->d, info->efe->id, &eventfd_desc_ops);
}
return 0;
}

err:
xfree(info ? info->efe : NULL);
xfree(info);
close(image_fd);
return ret;
int collect_eventfd(void)
{
return collect_image(CR_FD_EVENTFD, PB_EVENTFD,
sizeof(struct eventfd_file_info), collect_one_efd);
}
70 changes: 25 additions & 45 deletions eventpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,59 +148,39 @@ static struct file_desc_ops desc_ops = {
.open = eventpoll_open,
};

int collect_eventpoll(void)
static int collect_one_epoll_tfd(void *o, ProtobufCMessage *msg)
{
int image_fd;
int ret = -1;

image_fd = open_image_ro(CR_FD_EVENTPOLL_TFD);
if (image_fd < 0)
return -1;

while (1) {
struct eventpoll_tfd_file_info *info;
struct eventpoll_tfd_file_info *info = o;

info = xmalloc(sizeof(*info));
if (!info)
goto err;

ret = pb_read_one_eof(image_fd, &info->tdefe, PB_EVENTPOLL_TFD);
if (ret < 0)
goto err;
else if (!ret)
break;

INIT_LIST_HEAD(&info->list);

list_add(&info->list, &eventpoll_tfds);
pr_info_eventpoll_tfd("Collected ", info->tdefe);
}
info->tdefe = pb_msg(msg, EventpollTfdEntry);
list_add(&info->list, &eventpoll_tfds);
pr_info_eventpoll_tfd("Collected ", info->tdefe);

close_safe(&image_fd);
return 0;
}

image_fd = open_image_ro(CR_FD_EVENTPOLL);
if (image_fd < 0)
return -1;
static int collect_one_epoll(void *o, ProtobufCMessage *msg)
{
struct eventpoll_file_info *info = o;

while (1) {
struct eventpoll_file_info *info;
info->efe = pb_msg(msg, EventpollFileEntry);
file_desc_add(&info->d, info->efe->id, &desc_ops);
pr_info_eventpoll("Collected ", info->efe);

ret = -1;
info = xmalloc(sizeof(*info));
if (!info)
goto err;
return 0;
}

ret = pb_read_one_eof(image_fd, &info->efe, PB_EVENTPOLL);
if (ret < 0)
goto err;
else if (!ret)
break;
int collect_eventpoll(void)
{
int ret;

pr_info_eventpoll("Collected ", info->efe);
file_desc_add(&info->d, info->efe->id, &desc_ops);
}
ret = collect_image(CR_FD_EVENTPOLL_TFD, PB_EVENTPOLL_TFD,
sizeof(struct eventpoll_tfd_file_info),
collect_one_epoll_tfd);
if (!ret)
ret = collect_image(CR_FD_EVENTPOLL, PB_EVENTPOLL,
sizeof(struct eventpoll_file_info),
collect_one_epoll);

err:
close_safe(&image_fd);
return ret;
}
56 changes: 26 additions & 30 deletions fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,44 +123,40 @@ static struct file_desc_ops fifo_desc_ops = {
.open = open_fifo_fd,
};

int collect_fifo(void)
static int collect_one_fifo(void *o, ProtobufCMessage *base)
{
struct fifo_info *info = NULL, *f;
int img, ret;
struct fifo_info *info = o, *f;

img = open_image_ro(CR_FD_FIFO);
if (img < 0)
return -1;
info->fe = pb_msg(base, FifoEntry);
pr_info("Collected fifo entry ID %#x PIPE ID %#x\n",
info->fe->id, info->fe->pipe_id);

while (1) {
ret = -1;
info = xzalloc(sizeof(*info));
if (!info)
break;
file_desc_add(&info->d, info->fe->id, &fifo_desc_ops);

ret = pb_read_one_eof(img, &info->fe, PB_FIFO);
if (ret <= 0)
/* check who will restore the fifo data */
list_for_each_entry(f, &fifo_head, list)
if (f->fe->pipe_id == info->fe->pipe_id)
break;

pr_info("Collected fifo entry ID %#x PIPE ID %#x\n",
info->fe->id, info->fe->pipe_id);

file_desc_add(&info->d, info->fe->id, &fifo_desc_ops);
if (&f->list == &fifo_head) {
list_add(&info->list, &fifo_head);
info->restore_data = true;
} else {
INIT_LIST_HEAD(&info->list);
info->restore_data = false;
}

/* check who will restore the fifo data */
list_for_each_entry(f, &fifo_head, list)
if (f->fe->pipe_id == info->fe->pipe_id)
break;
return 0;
}

if (&f->list == &fifo_head) {
list_add(&info->list, &fifo_head);
info->restore_data = true;
}
}
int collect_fifo(void)
{
int ret;

xfree(info ? info->fe : NULL);
xfree(info);
close(img);
ret = collect_image(CR_FD_FIFO, PB_FIFO,
sizeof(struct fifo_info), collect_one_fifo);
if (!ret)
ret = collect_pipe_data(CR_FD_FIFO_DATA, pd_hash_fifo);

return collect_pipe_data(CR_FD_FIFO_DATA, pd_hash_fifo);
return ret;
}
50 changes: 17 additions & 33 deletions files-reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,44 +427,28 @@ static struct file_desc_ops reg_desc_ops = {
.open = open_fe_fd,
};

int collect_reg_files(void)
static int collect_one_regfile(void *o, ProtobufCMessage *base)
{
struct reg_file_info *rfi = NULL;
int fd, ret = -1;

fd = open_image_ro(CR_FD_REG_FILES);
if (fd < 0)
return -1;

while (1) {
RegFileEntry *rfe;

rfi = xmalloc(sizeof(*rfi));
ret = -1;
if (rfi == NULL)
break;

rfi->path = NULL;

ret = pb_read_one_eof(fd, &rfe, PB_REG_FILES);
if (ret <= 0)
break;
struct reg_file_info *rfi = o;

rfi->rfe = rfe;
rfi->path = rfe->name;
rfi->rfe = pb_msg(base, RegFileEntry);
rfi->path = rfi->rfe->name;
rfi->remap_path = NULL;

rfi->remap_path = NULL;
pr_info("Collected [%s] ID %#x\n", rfi->path, rfi->rfe->id);
file_desc_add(&rfi->d, rfi->rfe->id, &reg_desc_ops);

pr_info("Collected [%s] ID %#x\n", rfi->path, rfi->rfe->id);
file_desc_add(&rfi->d, rfi->rfe->id, &reg_desc_ops);
}
return 0;
}

if (rfi) {
xfree(rfi->path);
xfree(rfi);
}
int collect_reg_files(void)
{
int ret;

close(fd);
ret = collect_image(CR_FD_REG_FILES, PB_REG_FILES,
sizeof(struct reg_file_info), collect_one_regfile);
if (!ret)
ret = collect_remaps();

return collect_remaps();
return ret;
}
5 changes: 5 additions & 0 deletions include/protobuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ extern int pb_write_one(int fd, void *obj, int type);
#define pb_repeated_size(__obj, __member) \
(sizeof(*(__obj)->__member) * (__obj)->n_ ##__member)

#define pb_msg(__base, __type) \
container_of(__base, __type, base)

#include <google/protobuf-c/protobuf-c.h>

extern void do_pb_show_plain(int fd, int type, int single_entry,
Expand All @@ -87,4 +90,6 @@ extern void do_pb_show_plain(int fd, int type, int single_entry,
#define pb_show_vertical(__fd, __type) \
do_pb_show_plain(__fd, __type, 1, NULL, 0)

int collect_image(int fd_t, int obj_t, unsigned size,
int (*collect)(void *obj, ProtobufCMessage *msg));
#endif /* PROTOBUF_H__ */
74 changes: 24 additions & 50 deletions inotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,66 +210,40 @@ static int collect_mark(struct inotify_wd_info *mark)
}
}

pr_err("Can't find inotify with id 0x%08x\n", mark->iwe->id);
return -1;
}

int collect_inotify(void)
static int collect_one_ify(void *o, ProtobufCMessage *msg)
{
struct inotify_file_info *info;
struct inotify_wd_info *mark;
int image_fd = -1, ret = -1;

image_fd = open_image_ro(CR_FD_INOTIFY);
if (image_fd < 0)
return -1;

while (1) {
info = xmalloc(sizeof(*info));
if (!info)
return -1;

ret = pb_read_one_eof(image_fd, &info->ife, PB_INOTIFY);
if (ret < 0)
goto err;
else if (!ret)
break;

INIT_LIST_HEAD(&info->marks);
list_add(&info->list, &info_head);
file_desc_add(&info->d, info->ife->id, &desc_ops);
pr_info("Collected inotify: id 0x%08x flags 0x%08x\n", info->ife->id, info->ife->flags);
}
struct inotify_file_info *info = o;

close_safe(&image_fd);
info->ife = pb_msg(msg, InotifyFileEntry);
INIT_LIST_HEAD(&info->marks);
list_add(&info->list, &info_head);
file_desc_add(&info->d, info->ife->id, &desc_ops);
pr_info("Collected inotify: id 0x%08x flags 0x%08x\n", info->ife->id, info->ife->flags);

ret = -1;

image_fd = open_image_ro(CR_FD_INOTIFY_WD);
if (image_fd < 0)
goto err;
return 0;
}

while (1) {
ret = -1;
mark = xmalloc(sizeof(*mark));
if (!mark)
goto err;
static int collect_one_wd(void *o, ProtobufCMessage *msg)
{
struct inotify_wd_info *mark = o;

ret = pb_read_one_eof(image_fd, &mark->iwe, PB_INOTIFY_WD);
if (ret < 0)
goto err;
else if (!ret)
break;
mark->iwe = pb_msg(msg, InotifyWdEntry);
return collect_mark(mark);
}

if (collect_mark(mark)) {
ret = -1;
pr_err("Can't find inotify with id 0x%08x\n", mark->iwe->id);
goto err;
}
}
int collect_inotify(void)
{
int ret;

ret = 0;
err:
close_safe(&image_fd);
ret = collect_image(CR_FD_INOTIFY, PB_INOTIFY,
sizeof(struct inotify_file_info), collect_one_ify);
if (!ret)
ret = collect_image(CR_FD_INOTIFY_WD, PB_INOTIFY_WD,
sizeof(struct inotify_wd_info), collect_one_wd);

return ret;
}
Loading

0 comments on commit b914457

Please sign in to comment.