Browse Source

Fixup errno handling
-Erik

Eric Andersen 22 years ago
parent
commit
2e55dec21f

+ 8 - 4
libc/pwd_grp/__getpwent_r.c

@@ -24,6 +24,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <fcntl.h>
+#include <errno.h>
 #include "config.h"
 
 
@@ -42,12 +43,15 @@ int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pw
 	int line_len;
 	int i;
 
+	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 minimal of
+restart:
+	/* Read the passwd line into the static buffer using a minimum of
 	   syscalls. */
 	if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
-		return -1;
+		return EIO;
 	field_begin = strchr(line_buff, '\n');
 	if (field_begin != NULL)
 		lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)),
@@ -56,7 +60,7 @@ int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pw
 
 		do {
 			if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
-				return -1;
+				return EIO;
 		} while (!(field_begin = strchr(line_buff, '\n')));
 		lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1,
 			  SEEK_CUR);

+ 7 - 3
libc/pwd_grp/__getspent_r.c

@@ -20,6 +20,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <fcntl.h>
+#include <errno.h>
 #include "config.h"
 
 
@@ -28,11 +29,14 @@ int __getspent_r(struct spwd * spwd, char * line_buff, size_t buflen, int spwd_f
     char *endptr;
     int line_len;
 
+    if (buflen<PWD_BUFFER_SIZE)
+	return ERANGE;
+
     /* 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;
+	return EIO;
     endptr = strchr(line_buff, '\n');
     if (endptr != NULL)
 	lseek(spwd_fd, (long) (1 + endptr - (line_buff + line_len)), SEEK_CUR);
@@ -40,13 +44,13 @@ restart:
 	/* The line is too long - skip it. :-\ */
 	do {
 	    if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
-		return -1;
+		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)
+    if (__sgetspent_r(line_buff, spwd, line_buff, buflen) != 0)
 	goto restart;
 
     return 0;

+ 14 - 10
libc/pwd_grp/__sgetspent_r.c

@@ -19,6 +19,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <errno.h>
 #include "config.h"
 
 
@@ -35,15 +36,18 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
 	char *flag_ptr=NULL;
 	int i;
 
+	if (buflen<PWD_BUFFER_SIZE)
+		return ERANGE;
+
 	if (string != line_buff) {
 		if (strlen(string) >= buflen)
-			return -1;
+			return ERANGE;
 		strcpy(line_buff, string);
 	}
 
 	if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
 		*line_buff == '\t')
-		return -1;
+		return EINVAL;
 
 	field_begin = strchr(line_buff, '\n');
 	if (field_begin != NULL)
@@ -86,7 +90,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
 			if (field_begin == NULL) {
 				if (i==4 || i==7)
 					break;
-				return -1;
+				return EINVAL;
 			}
 			*field_begin++ = '\0';
 		}
@@ -97,7 +101,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
 	} else {
 		spwd->sp_lstchg = (gid_t) strtoul(lstchg_ptr, &endptr, 10);
 		if (*endptr != '\0')
-			return -1;
+			return EINVAL;
 	}
 
 	if (*min_ptr == '\0') {
@@ -105,7 +109,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
 	} else {
 		spwd->sp_min = (gid_t) strtoul(min_ptr, &endptr, 10);
 		if (*endptr != '\0')
-			return -1;
+			return EINVAL;
 	}
 
 	if (*max_ptr == '\0') {
@@ -113,7 +117,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
 	} else {
 		spwd->sp_max = (gid_t) strtoul(max_ptr, &endptr, 10);
 		if (*endptr != '\0')
-			return -1;
+			return EINVAL;
 	}
 
 	if (warn_ptr == NULL) {
@@ -129,7 +133,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
 		} else {
 			spwd->sp_warn = (gid_t) strtoul(warn_ptr, &endptr, 10);
 			if (*endptr != '\0')
-				return -1;
+				return EINVAL;
 		}
 
 		if (*inact_ptr == '\0') {
@@ -137,7 +141,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
 		} else {
 			spwd->sp_inact = (gid_t) strtoul(inact_ptr, &endptr, 10);
 			if (*endptr != '\0')
-				return -1;
+				return EINVAL;
 		}
 
 		if (*expire_ptr == '\0') {
@@ -145,7 +149,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
 		} else {
 			spwd->sp_expire = (gid_t) strtoul(expire_ptr, &endptr, 10);
 			if (*endptr != '\0')
-				return -1;
+				return EINVAL;
 		}
 
 		if (flag_ptr==NULL || *flag_ptr=='\0') {
@@ -153,7 +157,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
 		} else {
 			spwd->sp_flag = (gid_t) strtoul(flag_ptr, &endptr, 10);
 			if (*endptr != '\0')
-				return -1;
+				return EINVAL;
 		}
 	}
 

+ 4 - 3
libc/pwd_grp/fgetpwent.c

@@ -37,22 +37,23 @@ int fgetpwent_r (FILE *file, struct passwd *password,
 	char *buff, size_t buflen, struct passwd **crap)
 {
     if (file == NULL) {
-	__set_errno(EINTR);
-	return -1;
+	return EINTR;
     }
     return(__getpwent_r(password, buff, buflen, fileno(file)));
 }
 
 struct passwd *fgetpwent(FILE * file)
 {
+    int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct passwd pwd;
 
     LOCK;
-    if (fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), NULL) != -1) {
+    if ((ret=fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), NULL)) == 0) {
 	UNLOCK;
 	return &pwd;
     }
     UNLOCK;
+    __set_errno(ret);
     return NULL;
 }

+ 4 - 3
libc/pwd_grp/fgetspent.c

@@ -36,22 +36,23 @@ 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 EINTR;
     }
     return(__getspent_r(spwd, buff, buflen, fileno(file)));
 }
 
 struct spwd *fgetspent(FILE * file)
 {
+    int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
 
     LOCK;
-    if (fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), NULL) != -1) {
+    if ((ret=fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
     UNLOCK;
+    __set_errno(ret);
     return NULL;
 }

+ 14 - 9
libc/pwd_grp/getpwnam.c

@@ -37,38 +37,43 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
 #endif      
 
 int getpwnam_r (const char *name, struct passwd *password,
-	char *buff, size_t buflen, struct passwd **crap)
+	char *buff, size_t buflen, struct passwd **result)
 {
+    int ret;
     int passwd_fd;
 
     if (name == NULL) {
-	__set_errno(EINVAL);
-	return -1;
+	return EINVAL;
     }
 
-    if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0)
-	return -1;
+    if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0) {
+	return ENOENT;
+    }
 
-    while (__getpwent_r(password, buff, buflen, passwd_fd) != -1)
+    while ((ret=__getpwent_r(password, buff, buflen, passwd_fd)) == 0) {
 	if (!strcmp(password->pw_name, name)) {
+	    *result=password;
 	    close(passwd_fd);
 	    return 0;
 	}
+    }
 
     close(passwd_fd);
-    return -1;
+    return ret;
 }
 
 struct passwd *getpwnam(const char *name)
 {
+    int ret;
     static char line_buff[PWD_BUFFER_SIZE];
-    static struct passwd pwd;
+    static struct passwd pwd, *result;
 
     LOCK;
-    if (getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), NULL) != -1) {
+    if ((ret=getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), &result)) == 0) {
 	UNLOCK;
 	return &pwd;
     }
+    __set_errno(ret);
     UNLOCK;
     return NULL;
 }

+ 7 - 4
libc/pwd_grp/getpwuid.c

@@ -23,6 +23,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <paths.h>
+#include <errno.h>
 #include "config.h"
 
 #ifdef __UCLIBC_HAS_THREADS__
@@ -41,30 +42,32 @@ int getpwuid_r (uid_t uid, struct passwd *password,
     int passwd_fd;
 
     if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0)
-	return -1;
+	return errno;
 
-    while (__getpwent_r(password, buff, buflen, passwd_fd) != -1)
+    while (__getpwent_r(password, buff, buflen, passwd_fd) == 0)
 	if (password->pw_uid == uid) {
 	    close(passwd_fd);
 	    return 0;
 	}
 
     close(passwd_fd);
-    return -1;
+    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;
 
     LOCK;
-    if (getpwuid_r(uid, &pwd, line_buff,  sizeof(line_buff), NULL) != -1) {
+    if ((ret=getpwuid_r(uid, &pwd, line_buff,  sizeof(line_buff), NULL)) == 0) {
 	UNLOCK;
 	return &pwd;
     }
     UNLOCK;
+    __set_errno(ret);
     return NULL;
 }
 

+ 7 - 6
libc/pwd_grp/getspnam.c

@@ -40,34 +40,35 @@ int getspnam_r (const char *name, struct spwd *spwd,
     int spwd_fd;
 
     if (name == NULL) {
-	__set_errno(EINVAL);
-	return -1;
+	return EINVAL;
     }
 
     if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0)
-	return -1;
+	return errno;
 
-    while (__getspent_r(spwd, buff, buflen, spwd_fd) != -1)
+    while (__getspent_r(spwd, buff, buflen, spwd_fd) == 0)
 	if (!strcmp(spwd->sp_namp, name)) {
 	    close(spwd_fd);
 	    return 0;
 	}
 
     close(spwd_fd);
-    return -1;
+    return EINVAL;
 }
 
 struct spwd *getspnam(const char *name)
 {
+    int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
 
     LOCK;
-    if (getspnam_r(name, &spwd, line_buff,  sizeof(line_buff), NULL) != -1) {
+    if ((ret=getspnam_r(name, &spwd, line_buff,  sizeof(line_buff), NULL)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
     UNLOCK;
+    __set_errno(ret);
     return NULL;
 }
 

+ 10 - 5
libc/pwd_grp/getspuid.c

@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <errno.h>
 #include "config.h"
 
 #ifdef __UCLIBC_HAS_THREADS__
@@ -34,28 +35,32 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
 #endif      
 
 int getspuid_r (uid_t uid, struct spwd *spwd,
-	char *buff, size_t buflen, struct spwd **crap)
+	char *buff, size_t buflen, struct spwd **result)
 {
+    int ret;
     char pwd_buff[PWD_BUFFER_SIZE];
     struct passwd password;
 
-    if (getpwuid_r(uid, &password, pwd_buff,  sizeof(pwd_buff), NULL) < 0)
-	return -1;
+    ret = getpwuid_r(uid, &password, pwd_buff,  sizeof(pwd_buff), NULL);
+    if (ret != 0)
+	return ret;
 
-    return getspnam_r(password.pw_name, spwd, buff, buflen, crap);
+    return getspnam_r(password.pw_name, spwd, buff, buflen, result);
 }
 
 struct spwd *getspuid(uid_t uid)
 {
+    int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
 
     LOCK;
-    if (getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), NULL) != -1) {
+    if ((ret=getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
     UNLOCK;
+    __set_errno(ret);
     return NULL;
 }
 

+ 2 - 1
libc/pwd_grp/initgroups.c

@@ -23,6 +23,7 @@
 #include <fcntl.h>
 #include <paths.h>
 #include <stdlib.h>
+#include <errno.h>
 #include "config.h"
 
 #ifdef __UCLIBC_HAS_THREADS__
@@ -49,7 +50,7 @@ int initgroups(__const char *user, gid_t gid)
 
 
     if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0)
-	return -1;
+	return errno;
 
     num_groups = 0;
     group_list = (gid_t *) realloc(group_list, 1);

+ 7 - 3
libc/pwd_grp/pwent.c

@@ -67,23 +67,27 @@ void endpwent(void)
 int getpwent_r (struct passwd *password, char *buff, 
 	size_t buflen, struct passwd **crap)
 {
+    int ret;
     LOCK;
-    if (pw_fd != -1 && __getpwent_r(password, buff, buflen, pw_fd) != -1) {
+    if (pw_fd != -1 && (ret=__getpwent_r(password, buff, buflen, pw_fd)) == 0) {
 	UNLOCK;
 	return 0;
     }
     UNLOCK;
-    return -1;
+    __set_errno(ret);
+    return ret;
 }
 
 struct passwd *getpwent(void)
 {
+    int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct passwd pwd;
 
-    if (getpwent_r(&pwd, line_buff, sizeof(line_buff), NULL) != -1) {
+    if ((ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), NULL)) == 0) {
 	return &pwd;
     }
+    __set_errno(ret);
     return NULL;
 }
 

+ 3 - 1
libc/pwd_grp/sgetspent.c

@@ -40,14 +40,16 @@ int sgetspent_r (const char *string, struct spwd *spwd,
 
 struct spwd *sgetspent(const char *string)
 {
+    int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
 
     LOCK;
-    if (sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), NULL) != -1) {
+    if ((ret = sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
+    __set_errno(ret);
     UNLOCK;
     return NULL;
 }

+ 6 - 3
libc/pwd_grp/spent.c

@@ -64,26 +64,29 @@ void endspent(void)
 int getspent_r (struct spwd *spwd, char *buff, 
 	size_t buflen, struct spwd **crap)
 {
+    int ret;
     LOCK;
-    if (spwd_fd != -1 && __getspent_r(spwd, buff, buflen, spwd_fd) != -1) {
+    if (spwd_fd != -1 && (ret=__getspent_r(spwd, buff, buflen, spwd_fd)) == 0) {
 	UNLOCK;
 	return 0;
     }
     UNLOCK;
-    return -1;
+    return ret;
 }
 
 struct spwd *getspent(void)
 {
+    int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
 
     LOCK;
-    if (getspent_r(&spwd, line_buff, sizeof(line_buff), NULL) != -1) {
+    if ((ret=getspent_r(&spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
     UNLOCK;
+    __set_errno(ret);
     return NULL;
 }