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)
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 */
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);
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;
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))
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;
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"));
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;
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);
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);
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);
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))
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) {
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);
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);
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)
FILE *f;
int maj = 0, min = 0;
- f = fopen(path, "r");
+ f = fopen(path, "r" UL_CLOEXECSTR);
if (!f)
return 0;
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;
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) {
/* 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;
}
{
int fd = sysfs_open(cxt, attr);
- return fd < 0 ? NULL : fdopen(fd, "r");
+ return fd < 0 ? NULL : fdopen(fd, "r" UL_CLOEXECSTR);
}
!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);
{
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);