From 2e91a9c8145a5ff42bfcb15543aee8a03f4d05d5 Mon Sep 17 00:00:00 2001 From: Pavel Emelyanov Date: Wed, 29 Oct 2014 15:44:00 +0400 Subject: [PATCH] bfd: Don't flush read-only images Signed-off-by: Pavel Emelyanov --- bfd.c | 11 +++-------- image.c | 2 +- include/bfd.h | 3 ++- proc_parse.c | 6 +++--- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/bfd.c b/bfd.c index 3e1a3a62d..774368f4f 100644 --- a/bfd.c +++ b/bfd.c @@ -86,13 +86,14 @@ static void buf_put(struct xbuf *xb) xb->data = NULL; } -int bfdopen(struct bfd *f) +int bfdopen(struct bfd *f, int mode) { if (buf_get(&f->b)) { close(f->fd); return -1; } + f->mode = mode; return 0; } @@ -107,13 +108,7 @@ int bfd_flush_images(void) void bclose(struct bfd *f) { if (bfd_buffered(f)) { - /* - * FIXME -- when we bread from images we don't - * need to flush. This call doesn't fail simply - * becase we read _all_ data from them and the - * b->sz would be 0 by the time we close them. - */ - if (bflush(f) < 0) { + if ((f->mode != O_RDONLY) && bflush(f) < 0) { /* * This is to propagate error up. It's * hardly possible by returning and diff --git a/image.c b/image.c index f0cf3e4de..36f726dae 100644 --- a/image.c +++ b/image.c @@ -235,7 +235,7 @@ struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...) img->_x.fd = ret; if (oflags & O_NOBUF) bfd_setraw(&img->_x); - else if (bfdopen(&img->_x)) + else if (bfdopen(&img->_x, flags)) goto err; if (imgset_template[type].magic == RAW_IMAGE_MAGIC) diff --git a/include/bfd.h b/include/bfd.h index 55e2e9d7c..cd2f4ad5a 100644 --- a/include/bfd.h +++ b/include/bfd.h @@ -13,6 +13,7 @@ struct xbuf { struct bfd { int fd; + int mode; struct xbuf b; }; @@ -26,7 +27,7 @@ static inline void bfd_setraw(struct bfd *b) b->b.mem = NULL; } -int bfdopen(struct bfd *f); +int bfdopen(struct bfd *f, int mode); void bclose(struct bfd *f); char *breadline(struct bfd *f); int bwrite(struct bfd *f, const void *buf, int sz); diff --git a/proc_parse.c b/proc_parse.c index a53408cb7..12a72172e 100644 --- a/proc_parse.c +++ b/proc_parse.c @@ -313,7 +313,7 @@ int parse_smaps(pid_t pid, struct vm_area_list *vma_area_list, bool use_map_file if (f.fd < 0) goto err_n; - if (bfdopen(&f)) + if (bfdopen(&f, O_RDONLY)) goto err_n; if (use_map_files) { @@ -1158,7 +1158,7 @@ static int parse_fdinfo_pid_s(int pid, int fd, int type, return -1; } - if (bfdopen(&f)) + if (bfdopen(&f, O_RDONLY)) return -1; while (1) { @@ -1613,7 +1613,7 @@ int parse_posix_timers(pid_t pid, struct proc_posix_timers_stat *args) return -1; } - if (bfdopen(&f)) + if (bfdopen(&f, O_RDONLY)) return -1; while (1) {