From 1478c2de052374c6356db5513749a144c13791b1 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 7 Dec 2006 23:24:02 +0000 Subject: Major cleanup of internal mutex locking. Be more consistant in how we do things, and avoid potential deadlocks caused when a thread holding a uClibc internal lock get canceled and terminates without releasing the lock. This change also provides a single place, bits/uClibc_mutex.h, for thread libraries to modify to change all instances of internal locking. --- libc/pwd_grp/pwd_grp.c | 97 +++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 52 deletions(-) (limited to 'libc/pwd_grp/pwd_grp.c') diff --git a/libc/pwd_grp/pwd_grp.c b/libc/pwd_grp/pwd_grp.c index 2a9d4519a..d223a9e36 100644 --- a/libc/pwd_grp/pwd_grp.c +++ b/libc/pwd_grp/pwd_grp.c @@ -34,9 +34,7 @@ #ifdef __UCLIBC_HAS_SHADOW__ #include #endif -#ifdef __UCLIBC_HAS_THREADS__ -#include -#endif +#include libc_hidden_proto(strchr) libc_hidden_proto(strcmp) @@ -253,7 +251,7 @@ libc_hidden_def(sgetspent_r) #ifdef L_getpwnam_r #define GETXXKEY_R_FUNC getpwnam_r -#define GETXXKEY_R_PARSER __parsepwent +#define GETXXKEY_R_PARSER __parsepwent #define GETXXKEY_R_ENTTYPE struct passwd #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key)) #define DO_GETXXKEY_R_KEYTYPE const char *__restrict @@ -263,7 +261,7 @@ libc_hidden_def(sgetspent_r) #ifdef L_getgrnam_r #define GETXXKEY_R_FUNC getgrnam_r -#define GETXXKEY_R_PARSER __parsegrent +#define GETXXKEY_R_PARSER __parsegrent #define GETXXKEY_R_ENTTYPE struct group #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key)) #define DO_GETXXKEY_R_KEYTYPE const char *__restrict @@ -273,7 +271,7 @@ libc_hidden_def(sgetspent_r) #ifdef L_getspnam_r #define GETXXKEY_R_FUNC getspnam_r -#define GETXXKEY_R_PARSER __parsespent +#define GETXXKEY_R_PARSER __parsespent #define GETXXKEY_R_ENTTYPE struct spwd #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key)) #define DO_GETXXKEY_R_KEYTYPE const char *__restrict @@ -283,7 +281,7 @@ libc_hidden_def(sgetspent_r) #ifdef L_getpwuid_r #define GETXXKEY_R_FUNC getpwuid_r -#define GETXXKEY_R_PARSER __parsepwent +#define GETXXKEY_R_PARSER __parsepwent #define GETXXKEY_R_ENTTYPE struct passwd #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key) #define DO_GETXXKEY_R_KEYTYPE uid_t @@ -293,7 +291,7 @@ libc_hidden_def(sgetspent_r) #ifdef L_getgrgid_r #define GETXXKEY_R_FUNC getgrgid_r -#define GETXXKEY_R_PARSER __parsegrent +#define GETXXKEY_R_PARSER __parsegrent #define GETXXKEY_R_ENTTYPE struct group #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key) #define DO_GETXXKEY_R_KEYTYPE gid_t @@ -458,47 +456,40 @@ int getpw(uid_t uid, char *buf) #endif /**********************************************************************/ -#if defined(L_getpwent_r) || defined(L_getgrent_r) || defined(L_getspent_r) -#ifdef __UCLIBC_HAS_THREADS__ -# include -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; -#endif -#define LOCK __pthread_mutex_lock(&mylock) -#define UNLOCK __pthread_mutex_unlock(&mylock) -#endif #ifdef L_getpwent_r +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); static FILE *pwf /*= NULL*/; void setpwent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (pwf) { rewind(pwf); } - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } void endpwent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (pwf) { fclose(pwf); pwf = NULL; } - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } libc_hidden_proto(getpwent_r) -int getpwent_r(struct passwd *__restrict resultbuf, +int getpwent_r(struct passwd *__restrict resultbuf, char *__restrict buffer, size_t buflen, struct passwd **__restrict result) { int rv; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); *result = NULL; /* In case of error... */ @@ -516,7 +507,7 @@ int getpwent_r(struct passwd *__restrict resultbuf, } ERR: - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return rv; } @@ -525,26 +516,27 @@ libc_hidden_def(getpwent_r) #endif /**********************************************************************/ #ifdef L_getgrent_r +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); static FILE *grf /*= NULL*/; void setgrent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (grf) { rewind(grf); } - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } void endgrent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (grf) { fclose(grf); grf = NULL; } - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } libc_hidden_proto(getgrent_r) @@ -554,7 +546,7 @@ int getgrent_r(struct group *__restrict resultbuf, { int rv; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); *result = NULL; /* In case of error... */ @@ -572,7 +564,7 @@ int getgrent_r(struct group *__restrict resultbuf, } ERR: - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return rv; } @@ -581,35 +573,36 @@ libc_hidden_def(getgrent_r) #endif /**********************************************************************/ #ifdef L_getspent_r +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); static FILE *spf /*= NULL*/; void setspent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (spf) { rewind(spf); } - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } void endspent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (spf) { fclose(spf); spf = NULL; } - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } libc_hidden_proto(getspent_r) -int getspent_r(struct spwd *resultbuf, char *buffer, +int getspent_r(struct spwd *resultbuf, char *buffer, size_t buflen, struct spwd **result) { int rv; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); *result = NULL; /* In case of error... */ @@ -627,7 +620,7 @@ int getspent_r(struct spwd *resultbuf, char *buffer, } ERR: - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return rv; } @@ -842,11 +835,11 @@ int putgrent(const struct group *__restrict p, FILE *__restrict f) static const unsigned char _sp_off[] = { offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ - offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ + offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ - offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ - offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ - offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ + offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ + offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ + offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ }; int putspent(const struct spwd *p, FILE *stream) @@ -899,13 +892,13 @@ int putspent(const struct spwd *p, FILE *stream) #ifdef L___parsepwent static const unsigned char pw_off[] = { - offsetof(struct passwd, pw_name), /* 0 */ + offsetof(struct passwd, pw_name), /* 0 */ offsetof(struct passwd, pw_passwd), /* 1 */ offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */ - offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */ + offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */ offsetof(struct passwd, pw_gecos), /* 4 */ - offsetof(struct passwd, pw_dir), /* 5 */ - offsetof(struct passwd, pw_shell) /* 6 */ + offsetof(struct passwd, pw_dir), /* 5 */ + offsetof(struct passwd, pw_shell) /* 6 */ }; int attribute_hidden __parsepwent(void *data, char *line) @@ -918,7 +911,7 @@ int attribute_hidden __parsepwent(void *data, char *line) do { p = ((char *) ((struct passwd *) data)) + pw_off[i]; - if ((i & 6) ^ 2) { /* i!=2 and i!=3 */ + if ((i & 6) ^ 2) { /* i!=2 and i!=3 */ *((char **) p) = line; if (i==6) { return 0; @@ -958,7 +951,7 @@ int attribute_hidden __parsepwent(void *data, char *line) #ifdef L___parsegrent static const unsigned char gr_off[] = { - offsetof(struct group, gr_name), /* 0 */ + offsetof(struct group, gr_name), /* 0 */ offsetof(struct group, gr_passwd), /* 1 */ offsetof(struct group, gr_gid) /* 2 - not a char ptr */ }; @@ -1040,7 +1033,7 @@ int attribute_hidden __parsegrent(void *data, char *line) if (!--i) break; while (*++p) {} } while (1); - } + } *members = NULL; return 0; @@ -1059,12 +1052,12 @@ static const unsigned char sp_off[] = { offsetof(struct spwd, sp_namp), /* 0 */ offsetof(struct spwd, sp_pwdp), /* 1 */ offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ - offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ + offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ - offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ - offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ - offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ - offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */ + offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ + offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ + offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ + offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */ }; int attribute_hidden __parsespent(void *data, char * line) -- cgit v1.2.3