lib: use O_CLOEXEC in libcommon

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2013-04-03 16:13:06 +02:00
parent 7e3729e750
commit b1fa3e2234
9 changed files with 25 additions and 24 deletions

View file

@ -353,7 +353,7 @@ main(int argc, char **argv)
exit(EXIT_FAILURE); 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]); err(EXIT_FAILURE, "open %s failed", argv[1]);
if (blkdev_get_size(fd, &bytes) < 0) if (blkdev_get_size(fd, &bytes) < 0)

View file

@ -152,7 +152,7 @@ canonicalize_dm_name(const char *ptname)
char path[256], name[256], *res = NULL; char path[256], name[256], *res = NULL;
snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname); snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
if (!(f = fopen(path, "r"))) if (!(f = fopen(path, "r" UL_CLOEXECSTR)))
return NULL; return NULL;
/* read "<name>\n" from sysfs */ /* read "<name>\n" from sysfs */

View file

@ -37,7 +37,7 @@ int xmkstemp(char **tmpname, char *dir)
xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP, xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
program_invocation_short_name); program_invocation_short_name);
old_mode = umask(077); old_mode = umask(077);
fd = mkstemp(localtmp); fd = mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
umask(old_mode); umask(old_mode);
if (fd == -1) { if (fd == -1) {
free(localtmp); free(localtmp);

View file

@ -156,7 +156,7 @@ static int check_mntent_file(const char *mtab_file, const char *file,
is_root: is_root:
#define TEST_FILE "/.ismount-test-file" #define TEST_FILE "/.ismount-test-file"
*mount_flags |= MF_ISROOT; *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 (fd < 0) {
if (errno == EROFS) if (errno == EROFS)
*mount_flags |= MF_READONLY; *mount_flags |= MF_READONLY;
@ -261,7 +261,7 @@ static int is_swap_device(const char *file)
file_dev = st_buf.st_rdev; file_dev = st_buf.st_rdev;
#endif /* __GNU__ */ #endif /* __GNU__ */
if (!(f = fopen("/proc/swaps", "r"))) if (!(f = fopen("/proc/swaps", "r" UL_CLOEXECSTR)))
return 0; return 0;
/* Skip the first line */ /* Skip the first line */
if (!fgets(buf, sizeof(buf), f)) 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) || if ((stat(device, &st_buf) != 0) ||
!S_ISBLK(st_buf.st_mode)) !S_ISBLK(st_buf.st_mode))
return 0; return 0;
fd = open(device, O_RDONLY | O_EXCL); fd = open(device, O_RDONLY|O_EXCL|O_CLOEXEC);
if (fd < 0) { if (fd < 0) {
if (errno == EBUSY) if (errno == EBUSY)
*mount_flags |= MF_BUSY; *mount_flags |= MF_BUSY;

View file

@ -289,7 +289,7 @@ int loopcxt_get_fd(struct loopdev_cxt *lc)
if (lc->fd < 0) { if (lc->fd < 0) {
lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY; 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, DBG(lc, loopdev_debug("open %s [%s]: %s", lc->device,
lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro", lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro",
lc->fd < 0 ? "failed" : "ok")); 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")); DBG(lc, loopdev_debug("iter: scan /proc/partitions"));
if (!iter->proc) if (!iter->proc)
iter->proc = fopen(_PATH_PROC_PARTITIONS, "r"); iter->proc = fopen(_PATH_PROC_PARTITIONS, "r" UL_CLOEXECSTR);
if (!iter->proc) if (!iter->proc)
return 1; return 1;
@ -867,7 +867,7 @@ int loopmod_supports_partscan(void)
if (get_linux_version() >= KERNEL_VERSION(3,2,0)) if (get_linux_version() >= KERNEL_VERSION(3,2,0))
return 1; return 1;
f = fopen("/sys/module/loop/parameters/max_part", "r"); f = fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR);
if (!f) if (!f)
return 0; return 0;
rc = fscanf(f, "%d", &ret); 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) if (lc->info.lo_flags & LO_FLAGS_READ_ONLY)
mode = O_RDONLY; 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)) if (mode != O_RDONLY && (errno == EROFS || errno == EACCES))
file_fd = open(lc->filename, mode = O_RDONLY); 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")); DBG(lc, loopdev_debug("find_unused requested"));
if (lc->flags & LOOPDEV_FL_CONTROL) { 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) if (ctl >= 0)
rc = ioctl(ctl, LOOP_CTL_GET_FREE); rc = ioctl(ctl, LOOP_CTL_GET_FREE);

View file

@ -93,7 +93,7 @@ path_read_str(char *result, size_t len, const char *path, ...)
va_list ap; va_list ap;
va_start(ap, path); va_start(ap, path);
fd = path_vfopen("r", 1, path, ap); fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
va_end(ap); va_end(ap);
if (!fgets(result, len, fd)) if (!fgets(result, len, fd))
@ -113,7 +113,7 @@ path_read_s32(const char *path, ...)
int result; int result;
va_start(ap, path); va_start(ap, path);
fd = path_vfopen("r", 1, path, ap); fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
va_end(ap); va_end(ap);
if (fscanf(fd, "%d", &result) != 1) { if (fscanf(fd, "%d", &result) != 1) {
@ -154,7 +154,7 @@ path_write_str(const char *str, const char *path, ...)
va_list ap; va_list ap;
va_start(ap, path); va_start(ap, path);
fd = path_vopen(O_WRONLY, path, ap); fd = path_vopen(O_WRONLY|O_CLOEXEC, path, ap);
va_end(ap); va_end(ap);
result = write_all(fd, str, strlen(str)); result = write_all(fd, str, strlen(str));
close(fd); close(fd);
@ -184,7 +184,7 @@ path_cpuparse(int maxcpus, int islist, const char *path, va_list ap)
size_t setsize, len = maxcpus * 7; size_t setsize, len = maxcpus * 7;
char buf[len]; char buf[len];
fd = path_vfopen("r", 1, path, ap); fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
if (!fgets(buf, len, fd)) if (!fgets(buf, len, fd))
err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);

View file

@ -34,9 +34,9 @@ int random_get_fd(void)
struct timeval tv; struct timeval tv;
gettimeofday(&tv, 0); gettimeofday(&tv, 0);
fd = open("/dev/urandom", O_RDONLY); fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
if (fd == -1) if (fd == -1)
fd = open("/dev/random", O_RDONLY | O_NONBLOCK); fd = open("/dev/random", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (fd >= 0) { if (fd >= 0) {
i = fcntl(fd, F_GETFD); i = fcntl(fd, F_GETFD);
if (i >= 0) if (i >= 0)

View file

@ -88,7 +88,7 @@ dev_t sysfs_devname_to_devno(const char *name, const char *parent)
FILE *f; FILE *f;
int maj = 0, min = 0; int maj = 0, min = 0;
f = fopen(path, "r"); f = fopen(path, "r" UL_CLOEXECSTR);
if (!f) if (!f)
return 0; 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))) if (!sysfs_devno_path(devno, path, sizeof(path)))
goto err; goto err;
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY|O_CLOEXEC);
if (fd < 0) if (fd < 0)
goto err; goto err;
cxt->dir_fd = fd; 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) 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 && if (fd == -1 && errno == ENOENT &&
strncmp(attr, "queue/", 6) == 0 && cxt->parent) { 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 /* Exception for "queue/<attr>". These attributes are available
* for parental devices only * 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; return fd;
} }
@ -264,7 +265,7 @@ static FILE *sysfs_fopen(struct sysfs_cxt *cxt, const char *attr)
{ {
int fd = sysfs_open(cxt, 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)) !sysfs_scsi_host_attribute_path(cxt, type, buf, sizeof(buf), attr))
return NULL; return NULL;
if (!(f = fopen(buf, "r"))) if (!(f = fopen(buf, "r" UL_CLOEXECSTR)))
return NULL; return NULL;
rc = fscanf(f, "%1023[^\n]", buf); rc = fscanf(f, "%1023[^\n]", buf);

View file

@ -37,7 +37,7 @@ int is_whole_disk(const char *name)
{ {
int fd = -1, res = 0; int fd = -1, res = 0;
#ifdef HDIO_GETGEO #ifdef HDIO_GETGEO
fd = open(name, O_RDONLY); fd = open(name, O_RDONLY|O_CLOEXEC);
if (fd != -1) if (fd != -1)
#endif #endif
res = is_whole_disk_fd(fd, name); res = is_whole_disk_fd(fd, name);