From: Tobias Stoeckmann Date: Sun, 4 Jan 2026 15:28:39 +0000 (+0100) Subject: setpwnam: only unlock when actually locked X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7aa54eeeb07bb27f151e42dd91085256228e281;p=thirdparty%2Futil-linux.git setpwnam: only unlock when actually locked If no lock is held, do not unlock. With glibc this is fine, but let's not put too many expectations on library implementations. Signed-off-by: Tobias Stoeckmann --- diff --git a/login-utils/setpwnam.c b/login-utils/setpwnam.c index d2ecb00ea..f95a7726d 100644 --- a/login-utils/setpwnam.c +++ b/login-utils/setpwnam.c @@ -75,7 +75,7 @@ int setpwnam(struct passwd *pwd, const char *prefix) { FILE *fp = NULL, *pwf = NULL; int save_errno, rc; - uint8_t found = 0; + uint8_t found = 0, locked = 0; size_t namelen; size_t contlen; size_t buflen = 256; @@ -94,6 +94,7 @@ int setpwnam(struct passwd *pwd, const char *prefix) /* acquire exclusive lock */ if (lckpwdf() < 0) goto fail; + locked = 1; pwf = fopen(PASSWD_FILE, "r"); if (!pwf) goto fail; @@ -174,7 +175,8 @@ int setpwnam(struct passwd *pwd, const char *prefix) fail: save_errno = errno; - ulckpwdf(); + if (locked) + ulckpwdf(); if (fp != NULL) fclose(fp); if (tmpname != NULL)