From 1aa43890df5ecc09004007336f64ed1344a9d335 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Sat, 28 Jun 2003 07:49:05 +0000 Subject: Optimize several little things. Use stream unlocked. --- nis/nss_compat/compat-grp.c | 30 +++++++++++++++++------------- nis/nss_compat/compat-pwd.c | 23 +++++++++++++---------- nis/nss_compat/compat-spwd.c | 28 ++++++++++++++++------------ 3 files changed, 46 insertions(+), 35 deletions(-) (limited to 'nis') diff --git a/nis/nss_compat/compat-grp.c b/nis/nss_compat/compat-grp.c index fb6328cfca..a24db336e2 100644 --- a/nis/nss_compat/compat-grp.c +++ b/nis/nss_compat/compat-grp.c @@ -17,15 +17,16 @@ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ +#include #include #include -#include #include -#include -#include +#include +#include +#include #include #include -#include +#include static service_user *ni; static enum nss_status (*nss_setgrent) (int stayopen); @@ -106,7 +107,7 @@ internal_setgrent (ent_t *ent, int stayopen) if (ent->stream == NULL) { - ent->stream = fopen ("/etc/group", "r"); + ent->stream = fopen ("/etc/group", "rm"); if (ent->stream == NULL) status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; @@ -115,11 +116,11 @@ internal_setgrent (ent_t *ent, int stayopen) /* We have to make sure the file is `closed on exec'. */ int result, flags; - result = flags = fcntl (fileno (ent->stream), F_GETFD, 0); + result = flags = fcntl (fileno_unlocked (ent->stream), F_GETFD, 0); if (result >= 0) { flags |= FD_CLOEXEC; - result = fcntl (fileno (ent->stream), F_SETFD, flags); + result = fcntl (fileno_unlocked (ent->stream), F_SETFD, flags); } if (result < 0) { @@ -129,6 +130,9 @@ internal_setgrent (ent_t *ent, int stayopen) ent->stream = NULL; status = NSS_STATUS_UNAVAIL; } + else + /* We take care of locking ourself. */ + __fsetlocking (ent->stream, FSETLOCKING_BYCALLER); } } else @@ -252,8 +256,8 @@ getgrent_next_file (struct group *result, ent_t *ent, { fgetpos (ent->stream, &pos); buffer[buflen - 1] = '\xff'; - p = fgets (buffer, buflen, ent->stream); - if (p == NULL && feof (ent->stream)) + p = fgets_unlocked (buffer, buflen, ent->stream); + if (p == NULL && feof_unlocked (ent->stream)) return NSS_STATUS_NOTFOUND; if (p == NULL || buffer[buflen - 1] != '\xff') @@ -380,8 +384,8 @@ internal_getgrnam_r (const char *name, struct group *result, ent_t *ent, { fgetpos (ent->stream, &pos); buffer[buflen - 1] = '\xff'; - p = fgets (buffer, buflen, ent->stream); - if (p == NULL && feof (ent->stream)) + p = fgets_unlocked (buffer, buflen, ent->stream); + if (p == NULL && feof_unlocked (ent->stream)) return NSS_STATUS_NOTFOUND; if (p == NULL || buffer[buflen - 1] != '\xff') @@ -507,8 +511,8 @@ internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent, { fgetpos (ent->stream, &pos); buffer[buflen - 1] = '\xff'; - p = fgets (buffer, buflen, ent->stream); - if (p == NULL && feof (ent->stream)) + p = fgets_unlocked (buffer, buflen, ent->stream); + if (p == NULL && feof_unlocked (ent->stream)) return NSS_STATUS_NOTFOUND; if (p == NULL || buffer[buflen - 1] != '\xff') diff --git a/nis/nss_compat/compat-pwd.c b/nis/nss_compat/compat-pwd.c index c47ad31e46..d8b9ba18d6 100644 --- a/nis/nss_compat/compat-pwd.c +++ b/nis/nss_compat/compat-pwd.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1996,1997,1998,1999,2001,2002,2003 Free Software Foundation, Inc. +/* Copyright (C) 1996-1999,2001,2002,2003 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Thorsten Kukuk , 1996. @@ -223,7 +223,7 @@ internal_setpwent (ent_t *ent, int stayopen) if (ent->stream == NULL) { - ent->stream = fopen ("/etc/passwd", "r"); + ent->stream = fopen ("/etc/passwd", "rm"); if (ent->stream == NULL) status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; @@ -232,11 +232,11 @@ internal_setpwent (ent_t *ent, int stayopen) /* We have to make sure the file is `closed on exec'. */ int result, flags; - result = flags = fcntl (fileno (ent->stream), F_GETFD, 0); + result = flags = fcntl (fileno_unlocked (ent->stream), F_GETFD, 0); if (result >= 0) { flags |= FD_CLOEXEC; - result = fcntl (fileno (ent->stream), F_SETFD, flags); + result = fcntl (fileno_unlocked (ent->stream), F_SETFD, flags); } if (result < 0) { @@ -246,6 +246,9 @@ internal_setpwent (ent_t *ent, int stayopen) ent->stream = NULL; status = NSS_STATUS_UNAVAIL; } + else + /* We take care of locking ourself. */ + __fsetlocking (ent->stream, FSETLOCKING_BYCALLER); } } else @@ -497,8 +500,8 @@ getpwent_next_file (struct passwd *result, ent_t *ent, { fgetpos (ent->stream, &pos); buffer[buflen - 1] = '\xff'; - p = fgets (buffer, buflen, ent->stream); - if (p == NULL && feof (ent->stream)) + p = fgets_unlocked (buffer, buflen, ent->stream); + if (p == NULL && feof_unlocked (ent->stream)) return NSS_STATUS_NOTFOUND; if (p == NULL || buffer[buflen - 1] != '\xff') @@ -691,8 +694,8 @@ internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent, { fgetpos (ent->stream, &pos); buffer[buflen - 1] = '\xff'; - p = fgets (buffer, buflen, ent->stream); - if (p == NULL && feof (ent->stream)) + p = fgets_unlocked (buffer, buflen, ent->stream); + if (p == NULL && feof_unlocked (ent->stream)) { return NSS_STATUS_NOTFOUND; } @@ -894,8 +897,8 @@ internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent, { fgetpos (ent->stream, &pos); buffer[buflen - 1] = '\xff'; - p = fgets (buffer, buflen, ent->stream); - if (p == NULL && feof (ent->stream)) + p = fgets_unlocked (buffer, buflen, ent->stream); + if (p == NULL && feof_unlocked (ent->stream)) return NSS_STATUS_NOTFOUND; if (p == NULL || buffer[buflen - 1] != '\xff') diff --git a/nis/nss_compat/compat-spwd.c b/nis/nss_compat/compat-spwd.c index a2757c4b66..ed6bcff7df 100644 --- a/nis/nss_compat/compat-spwd.c +++ b/nis/nss_compat/compat-spwd.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1996,1997,1998,1999,2001,2002,2003 Free Software Foundation, Inc. +/* Copyright (C) 1996-1999,2001,2002,2003 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Thorsten Kukuk , 1996. @@ -17,17 +17,18 @@ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#include -#include #include +#include #include #include +#include +#include #include +#include #include -#include #include #include -#include +#include #include "netgroup.h" @@ -177,7 +178,7 @@ internal_setspent (ent_t *ent, int stayopen) if (ent->stream == NULL) { - ent->stream = fopen ("/etc/shadow", "r"); + ent->stream = fopen ("/etc/shadow", "rm"); if (ent->stream == NULL) status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; @@ -186,11 +187,11 @@ internal_setspent (ent_t *ent, int stayopen) /* We have to make sure the file is `closed on exec'. */ int result, flags; - result = flags = fcntl (fileno (ent->stream), F_GETFD, 0); + result = flags = fcntl (fileno_unlocked (ent->stream), F_GETFD, 0); if (result >= 0) { flags |= FD_CLOEXEC; - result = fcntl (fileno (ent->stream), F_SETFD, flags); + result = fcntl (fileno_unlocked (ent->stream), F_SETFD, flags); } if (result < 0) { @@ -200,6 +201,9 @@ internal_setspent (ent_t *ent, int stayopen) ent->stream = NULL; status = NSS_STATUS_UNAVAIL; } + else + /* We take care of locking ourself. */ + __fsetlocking (ent->stream, FSETLOCKING_BYCALLER); } } else @@ -449,8 +453,8 @@ getspent_next_file (struct spwd *result, ent_t *ent, { fgetpos (ent->stream, &pos); buffer[buflen - 1] = '\xff'; - p = fgets (buffer, buflen, ent->stream); - if (p == NULL && feof (ent->stream)) + p = fgets_unlocked (buffer, buflen, ent->stream); + if (p == NULL && feof_unlocked (ent->stream)) return NSS_STATUS_NOTFOUND; if (p == NULL || buffer[buflen - 1] != '\xff') @@ -638,8 +642,8 @@ internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent, { fgetpos (ent->stream, &pos); buffer[buflen - 1] = '\xff'; - p = fgets (buffer, buflen, ent->stream); - if (p == NULL && feof (ent->stream)) + p = fgets_unlocked (buffer, buflen, ent->stream); + if (p == NULL && feof_unlocked (ent->stream)) return NSS_STATUS_NOTFOUND; if (p == NULL || buffer[buflen - 1] != '\xff') -- cgit v1.2.1