* [LTP] [PATCH v3 2/5] kernel/syscalls/ipc/lib: rewrite getuserid
[not found] <554C57EF.6070603@cn.fujitsu.com>
@ 2015-05-08 8:31 ` Wei,Jiangang
2015-05-11 16:25 ` Cyril Hrubis
0 siblings, 1 reply; 2+ messages in thread
From: Wei,Jiangang @ 2015-05-08 8:31 UTC (permalink / raw)
To: ltp-list
* No need to allocate memory for getpwnam()'s return,
So remove these codes.
* replace getpwnam() with getpwnam_r() that is thread-safe.
Signed-off-by: Wei,Jiangang <weijg.fnst@cn.fujitsu.com>
---
testcases/kernel/syscalls/ipc/lib/libipc.c | 43 +++++++++++++++++++-----------
1 file changed, 28 insertions(+), 15 deletions(-)
diff --git a/testcases/kernel/syscalls/ipc/lib/libipc.c b/testcases/kernel/syscalls/ipc/lib/libipc.c
index f10e257..63304f8 100644
--- a/testcases/kernel/syscalls/ipc/lib/libipc.c
+++ b/testcases/kernel/syscalls/ipc/lib/libipc.c
@@ -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;
}
/*
--
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
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [LTP] [PATCH v3 2/5] kernel/syscalls/ipc/lib: rewrite getuserid
2015-05-08 8:31 ` [LTP] [PATCH v3 2/5] kernel/syscalls/ipc/lib: rewrite getuserid Wei,Jiangang
@ 2015-05-11 16:25 ` Cyril Hrubis
0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2015-05-11 16:25 UTC (permalink / raw)
To: Wei,Jiangang; +Cc: ltp-list
Hi!
> * 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.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
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
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-05-11 16:26 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <554C57EF.6070603@cn.fujitsu.com>
2015-05-08 8:31 ` [LTP] [PATCH v3 2/5] kernel/syscalls/ipc/lib: rewrite getuserid Wei,Jiangang
2015-05-11 16:25 ` Cyril Hrubis
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.