From fa71903f4ceea373c01a301c996369a63f76fbb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Esser?= Date: Tue, 17 Dec 2024 11:23:15 +0100 Subject: [PATCH] libnss_tcb: Initialize or rewind dirstream from inside setspent(3). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On first call to setspent(3) initialize the directory stream properly; on subsequent calls use rewinddir(3) to reset the position of the directory stream to the beginning of the directory, and also update the existing directory stream to refer to the current state of the underlying directory it operates on. As all internal functions are operating on thread-local storage now, this operation will be safe, since it will emit no effects outside of the thread calling the setspent(3) function itself. Signed-off-by: Björn Esser --- ChangeLog | 13 +++++++++++++ libs/nss.c | 14 +++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index bdc840c..0f5b3d6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,18 @@ 2021-12-18 Björn Esser + libnss_tcb: Initialize or rewind dirstream from inside setspent(3). + On first call to setspent(3) initialize the directory stream properly; + on subsequent calls use rewinddir(3) to reset the position of the + directory stream to the beginning of the directory, and also update + the existing directory stream to refer to the current state of the + underlying directory it operates on. As all internal functions are + operating on thread-local storage now, this operation will be safe, + since it will emit no effects outside of the thread calling the + setspent(3) function itself. + * libs/nss.c (_nss_tcb_setspent): Initialize or rewind dirstream. + (_nss_tcb_getspent_r): Move initialization of the dirstream to + _nss_tcb_setspent. + libtcb: Add versioning to exported symbols. This change is implemented for adding some interfaces to libtcb to give it a more consumer friendly API and thus makes porting existing diff --git a/libs/nss.c b/libs/nss.c index f9d8370..cfaf67b 100644 --- a/libs/nss.c +++ b/libs/nss.c @@ -15,6 +15,13 @@ static __thread DIR *tcbdir = NULL; int _nss_tcb_setspent(void) { + if (!tcbdir) { + tcbdir = opendir(TCB_DIR); + if (!tcbdir) + return NSS_STATUS_UNAVAIL; + } + + rewinddir(tcbdir); return 1; } @@ -100,11 +107,8 @@ int _nss_tcb_getspent_r(struct spwd *__result_buf, off_t currpos; int retval, saved_errno; - if (!tcbdir) { - tcbdir = opendir(TCB_DIR); - if (!tcbdir) - return NSS_STATUS_UNAVAIL; - } + if (!tcbdir) + return NSS_STATUS_UNAVAIL; do { currpos = telldir(tcbdir);