I have a number of C++ projects that are related. I am developing under RHEL 6 using Netbeans 8.1 as the IDE. I am using the GNU toolchain.
These projects share a couple of libraries. One holds a set of simple utilities like converting a string to uppercase or getting "root"s home directory. That getRootDir method has started giving me grief and I am not sure why. It is not a complex method. It calls getpwuid(0) and puts the result in a struct passwd *pwd structure. It works in the unit test.
I can't easily debug it in operation. I have syslog calls that dump diagnostic info in the logs. I can't attach to the process (or don't know how) because it is forked from a login process (PAM). It doesn't block and is very short lived.
I get the message from the routine: "could not get roots passwd info"
Is there some reason that a process could not get the etc passwd entry for root?
const char * Utilities::getRootDir() {
char error[1024];
#ifdef DEBUG
syslog(LOG_AUTHPRIV | LOG_INFO, "%s: start", __func__);
#endif
struct passwd *pwd = NULL;
ero = 0;
pwd = getpwuid(0);
#ifdef DEBUG
syslog(LOG_AUTHPRIV | LOG_INFO, "%s: getpwuid: %s", __func__, pwd==NULL?"NULL":pwd->pw_dir);
#endif
if (pwd == NULL) {
if (ero) {
snprintf(error, 1024, "%s: getpwnam failed: %s", __func__,
strerror(ero));
} else {
snprintf(error, 1024, "%s: could not get roots passwd info",
__func__);
}
syslog(LOG_AUTHPRIV | LOG_INFO, "%s", error);
throw MyException(error);
}
if (pwd->pw_dir == NULL || strlen(pwd->pw_dir) == 0) {
snprintf(error, 1024,
"%s: home dir of root seems to be empty",
__func__);
syslog(LOG_AUTHPRIV | LOG_INFO, "%s", error);
throw MyException(error);
}
#ifdef DEBUG
syslog(LOG_AUTHPRIV | LOG_INFO, "%s: root path = %s",
__func__, pwd->pw_dir);
#endif
#ifdef DEBUG
syslog(LOG_AUTHPRIV | LOG_INFO, "%s: end", __func__);
#endif
retu strdup(pwd->pw_dir);
}
