diff options
author | Eric Andersen <andersen@codepoet.org> | 2003-10-10 07:34:27 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2003-10-10 07:34:27 +0000 |
commit | 56104838733c81ba410ac41f767ac72267049489 (patch) | |
tree | 54057d78cdae893235b86787a735629396d857a5 /libc/pwd_grp | |
parent | 61011662018fa98c4610c1ae826e417678cd5c80 (diff) |
Implement getgrgid_r and getgrnam_r. Rework group handling code to be fully
reentrant, since there was still a shared static value. indent stuff,
Diffstat (limited to 'libc/pwd_grp')
-rw-r--r-- | libc/pwd_grp/Makefile | 4 | ||||
-rw-r--r-- | libc/pwd_grp/__getgrent.c | 121 | ||||
-rw-r--r-- | libc/pwd_grp/__getgrent_r.c | 114 | ||||
-rw-r--r-- | libc/pwd_grp/__getpwent_r.c | 77 | ||||
-rw-r--r-- | libc/pwd_grp/__getspent_r.c | 46 | ||||
-rw-r--r-- | libc/pwd_grp/__sgetspent_r.c | 58 | ||||
-rw-r--r-- | libc/pwd_grp/config.h | 4 | ||||
-rw-r--r-- | libc/pwd_grp/fgetgrent.c | 51 | ||||
-rw-r--r-- | libc/pwd_grp/fgetpwent.c | 40 | ||||
-rw-r--r-- | libc/pwd_grp/fgetspent.c | 40 | ||||
-rw-r--r-- | libc/pwd_grp/getgrgid.c | 60 | ||||
-rw-r--r-- | libc/pwd_grp/getgrnam.c | 63 | ||||
-rw-r--r-- | libc/pwd_grp/getpw.c | 8 | ||||
-rw-r--r-- | libc/pwd_grp/getpwnam.c | 59 | ||||
-rw-r--r-- | libc/pwd_grp/getpwuid.c | 49 | ||||
-rw-r--r-- | libc/pwd_grp/getspnam.c | 54 | ||||
-rw-r--r-- | libc/pwd_grp/getspuid.c | 44 | ||||
-rw-r--r-- | libc/pwd_grp/grent.c | 58 | ||||
-rw-r--r-- | libc/pwd_grp/initgroups.c | 67 | ||||
-rw-r--r-- | libc/pwd_grp/lckpwdf.c | 227 | ||||
-rw-r--r-- | libc/pwd_grp/putpwent.c | 8 | ||||
-rw-r--r-- | libc/pwd_grp/putspent.c | 1 | ||||
-rw-r--r-- | libc/pwd_grp/pwent.c | 60 | ||||
-rw-r--r-- | libc/pwd_grp/sgetspent.c | 34 | ||||
-rw-r--r-- | libc/pwd_grp/spent.c | 62 |
25 files changed, 738 insertions, 671 deletions
diff --git a/libc/pwd_grp/Makefile b/libc/pwd_grp/Makefile index c34142d6b..06ba857f0 100644 --- a/libc/pwd_grp/Makefile +++ b/libc/pwd_grp/Makefile @@ -25,7 +25,7 @@ TOPDIR=../../ include $(TOPDIR)Rules.mak CSRC= pwent.c getpwnam.c getpwuid.c putpwent.c getpw.c fgetpwent.c \ - __getgrent.c grent.c getgrnam.c getgrgid.c fgetgrent.c \ + __getgrent_r.c grent.c getgrnam.c getgrgid.c fgetgrent.c \ initgroups.c __getpwent_r.c ifeq ($(HAS_SHADOW),y) @@ -49,7 +49,7 @@ $(COBJS): %.o : %.c $(OBJ): Makefile -__getgrent.c: config.h +__getgrent_r.c: config.h initgroups.c: config.h clean: diff --git a/libc/pwd_grp/__getgrent.c b/libc/pwd_grp/__getgrent.c deleted file mode 100644 index ad0b8beb5..000000000 --- a/libc/pwd_grp/__getgrent.c +++ /dev/null @@ -1,121 +0,0 @@ -/* - * __getgrent.c - This file is part of the libc-8086/grp package for ELKS, - * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - -#include <features.h> -#include <unistd.h> -#include <stdlib.h> -#include <string.h> -#include "config.h" - - -#ifdef __UCLIBC_HAS_THREADS__ -#include <pthread.h> -/* This function should always be called under lock, so we - * do not lock things in here... */ -pthread_mutex_t __getgrent_lock = PTHREAD_MUTEX_INITIALIZER; -#endif - -/* - * This is the core group-file read function. It behaves exactly like - * getgrent() except that it is passed a file descriptor. getgrent() - * is just a wrapper for this function. - */ -struct group *__getgrent(int grp_fd, char *line_buff, char **members) -{ - short line_index; - short buff_size; - 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: - line_index = 0; - buff_size = 256; - - 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); - } - } - - /* 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'; - - 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; - - member_num = 0; - field_begin = ptr; - - 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; - - group.gr_mem = members; - return &group; -} diff --git a/libc/pwd_grp/__getgrent_r.c b/libc/pwd_grp/__getgrent_r.c new file mode 100644 index 000000000..69559bdb1 --- /dev/null +++ b/libc/pwd_grp/__getgrent_r.c @@ -0,0 +1,114 @@ +/* vi: set sw=4 ts=4: */ +/* + * __getgrent.c - This file is part of the libc-8086/grp package for ELKS, + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <features.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include "config.h" + + +/* + * This is the core group-file read function. It behaves exactly like + * getgrent() except that it is passed a file descriptor. getgrent() + * is just a wrapper for this function. + */ +int __getgrent_r (struct group *__restrict group, + char *__restrict line_buff, size_t buflen, int grp_fd) +{ + char *endptr, *field_begin, **members; + int i, line_len, member_num = 0; + + + if (buflen<GRP_BUFFER_SIZE) { + return ERANGE; + } + + /* We use the restart label to handle malformatted lines */ +restart: + /* Read the group line into the buffer for processing */ + if ((line_len = read(grp_fd, line_buff, buflen)) <= 0) { + return EIO; + } + 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, buflen)) <= 0) { + return EIO; + } + } 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'; + + /* We've read the line; now parse it. */ + field_begin = line_buff; + for (i = 0; i < 3; i++) { + switch (i) { + case 0: + group->gr_name = field_begin; + break; + case 1: + group->gr_passwd = field_begin; + break; + case 2: + group->gr_gid = (__gid_t) strtoul(field_begin, &endptr, 10); + if (*endptr != ':') + goto restart; + break; + } + if (i < 3) { + field_begin = strchr(field_begin, ':'); + if (field_begin == NULL) + break; + *field_begin++ = '\0'; + } + } + + members = (char **) malloc(sizeof(char *)); + if (members==NULL) { + return ENOMEM; + } + while(field_begin && strlen(field_begin)) { + members[member_num++] = field_begin; + members = (char **) realloc(members, (member_num + 1) * sizeof(char *)); + if (members==NULL) { + return ENOMEM; + } + endptr = strchr(field_begin, ','); + if (endptr == NULL) { + /* Final entry */ + break; + } + *field_begin++ = '\0'; + } + members[member_num] = NULL; + group->gr_mem = members; + return 0; +} diff --git a/libc/pwd_grp/__getpwent_r.c b/libc/pwd_grp/__getpwent_r.c index 5cfdd89ba..6e6de4038 100644 --- a/libc/pwd_grp/__getpwent_r.c +++ b/libc/pwd_grp/__getpwent_r.c @@ -1,7 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * __getpwent_r.c - This file is part of the libc-8086/pwd package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. - * Copyright (C) 2001 Erik Andersen <andersee@debian.org> + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -18,6 +19,7 @@ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * March 7, 2001 -- Reworked to be reentrant by Erik Andersen + * Oct 9, 2003 -- Reworked again by Erik Andersen to be fully reentrant */ #include <stdlib.h> @@ -36,38 +38,35 @@ int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pwd_fd) { - char *field_begin; - char *endptr; + char *endptr, *field_begin; char *gid_ptr=NULL; char *uid_ptr=NULL; - int line_len; - int i; + int i, line_len; - if (buflen<PWD_BUFFER_SIZE) + if (buflen<PWD_BUFFER_SIZE) { return ERANGE; + } /* We use the restart label to handle malformatted lines */ restart: - /* Read the passwd line into the static buffer using a minimum of - syscalls. */ - if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) + /* Read the passwd line into the buffer for processing */ + if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) { return EIO; + } field_begin = strchr(line_buff, '\n'); if (field_begin != NULL) - lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)), - SEEK_CUR); - else { /* The line is too long - skip it. :-\ */ - + lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)), SEEK_CUR); + else { + /* The line is too long - skip it. :-\ */ do { - if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) + if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) { return EIO; + } } while (!(field_begin = strchr(line_buff, '\n'))); - lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1, - SEEK_CUR); + lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1, SEEK_CUR); goto restart; } - if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || - *line_buff == '\t') + if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || *line_buff == '\t') goto restart; *field_begin = '\0'; @@ -75,27 +74,27 @@ restart: field_begin = line_buff; for (i = 0; i < 7; i++) { switch (i) { - case 0: - passwd->pw_name = field_begin; - break; - case 1: - passwd->pw_passwd = field_begin; - break; - case 2: - uid_ptr = field_begin; - break; - case 3: - gid_ptr = field_begin; - break; - case 4: - passwd->pw_gecos = field_begin; - break; - case 5: - passwd->pw_dir = field_begin; - break; - case 6: - passwd->pw_shell = field_begin; - break; + case 0: + passwd->pw_name = field_begin; + break; + case 1: + passwd->pw_passwd = field_begin; + break; + case 2: + uid_ptr = field_begin; + break; + case 3: + gid_ptr = field_begin; + break; + case 4: + passwd->pw_gecos = field_begin; + break; + case 5: + passwd->pw_dir = field_begin; + break; + case 6: + passwd->pw_shell = field_begin; + break; } if (i < 6) { field_begin = strchr(field_begin, ':'); diff --git a/libc/pwd_grp/__getspent_r.c b/libc/pwd_grp/__getspent_r.c index 73e40f031..8ac7d40ea 100644 --- a/libc/pwd_grp/__getspent_r.c +++ b/libc/pwd_grp/__getspent_r.c @@ -1,4 +1,6 @@ +/* vi: set sw=4 ts=4: */ /* __getspent_r.c - Based on __getpwent_r.c + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -26,32 +28,32 @@ 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; - if (buflen<PWD_BUFFER_SIZE) - return ERANGE; + if (buflen<PWD_BUFFER_SIZE) + return ERANGE; - /* We use the restart label to handle malformatted lines */ + /* 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 EIO; - 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) + /* Read the shadow line into the buffer using a minimum of syscalls. */ + if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0) return EIO; - } while (!(endptr = strchr(line_buff, '\n'))); - lseek(spwd_fd, (long) (endptr - line_buff) - line_len + 1, SEEK_CUR); - goto restart; - } + 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 EIO; + } 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/__sgetspent_r.c b/libc/pwd_grp/__sgetspent_r.c index d11cfe62e..fbd40fc62 100644 --- a/libc/pwd_grp/__sgetspent_r.c +++ b/libc/pwd_grp/__sgetspent_r.c @@ -1,5 +1,7 @@ +/* vi: set sw=4 ts=4: */ /* * __sgetspent_r.c - Based on __getpwent_r.c + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -46,7 +48,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz } if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || - *line_buff == '\t') + *line_buff == '\t') return EINVAL; field_begin = strchr(line_buff, '\n'); @@ -57,33 +59,33 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz field_begin = line_buff; for (i = 0; i < 9; i++) { switch (i) { - case 0: - spwd->sp_namp = field_begin; - break; - case 1: - spwd->sp_pwdp = field_begin; - break; - case 2: - lstchg_ptr = field_begin; - break; - case 3: - min_ptr = field_begin; - break; - case 4: - max_ptr = field_begin; - break; - case 5: - warn_ptr = field_begin; - break; - case 6: - inact_ptr = field_begin; - break; - case 7: - expire_ptr = field_begin; - break; - case 8: - flag_ptr = field_begin; - break; + case 0: + spwd->sp_namp = field_begin; + break; + case 1: + spwd->sp_pwdp = field_begin; + break; + case 2: + lstchg_ptr = field_begin; + break; + case 3: + min_ptr = field_begin; + break; + case 4: + max_ptr = field_begin; + break; + case 5: + warn_ptr = field_begin; + break; + case 6: + inact_ptr = field_begin; + break; + case 7: + expire_ptr = field_begin; + break; + case 8: + flag_ptr = field_begin; + break; } if (i < 8) { field_begin = strchr(field_begin, ':'); diff --git a/libc/pwd_grp/config.h b/libc/pwd_grp/config.h index 25f450d6c..fb678876b 100644 --- a/libc/pwd_grp/config.h +++ b/libc/pwd_grp/config.h @@ -28,10 +28,12 @@ #include <shadow.h> #define PWD_BUFFER_SIZE 256 +#define GRP_BUFFER_SIZE 256 /* These are used internally to uClibc */ -extern struct group *__getgrent(int grp_fd, char *line_buff, char **members); +extern int __getgrent_r (struct group *__restrict group, + char *__restrict line_buff, size_t buflen, int grp_fd); extern int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pwd_fd); extern int __getspent_r(struct spwd * spwd, char * line_buff, diff --git a/libc/pwd_grp/fgetgrent.c b/libc/pwd_grp/fgetgrent.c index 2c7917601..d1a6514bd 100644 --- a/libc/pwd_grp/fgetgrent.c +++ b/libc/pwd_grp/fgetgrent.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * fgetgrent.c - This file is part of the libc-8086/grp package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -24,28 +26,49 @@ #ifdef __UCLIBC_HAS_THREADS__ #include <pthread.h> -extern pthread_mutex_t __getgrent_lock; -# define LOCK pthread_mutex_lock(&__getgrent_lock) -# define UNLOCK pthread_mutex_unlock(&__getgrent_lock); +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 char *line_buff = NULL; -static char **members = NULL; + +int fgetgrent_r (FILE *__restrict file, struct group *__restrict grp, + char *__restrict buff, size_t buflen, + struct group **__restrict result) +{ + int ret; + if (file == NULL) { + return EINTR; + } + *result = NULL; + flockfile(file); + ret = __getgrent_r(grp, buff, buflen, fileno(file)); + funlockfile(file); + if (ret == 0) { + *result = grp; + return 0; + } + __set_errno(ret); + return ret; +} struct group *fgetgrent(FILE * file) { - struct group *grp; + int ret; + struct group *result; + static struct group grp; + static char line_buff[PWD_BUFFER_SIZE]; - if (file == NULL) { - __set_errno(EINTR); + LOCK; + ret=fgetgrent_r(file, &grp, line_buff, sizeof(line_buff), &result); + if (ret == 0) { + UNLOCK; + return result; + } + UNLOCK; + __set_errno(ret); return NULL; - } - - LOCK; - grp = __getgrent(fileno(file), line_buff, members); - UNLOCK; - return grp; } diff --git a/libc/pwd_grp/fgetpwent.c b/libc/pwd_grp/fgetpwent.c index 174629fcb..14ac73698 100644 --- a/libc/pwd_grp/fgetpwent.c +++ b/libc/pwd_grp/fgetpwent.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * fgetpwent.c - This file is part of the libc-8086/pwd package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -36,29 +38,29 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; int fgetpwent_r (FILE *file, struct passwd *password, char *buff, size_t buflen, struct passwd **result) { - int res; - if (file == NULL) { - return EINTR; - } - *result = NULL; - res = __getpwent_r(password, buff, buflen, fileno(file)); - *result = password; - return res; + int res; + if (file == NULL) { + return EINTR; + } + *result = NULL; + res = __getpwent_r(password, buff, buflen, fileno(file)); + *result = password; + return res; } struct passwd *fgetpwent(FILE * file) { - int ret; - static char line_buff[PWD_BUFFER_SIZE]; - static struct passwd pwd; - struct passwd *result; + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct passwd pwd; + struct passwd *result; - LOCK; - if ((ret=fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), &result)) == 0) { + LOCK; + if ((ret=fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &pwd; + } UNLOCK; - return &pwd; - } - UNLOCK; - __set_errno(ret); - return NULL; + __set_errno(ret); + return NULL; } diff --git a/libc/pwd_grp/fgetspent.c b/libc/pwd_grp/fgetspent.c index 2ecf3a174..497db40cc 100644 --- a/libc/pwd_grp/fgetspent.c +++ b/libc/pwd_grp/fgetspent.c @@ -1,5 +1,7 @@ +/* vi: set sw=4 ts=4: */ /* * fgetspent.c - Based on fgetpwent.c + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -35,29 +37,29 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; int fgetspent_r (FILE *file, struct spwd *spwd, char *buff, size_t buflen, struct spwd **result) { - int res; - if (file == NULL) { - return EINTR; - } - *result = NULL; - res = __getspent_r(spwd, buff, buflen, fileno(file)); - *result = spwd; - return res; + int res; + if (file == NULL) { + return EINTR; + } + *result = NULL; + res = __getspent_r(spwd, buff, buflen, fileno(file)); + *result = spwd; + return res; } struct spwd *fgetspent(FILE * file) { - int ret; - static char line_buff[PWD_BUFFER_SIZE]; - static struct spwd spwd; - struct spwd *result; + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct spwd spwd; + struct spwd *result; - LOCK; - if ((ret=fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), &result)) == 0) { + LOCK; + if ((ret=fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &spwd; + } UNLOCK; - return &spwd; - } - UNLOCK; - __set_errno(ret); - return NULL; + __set_errno(ret); + return NULL; } diff --git a/libc/pwd_grp/getgrgid.c b/libc/pwd_grp/getgrgid.c index 3575a749b..b8f2a29f5 100644 --- a/libc/pwd_grp/getgrgid.c +++ b/libc/pwd_grp/getgrgid.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * getgrgid.c - This file is part of the libc-8086/grp package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -22,48 +24,56 @@ #include <unistd.h> #include <fcntl.h> #include <paths.h> +#include <errno.h> #include "config.h" #ifdef __UCLIBC_HAS_THREADS__ #include <pthread.h> -extern pthread_mutex_t __getgrent_lock; -# define LOCK pthread_mutex_lock(&__getgrent_lock) -# define UNLOCK pthread_mutex_unlock(&__getgrent_lock); +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 char *line_buff = NULL; -static char **members = NULL; -struct group *getgrgid(const gid_t gid) +/* Search for an entry with a matching group ID. */ +int getgrgid_r (gid_t gid, struct group *group, char *buffer, + size_t buflen, struct group **result) { - struct group *group; - int grp_fd; + int grp_fd; + if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) + return errno; - if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) - return NULL; - - LOCK; - while ((group = __getgrent(grp_fd, line_buff, members)) != NULL) - if (group->gr_gid == gid) { - close(grp_fd); - UNLOCK; - return group; + *result = NULL; + while (__getgrent_r(group, buffer, buflen, grp_fd) == 0) { + if (group->gr_gid == gid) { + close(grp_fd); + *result = group; + return 0; + } } - close(grp_fd); - UNLOCK; - return NULL; + close(grp_fd); + return EINVAL; } -#if 0 -/* Search for an entry with a matching group ID. */ -int getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer, - size_t buflen, struct group **result) +struct group *getgrgid(const gid_t gid) { + int ret; + struct group *result; + static struct group grp; + static char line_buff[GRP_BUFFER_SIZE]; + LOCK; + if ((ret=getgrgid_r(gid, &grp, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &grp; + } + UNLOCK; + __set_errno(ret); + return NULL; } -#endif + diff --git a/libc/pwd_grp/getgrnam.c b/libc/pwd_grp/getgrnam.c index 2c8d030e0..27d714407 100644 --- a/libc/pwd_grp/getgrnam.c +++ b/libc/pwd_grp/getgrnam.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * getgrnam.c - This file is part of the libc-8086/grp package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -18,6 +20,7 @@ * */ +#include <features.h> #include <unistd.h> #include <string.h> #include <errno.h> @@ -27,39 +30,57 @@ #ifdef __UCLIBC_HAS_THREADS__ #include <pthread.h> -extern pthread_mutex_t __getgrent_lock; -# define LOCK pthread_mutex_lock(&__getgrent_lock) -# define UNLOCK pthread_mutex_unlock(&__getgrent_lock); -#else +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 char *line_buff = NULL; -static char **members = NULL; +#endif - -struct group *getgrnam(const char *name) +int getgrnam_r (const char *name, struct group *group, + char *buff, size_t buflen, struct group **result) { - int grp_fd; - struct group *group; + int ret; + int group_fd; + + *result = NULL; if (name == NULL) { - __set_errno(EINVAL); - return NULL; + return EINVAL; } - if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) - return NULL; + if ((group_fd = open(_PATH_GROUP, O_RDONLY)) < 0) { + return ENOENT; + } - LOCK; - while ((group = __getgrent(grp_fd, line_buff, members)) != NULL) + while ((ret=__getgrent_r(group, buff, buflen, group_fd)) == 0) { if (!strcmp(group->gr_name, name)) { - close(grp_fd); - UNLOCK; - return group; + close(group_fd); + *result = group; + return 0; } + } + + close(group_fd); + return ret; +} - close(grp_fd); +struct group *getgrnam(const char *name) +{ + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct group grp; + struct group *result; + + LOCK; + if ((ret=getgrnam_r(name, &grp, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &grp; + } + __set_errno(ret); UNLOCK; return NULL; } + + diff --git a/libc/pwd_grp/getpw.c b/libc/pwd_grp/getpw.c index 4731bb961..6665b6090 100644 --- a/libc/pwd_grp/getpw.c +++ b/libc/pwd_grp/getpw.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * getpw.c - This file is part of the libc-8086/pwd package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -35,9 +37,9 @@ int getpw(uid_t uid, char *buf) return -1; if (sprintf - (buf, "%s:%s:%u:%u:%s:%s:%s", passwd->pw_name, passwd->pw_passwd, - passwd->pw_gid, passwd->pw_uid, passwd->pw_gecos, passwd->pw_dir, - passwd->pw_shell) < 0) { + (buf, "%s:%s:%u:%u:%s:%s:%s", passwd->pw_name, passwd->pw_passwd, + passwd->pw_gid, passwd->pw_uid, passwd->pw_gecos, passwd->pw_dir, + passwd->pw_shell) < 0) { __set_errno(ENOBUFS); return -1; } diff --git a/libc/pwd_grp/getpwnam.c b/libc/pwd_grp/getpwnam.c index 7e81fa081..b9a55f114 100644 --- a/libc/pwd_grp/getpwnam.c +++ b/libc/pwd_grp/getpwnam.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * getpwnam.c - This file is part of the libc-8086/pwd package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -39,46 +41,45 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; int getpwnam_r (const char *name, struct passwd *password, char *buff, size_t buflen, struct passwd **result) { - int ret; - int passwd_fd; + int ret; + int passwd_fd; - *result = NULL; + *result = NULL; - if (name == NULL) { - return EINVAL; - } + if (name == NULL) { + return EINVAL; + } - if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0) { - return ENOENT; - } + if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0) { + return ENOENT; + } - while ((ret=__getpwent_r(password, buff, buflen, passwd_fd)) == 0) { - if (!strcmp(password->pw_name, name)) { - *result=password; - close(passwd_fd); - *result = password; - return 0; + while ((ret=__getpwent_r(password, buff, buflen, passwd_fd)) == 0) { + if (!strcmp(password->pw_name, name)) { + close(passwd_fd); + *result = password; + return 0; + } } - } - close(passwd_fd); - return ret; + close(passwd_fd); + return ret; } struct passwd *getpwnam(const char *name) { - int ret; - static char line_buff[PWD_BUFFER_SIZE]; - static struct passwd pwd; - struct passwd *result; + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct passwd pwd; + struct passwd *result; - LOCK; - if ((ret=getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), &result)) == 0) { + LOCK; + if ((ret=getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &pwd; + } + __set_errno(ret); UNLOCK; - return &pwd; - } - __set_errno(ret); - UNLOCK; - return NULL; + return NULL; } diff --git a/libc/pwd_grp/getpwuid.c b/libc/pwd_grp/getpwuid.c index 4a85aa289..5ec890647 100644 --- a/libc/pwd_grp/getpwuid.c +++ b/libc/pwd_grp/getpwuid.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * getpwuid.c - This file is part of the libc-8086/pwd package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -39,38 +41,37 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; int getpwuid_r (uid_t uid, struct passwd *password, char *buff, size_t buflen, struct passwd **result) { - int passwd_fd; + int passwd_fd; + if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0) + return errno; - if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0) - return errno; - - *result = NULL; - while (__getpwent_r(password, buff, buflen, passwd_fd) == 0) - if (password->pw_uid == uid) { - close(passwd_fd); - *result = password; - return 0; + *result = NULL; + while (__getpwent_r(password, buff, buflen, passwd_fd) == 0) { + if (password->pw_uid == uid) { + close(passwd_fd); + *result = password; + return 0; + } } - close(passwd_fd); - return EINVAL; + close(passwd_fd); + return EINVAL; } struct passwd *getpwuid(uid_t uid) { - int ret; - /* file descriptor for the password file currently open */ - static char line_buff[PWD_BUFFER_SIZE]; - static struct passwd pwd; - struct passwd *result; + int ret; + struct passwd *result; + static struct passwd pwd; + static char line_buff[PWD_BUFFER_SIZE]; - LOCK; - if ((ret=getpwuid_r(uid, &pwd, line_buff, sizeof(line_buff), &result)) == 0) { + LOCK; + if ((ret=getpwuid_r(uid, &pwd, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &pwd; + } UNLOCK; - return &pwd; - } - UNLOCK; - __set_errno(ret); - return NULL; + __set_errno(ret); + return NULL; } diff --git a/libc/pwd_grp/getspnam.c b/libc/pwd_grp/getspnam.c index f0a4155bf..2b1d4c95c 100644 --- a/libc/pwd_grp/getspnam.c +++ b/libc/pwd_grp/getspnam.c @@ -1,5 +1,7 @@ +/* vi: set sw=4 ts=4: */ /* * getspnam.c - Based on getpwnam.c + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -37,41 +39,41 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; int getspnam_r (const char *name, struct spwd *spwd, char *buff, size_t buflen, struct spwd **result) { - int spwd_fd; + int spwd_fd; - if (name == NULL) { - return EINVAL; - } + if (name == NULL) { + return EINVAL; + } - if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0) - return errno; + if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0) + return errno; - *result = NULL; - while (__getspent_r(spwd, buff, buflen, spwd_fd) == 0) - if (!strcmp(spwd->sp_namp, name)) { - close(spwd_fd); - *result = spwd; - return 0; - } + *result = NULL; + while (__getspent_r(spwd, buff, buflen, spwd_fd) == 0) + if (!strcmp(spwd->sp_namp, name)) { + close(spwd_fd); + *result = spwd; + return 0; + } - close(spwd_fd); - return EINVAL; + close(spwd_fd); + return EINVAL; } struct spwd *getspnam(const char *name) { - int ret; - static char line_buff[PWD_BUFFER_SIZE]; - static struct spwd spwd; - struct spwd *result; + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct spwd spwd; + struct spwd *result; - LOCK; - if ((ret=getspnam_r(name, &spwd, line_buff, sizeof(line_buff), &result)) == 0) { + LOCK; + if ((ret=getspnam_r(name, &spwd, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &spwd; + } UNLOCK; - return &spwd; - } - UNLOCK; - __set_errno(ret); - return NULL; + __set_errno(ret); + return NULL; } diff --git a/libc/pwd_grp/getspuid.c b/libc/pwd_grp/getspuid.c index b25fddb49..c97d186bd 100644 --- a/libc/pwd_grp/getspuid.c +++ b/libc/pwd_grp/getspuid.c @@ -1,5 +1,7 @@ +/* vi: set sw=4 ts=4: */ /* * getspuid.c - Based on getpwuid.c + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -37,34 +39,34 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; int getspuid_r (uid_t uid, struct spwd *spwd, char *buff, size_t buflen, struct spwd **result) { - int ret; - char pwd_buff[PWD_BUFFER_SIZE]; - struct passwd password; + int ret; + char pwd_buff[PWD_BUFFER_SIZE]; + struct passwd password; - *result = NULL; - ret = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), NULL); - if (ret != 0) - return ret; + *result = NULL; + ret = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), NULL); + if (ret != 0) + return ret; - ret = getspnam_r(password.pw_name, spwd, buff, buflen, result); - *result = spwd; - return ret; + ret = getspnam_r(password.pw_name, spwd, buff, buflen, result); + *result = spwd; + return ret; } struct spwd *getspuid(uid_t uid) { - int ret; - static char line_buff[PWD_BUFFER_SIZE]; - static struct spwd spwd; - struct spwd *result; + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct spwd spwd; + struct spwd *result; - LOCK; - if ((ret=getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), &result)) == 0) { + LOCK; + if ((ret=getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &spwd; + } UNLOCK; - return &spwd; - } - UNLOCK; - __set_errno(ret); - return NULL; + __set_errno(ret); + return NULL; } diff --git a/libc/pwd_grp/grent.c b/libc/pwd_grp/grent.c index 9418ea937..128520e8c 100644 --- a/libc/pwd_grp/grent.c +++ b/libc/pwd_grp/grent.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * grent.c - This file is part of the libc-8086/grp package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -28,6 +30,7 @@ #include <unistd.h> #include <fcntl.h> #include <paths.h> +#include <errno.h> #include "config.h" #ifdef __UCLIBC_HAS_THREADS__ @@ -40,50 +43,43 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; # define UNLOCK #endif -#ifdef __UCLIBC_HAS_THREADS__ -#include <pthread.h> -extern pthread_mutex_t __getgrent_lock; -# define GRENT_LOCK pthread_mutex_lock(&__getgrent_lock) -# define GRENT_UNLOCK pthread_mutex_unlock(&__getgrent_lock); -#else -# define GRENT_LOCK -# define GRENT_UNLOCK -#endif - static int grp_fd = -1; -static char *line_buff = NULL; -static char **members = NULL; void setgrent(void) { - LOCK; - if (grp_fd != -1) - close(grp_fd); - grp_fd = open(_PATH_GROUP, O_RDONLY); - UNLOCK; + LOCK; + if (grp_fd != -1) + close(grp_fd); + grp_fd = open(_PATH_GROUP, O_RDONLY); + UNLOCK; } void endgrent(void) { - LOCK; - if (grp_fd != -1) - close(grp_fd); - grp_fd = -1; - UNLOCK; + LOCK; + if (grp_fd != -1) + close(grp_fd); + grp_fd = -1; + UNLOCK; } struct group *getgrent(void) { - struct group *r; + int ret; + static struct group grp; + static char line_buff[PWD_BUFFER_SIZE]; - LOCK; - if (grp_fd == -1) { + LOCK; + if (grp_fd == -1) { + UNLOCK; + return NULL; + } + ret = __getgrent_r(&grp, line_buff, sizeof(line_buff), grp_fd); + if (ret == 0) { + UNLOCK; + return &grp; + } UNLOCK; + __set_errno(ret); return NULL; - } - UNLOCK; - GRENT_LOCK; - r = __getgrent(grp_fd, line_buff, members); - GRENT_UNLOCK; - return r; } diff --git a/libc/pwd_grp/initgroups.c b/libc/pwd_grp/initgroups.c index 24dadc18f..bc537069c 100644 --- a/libc/pwd_grp/initgroups.c +++ b/libc/pwd_grp/initgroups.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * initgroups.c - This file is part of the libc-8086/grp package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -28,51 +30,46 @@ #ifdef __UCLIBC_HAS_THREADS__ #include <pthread.h> -extern pthread_mutex_t __getgrent_lock; -# define LOCK pthread_mutex_lock(&__getgrent_lock) -# define UNLOCK pthread_mutex_unlock(&__getgrent_lock); +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 char *line_buff = NULL; -static char **members = NULL; - int initgroups(__const char *user, gid_t gid) { - register struct group *group; - - gid_t *group_list = NULL; - register char **tmp_mem; - int num_groups; - int grp_fd; - + gid_t *group_list = NULL; + register char **tmp_mem; + int num_groups; + int grp_fd; + static struct group group; + static char line_buff[PWD_BUFFER_SIZE]; - if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) - return errno; + if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) + return errno; - num_groups = 0; - group_list = (gid_t *) realloc(group_list, 1); - group_list[num_groups] = gid; - LOCK; - while ((group = __getgrent(grp_fd, line_buff, members)) != NULL) - { - if (group->gr_gid != gid) + num_groups = 0; + group_list = (gid_t *) realloc(group_list, 1); + group_list[num_groups] = gid; + LOCK; + while (__getgrent_r(&group, line_buff, sizeof(line_buff), grp_fd) == 0) { - tmp_mem = group->gr_mem; - while (*tmp_mem != NULL) { - if (!strcmp(*tmp_mem, user)) { - num_groups++; - group_list = (gid_t *) realloc(group_list, num_groups * - sizeof(gid_t *)); - group_list[num_groups-1] = group->gr_gid; + if (group.gr_gid != gid) + { + tmp_mem = group.gr_mem; + while (*tmp_mem != NULL) { + if (!strcmp(*tmp_mem, user)) { + num_groups++; + group_list = (gid_t *) realloc(group_list, num_groups * sizeof(gid_t *)); + group_list[num_groups-1] = group.gr_gid; + } + tmp_mem++; + } } - tmp_mem++; - } } - } - close(grp_fd); - UNLOCK; - return setgroups(num_groups, group_list); + close(grp_fd); + UNLOCK; + return setgroups(num_groups, group_list); } diff --git a/libc/pwd_grp/lckpwdf.c b/libc/pwd_grp/lckpwdf.c index 6b20bd7e4..e09b611e2 100644 --- a/libc/pwd_grp/lckpwdf.c +++ b/libc/pwd_grp/lckpwdf.c @@ -1,3 +1,4 @@ +/* vi: set sw=4 ts=4: */ /* Handle locking of password file. Copyright (C) 1996, 1998 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -49,132 +50,132 @@ static void noop_handler __P ((int __sig)); int lckpwdf (void) { - int flags; - sigset_t saved_set; /* Saved set of caught signals. */ - struct sigaction saved_act; /* Saved signal action. */ - sigset_t new_set; /* New set of caught signals. */ - struct sigaction new_act; /* New signal action. */ - struct flock fl; /* Information struct for locking. */ - int result; - - if (lock_fd != -1) - /* Still locked by own process. */ - return -1; - - LOCK; - - lock_fd = open (_PATH_PASSWD, O_WRONLY); - 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); - if (flags == -1) { - /* Cannot get file flags. */ - close(lock_fd); - lock_fd = -1; - UNLOCK; - return -1; - } - flags |= FD_CLOEXEC; /* Close on exit. */ - if (fcntl (lock_fd, F_SETFD, flags) < 0) { - /* Cannot set new flags. */ - close(lock_fd); - lock_fd = -1; - UNLOCK; - return -1; - } - - /* Now we have to get exclusive write access. Since multiple - process could try this we won't stop when it first fails. - Instead we set a timeout for the system call. Once the timer - expires it is likely that there are some problems which cannot be - resolved by waiting. - - It is important that we don't change the signal state. We must - restore the old signal behaviour. */ - memset (&new_act, '\0', sizeof (struct sigaction)); - new_act.sa_handler = noop_handler; - sigfillset (&new_act.sa_mask); - new_act.sa_flags = 0ul; - - /* Install new action handler for alarm and save old. */ - if (sigaction (SIGALRM, &new_act, &saved_act) < 0) { - /* Cannot install signal handler. */ - close(lock_fd); - lock_fd = -1; - UNLOCK; - return -1; - } - - /* Now make sure the alarm signal is not blocked. */ - sigemptyset (&new_set); - sigaddset (&new_set, SIGALRM); - if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) { - sigaction (SIGALRM, &saved_act, NULL); - close(lock_fd); - lock_fd = -1; - UNLOCK; - return -1; - } - - /* Start timer. If we cannot get the lock in the specified time we - get a signal. */ - alarm (TIMEOUT); + int flags; + sigset_t saved_set; /* Saved set of caught signals. */ + struct sigaction saved_act; /* Saved signal action. */ + sigset_t new_set; /* New set of caught signals. */ + struct sigaction new_act; /* New signal action. */ + struct flock fl; /* Information struct for locking. */ + int result; + + if (lock_fd != -1) + /* Still locked by own process. */ + return -1; - /* Try to get the lock. */ - memset (&fl, '\0', sizeof (struct flock)); - fl.l_type = F_WRLCK; - fl.l_whence = SEEK_SET; - result = fcntl (lock_fd, F_SETLKW, &fl); - - /* Clear alarm. */ - alarm (0); + LOCK; - /* Restore old set of handled signals. We don't need to know - about the current one.*/ - sigprocmask (SIG_SETMASK, &saved_set, NULL); + lock_fd = open (_PATH_PASSWD, O_WRONLY); + 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); + if (flags == -1) { + /* Cannot get file flags. */ + close(lock_fd); + lock_fd = -1; + UNLOCK; + return -1; + } + flags |= FD_CLOEXEC; /* Close on exit. */ + if (fcntl (lock_fd, F_SETFD, flags) < 0) { + /* Cannot set new flags. */ + close(lock_fd); + lock_fd = -1; + UNLOCK; + return -1; + } + + /* Now we have to get exclusive write access. Since multiple + process could try this we won't stop when it first fails. + Instead we set a timeout for the system call. Once the timer + expires it is likely that there are some problems which cannot be + resolved by waiting. + + It is important that we don't change the signal state. We must + restore the old signal behaviour. */ + memset (&new_act, '\0', sizeof (struct sigaction)); + new_act.sa_handler = noop_handler; + sigfillset (&new_act.sa_mask); + new_act.sa_flags = 0ul; + + /* Install new action handler for alarm and save old. */ + if (sigaction (SIGALRM, &new_act, &saved_act) < 0) { + /* Cannot install signal handler. */ + close(lock_fd); + lock_fd = -1; + UNLOCK; + return -1; + } + + /* Now make sure the alarm signal is not blocked. */ + sigemptyset (&new_set); + sigaddset (&new_set, SIGALRM); + if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) { + sigaction (SIGALRM, &saved_act, NULL); + close(lock_fd); + lock_fd = -1; + UNLOCK; + return -1; + } + + /* Start timer. If we cannot get the lock in the specified time we + get a signal. */ + alarm (TIMEOUT); + + /* Try to get the lock. */ + memset (&fl, '\0', sizeof (struct flock)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + result = fcntl (lock_fd, F_SETLKW, &fl); + + /* Clear alarm. */ + alarm (0); + + /* Restore old set of handled signals. We don't need to know + about the current one.*/ + sigprocmask (SIG_SETMASK, &saved_set, NULL); + + /* Restore old action handler for alarm. We don't need to know + about the current one. */ + sigaction (SIGALRM, &saved_act, NULL); - /* Restore old action handler for alarm. We don't need to know - about the current one. */ - sigaction (SIGALRM, &saved_act, NULL); + if (result < 0) { + close(lock_fd); + lock_fd = -1; + UNLOCK; + return -1; + } - if (result < 0) { - close(lock_fd); - lock_fd = -1; UNLOCK; - return -1; - } - - UNLOCK; - return 0; + return 0; } int ulckpwdf (void) { - int result; - - if (lock_fd == -1) { - /* There is no lock set. */ - result = -1; - } - else { - LOCK; - result = close (lock_fd); - /* Mark descriptor as unused. */ - lock_fd = -1; - UNLOCK; - } - - return result; + int result; + + if (lock_fd == -1) { + /* There is no lock set. */ + result = -1; + } + else { + LOCK; + result = close (lock_fd); + /* Mark descriptor as unused. */ + lock_fd = -1; + UNLOCK; + } + + return result; } static void noop_handler (int sig) { - /* We simply return which makes the `fcntl' call return with an error. */ + /* We simply return which makes the `fcntl' call return with an error. */ } diff --git a/libc/pwd_grp/putpwent.c b/libc/pwd_grp/putpwent.c index da575b490..bbc8ec3a8 100644 --- a/libc/pwd_grp/putpwent.c +++ b/libc/pwd_grp/putpwent.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * putpwent.c - This file is part of the libc-8086/pwd package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -29,9 +31,9 @@ int putpwent(const struct passwd *passwd, FILE * f) return -1; } if (fprintf - (f, "%s:%s:%u:%u:%s:%s:%s\n", passwd->pw_name, passwd->pw_passwd, - passwd->pw_uid, passwd->pw_gid, passwd->pw_gecos, passwd->pw_dir, - passwd->pw_shell) < 0) + (f, "%s:%s:%u:%u:%s:%s:%s\n", passwd->pw_name, passwd->pw_passwd, + passwd->pw_uid, passwd->pw_gid, passwd->pw_gecos, passwd->pw_dir, + passwd->pw_shell) < 0) return -1; return 0; diff --git a/libc/pwd_grp/putspent.c b/libc/pwd_grp/putspent.c index 89a69887c..401401705 100644 --- a/libc/pwd_grp/putspent.c +++ b/libc/pwd_grp/putspent.c @@ -1,3 +1,4 @@ +/* vi: set sw=4 ts=4: */ /* Copyright (C) 1991, 1992, 1996, 1997, 1998 Free Software Foundation, Inc. This file is part of the GNU C Library. diff --git a/libc/pwd_grp/pwent.c b/libc/pwd_grp/pwent.c index b8ae812cb..e3488f4f5 100644 --- a/libc/pwd_grp/pwent.c +++ b/libc/pwd_grp/pwent.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * pwent.c - This file is part of the libc-8086/pwd package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -47,50 +49,50 @@ static int pw_fd = -1; void setpwent(void) { - LOCK; - if (pw_fd != -1) - close(pw_fd); + LOCK; + if (pw_fd != -1) + close(pw_fd); - pw_fd = open(_PATH_PASSWD, O_RDONLY); - UNLOCK; + pw_fd = open(_PATH_PASSWD, O_RDONLY); + UNLOCK; } void endpwent(void) { - LOCK; - if (pw_fd != -1) - close(pw_fd); - pw_fd = -1; - UNLOCK; + 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 **result) { - int ret=EINVAL; - LOCK; - *result = NULL; - if (pw_fd != -1 && (ret=__getpwent_r(password, buff, buflen, pw_fd)) == 0) { + int ret=EINVAL; + LOCK; + *result = NULL; + if (pw_fd != -1 && (ret=__getpwent_r(password, buff, buflen, pw_fd)) == 0) { + UNLOCK; + *result = password; + return 0; + } UNLOCK; - *result = password; - return 0; - } - UNLOCK; - __set_errno(ret); - return ret; + __set_errno(ret); + return ret; } struct passwd *getpwent(void) { - int ret; - static char line_buff[PWD_BUFFER_SIZE]; - static struct passwd pwd; - struct passwd *result; + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct passwd pwd; + struct passwd *result; - if ((ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) == 0) { - return &pwd; - } - __set_errno(ret); - return NULL; + if ((ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) == 0) { + return &pwd; + } + __set_errno(ret); + return NULL; } diff --git a/libc/pwd_grp/sgetspent.c b/libc/pwd_grp/sgetspent.c index d2e2ca71a..1c6bafbc2 100644 --- a/libc/pwd_grp/sgetspent.c +++ b/libc/pwd_grp/sgetspent.c @@ -1,5 +1,7 @@ +/* vi: set sw=4 ts=4: */ /* * sgetspent.c + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -35,26 +37,26 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; int sgetspent_r (const char *string, struct spwd *spwd, char *buff, size_t buflen, struct spwd **result) { - int ret; - *result = NULL; - ret = __sgetspent_r(string, spwd, buff, buflen); - *result = spwd; - return ret; + int ret; + *result = NULL; + ret = __sgetspent_r(string, spwd, buff, buflen); + *result = spwd; + return ret; } struct spwd *sgetspent(const char *string) { - int ret; - static char line_buff[PWD_BUFFER_SIZE]; - static struct spwd spwd; - struct spwd *result; + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct spwd spwd; + struct spwd *result; - LOCK; - if ((ret = sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result)) == 0) { + LOCK; + if ((ret = sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &spwd; + } + __set_errno(ret); UNLOCK; - return &spwd; - } - __set_errno(ret); - UNLOCK; - return NULL; + return NULL; } diff --git a/libc/pwd_grp/spent.c b/libc/pwd_grp/spent.c index 46f3a32be..126e8dec6 100644 --- a/libc/pwd_grp/spent.c +++ b/libc/pwd_grp/spent.c @@ -1,5 +1,7 @@ +/* vi: set sw=4 ts=4: */ /* * spent.c - Based on pwent.c + * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -45,51 +47,51 @@ static int spwd_fd = -1; void setspent(void) { - LOCK; - if (spwd_fd != -1) - close(spwd_fd); - spwd_fd = open(_PATH_SHADOW, O_RDONLY); - UNLOCK; + 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; + 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 **result) { - int ret=EINVAL; - LOCK; - *result = NULL; - if (spwd_fd != -1 && (ret=__getspent_r(spwd, buff, buflen, spwd_fd)) == 0) { + int ret=EINVAL; + LOCK; + *result = NULL; + if (spwd_fd != -1 && (ret=__getspent_r(spwd, buff, buflen, spwd_fd)) == 0) { + UNLOCK; + *result = spwd; + return 0; + } UNLOCK; - *result = spwd; - return 0; - } - UNLOCK; - return ret; + return ret; } struct spwd *getspent(void) { - int ret; - static char line_buff[PWD_BUFFER_SIZE]; - static struct spwd spwd; - struct spwd *result; + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct spwd spwd; + struct spwd *result; - LOCK; - if ((ret=getspent_r(&spwd, line_buff, sizeof(line_buff), &result)) == 0) { + LOCK; + if ((ret=getspent_r(&spwd, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &spwd; + } UNLOCK; - return &spwd; - } - UNLOCK; - __set_errno(ret); - return NULL; + __set_errno(ret); + return NULL; } |