Skip to content

Commit 668d447

Browse files
lib{spl,[uz]util}: set O_CLOEXEC on all fds
As found by git grep -E '(open|setmntent|pipe2?)\(' | grep -vE '((zfs|zpool)_|fd|dl|lzc_re|pidfile_|g_)open\(' uu_open_tmp() is unused here and seems very unmaintained, maybe it's time to get rid of it? Signed-off-by: Ahelenia Ziemiańska <[email protected]>
1 parent 29f37f4 commit 668d447

File tree

7 files changed

+12
-17
lines changed

7 files changed

+12
-17
lines changed

lib/libspl/os/linux/gethostid.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ get_spl_hostid(void)
4545
return (hostid & HOSTID_MASK);
4646
}
4747

48-
f = fopen("/sys/module/spl/parameters/spl_hostid", "r");
48+
f = fopen("/sys/module/spl/parameters/spl_hostid", "re");
4949
if (!f)
5050
return (0);
5151

@@ -74,7 +74,7 @@ get_system_hostid(void)
7474
unsigned long hostid;
7575
int hostid_size = 4; /* 4 bytes regardless of arch */
7676

77-
fd = open("/etc/hostid", O_RDONLY);
77+
fd = open("/etc/hostid", O_RDONLY | O_CLOEXEC);
7878
if (fd >= 0) {
7979
rc = read(fd, &hostid, hostid_size);
8080
if (rc > 0)

lib/libspl/os/linux/getmntany.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ getextmntent(const char *path, struct extmnttab *entry, struct stat64 *statbuf)
128128

129129

130130
#ifdef HAVE_SETMNTENT
131-
if ((fp = setmntent(MNTTAB, "r")) == NULL) {
131+
if ((fp = setmntent(MNTTAB, "re")) == NULL) {
132132
#else
133-
if ((fp = fopen(MNTTAB, "r")) == NULL) {
133+
if ((fp = fopen(MNTTAB, "re")) == NULL) {
134134
#endif
135135
(void) fprintf(stderr, "cannot open %s\n", MNTTAB);
136136
return (-1);

lib/libuutil/uu_open.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@
3636
#include <stdio.h>
3737
#include <unistd.h>
3838

39-
#ifdef _LP64
40-
#define TMPPATHFMT "%s/uu%ld"
41-
#else /* _LP64 */
42-
#define TMPPATHFMT "%s/uu%lld"
43-
#endif /* _LP64 */
44-
4539
/*ARGSUSED*/
4640
int
4741
uu_open_tmp(const char *dir, uint_t uflags)
@@ -55,7 +49,7 @@ uu_open_tmp(const char *dir, uint_t uflags)
5549
for (;;) {
5650
(void) snprintf(fname, PATH_MAX, "%s/uu%lld", dir, gethrtime());
5751

58-
f = open(fname, O_CREAT | O_EXCL | O_RDWR, 0600);
52+
f = open(fname, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0600);
5953

6054
if (f >= 0 || errno != EEXIST)
6155
break;

lib/libzutil/os/freebsd/zutil_import_os.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ zpool_open_func(void *arg)
127127
/*
128128
* O_NONBLOCK so we don't hang trying to open things like serial ports.
129129
*/
130-
if ((fd = open(rn->rn_name, O_RDONLY|O_NONBLOCK)) < 0)
130+
if ((fd = open(rn->rn_name, O_RDONLY|O_NONBLOCK|O_CLOEXEC)) < 0)
131131
return;
132132

133133
/*

lib/libzutil/os/linux/zutil_device_path_os.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ zfs_dev_is_whole_disk(const char *dev_name)
390390
struct dk_gpt *label;
391391
int fd;
392392

393-
if ((fd = open(dev_name, O_RDONLY | O_DIRECT)) < 0)
393+
if ((fd = open(dev_name, O_RDONLY | O_DIRECT | O_CLOEXEC)) < 0)
394394
return (B_FALSE);
395395

396396
if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {

lib/libzutil/os/linux/zutil_import_os.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ zpool_open_func(void *arg)
136136
* cache which may be stale for multipath devices. An EINVAL errno
137137
* indicates O_DIRECT is unsupported so fallback to just O_RDONLY.
138138
*/
139-
fd = open(rn->rn_name, O_RDONLY | O_DIRECT);
139+
fd = open(rn->rn_name, O_RDONLY | O_DIRECT | O_CLOEXEC);
140140
if ((fd < 0) && (errno == EINVAL))
141-
fd = open(rn->rn_name, O_RDONLY);
141+
fd = open(rn->rn_name, O_RDONLY | O_CLOEXEC);
142142
if ((fd < 0) && (errno == EACCES))
143143
hdl->lpc_open_access_error = B_TRUE;
144144
if (fd < 0)

lib/libzutil/zutil_import.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,8 @@ zpool_find_import_impl(libpc_handle_t *hdl, importargs_t *iarg,
13451345
* would prevent a zdb -e of active pools with
13461346
* no cachefile.
13471347
*/
1348-
fd = open(slice->rn_name, O_RDONLY | O_EXCL);
1348+
fd = open(slice->rn_name,
1349+
O_RDONLY | O_EXCL | O_CLOEXEC);
13491350
if (fd >= 0 || iarg->can_be_active) {
13501351
if (fd >= 0)
13511352
close(fd);
@@ -1437,7 +1438,7 @@ zpool_find_import_cached(libpc_handle_t *hdl, importargs_t *iarg)
14371438

14381439
verify(iarg->poolname == NULL || iarg->guid == 0);
14391440

1440-
if ((fd = open(iarg->cachefile, O_RDONLY)) < 0) {
1441+
if ((fd = open(iarg->cachefile, O_RDONLY | O_CLOEXEC)) < 0) {
14411442
zutil_error_aux(hdl, "%s", strerror(errno));
14421443
(void) zutil_error(hdl, EZFS_BADCACHE,
14431444
dgettext(TEXT_DOMAIN, "failed to open cache file"));

0 commit comments

Comments
 (0)