util: Fix error message when getpwuid_r fails to find the user

getpwuid_r returns success but sets the return structure to NULL when it
fails to deliver data about the requested uid. In our helper code this
created following strange error messages:

" ... cannot getpwuid_r(1234): Success"

This patch creates a more helpful message:
" ... getpwuid_r failed to retrieve data for uid '1234'"
This commit is contained in:
Peter Krempa 2012-08-28 16:51:05 +02:00
parent a22909d5c2
commit f2bb32b1d2
1 changed files with 16 additions and 6 deletions

View File

@ -2629,6 +2629,7 @@ int
virSetUIDGID(uid_t uid, gid_t gid) virSetUIDGID(uid_t uid, gid_t gid)
{ {
int err; int err;
char *buf = NULL;
if (gid > 0) { if (gid > 0) {
if (setregid(gid, gid) < 0) { if (setregid(gid, gid) < 0) {
@ -2642,7 +2643,6 @@ virSetUIDGID(uid_t uid, gid_t gid)
if (uid > 0) { if (uid > 0) {
# ifdef HAVE_INITGROUPS # ifdef HAVE_INITGROUPS
struct passwd pwd, *pwd_result; struct passwd pwd, *pwd_result;
char *buf = NULL;
size_t bufsize; size_t bufsize;
int rc; int rc;
@ -2659,25 +2659,32 @@ virSetUIDGID(uid_t uid, gid_t gid)
&pwd_result)) == ERANGE) { &pwd_result)) == ERANGE) {
if (VIR_RESIZE_N(buf, bufsize, bufsize, bufsize) < 0) { if (VIR_RESIZE_N(buf, bufsize, bufsize, bufsize) < 0) {
virReportOOMError(); virReportOOMError();
VIR_FREE(buf);
err = ENOMEM; err = ENOMEM;
goto error; goto error;
} }
} }
if (rc || !pwd_result) {
if (rc) {
virReportSystemError(err = rc, _("cannot getpwuid_r(%d)"), virReportSystemError(err = rc, _("cannot getpwuid_r(%d)"),
(unsigned int) uid); (unsigned int) uid);
VIR_FREE(buf);
goto error; goto error;
} }
if (!pwd_result) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("getpwuid_r failed to retrieve data "
"for uid '%d'"),
(unsigned int) uid);
err = EINVAL;
goto error;
}
if (initgroups(pwd.pw_name, pwd.pw_gid) < 0) { if (initgroups(pwd.pw_name, pwd.pw_gid) < 0) {
virReportSystemError(err = errno, virReportSystemError(err = errno,
_("cannot initgroups(\"%s\", %d)"), _("cannot initgroups(\"%s\", %d)"),
pwd.pw_name, (unsigned int) pwd.pw_gid); pwd.pw_name, (unsigned int) pwd.pw_gid);
VIR_FREE(buf);
goto error; goto error;
} }
VIR_FREE(buf);
# endif # endif
if (setreuid(uid, uid) < 0) { if (setreuid(uid, uid) < 0) {
virReportSystemError(err = errno, virReportSystemError(err = errno,
@ -2686,9 +2693,12 @@ virSetUIDGID(uid_t uid, gid_t gid)
goto error; goto error;
} }
} }
VIR_FREE(buf);
return 0; return 0;
error: error:
VIR_FREE(buf);
errno = err; errno = err;
return -1; return -1;
} }