From b1fa3e2234fab95960eaa8499384000f189def13 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 3 Apr 2013 16:13:06 +0200 Subject: [PATCH] lib: use O_CLOEXEC in libcommon Signed-off-by: Karel Zak --- lib/blkdev.c | 2 +- lib/canonicalize.c | 2 +- lib/fileutils.c | 2 +- lib/ismounted.c | 6 +++--- lib/loopdev.c | 10 +++++----- lib/path.c | 8 ++++---- lib/randutils.c | 4 ++-- lib/sysfs.c | 13 +++++++------ lib/wholedisk.c | 2 +- 9 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/blkdev.c b/lib/blkdev.c index 514082e34f..09c1a2ff3d 100644 --- a/lib/blkdev.c +++ b/lib/blkdev.c @@ -353,7 +353,7 @@ main(int argc, char **argv) exit(EXIT_FAILURE); } - if ((fd = open(argv[1], O_RDONLY)) < 0) + if ((fd = open(argv[1], O_RDONLY|O_CLOEXEC)) < 0) err(EXIT_FAILURE, "open %s failed", argv[1]); if (blkdev_get_size(fd, &bytes) < 0) diff --git a/lib/canonicalize.c b/lib/canonicalize.c index b70acd18c6..727806e1ec 100644 --- a/lib/canonicalize.c +++ b/lib/canonicalize.c @@ -152,7 +152,7 @@ canonicalize_dm_name(const char *ptname) char path[256], name[256], *res = NULL; snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname); - if (!(f = fopen(path, "r"))) + if (!(f = fopen(path, "r" UL_CLOEXECSTR))) return NULL; /* read "\n" from sysfs */ diff --git a/lib/fileutils.c b/lib/fileutils.c index ff8bb8617f..92b474cef4 100644 --- a/lib/fileutils.c +++ b/lib/fileutils.c @@ -37,7 +37,7 @@ int xmkstemp(char **tmpname, char *dir) xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP, program_invocation_short_name); old_mode = umask(077); - fd = mkstemp(localtmp); + fd = mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC); umask(old_mode); if (fd == -1) { free(localtmp); diff --git a/lib/ismounted.c b/lib/ismounted.c index d9f1f57d00..2463f3356a 100644 --- a/lib/ismounted.c +++ b/lib/ismounted.c @@ -156,7 +156,7 @@ static int check_mntent_file(const char *mtab_file, const char *file, is_root: #define TEST_FILE "/.ismount-test-file" *mount_flags |= MF_ISROOT; - fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600); + fd = open(TEST_FILE, O_RDWR|O_CREAT|O_CLOEXEC, 0600); if (fd < 0) { if (errno == EROFS) *mount_flags |= MF_READONLY; @@ -261,7 +261,7 @@ static int is_swap_device(const char *file) file_dev = st_buf.st_rdev; #endif /* __GNU__ */ - if (!(f = fopen("/proc/swaps", "r"))) + if (!(f = fopen("/proc/swaps", "r" UL_CLOEXECSTR))) return 0; /* Skip the first line */ if (!fgets(buf, sizeof(buf), f)) @@ -339,7 +339,7 @@ int check_mount_point(const char *device, int *mount_flags, if ((stat(device, &st_buf) != 0) || !S_ISBLK(st_buf.st_mode)) return 0; - fd = open(device, O_RDONLY | O_EXCL); + fd = open(device, O_RDONLY|O_EXCL|O_CLOEXEC); if (fd < 0) { if (errno == EBUSY) *mount_flags |= MF_BUSY; diff --git a/lib/loopdev.c b/lib/loopdev.c index a25a2fae41..2a696ab4d6 100644 --- a/lib/loopdev.c +++ b/lib/loopdev.c @@ -289,7 +289,7 @@ int loopcxt_get_fd(struct loopdev_cxt *lc) if (lc->fd < 0) { lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY; - lc->fd = open(lc->device, lc->mode); + lc->fd = open(lc->device, lc->mode | O_CLOEXEC); DBG(lc, loopdev_debug("open %s [%s]: %s", lc->device, lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro", lc->fd < 0 ? "failed" : "ok")); @@ -492,7 +492,7 @@ static int loopcxt_next_from_proc(struct loopdev_cxt *lc) DBG(lc, loopdev_debug("iter: scan /proc/partitions")); if (!iter->proc) - iter->proc = fopen(_PATH_PROC_PARTITIONS, "r"); + iter->proc = fopen(_PATH_PROC_PARTITIONS, "r" UL_CLOEXECSTR); if (!iter->proc) return 1; @@ -867,7 +867,7 @@ int loopmod_supports_partscan(void) if (get_linux_version() >= KERNEL_VERSION(3,2,0)) return 1; - f = fopen("/sys/module/loop/parameters/max_part", "r"); + f = fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR); if (!f) return 0; rc = fscanf(f, "%d", &ret); @@ -1104,7 +1104,7 @@ int loopcxt_setup_device(struct loopdev_cxt *lc) if (lc->info.lo_flags & LO_FLAGS_READ_ONLY) mode = O_RDONLY; - if ((file_fd = open(lc->filename, mode)) < 0) { + if ((file_fd = open(lc->filename, mode | O_CLOEXEC)) < 0) { if (mode != O_RDONLY && (errno == EROFS || errno == EACCES)) file_fd = open(lc->filename, mode = O_RDONLY); @@ -1205,7 +1205,7 @@ int loopcxt_find_unused(struct loopdev_cxt *lc) DBG(lc, loopdev_debug("find_unused requested")); if (lc->flags & LOOPDEV_FL_CONTROL) { - int ctl = open(_PATH_DEV_LOOPCTL, O_RDWR); + int ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC); if (ctl >= 0) rc = ioctl(ctl, LOOP_CTL_GET_FREE); diff --git a/lib/path.c b/lib/path.c index 4f955d91cd..7a68d9fe2e 100644 --- a/lib/path.c +++ b/lib/path.c @@ -93,7 +93,7 @@ path_read_str(char *result, size_t len, const char *path, ...) va_list ap; va_start(ap, path); - fd = path_vfopen("r", 1, path, ap); + fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap); va_end(ap); if (!fgets(result, len, fd)) @@ -113,7 +113,7 @@ path_read_s32(const char *path, ...) int result; va_start(ap, path); - fd = path_vfopen("r", 1, path, ap); + fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap); va_end(ap); if (fscanf(fd, "%d", &result) != 1) { @@ -154,7 +154,7 @@ path_write_str(const char *str, const char *path, ...) va_list ap; va_start(ap, path); - fd = path_vopen(O_WRONLY, path, ap); + fd = path_vopen(O_WRONLY|O_CLOEXEC, path, ap); va_end(ap); result = write_all(fd, str, strlen(str)); close(fd); @@ -184,7 +184,7 @@ path_cpuparse(int maxcpus, int islist, const char *path, va_list ap) size_t setsize, len = maxcpus * 7; char buf[len]; - fd = path_vfopen("r", 1, path, ap); + fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap); if (!fgets(buf, len, fd)) err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); diff --git a/lib/randutils.c b/lib/randutils.c index 80893d3db9..ed79aad920 100644 --- a/lib/randutils.c +++ b/lib/randutils.c @@ -34,9 +34,9 @@ int random_get_fd(void) struct timeval tv; gettimeofday(&tv, 0); - fd = open("/dev/urandom", O_RDONLY); + fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); if (fd == -1) - fd = open("/dev/random", O_RDONLY | O_NONBLOCK); + fd = open("/dev/random", O_RDONLY | O_NONBLOCK | O_CLOEXEC); if (fd >= 0) { i = fcntl(fd, F_GETFD); if (i >= 0) diff --git a/lib/sysfs.c b/lib/sysfs.c index a87e9e3d07..ed0173a1cc 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -88,7 +88,7 @@ dev_t sysfs_devname_to_devno(const char *name, const char *parent) FILE *f; int maj = 0, min = 0; - f = fopen(path, "r"); + f = fopen(path, "r" UL_CLOEXECSTR); if (!f) return 0; @@ -149,7 +149,7 @@ int sysfs_init(struct sysfs_cxt *cxt, dev_t devno, struct sysfs_cxt *parent) if (!sysfs_devno_path(devno, path, sizeof(path))) goto err; - fd = open(path, O_RDONLY); + fd = open(path, O_RDONLY|O_CLOEXEC); if (fd < 0) goto err; cxt->dir_fd = fd; @@ -205,7 +205,7 @@ int sysfs_has_attribute(struct sysfs_cxt *cxt, const char *attr) static int sysfs_open(struct sysfs_cxt *cxt, const char *attr) { - int fd = open_at(cxt->dir_fd, cxt->dir_path, attr, O_RDONLY); + int fd = open_at(cxt->dir_fd, cxt->dir_path, attr, O_RDONLY|O_CLOEXEC); if (fd == -1 && errno == ENOENT && strncmp(attr, "queue/", 6) == 0 && cxt->parent) { @@ -213,7 +213,8 @@ static int sysfs_open(struct sysfs_cxt *cxt, const char *attr) /* Exception for "queue/". These attributes are available * for parental devices only */ - fd = open_at(cxt->parent->dir_fd, cxt->dir_path, attr, O_RDONLY); + fd = open_at(cxt->parent->dir_fd, cxt->dir_path, attr, + O_RDONLY|O_CLOEXEC); } return fd; } @@ -264,7 +265,7 @@ static FILE *sysfs_fopen(struct sysfs_cxt *cxt, const char *attr) { int fd = sysfs_open(cxt, attr); - return fd < 0 ? NULL : fdopen(fd, "r"); + return fd < 0 ? NULL : fdopen(fd, "r" UL_CLOEXECSTR); } @@ -706,7 +707,7 @@ char *sysfs_scsi_host_strdup_attribute(struct sysfs_cxt *cxt, !sysfs_scsi_host_attribute_path(cxt, type, buf, sizeof(buf), attr)) return NULL; - if (!(f = fopen(buf, "r"))) + if (!(f = fopen(buf, "r" UL_CLOEXECSTR))) return NULL; rc = fscanf(f, "%1023[^\n]", buf); diff --git a/lib/wholedisk.c b/lib/wholedisk.c index 1dbb90c5c0..5161a1e98e 100644 --- a/lib/wholedisk.c +++ b/lib/wholedisk.c @@ -37,7 +37,7 @@ int is_whole_disk(const char *name) { int fd = -1, res = 0; #ifdef HDIO_GETGEO - fd = open(name, O_RDONLY); + fd = open(name, O_RDONLY|O_CLOEXEC); if (fd != -1) #endif res = is_whole_disk_fd(fd, name); -- 2.47.2