summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2013-04-03 16:13:06 +0200
committerKarel Zak <kzak@redhat.com>2013-04-03 16:13:06 +0200
commitb1fa3e2234fab95960eaa8499384000f189def13 (patch)
tree0db13933f5db07cb90cc40ff736829c11ad43509 /lib
parent7e3729e7509ce25b2f9eb5a78f35232b6cd9b998 (diff)
downloadutil-linux-b1fa3e2234fab95960eaa8499384000f189def13.tar.gz
lib: use O_CLOEXEC in libcommon
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/blkdev.c2
-rw-r--r--lib/canonicalize.c2
-rw-r--r--lib/fileutils.c2
-rw-r--r--lib/ismounted.c6
-rw-r--r--lib/loopdev.c10
-rw-r--r--lib/path.c8
-rw-r--r--lib/randutils.c4
-rw-r--r--lib/sysfs.c13
-rw-r--r--lib/wholedisk.c2
9 files changed, 25 insertions, 24 deletions
diff --git a/lib/blkdev.c b/lib/blkdev.c
index 514082e3..09c1a2ff 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 b70acd18..727806e1 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 "<name>\n" from sysfs */
diff --git a/lib/fileutils.c b/lib/fileutils.c
index ff8bb861..92b474ce 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 d9f1f57d..2463f335 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 a25a2fae..2a696ab4 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 4f955d91..7a68d9fe 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 80893d3d..ed79aad9 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 a87e9e3d..ed0173a1 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/<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);
diff --git a/lib/wholedisk.c b/lib/wholedisk.c
index 1dbb90c5..5161a1e9 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);