]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: use O_CLOEXEC in libcommon
authorKarel Zak <kzak@redhat.com>
Wed, 3 Apr 2013 14:13:06 +0000 (16:13 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 3 Apr 2013 14:13:06 +0000 (16:13 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
lib/blkdev.c
lib/canonicalize.c
lib/fileutils.c
lib/ismounted.c
lib/loopdev.c
lib/path.c
lib/randutils.c
lib/sysfs.c
lib/wholedisk.c

index 514082e34f907498fe6fb1be4c301311af2d775a..09c1a2ff3d78d7c46d1080b98aa68fb1cb0d5c9a 100644 (file)
@@ -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)
index b70acd18c63b61d40a7fd07fad92349d606b966a..727806e1ece645f404bcfd3f156d7534491c67a0 100644 (file)
@@ -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 "<name>\n" from sysfs */
index ff8bb8617fbf57a26f011a58c15fa664f5252c77..92b474cef4a159dbb6013bf61045a4817c676c47 100644 (file)
@@ -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);
index d9f1f57d00a9ff7175a0421314dd61de5301301f..2463f3356a8018ec6ce7745f73af30fc63cb1783 100644 (file)
@@ -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;
index a25a2fae41dd8a4bfa273333750316610054fd04..2a696ab4d6e2791724e8c2fb9544780f9157ced3 100644 (file)
@@ -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);
index 4f955d91cda8eea92ce1bbcfd24f94386714f26d..7a68d9fe2e708abe9bfc811648ed3c20b87e0251 100644 (file)
@@ -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);
index 80893d3db941b4038e770f7b70bd368e8be4efba..ed79aad920fe49950400b99ca37e7184cd3c04f9 100644 (file)
@@ -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)
index a87e9e3d07a690dfb143bd66bcae5e9a667c1cc2..ed0173a1cc8b9145030e4ee3471d766a78777084 100644 (file)
@@ -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/<attr>". 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);
index 1dbb90c5c019e978ebbba55d251d6cd1f180fa91..5161a1e98ef5645f873298d7285356891405adc4 100644 (file)
@@ -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);