Skip to content

Commit

Permalink
Added error for writing to /dev/ on Linux.
Browse files Browse the repository at this point in the history
Starting in Linux 5.10, trying to write to /dev/ files errors out.
Prefer to inform people when this happens rather than hoping they
guess what's wrong.

Signed-off-by: Rich Ercolani <[email protected]>
  • Loading branch information
rincebrain committed May 3, 2021
1 parent c903a75 commit 1ddd9a0
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,29 @@ finish_progress(char *done)
pt_header = NULL;
}

static int
is_dev_fs(int fd)
{
int nfd = open("/dev/null", O_WRONLY);
struct stat st, nst;
fstat(fd, &st);
fstat(nfd, &nst);
int res = (st.st_dev == nst.st_dev);
close(nfd);
return (res);
}

static int
is_dev_error(int err, int fd)
{
#ifdef __linux__
if (err == EINVAL && is_dev_fs(fd)) {
return (1);
}
#endif
return (0);
}

static int
zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
{
Expand Down Expand Up @@ -4563,19 +4586,30 @@ zfs_do_send(int argc, char **argv)
"You must redirect standard output.\n"));
return (1);
}

if (flags.saved) {
zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
if (zhp == NULL)
return (1);

err = zfs_send_saved(zhp, &flags, STDOUT_FILENO,
resume_token);
if (err != 0 && is_dev_error(errno, STDOUT_FILENO)) {
(void) fprintf(stderr,
gettext("Error: Stream cannot be written to /dev/"
" files.\n"));

}
zfs_close(zhp);
return (err != 0);
} else if (resume_token != NULL) {
return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
resume_token));
err = zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
resume_token);
if (err != 0 && is_dev_error(errno, STDOUT_FILENO)) {
(void) fprintf(stderr,
gettext("Error: Stream cannot be written to /dev/"
" files.\n"));
}
return (err);
}

if (flags.skipmissing && !flags.replicate) {
Expand Down Expand Up @@ -4626,6 +4660,11 @@ zfs_do_send(int argc, char **argv)
err = zfs_send_one(zhp, fromname, STDOUT_FILENO, &flags,
redactbook);
zfs_close(zhp);
if (err != 0 && is_dev_error(errno, STDOUT_FILENO)) {
(void) fprintf(stderr,
gettext("Error: Stream cannot be written to /dev/"
" files.\n"));
}
return (err != 0);
}

Expand Down Expand Up @@ -4702,6 +4741,11 @@ zfs_do_send(int argc, char **argv)
nvlist_free(dbgnv);
}
zfs_close(zhp);
if (err != 0 && is_dev_error(errno, STDOUT_FILENO)) {
(void) fprintf(stderr,
gettext("Error: Stream cannot be written to /dev/ "
"files.\n"));
}

return (err != 0);
}
Expand Down

0 comments on commit 1ddd9a0

Please sign in to comment.