Skip to content

Commit 72ef702

Browse files
besser82solardiz
authored andcommitted
libnss_tcb: Use readdir(3) with glibc >= 2.24.
It is recommended that applications use readdir(3) instead of readdir_r(3). Furthermore, since version 2.24, glibc deprecates readdir_r(3). Also use thread local storage for the underlying directory stream in this case. Signed-off-by: Björn Esser <[email protected]>
1 parent 10ab0c5 commit 72ef702

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
* pam_tcb/Makefile: Likewise.
2222
* progs/Makefile: Likewise.
2323

24+
libnss_tcb: Use readdir(3) with glibc >= 2.24.
25+
* libs/nss.c (_nss_tcb_getspnam_r): glibc, since version 2.24,
26+
has deprecated readdir_r(3). It is recommended that applications
27+
use readdir(3) instead of readdir_r(3). Also use thread local
28+
storage for the underlying directory stream in this case.
29+
* LICENSE: Update copyright for this contribution.
30+
2431
2021-09-25 Dmitry V. Levin <ldv at owl.openwall.com>
2532

2633
Add github CI.

libs/nss.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@
1111

1212
#include "tcb.h"
1313

14+
/* readdir_r(3) is deprecated since glibc 2.24 */
15+
#if defined __GLIBC_PREREQ && __GLIBC_PREREQ(2, 24)
16+
#define USE_READDIR 1
17+
#else
18+
#define USE_READDIR 0
19+
#endif
20+
21+
#if USE_READDIR
22+
static __thread DIR *tcbdir = NULL;
23+
#else
1424
static DIR *tcbdir = NULL;
25+
#endif
1526

1627
int _nss_tcb_setspent(void)
1728
{
@@ -96,7 +107,10 @@ int _nss_tcb_getspnam_r(const char *name, struct spwd *__result_buf,
96107
int _nss_tcb_getspent_r(struct spwd *__result_buf,
97108
char *__buffer, size_t __buflen, struct spwd **__result)
98109
{
99-
struct dirent entry, *result;
110+
#if !USE_READDIR
111+
struct dirent entry;
112+
#endif
113+
struct dirent *result;
100114
off_t currpos;
101115
int retval, saved_errno;
102116

@@ -108,8 +122,15 @@ int _nss_tcb_getspent_r(struct spwd *__result_buf,
108122

109123
do {
110124
currpos = telldir(tcbdir);
125+
#if USE_READDIR
126+
saved_errno = errno;
127+
errno = 0;
128+
result = readdir(tcbdir);
129+
if (!result && errno) {
130+
#else
111131
if (readdir_r(tcbdir, &entry, &result)) {
112132
saved_errno = errno;
133+
#endif
113134
closedir(tcbdir);
114135
errno = saved_errno;
115136
tcbdir = NULL;
@@ -121,6 +142,9 @@ int _nss_tcb_getspent_r(struct spwd *__result_buf,
121142
tcbdir = NULL;
122143
return NSS_STATUS_NOTFOUND;
123144
}
145+
#if USE_READDIR
146+
errno = saved_errno;
147+
#endif
124148
} while (!strcmp(result->d_name, ".") ||
125149
!strcmp(result->d_name, "..") || result->d_name[0] == ':');
126150

0 commit comments

Comments
 (0)