about summary refs log tree commit diff
path: root/src/passwd/nscd_query.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2015-06-09 20:09:27 +0000
committerRich Felker <dalias@aerifal.cx>2015-06-09 20:15:49 +0000
commitbd1eaceaa3975bd2a2a34e211cff896affaecadf (patch)
treefcaa79adef524e52c3beb6b9b4f31a7a2ad775bb /src/passwd/nscd_query.c
parent75ce4503950621b11fcc7f1fd1187dbcf3cde312 (diff)
downloadmusl-bd1eaceaa3975bd2a2a34e211cff896affaecadf.tar.gz
musl-bd1eaceaa3975bd2a2a34e211cff896affaecadf.tar.xz
musl-bd1eaceaa3975bd2a2a34e211cff896affaecadf.zip
fix spurious errors from pwd/grp functions when nscd backend is absent
for several pwd/grp functions, the only way the caller can distinguish
between a successful negative result ("no such user/group") and an
internal error is by clearing errno before the call and checking errno
afterwards. the nscd backend support code correctly simulated a
not-found response on systems where such a backend is not running, but
failed to restore errno.

this commit also fixed an outdated/incorrect comment.
Diffstat (limited to 'src/passwd/nscd_query.c')
-rw-r--r--src/passwd/nscd_query.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/passwd/nscd_query.c b/src/passwd/nscd_query.c
index 69a7815e..d38e371b 100644
--- a/src/passwd/nscd_query.c
+++ b/src/passwd/nscd_query.c
@@ -32,6 +32,7 @@ FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *
 		},
 		.msg_iovlen = 2
 	};
+	int errno_save = errno;
 
 	*swap = 0;
 retry:
@@ -50,11 +51,14 @@ retry:
 		return f;
 
 	if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
-		/* If there isn't a running nscd we return -1 to indicate that
-		 * that is precisely what happened
-		 */
-		if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
+		/* If there isn't a running nscd we simulate a "not found"
+		 * result and the caller is responsible for calling
+		 * fclose on the (unconnected) socket. The value of
+		 * errno must be left unchanged in this case.  */
+		if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
+			errno = errno_save;
 			return f;
+		}
 		goto error;
 	}