git: ba391775e8ac - main - pw: remove crontab with unlinkat instead of spawning crontab

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Fri, 04 Sep 2026 14:28:57 UTC
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=ba391775e8acb7a539c18f1231a396a7d0d763e9

commit ba391775e8acb7a539c18f1231a396a7d0d763e9
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2026-09-04 12:56:37 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2026-09-04 14:28:47 +0000

    pw: remove crontab with unlinkat instead of spawning crontab
    
    crontab -r only unlinks the crontab file, so spawn it directly with
    unlinkat() relative to conf.rootfd.  This also makes the crontab
    removal work with pw -R.
    
    MFC After:      1 week
---
 usr.sbin/pw/pw_user.c                | 26 +++++---------------------
 usr.sbin/pw/tests/pw_userdel_test.sh | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/usr.sbin/pw/pw_user.c b/usr.sbin/pw/pw_user.c
index 9c48ef3fcb5e..26e414831448 100644
--- a/usr.sbin/pw/pw_user.c
+++ b/usr.sbin/pw/pw_user.c
@@ -28,7 +28,6 @@
  */
 
 #include <sys/param.h>
-#include <sys/wait.h>
 
 #include <assert.h>
 #include <ctype.h>
@@ -45,7 +44,6 @@
 #include <sysexits.h>
 #include <termios.h>
 #include <unistd.h>
-#include <spawn.h>
 
 #include "pw.h"
 #include "bitmap.h"
@@ -53,7 +51,6 @@
 
 #define LOGNAMESIZE (MAXLOGNAME-1)
 
-extern char **environ;
 static		char locked_str[] = "*LOCKED*";
 
 static struct passwd fakeuser = {
@@ -926,25 +923,12 @@ pw_user_del(int argc, char **argv, char *arg1)
 	if (strcmp(pwd->pw_name, "root") == 0)
 		errx(EX_DATAERR, "cannot remove user 'root'");
 
-	if (!PWALTDIR()) {
+	if (PWALTDIR() != PWF_ALT) {
 		/* Remove crontabs */
-		snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
-		if (access(file, F_OK) == 0) {
-			const char *argv[] = {
-				"crontab",
-				"-u",
-				pwd->pw_name,
-				"-r",
-				NULL
-			};
-			pid_t pid;
-
-			if (posix_spawnp(&pid, argv[0], NULL, NULL,
-						(char *const *) argv, environ)) {
-				warn("Failed to execute '%s %s'",
-						argv[0], argv[1]);
-			} else
-				(void) waitpid(pid, NULL, 0);
+		int cfd = openat(conf.rootfd, "var/cron/tabs", O_DIRECTORY | O_CLOEXEC);
+		if (cfd != -1) {
+			unlinkat(cfd, pwd->pw_name, 0);
+			close(cfd);
 		}
 	}
 
diff --git a/usr.sbin/pw/tests/pw_userdel_test.sh b/usr.sbin/pw/tests/pw_userdel_test.sh
index 647384615fb4..23c85ef185cb 100755
--- a/usr.sbin/pw/tests/pw_userdel_test.sh
+++ b/usr.sbin/pw/tests/pw_userdel_test.sh
@@ -110,6 +110,23 @@ delete_at_jobs_cleanup() {
 	rm -rf ${HOME}/var/at/jobs
 }
 
+atf_test_case delete_crontab cleanup
+delete_crontab_body() {
+	populate_root_etc_skel
+
+	mkdir -p ${HOME}/var/cron/tabs
+
+	atf_check -s exit:0 ${RPW} useradd foo
+
+	atf_check -s exit:0 touch ${HOME}/var/cron/tabs/foo
+	atf_check -s exit:0 ${RPW} userdel foo
+
+	[ ! -e ${HOME}/var/cron/tabs/foo ] || atf_fail "crontab not removed"
+}
+delete_crontab_cleanup() {
+	rm -rf ${HOME}/var/cron/tabs
+}
+
 atf_init_test_cases() {
 	atf_add_test_case rmuser_seperate_group
 	atf_add_test_case user_do_not_try_to_delete_root_if_user_unknown
@@ -119,4 +136,5 @@ atf_init_test_cases() {
 	atf_add_test_case home_shared
 	atf_add_test_case home_regular_dir
 	atf_add_test_case delete_at_jobs
+	atf_add_test_case delete_crontab
 }