libnbd/0001-lib-Try-LOGNAME-before-using-getlogin_r.patch
2019-08-21 11:34:19 +01:00

51 lines
1.4 KiB
Diff

From e0b30bceab1cf61d8129b874d0122bc2cccec0e9 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Wed, 21 Aug 2019 09:57:54 +0100
Subject: [PATCH 1/3] lib: Try $LOGNAME before using getlogin_r.
Fixes tests in Koji which failed with
nbd_connect_unix: getlogin: No such device or address
---
lib/crypto.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/lib/crypto.c b/lib/crypto.c
index 703bc84..2a59943 100644
--- a/lib/crypto.c
+++ b/lib/crypto.c
@@ -102,7 +102,7 @@ nbd_unlocked_set_tls_username (struct nbd_handle *h, const char *username)
char *
nbd_unlocked_get_tls_username (struct nbd_handle *h)
{
- char *ret;
+ char *s, *ret;
if (h->tls_username) {
ret = strdup (h->tls_username);
@@ -113,7 +113,21 @@ nbd_unlocked_get_tls_username (struct nbd_handle *h)
return ret;
}
- /* Otherwise we return the local login name. */
+ /* Otherwise we return the local login name. Try $LOGNAME first for
+ * two reasons: (1) So the user can override it. (2) Because
+ * getlogin fails with ENXIO if there is no controlling terminal
+ * (which is often the case in test and embedded environments).
+ */
+ s = getenv ("LOGNAME");
+ if (s) {
+ ret = strdup (s);
+ if (ret == NULL) {
+ set_error (errno, "strdup");
+ return NULL;
+ }
+ return ret;
+ }
+
ret = malloc (L_cuserid);
if (ret == NULL) {
set_error (errno, "malloc");
--
2.22.0