lib/: 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\('

FreeBSD's pidfile_open() says nothing about the flags of the files it
opens, but we can't do anything about it anyway; the implementation does
open all files with O_CLOEXEC

Consider this output with zpool.d/media appended with
"pid=$$; (ls -l /proc/$pid/fd > /dev/tty)":
  $ /sbin/zpool iostat -vc media
  lrwx------ 0 -> /dev/pts/0
  l-wx------ 1 -> 'pipe:[3278500]'
  l-wx------ 2 -> /dev/null
  lrwx------ 3 -> /dev/zfs
  lr-x------ 4 -> /proc/31895/mounts
  lrwx------ 5 -> /dev/zfs
  lr-x------ 10 -> /usr/lib/zfs-linux/zpool.d/media
vs
  $ ./zpool iostat -vc vendor,upath,iostat,media
  lrwx------ 0 -> /dev/pts/0
  l-wx------ 1 -> 'pipe:[3279887]'
  l-wx------ 2 -> /dev/null
  lr-x------ 10 -> /usr/lib/zfs-linux/zpool.d/media

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #11866
This commit is contained in:
наб
2021-04-08 22:17:38 +02:00
committed by Brian Behlendorf
parent ab88e9e264
commit 2453d0263d
22 changed files with 51 additions and 62 deletions
+5 -5
View File
@@ -66,7 +66,7 @@ static int
nfs_exports_lock(void)
{
nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
O_RDWR | O_CREAT, 0600);
O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (nfs_lock_fd == -1) {
fprintf(stderr, "failed to lock %s: %s\n",
ZFS_EXPORTS_LOCK, strerror(errno));
@@ -453,7 +453,7 @@ nfs_add_entry(const char *filename, const char *sharepath,
if (linux_opts == NULL)
linux_opts = "";
FILE *fp = fopen(filename, "a+");
FILE *fp = fopen(filename, "a+e");
if (fp == NULL) {
fprintf(stderr, "failed to open %s file: %s", filename,
strerror(errno));
@@ -489,8 +489,8 @@ nfs_copy_entries(char *filename, const char *mountpoint)
size_t buflen = 0;
int error = SA_OK;
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
FILE *newfp = fopen(filename, "w+");
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "re");
FILE *newfp = fopen(filename, "w+e");
if (newfp == NULL) {
fprintf(stderr, "failed to open %s file: %s", filename,
strerror(errno));
@@ -632,7 +632,7 @@ nfs_is_shared(sa_share_impl_t impl_share)
size_t buflen = 0;
char *buf = NULL;
FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
FILE *fp = fopen(ZFS_EXPORTS_FILE, "re");
if (fp == NULL) {
return (B_FALSE);
}
+1 -1
View File
@@ -107,7 +107,7 @@ smb_retrieve_shares(void)
if (!S_ISREG(eStat.st_mode))
continue;
if ((share_file_fp = fopen(file_path, "r")) == NULL) {
if ((share_file_fp = fopen(file_path, "re")) == NULL) {
rc = SA_SYSTEM_ERR;
goto out;
}