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/{null,zero} 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 27, 2021
1 parent 0bb736c commit 59cd11f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
58 changes: 56 additions & 2 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,38 @@ finish_progress(char *done)
pt_header = NULL;
}

/* This function checks if the passed fd refers to /dev/null or /dev/zero */
#ifdef __linux__
static int
is_dev_files(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);
if (res != 1) {
nfd = open("/dev/zero", O_WRONLY);
fstat(nfd, &nst);
res = (st.st_dev == nst.st_dev);
close(nfd);
}
return (res);
}
#endif

static int
is_dev_error(int err, int fd)
{
#ifdef __linux__
if (err == EINVAL && is_dev_files(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 @@ -4571,11 +4603,23 @@ zfs_do_send(int argc, char **argv)

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/{null,zero} 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/{null,zero} files.\n"));
}
return (err);
}

if (flags.skipmissing && !flags.replicate) {
Expand Down Expand Up @@ -4626,6 +4670,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/{null,zero} files.\n"));
}
return (err != 0);
}

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

return (err != 0);
}
Expand Down
1 change: 1 addition & 0 deletions lib/libzfs/libzfs_sendrecv.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
case ERANGE:
case EFAULT:
case EROFS:
case EINVAL:
zfs_error_aux(hdl, strerror(errno));
return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));

Expand Down

0 comments on commit 59cd11f

Please sign in to comment.