Skip to contentSkip to conversation

kernel/syscalls/ipc/lib: rewrite getuserid

PATCH In review Wei,Jiangang 2 participants 2 messages May 8, 2015 – May 11, 2015 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v3 2/5 kernel/syscalls/ipc/lib: rewrite getuserid

* No need to allocate memory for getpwnam()'s return, So remove these codes.

* replace getpwnam() with getpwnam_r() that is thread-safe.

---
 testcases/kernel/syscalls/ipc/lib/libipc.c | 43 +++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 15 deletions(-)
testcases/kernel/syscalls/ipc/lib/libipc.c +28 -15
index f10e257..63304f8 100644
@@ -141,21 +141,34 @@ void rm_sema(int sem_id)
  */
 int getuserid(char *user)
 {
-	struct passwd *ent;
-
-	/* allocate some space for the passwd struct */
-	if ((ent = malloc(sizeof(struct passwd))) == NULL) {
-		tst_brkm(TBROK, cleanup, "couldn't allocate space for passwd"
-			 " structure");
-	}
-
-	/* get the uid value for the user */
-	if ((ent = getpwnam(user)) == NULL) {
-		tst_brkm(TBROK, cleanup, "Couldn't get password entry for %s",
-			 user);
-	}
-
-	return (ent->pw_uid);
+	struct passwd pwd;
+	struct passwd *result;
+	char *buf;
+	size_t buflen;
+	int s;
+
+	buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+	if (buflen == -1)
+		/* Should be more than enough */
+		buflen = 16384;
+
+	buf = malloc(buflen);
+	if (buf == NULL)
+		tst_brkm(TBROK, NULL, "malloc failed.\n");
+
+	s = getpwnam_r(user, &pwd, buf, buflen, &result);
+	if (result == NULL) {
+		free(buf);
+		if (s == 0) {
+			tst_brkm(TBROK, NULL, "Not found: %s", user);
+		} else {
+			tst_brkm(TBROK, NULL, "getpwnam_r failed. (%d) %s",
+				(s), strerror(s));
+		}
+	}
+	free(buf);
+
+	return pwd.pw_uid;
 }
 
 /*
Signature · 13 lines
1.9.3


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#2 lore raw

Hi!

Quoted 4 lines

* No need to allocate memory for getpwnam()'s return, So remove these codes.

* replace getpwnam() with getpwnam_r() that is thread-safe.

Hmm, is this code actually called from a testcase that runs in several threads? Otherwise it looks like overly complicated solution for problem that does not exist.

Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list