Re: [Openais] startup error - getpwnam_r() returns ERANGE for some systems

2011-07-06 Thread Steven Dake
On 07/05/2011 07:20 PM, Tim Beale wrote:
 Hi,
 
 We've just upgraded to corosync v1.3.1 and struck a problem with corosync
 failing to startup. The problem is the getpwnam_r()/getgrnam_r() calls return
 ERANGE on our system, meaning insufficient buffer space was supplied (the
 expected buffer length is 256, rather than 250).
 I don't know much about this code, but judging by the man page for getpwnam_r,
 the correct way to determine the buffersize on any given system is to use
 sysconf(). Attached is a patch that does this.
 
 Cheers,
 Tim
 
 
 

Thanks for the work - it is appreciated.

I have reviewed and merged your patch.

Regards
-steve

 ___
 Openais mailing list
 Openais@lists.linux-foundation.org
 https://lists.linux-foundation.org/mailman/listinfo/openais

___
Openais mailing list
Openais@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/openais


[Openais] startup error - getpwnam_r() returns ERANGE for some systems

2011-07-05 Thread Tim Beale
Hi,

We've just upgraded to corosync v1.3.1 and struck a problem with corosync
failing to startup. The problem is the getpwnam_r()/getgrnam_r() calls return
ERANGE on our system, meaning insufficient buffer space was supplied (the
expected buffer length is 256, rather than 250).
I don't know much about this code, but judging by the man page for getpwnam_r,
the correct way to determine the buffersize on any given system is to use
sysconf(). Attached is a patch that does this.

Cheers,
Tim
From: Tim Beale tim.be...@alliedtelesis.co.nz

getpwnam_r()/getgrnam_r() returns ERANGE for some systems

On our system the expected buffer length is 256. This means calls to 
getpwnam_r()/getgrnam_r() return ERANGE error and corosync fails to startup.
These 2 functions return ERANGE when insufficient buffer space is supplied. 
Judging by the man page for getpwnam_r, the correct way to determine the 
buffersize on any given system is to use sysconf().
---

 exec/mainconfig.c |   32 +++-
 1 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/exec/mainconfig.c b/exec/mainconfig.c
index 44bd47a..32da834 100644
--- a/exec/mainconfig.c
+++ b/exec/mainconfig.c
@@ -39,6 +39,7 @@
 #include string.h
 #include stdlib.h
 #include errno.h
+#include unistd.h
 #include sys/socket.h
 #include netinet/in.h
 #include arpa/inet.h
@@ -568,11 +569,20 @@ parse_error:
 
 static int uid_determine (const char *req_user)
 {
+	int pw_uid = 0;
 	struct passwd passwd;
 	struct passwd* pwdptr = passwd;
 	struct passwd* temp_pwd_pt;
-	char pwdbuffer[200];
-	int  pwdlinelen = sizeof(pwdbuffer);
+	char *pwdbuffer;
+	int  pwdlinelen;
+
+	pwdlinelen = sysconf (_SC_GETPW_R_SIZE_MAX);
+
+	if (pwdlinelen == -1) {
+		pwdlinelen = 256;
+	}
+
+	pwdbuffer = malloc (pwdlinelen);
 
 	if ((getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, temp_pwd_pt)) != 0) {
 		log_printf (LOGSYS_LEVEL_ERROR,
@@ -580,8 +590,10 @@ static int uid_determine (const char *req_user)
 			req_user);
 		corosync_exit_error (AIS_DONE_UID_DETERMINE);
 	}
+	pw_uid = passwd.pw_uid;
+	free (pwdbuffer);
 
-	return passwd.pw_uid;
+	return pw_uid;
 }
 
 static int gid_determine (const char *req_group)
@@ -590,8 +602,16 @@ static int gid_determine (const char *req_group)
 	struct group group;
 	struct group * grpptr = group;
 	struct group * temp_grp_pt;
-	char grpbuffer[200];
-	int  grplinelen = sizeof(grpbuffer);
+	char *grpbuffer;
+	int  grplinelen;
+
+	grplinelen = sysconf (_SC_GETGR_R_SIZE_MAX);
+
+	if (grplinelen == -1) {
+		grplinelen = 256;
+	}
+
+	grpbuffer = malloc (grplinelen);
 
 	if ((getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, temp_grp_pt)) != 0) {
 		log_printf (LOGSYS_LEVEL_ERROR,
@@ -600,6 +620,8 @@ static int gid_determine (const char *req_group)
 		corosync_exit_error (AIS_DONE_GID_DETERMINE);
 	}
 	ais_gid = group.gr_gid;
+	free (grpbuffer);
+
 	return ais_gid;
 }
 
___
Openais mailing list
Openais@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/openais