1*4882a593SmuzhiyunFrom 9e42c405f30d2b52d019598436ea346ef8586f43 Mon Sep 17 00:00:00 2001
2*4882a593SmuzhiyunFrom: =?UTF-8?q?No=C3=A9=20Rubinstein?= <nrubinstein@aldebaran.com>
3*4882a593SmuzhiyunDate: Wed, 24 Aug 2016 18:55:25 +0200
4*4882a593SmuzhiyunSubject: [PATCH] Check that getpwent_r is available before using it
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunThis fixes building trousers with musl
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunSigned-off-by: Noé Rubinstein <nrubinstein@aldebaran.com>
9*4882a593Smuzhiyun[Bernd: Rebased for version 0.3.14]
10*4882a593SmuzhiyunSigned-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
11*4882a593Smuzhiyun---
12*4882a593Smuzhiyun configure.ac        |  4 ++++
13*4882a593Smuzhiyun src/tspi/ps/tspps.c | 10 +++++-----
14*4882a593Smuzhiyun 2 files changed, 9 insertions(+), 5 deletions(-)
15*4882a593Smuzhiyun
16*4882a593Smuzhiyundiff --git a/configure.in b/configure.in
17*4882a593Smuzhiyunindex fd3f5f1..e3d7acf 100644
18*4882a593Smuzhiyun--- a/configure.ac
19*4882a593Smuzhiyun+++ b/configure.ac
20*4882a593Smuzhiyun@@ -145,6 +145,10 @@ else
21*4882a593Smuzhiyun 	AC_MSG_ERROR(["gtk", "openssl" and "none" are the only supported gui options for trousers])
22*4882a593Smuzhiyun fi
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun+# Look for getpwent_r. If it is not found, getpwent will be used instead, with
25*4882a593Smuzhiyun+# an additional mutex.
26*4882a593Smuzhiyun+AC_CHECK_FUNC(getpwent_r, [AC_DEFINE(HAVE_GETPWENT_R)])
27*4882a593Smuzhiyun+
28*4882a593Smuzhiyun #
29*4882a593Smuzhiyun # The default port that the TCS daemon listens on
30*4882a593Smuzhiyun #
31*4882a593Smuzhiyundiff --git a/src/tspi/ps/tspps.c b/src/tspi/ps/tspps.c
32*4882a593Smuzhiyunindex c6f9c3d..9d00d2a 100644
33*4882a593Smuzhiyun--- a/src/tspi/ps/tspps.c
34*4882a593Smuzhiyun+++ b/src/tspi/ps/tspps.c
35*4882a593Smuzhiyun@@ -45,7 +45,7 @@
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun static int user_ps_fd = -1;
38*4882a593Smuzhiyun static MUTEX_DECLARE_INIT(user_ps_lock);
39*4882a593Smuzhiyun-#if (defined (__FreeBSD__) || defined (__OpenBSD__))
40*4882a593Smuzhiyun+#ifndef HAVE_GETPWENT_R
41*4882a593Smuzhiyun static MUTEX_DECLARE_INIT(user_ps_path);
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun static struct flock fl;
44*4882a593Smuzhiyun@@ -60,7 +60,7 @@ get_user_ps_path(char **file)
45*4882a593Smuzhiyun 	TSS_RESULT result;
46*4882a593Smuzhiyun 	char *file_name = NULL, *home_dir = NULL;
47*4882a593Smuzhiyun 	struct passwd *pwp;
48*4882a593Smuzhiyun-#if (defined (__linux) || defined (linux) || defined(__GLIBC__))
49*4882a593Smuzhiyun+#ifdef HAVE_GETPWENT_R
50*4882a593Smuzhiyun 	struct passwd pw;
51*4882a593Smuzhiyun #endif
52*4882a593Smuzhiyun 	struct stat stat_buf;
53*4882a593Smuzhiyun@@ -72,7 +72,7 @@ get_user_ps_path(char **file)
54*4882a593Smuzhiyun 		*file = strdup(file_name);
55*4882a593Smuzhiyun 		return (*file) ? TSS_SUCCESS : TSPERR(TSS_E_OUTOFMEMORY);
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun-#if (defined (__FreeBSD__) || defined (__OpenBSD__))
58*4882a593Smuzhiyun+#ifndef HAVE_GETPWENT_R
59*4882a593Smuzhiyun 	MUTEX_LOCK(user_ps_path);
60*4882a593Smuzhiyun #endif
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun@@ -90,7 +90,7 @@ get_user_ps_path(char **file)
63*4882a593Smuzhiyun #else
64*4882a593Smuzhiyun 	setpwent();
65*4882a593Smuzhiyun 	while (1) {
66*4882a593Smuzhiyun-#if (defined (__linux) || defined (linux) || defined(__GLIBC__))
67*4882a593Smuzhiyun+#ifdef HAVE_GETPWENT_R
68*4882a593Smuzhiyun 		rc = getpwent_r(&pw, buf, PASSWD_BUFSIZE, &pwp);
69*4882a593Smuzhiyun 		if (rc) {
70*4882a593Smuzhiyun 			LogDebugFn("USER PS: Error getting path to home directory: getpwent_r: %s",
71*4882a593Smuzhiyun@@ -99,7 +99,7 @@ get_user_ps_path(char **file)
72*4882a593Smuzhiyun 			return TSPERR(TSS_E_INTERNAL_ERROR);
73*4882a593Smuzhiyun 		}
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun-#elif (defined (__FreeBSD__) || defined (__OpenBSD__))
76*4882a593Smuzhiyun+#else
77*4882a593Smuzhiyun 		if ((pwp = getpwent()) == NULL) {
78*4882a593Smuzhiyun 			LogDebugFn("USER PS: Error getting path to home directory: getpwent: %s",
79*4882a593Smuzhiyun                                    strerror(rc));
80*4882a593Smuzhiyun--
81*4882a593Smuzhiyun2.1.4
82*4882a593Smuzhiyun
83