Page MenuHomeFreeBSD

D45348.id139059.diff
No OneTemporary

D45348.id139059.diff

diff --git a/usr.sbin/pw/pw.8 b/usr.sbin/pw/pw.8
--- a/usr.sbin/pw/pw.8
+++ b/usr.sbin/pw/pw.8
@@ -741,6 +741,9 @@
the user, or symbolic links owned by anyone under the user's home directory.
Finally, after deleting all contents owned by the user only empty directories
will be removed.
+If the home directory is a ZFS dataset and has been emptied,
+the dataset will be destroyed.
+ZFS datasets within the home directory are not handled.
If any additional cleanup work is required, this is left to the administrator.
.El
.Pp
@@ -1077,7 +1080,8 @@
.Xr passwd 5 ,
.Xr pw.conf 5 ,
.Xr pwd_mkdb 8 ,
-.Xr vipw 8
+.Xr vipw 8 ,
+.Xr zfs 8
.Sh HISTORY
The
.Nm
diff --git a/usr.sbin/pw/pwupd.h b/usr.sbin/pw/pwupd.h
--- a/usr.sbin/pw/pwupd.h
+++ b/usr.sbin/pw/pwupd.h
@@ -139,7 +139,7 @@
void copymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid,
gid_t gid, int flags);
-void rm_r(int rootfd, char const * dir, uid_t uid);
+bool rm_r(int rootfd, char const * dir, uid_t uid);
__END_DECLS
#endif /* !_PWUPD_H */
diff --git a/usr.sbin/pw/rm_r.c b/usr.sbin/pw/rm_r.c
--- a/usr.sbin/pw/rm_r.c
+++ b/usr.sbin/pw/rm_r.c
@@ -29,32 +29,48 @@
#include <sys/stat.h>
#include <dirent.h>
+#include <err.h>
+#include <errno.h>
#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pwupd.h"
-void
+static bool try_dataset_remove(const char *home);
+
+/*
+ * "rm -r" a directory tree. If the top-level directory cannot be removed
+ * due to EBUSY, indicating that it is a ZFS dataset, and we have emptied
+ * it, destroy the dataset. Return true if any files or directories
+ * remain.
+ */
+bool
rm_r(int rootfd, const char *path, uid_t uid)
{
int dirfd;
DIR *d;
struct dirent *e;
struct stat st;
+ const char *fullpath;
+ bool skipped = false;
+ fullpath = path;
if (*path == '/')
path++;
dirfd = openat(rootfd, path, O_DIRECTORY);
if (dirfd == -1) {
- return;
+ return (true);
}
d = fdopendir(dirfd);
if (d == NULL) {
(void)close(dirfd);
- return;
+ return (true);
}
while ((e = readdir(d)) != NULL) {
if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
@@ -62,16 +78,73 @@
if (fstatat(dirfd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0)
continue;
- if (S_ISDIR(st.st_mode))
- rm_r(dirfd, e->d_name, uid);
- else if (S_ISLNK(st.st_mode) || st.st_uid == uid)
+ if (S_ISDIR(st.st_mode)) {
+ if (rm_r(dirfd, e->d_name, uid) == true)
+ skipped = true;
+ } else if (S_ISLNK(st.st_mode) || st.st_uid == uid)
unlinkat(dirfd, e->d_name, 0);
+ else
+ skipped = true;
}
closedir(d);
if (fstatat(rootfd, path, &st, AT_SYMLINK_NOFOLLOW) != 0)
- return;
- if (S_ISLNK(st.st_mode))
- unlinkat(rootfd, path, 0);
- else if (st.st_uid == uid)
- unlinkat(rootfd, path, AT_REMOVEDIR);
+ return (skipped);
+ if (S_ISLNK(st.st_mode)) {
+ if (unlinkat(rootfd, path, 0) == -1)
+ skipped = true;
+ } else if (st.st_uid == uid) {
+ if (unlinkat(rootfd, path, AT_REMOVEDIR) == -1) {
+ if (errno == EBUSY && skipped == false)
+ skipped = try_dataset_remove(fullpath);
+ else
+ skipped = true;
+ }
+ } else
+ skipped = true;
+
+ return (skipped);
+}
+
+/*
+ * If the home directory is a ZFS dataset, attempt to destroy it.
+ * Return true if the dataset is not destroyed.
+ * This would be more straightforward as a shell script.
+ */
+static bool
+try_dataset_remove(const char *home)
+{
+ char cmd[PATH_MAX + 80], dataset[PATH_MAX + 80];
+ char *newline;
+ FILE *cmdout;
+ bool skipped = true;
+
+ /* see if this is a full path (top-level directory) */
+ if (*home != '/')
+ return (skipped);
+ /* see if ZFS is loaded */
+ if (system("/sbin/kldstat -q -m zfs") != 0)
+ return (skipped);
+ /* This won't work if root dir is not / (-R option) */
+ if (strcmp(conf.rootdir, "/") != 0) {
+ warnx("cannot destroy home dataset when -R was used");
+ return (skipped);
+ }
+ /* if so, find dataset name */
+ snprintf(cmd, sizeof(cmd), "/sbin/zfs list -Ho name %s", home);
+ cmdout = popen(cmd, "r");
+ if (cmdout == NULL)
+ return (skipped);
+ if (fgets(dataset, sizeof(dataset), cmdout) == NULL) {
+ fclose(cmdout);
+ return (skipped);
+ }
+ /* If the zfs list command didn't succeed, stop. */
+ if (fclose(cmdout) != 0)
+ return (skipped);
+ if ((newline = strchr(dataset, '\n')) == NULL)
+ return (skipped);
+ *newline = '\0';
+ snprintf(cmd, sizeof(cmd), "zfs destroy %s", dataset);
+ skipped = (system(cmd) != 0);
+ return (skipped);
}

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 21, 9:44 AM (21 h, 25 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
25759213
Default Alt Text
D45348.id139059.diff (4 KB)

Event Timeline