From 7a01614c68c5f4974057003e57b3b7146f11e96b Mon Sep 17 00:00:00 2001 From: carns Date: Fri, 30 Mar 2012 01:13:59 +0000 Subject: [PATCH] add getenv(LOGNAME) fallback for finding user name git-svn-id: https://svn.mcs.anl.gov/repos/darshan/trunk@545 3b7491f3-a168-0410-bf4b-c445ed680a29 --- darshan-runtime/lib/darshan-mpi-io.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/darshan-runtime/lib/darshan-mpi-io.c b/darshan-runtime/lib/darshan-mpi-io.c index 7248c9c..ef4f8e0 100644 --- a/darshan-runtime/lib/darshan-mpi-io.c +++ b/darshan-runtime/lib/darshan-mpi-io.c @@ -437,12 +437,34 @@ void darshan_shutdown(int timing_flag) start_time_tmp += final_job->log_job.start_time; my_tm = localtime(&start_time_tmp); - /* note: getpwuid() causes link errors for static binaries */ + /* get the username for this job. In order we will try each of the + * following until one of them succeeds: + * + * - cuserid() + * - getenv("LOGNAME") + * - snprintf(..., geteuid()); + * + * Note that we do not use getpwuid() because it generally will not + * work in statically compiled binaries. + */ + cuserid(cuser); + + /* if cuserid() didn't work, then check the environment */ + if (strcmp(cuser, "") == 0) + { + char* logname_string; + logname_string = getenv("LOGNAME"); + if(logname_string) + { + strncpy(cuser, logname_string, (L_cuserid-1)); + } + + } + + /* if cuserid() and environment both fail, then fall back to uid */ if (strcmp(cuser, "") == 0) { - /* if node config is wrong, cuserid can return an empty string - this is backup in case that happens. */ uid_t uid = geteuid(); snprintf(cuser, sizeof(cuser), "%u", uid); } -- 2.26.2