diff options
author | Eric Andersen <andersen@codepoet.org> | 2002-08-08 07:28:33 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2002-08-08 07:28:33 +0000 |
commit | f6cfb61578920a2aba19afbf320e007efd863730 (patch) | |
tree | ccb2c728bbc974843bcdcad15be710ca0fdc432c | |
parent | 82dc33223c219e05fe2637f675e11cd85438f7be (diff) |
Fix locking
-Erik
-rw-r--r-- | libc/pwd_grp/__getgrent.c | 226 | ||||
-rw-r--r-- | libc/pwd_grp/__getspent_r.c | 45 | ||||
-rw-r--r-- | libc/pwd_grp/fgetpwent.c | 14 | ||||
-rw-r--r-- | libc/pwd_grp/fgetspent.c | 36 | ||||
-rw-r--r-- | libc/pwd_grp/getpwnam.c | 14 | ||||
-rw-r--r-- | libc/pwd_grp/getpwuid.c | 14 | ||||
-rw-r--r-- | libc/pwd_grp/getspnam.c | 14 | ||||
-rw-r--r-- | libc/pwd_grp/getspuid.c | 14 | ||||
-rw-r--r-- | libc/pwd_grp/grent.c | 35 | ||||
-rw-r--r-- | libc/pwd_grp/initgroups.c | 54 | ||||
-rw-r--r-- | libc/pwd_grp/lckpwdf.c | 26 | ||||
-rw-r--r-- | libc/pwd_grp/pwent.c | 21 | ||||
-rw-r--r-- | libc/pwd_grp/sgetspent.c | 14 | ||||
-rw-r--r-- | libc/pwd_grp/spent.c | 22 |
14 files changed, 372 insertions, 177 deletions
diff --git a/libc/pwd_grp/__getgrent.c b/libc/pwd_grp/__getgrent.c index 0827df131..e89f914ad 100644 --- a/libc/pwd_grp/__getgrent.c +++ b/libc/pwd_grp/__getgrent.c @@ -18,11 +18,22 @@ * */ +#include <features.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + /* * This is the core group-file read function. It behaves exactly like * getgrent() except that it is passed a file descriptor. getgrent() @@ -31,130 +42,139 @@ struct group *__getgrent(int grp_fd) { #ifdef GR_SCALE_DYNAMIC - static char *line_buff = NULL; - static char **members = NULL; - short line_index; - short buff_size; + static char *line_buff = NULL; + static char **members = NULL; + short line_index; + short buff_size; #else - static char line_buff[GR_MAX_LINE_LEN]; - static char *members[GR_MAX_MEMBERS]; + static char line_buff[GR_MAX_LINE_LEN]; + static char *members[GR_MAX_MEMBERS]; #endif - static struct group group; - register char *ptr; - char *field_begin; - short member_num; - char *endptr; - int line_len; + static struct group group; + register char *ptr; + char *field_begin; + short member_num; + char *endptr; + int line_len; - /* We use the restart label to handle malformatted lines */ - restart: + LOCK; + + /* We use the restart label to handle malformatted lines */ +restart: #ifdef GR_SCALE_DYNAMIC - line_index = 0; - buff_size = 256; + line_index = 0; + buff_size = 256; #endif #ifdef GR_SCALE_DYNAMIC - line_buff = realloc(line_buff, buff_size); - while (1) { - if ((line_len = read(grp_fd, line_buff + line_index, - buff_size - line_index)) <= 0) - return NULL; - field_begin = strchr(line_buff, '\n'); - if (field_begin != NULL) { - lseek(grp_fd, - (long) (1 + field_begin - - (line_len + line_index + line_buff)), SEEK_CUR); - *field_begin = '\0'; - if (*line_buff == '#' || *line_buff == ' ' - || *line_buff == '\n' || *line_buff == '\t') - goto restart; - break; - } else { /* Allocate some more space */ - - line_index = buff_size; - buff_size += 256; - line_buff = realloc(line_buff, buff_size); - } + line_buff = realloc(line_buff, buff_size); + while (1) { + if ((line_len = read(grp_fd, line_buff + line_index, + buff_size - line_index)) <= 0) { + UNLOCK; + return NULL; } -#else - /* Read the line into the static buffer */ - if ((line_len = read(grp_fd, line_buff, GR_MAX_LINE_LEN)) <= 0) - return NULL; field_begin = strchr(line_buff, '\n'); - if (field_begin != NULL) - lseek(grp_fd, (long) (1 + field_begin - (line_buff + line_len)), - SEEK_CUR); - else { /* The line is too long - skip it :-\ */ - - do { - if ((line_len = read(grp_fd, line_buff, GR_MAX_LINE_LEN)) <= 0) - return NULL; - } while (!(field_begin = strchr(line_buff, '\n'))); - lseek(grp_fd, (long) ((field_begin - line_buff) - line_len + 1), - SEEK_CUR); + if (field_begin != NULL) { + lseek(grp_fd, + (long) (1 + field_begin - + (line_len + line_index + line_buff)), SEEK_CUR); + *field_begin = '\0'; + if (*line_buff == '#' || *line_buff == ' ' + || *line_buff == '\n' || *line_buff == '\t') goto restart; + break; + } else { /* Allocate some more space */ + + line_index = buff_size; + buff_size += 256; + line_buff = realloc(line_buff, buff_size); } - if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || - *line_buff == '\t') - goto restart; - *field_begin = '\0'; + } +#else + /* Read the line into the static buffer */ + if ((line_len = read(grp_fd, line_buff, GR_MAX_LINE_LEN)) <= 0) { + UNLOCK; + return NULL; + } + field_begin = strchr(line_buff, '\n'); + if (field_begin != NULL) + lseek(grp_fd, (long) (1 + field_begin - (line_buff + line_len)), + SEEK_CUR); + else { /* The line is too long - skip it :-\ */ + + do { + if ((line_len = read(grp_fd, line_buff, GR_MAX_LINE_LEN)) <= 0) { + UNLOCK; + return NULL; + } + } while (!(field_begin = strchr(line_buff, '\n'))); + lseek(grp_fd, (long) ((field_begin - line_buff) - line_len + 1), + SEEK_CUR); + goto restart; + } + if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || + *line_buff == '\t') + goto restart; + *field_begin = '\0'; #endif /* GR_SCALE_DYNAMIC */ - /* Now parse the line */ - group.gr_name = line_buff; - ptr = strchr(line_buff, ':'); - if (ptr == NULL) - goto restart; - *ptr++ = '\0'; + /* Now parse the line */ + group.gr_name = line_buff; + ptr = strchr(line_buff, ':'); + if (ptr == NULL) + goto restart; + *ptr++ = '\0'; - group.gr_passwd = ptr; - ptr = strchr(ptr, ':'); - if (ptr == NULL) - goto restart; - *ptr++ = '\0'; + group.gr_passwd = ptr; + ptr = strchr(ptr, ':'); + if (ptr == NULL) + goto restart; + *ptr++ = '\0'; - field_begin = ptr; - ptr = strchr(ptr, ':'); - if (ptr == NULL) - goto restart; - *ptr++ = '\0'; + field_begin = ptr; + ptr = strchr(ptr, ':'); + if (ptr == NULL) + goto restart; + *ptr++ = '\0'; - group.gr_gid = (gid_t) strtoul(field_begin, &endptr, 10); - if (*endptr != '\0') - goto restart; + group.gr_gid = (gid_t) strtoul(field_begin, &endptr, 10); + if (*endptr != '\0') + goto restart; - member_num = 0; - field_begin = ptr; + member_num = 0; + field_begin = ptr; #ifdef GR_SCALE_DYNAMIC - if (members != NULL) - free(members); - members = (char **) malloc((member_num + 1) * sizeof(char *)); - for ( ; field_begin && *field_begin != '\0'; field_begin = ptr) { - if ((ptr = strchr(field_begin, ',')) != NULL) - *ptr++ = '\0'; - members[member_num++] = field_begin; - members = (char **) realloc(members, - (member_num + 1) * sizeof(char *)); - } - members[member_num] = NULL; + if (members != NULL) + free(members); + members = (char **) malloc((member_num + 1) * sizeof(char *)); + for ( ; field_begin && *field_begin != '\0'; field_begin = ptr) { + if ((ptr = strchr(field_begin, ',')) != NULL) + *ptr++ = '\0'; + members[member_num++] = field_begin; + members = (char **) realloc(members, + (member_num + 1) * sizeof(char *)); + } + members[member_num] = NULL; #else - while ((ptr = strchr(ptr, ',')) != NULL) { - *ptr = '\0'; - ptr++; - members[member_num] = field_begin; - field_begin = ptr; - member_num++; - } - if (*field_begin == '\0') - members[member_num] = NULL; - else { - members[member_num] = field_begin; - members[member_num + 1] = NULL; - } + while ((ptr = strchr(ptr, ',')) != NULL) { + *ptr = '\0'; + ptr++; + members[member_num] = field_begin; + field_begin = ptr; + member_num++; + } + if (*field_begin == '\0') + members[member_num] = NULL; + else { + members[member_num] = field_begin; + members[member_num + 1] = NULL; + } #endif - group.gr_mem = members; - return &group; + group.gr_mem = members; + UNLOCK; + return &group; } diff --git a/libc/pwd_grp/__getspent_r.c b/libc/pwd_grp/__getspent_r.c index 2f87a1cc6..7150520d2 100644 --- a/libc/pwd_grp/__getspent_r.c +++ b/libc/pwd_grp/__getspent_r.c @@ -25,32 +25,29 @@ int __getspent_r(struct spwd * spwd, char * line_buff, size_t buflen, int spwd_fd) { - char *endptr; - int line_len; + char *endptr; + int line_len; - /* We use the restart label to handle malformatted lines */ - restart: - /* Read the shadow line into the static buffer using a minimal of - syscalls. */ - if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0) + /* We use the restart label to handle malformatted lines */ +restart: + /* Read the shadow line into the buffer using a minimum of syscalls. */ + if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0) + return -1; + endptr = strchr(line_buff, '\n'); + if (endptr != NULL) + lseek(spwd_fd, (long) (1 + endptr - (line_buff + line_len)), SEEK_CUR); + else { + /* The line is too long - skip it. :-\ */ + do { + if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0) return -1; - endptr = strchr(line_buff, '\n'); - if (endptr != NULL) - lseek(spwd_fd, (long) (1 + endptr - (line_buff + line_len)), - SEEK_CUR); - else { /* The line is too long - skip it. :-\ */ + } while (!(endptr = strchr(line_buff, '\n'))); + lseek(spwd_fd, (long) (endptr - line_buff) - line_len + 1, SEEK_CUR); + goto restart; + } - do { - if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0) - return -1; - } while (!(endptr = strchr(line_buff, '\n'))); - lseek(spwd_fd, (long) (endptr - line_buff) - line_len + 1, - SEEK_CUR); - goto restart; - } + if (__sgetspent_r(line_buff, spwd, line_buff, buflen) < 0) + goto restart; - if (__sgetspent_r(line_buff, spwd, line_buff, buflen) < 0) - goto restart; - - return 0; + return 0; } diff --git a/libc/pwd_grp/fgetpwent.c b/libc/pwd_grp/fgetpwent.c index e025cf6cc..5ca08d3b0 100644 --- a/libc/pwd_grp/fgetpwent.c +++ b/libc/pwd_grp/fgetpwent.c @@ -18,10 +18,21 @@ * */ +#include <features.h> #include <errno.h> #include <stdio.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + int fgetpwent_r (FILE *file, struct passwd *password, char *buff, size_t buflen, struct passwd **crap) { @@ -37,8 +48,11 @@ struct passwd *fgetpwent(FILE * file) static char line_buff[PWD_BUFFER_SIZE]; static struct passwd pwd; + LOCK; if (fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), NULL) != -1) { + UNLOCK; return &pwd; } + UNLOCK; return NULL; } diff --git a/libc/pwd_grp/fgetspent.c b/libc/pwd_grp/fgetspent.c index 68053b39e..20698da1b 100644 --- a/libc/pwd_grp/fgetspent.c +++ b/libc/pwd_grp/fgetspent.c @@ -17,27 +17,41 @@ * */ +#include <features.h> #include <errno.h> #include <stdio.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + int fgetspent_r (FILE *file, struct spwd *spwd, char *buff, size_t buflen, struct spwd **crap) { - if (file == NULL) { - __set_errno(EINTR); - return -1; - } - return(__getspent_r(spwd, buff, buflen, fileno(file))); + if (file == NULL) { + __set_errno(EINTR); + return -1; + } + return(__getspent_r(spwd, buff, buflen, fileno(file))); } struct spwd *fgetspent(FILE * file) { - static char line_buff[PWD_BUFFER_SIZE]; - static struct spwd spwd; + static char line_buff[PWD_BUFFER_SIZE]; + static struct spwd spwd; - if (fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), NULL) != -1) { - return &spwd; - } - return NULL; + LOCK; + if (fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), NULL) != -1) { + UNLOCK; + return &spwd; + } + UNLOCK; + return NULL; } diff --git a/libc/pwd_grp/getpwnam.c b/libc/pwd_grp/getpwnam.c index b0674b15d..00adb57ca 100644 --- a/libc/pwd_grp/getpwnam.c +++ b/libc/pwd_grp/getpwnam.c @@ -18,6 +18,7 @@ * */ +#include <features.h> #include <unistd.h> #include <string.h> #include <errno.h> @@ -25,6 +26,16 @@ #include <paths.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + int getpwnam_r (const char *name, struct passwd *password, char *buff, size_t buflen, struct passwd **crap) { @@ -53,9 +64,12 @@ struct passwd *getpwnam(const char *name) static char line_buff[PWD_BUFFER_SIZE]; static struct passwd pwd; + LOCK; if (getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), NULL) != -1) { + UNLOCK; return &pwd; } + UNLOCK; return NULL; } diff --git a/libc/pwd_grp/getpwuid.c b/libc/pwd_grp/getpwuid.c index ff3637abe..48ee6123d 100644 --- a/libc/pwd_grp/getpwuid.c +++ b/libc/pwd_grp/getpwuid.c @@ -18,12 +18,23 @@ * */ +#include <features.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <paths.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + int getpwuid_r (uid_t uid, struct passwd *password, char *buff, size_t buflen, struct passwd **crap) { @@ -48,9 +59,12 @@ struct passwd *getpwuid(uid_t uid) static char line_buff[PWD_BUFFER_SIZE]; static struct passwd pwd; + LOCK; if (getpwuid_r(uid, &pwd, line_buff, sizeof(line_buff), NULL) != -1) { + UNLOCK; return &pwd; } + UNLOCK; return NULL; } diff --git a/libc/pwd_grp/getspnam.c b/libc/pwd_grp/getspnam.c index d8a29a8a2..9d578f09a 100644 --- a/libc/pwd_grp/getspnam.c +++ b/libc/pwd_grp/getspnam.c @@ -17,12 +17,23 @@ * */ +#include <features.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + int getspnam_r (const char *name, struct spwd *spwd, char *buff, size_t buflen, struct spwd **crap) { @@ -51,9 +62,12 @@ struct spwd *getspnam(const char *name) static char line_buff[PWD_BUFFER_SIZE]; static struct spwd spwd; + LOCK; if (getspnam_r(name, &spwd, line_buff, sizeof(line_buff), NULL) != -1) { + UNLOCK; return &spwd; } + UNLOCK; return NULL; } diff --git a/libc/pwd_grp/getspuid.c b/libc/pwd_grp/getspuid.c index f06eb1ad0..d57b2aa0b 100644 --- a/libc/pwd_grp/getspuid.c +++ b/libc/pwd_grp/getspuid.c @@ -17,11 +17,22 @@ * */ +#include <features.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + int getspuid_r (uid_t uid, struct spwd *spwd, char *buff, size_t buflen, struct spwd **crap) { @@ -39,9 +50,12 @@ struct spwd *getspuid(uid_t uid) static char line_buff[PWD_BUFFER_SIZE]; static struct spwd spwd; + LOCK; if (getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), NULL) != -1) { + UNLOCK; return &spwd; } + UNLOCK; return NULL; } diff --git a/libc/pwd_grp/grent.c b/libc/pwd_grp/grent.c index 42db9f9b6..f867a1aaf 100644 --- a/libc/pwd_grp/grent.c +++ b/libc/pwd_grp/grent.c @@ -24,30 +24,47 @@ * in together. */ +#include <features.h> #include <unistd.h> #include <fcntl.h> #include <paths.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + static int grp_fd = -1; void setgrent(void) { - if (grp_fd != -1) - close(grp_fd); - grp_fd = open(_PATH_GROUP, O_RDONLY); + LOCK; + if (grp_fd != -1) + close(grp_fd); + grp_fd = open(_PATH_GROUP, O_RDONLY); + UNLOCK; } void endgrent(void) { - if (grp_fd != -1) - close(grp_fd); - grp_fd = -1; + LOCK; + if (grp_fd != -1) + close(grp_fd); + grp_fd = -1; + UNLOCK; } struct group *getgrent(void) { - if (grp_fd == -1) - return NULL; - return __getgrent(grp_fd); + LOCK; + if (grp_fd == -1) + return NULL; + return __getgrent(grp_fd); + UNLOCK; } diff --git a/libc/pwd_grp/initgroups.c b/libc/pwd_grp/initgroups.c index 91af42f42..fb19c0327 100644 --- a/libc/pwd_grp/initgroups.c +++ b/libc/pwd_grp/initgroups.c @@ -27,49 +27,49 @@ int initgroups(__const char *user, gid_t gid) { - register struct group *group; + register struct group *group; #ifndef GR_DYNAMIC_GROUP_LIST - gid_t group_list[GR_MAX_GROUPS]; + gid_t group_list[GR_MAX_GROUPS]; #else - gid_t *group_list = NULL; + gid_t *group_list = NULL; #endif - register char **tmp_mem; - int num_groups; - int grp_fd; + register char **tmp_mem; + int num_groups; + int grp_fd; - if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) - return -1; + if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) + return -1; - num_groups = 0; + num_groups = 0; #ifdef GR_DYNAMIC_GROUP_LIST - group_list = (gid_t *) realloc(group_list, 1); + group_list = (gid_t *) realloc(group_list, 1); #endif - group_list[num_groups] = gid; + group_list[num_groups] = gid; #ifndef GR_DYNAMIC_GROUP_LIST - while (num_groups < GR_MAX_GROUPS && - (group = __getgrent(grp_fd)) != NULL) + while (num_groups < GR_MAX_GROUPS && + (group = __getgrent(grp_fd)) != NULL) #else while ((group = __getgrent(grp_fd)) != NULL) #endif { - if (group->gr_gid != gid); - { - tmp_mem = group->gr_mem; - while (*tmp_mem != NULL) { - if (!strcmp(*tmp_mem, user)) { - num_groups++; + if (group->gr_gid != gid); + { + tmp_mem = group->gr_mem; + while (*tmp_mem != NULL) { + if (!strcmp(*tmp_mem, user)) { + num_groups++; #ifdef GR_DYNAMIC_GROUP_LIST - group_list = (gid_t *) realloc(group_list, num_groups * - sizeof(gid_t *)); + group_list = (gid_t *) realloc(group_list, num_groups * + sizeof(gid_t *)); #endif - group_list[num_groups-1] = group->gr_gid; - } - tmp_mem++; - } + group_list[num_groups-1] = group->gr_gid; + } + tmp_mem++; } + } } - close(grp_fd); - return setgroups(num_groups, group_list); + close(grp_fd); + return setgroups(num_groups, group_list); } diff --git a/libc/pwd_grp/lckpwdf.c b/libc/pwd_grp/lckpwdf.c index 148d2c93a..6b20bd7e4 100644 --- a/libc/pwd_grp/lckpwdf.c +++ b/libc/pwd_grp/lckpwdf.c @@ -18,6 +18,7 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include <features.h> #include <fcntl.h> #include <signal.h> #include <string.h> @@ -25,6 +26,16 @@ #include <sys/file.h> #include <paths.h> +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + /* How long to wait for getting the lock before returning with an error. */ #define TIMEOUT 15 /* sec */ @@ -50,10 +61,14 @@ int lckpwdf (void) /* Still locked by own process. */ return -1; + LOCK; + lock_fd = open (_PATH_PASSWD, O_WRONLY); - if (lock_fd == -1) + if (lock_fd == -1) { /* Cannot create lock file. */ + UNLOCK; return -1; + } /* Make sure file gets correctly closed when process finished. */ flags = fcntl (lock_fd, F_GETFD, 0); @@ -61,6 +76,7 @@ int lckpwdf (void) /* Cannot get file flags. */ close(lock_fd); lock_fd = -1; + UNLOCK; return -1; } flags |= FD_CLOEXEC; /* Close on exit. */ @@ -68,6 +84,7 @@ int lckpwdf (void) /* Cannot set new flags. */ close(lock_fd); lock_fd = -1; + UNLOCK; return -1; } @@ -89,6 +106,7 @@ int lckpwdf (void) /* Cannot install signal handler. */ close(lock_fd); lock_fd = -1; + UNLOCK; return -1; } @@ -99,6 +117,7 @@ int lckpwdf (void) sigaction (SIGALRM, &saved_act, NULL); close(lock_fd); lock_fd = -1; + UNLOCK; return -1; } @@ -126,9 +145,11 @@ int lckpwdf (void) if (result < 0) { close(lock_fd); lock_fd = -1; + UNLOCK; return -1; } + UNLOCK; return 0; } @@ -142,10 +163,11 @@ int ulckpwdf (void) result = -1; } else { + LOCK; result = close (lock_fd); - /* Mark descriptor as unused. */ lock_fd = -1; + UNLOCK; } return result; diff --git a/libc/pwd_grp/pwent.c b/libc/pwd_grp/pwent.c index 55fd249bc..1bc129bf6 100644 --- a/libc/pwd_grp/pwent.c +++ b/libc/pwd_grp/pwent.c @@ -18,6 +18,7 @@ * */ +#include <features.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> @@ -25,6 +26,16 @@ #include <paths.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + /* * setpwent(), endpwent(), and getpwent() are included in the same object * file, since one cannot be used without the other two, so it makes sense to @@ -36,25 +47,32 @@ static int pw_fd = -1; void setpwent(void) { + LOCK; if (pw_fd != -1) close(pw_fd); pw_fd = open(_PATH_PASSWD, O_RDONLY); + UNLOCK; } void endpwent(void) { + LOCK; if (pw_fd != -1) close(pw_fd); pw_fd = -1; + UNLOCK; } int getpwent_r (struct passwd *password, char *buff, size_t buflen, struct passwd **crap) { + LOCK; if (pw_fd != -1 && __getpwent_r(password, buff, buflen, pw_fd) != -1) { + UNLOCK; return 0; } + UNLOCK; return -1; } @@ -62,9 +80,12 @@ struct passwd *getpwent(void) { static char line_buff[PWD_BUFFER_SIZE]; static struct passwd pwd; + LOCK; if (getpwent_r(&pwd, line_buff, sizeof(line_buff), NULL) != -1) { + UNLOCK; return &pwd; } + UNLOCK; return NULL; } diff --git a/libc/pwd_grp/sgetspent.c b/libc/pwd_grp/sgetspent.c index 83b1144ee..1925a78ec 100644 --- a/libc/pwd_grp/sgetspent.c +++ b/libc/pwd_grp/sgetspent.c @@ -17,10 +17,21 @@ * */ +#include <features.h> #include <errno.h> #include <stdio.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + int sgetspent_r (const char *string, struct spwd *spwd, char *buff, size_t buflen, struct spwd **crap) { @@ -32,8 +43,11 @@ struct spwd *sgetspent(const char *string) static char line_buff[PWD_BUFFER_SIZE]; static struct spwd spwd; + LOCK; if (sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), NULL) != -1) { + UNLOCK; return &spwd; } + UNLOCK; return NULL; } diff --git a/libc/pwd_grp/spent.c b/libc/pwd_grp/spent.c index c1305b15e..d1b8ca07c 100644 --- a/libc/pwd_grp/spent.c +++ b/libc/pwd_grp/spent.c @@ -17,12 +17,23 @@ * */ +#include <features.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include "config.h" +#ifdef __UCLIBC_HAS_THREADS__ +#include <pthread.h> +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); +#else +# define LOCK +# define UNLOCK +#endif + /* * setspent(), endspent(), and getspent() are included in the same object * file, since one cannot be used without the other two, so it makes sense to @@ -34,25 +45,31 @@ static int spwd_fd = -1; void setspent(void) { + LOCK; if (spwd_fd != -1) close(spwd_fd); - spwd_fd = open(_PATH_SHADOW, O_RDONLY); + UNLOCK; } void endspent(void) { + LOCK; if (spwd_fd != -1) close(spwd_fd); spwd_fd = -1; + UNLOCK; } int getspent_r (struct spwd *spwd, char *buff, size_t buflen, struct spwd **crap) { + LOCK; if (spwd_fd != -1 && __getspent_r(spwd, buff, buflen, spwd_fd) != -1) { + UNLOCK; return 0; } + UNLOCK; return -1; } @@ -61,9 +78,12 @@ struct spwd *getspent(void) static char line_buff[PWD_BUFFER_SIZE]; static struct spwd spwd; + LOCK; if (getspent_r(&spwd, line_buff, sizeof(line_buff), NULL) != -1) { + UNLOCK; return &spwd; } + UNLOCK; return NULL; } |