From: Andre Noll Date: Sun, 12 May 2024 01:09:58 +0000 (+0200) Subject: Consult $HOME and $LOGNAME rather than calling getpwuid(), X-Git-Tag: v1.0.2~26 X-Git-Url: http://git.tue.mpg.de/?a=commitdiff_plain;h=5244a093e7fb783f1e9616baad572ff1cb515cb6;p=dss.git Consult $HOME and $LOGNAME rather than calling getpwuid(), Quiting getpwnam(3): An application that wants to determine its user’s home directory should inspect the value of HOME (rather than the value getpwuid(getuid())->pw_dir) since this allows the user to modify their notion of "the home directory" during a login session. The old behaviour of get_homedir() to return "/tmp" in the error case is misleading at best, so abort in this case. --- diff --git a/str.c b/str.c index 1377630..eeb635c 100644 --- a/str.c +++ b/str.c @@ -151,16 +151,19 @@ __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...) } } -/** +/* * Get the home directory of the current user. * - * \return A dynamically allocated string that must be freed by the caller. If - * the home directory could not be found, this function returns "/tmp". + * Returns a dynamically allocated string that must be freed by the caller. If + * the HOME environment variable is unset or empty, the function aborts. */ __must_check __malloc char *get_homedir(void) { - struct passwd *pw = getpwuid(getuid()); - return dss_strdup(pw? pw->pw_dir : "/tmp"); + const char *home = getenv("HOME"); + if (home && *home) + return dss_strdup(home); + DSS_EMERG_LOG(("fatal: HOME is unset or empty\n")); + exit(EXIT_FAILURE); } /** @@ -192,19 +195,18 @@ int dss_atoi64(const char *str, int64_t *value) return 1; } -/** +/* * Get the logname of the current user. * - * \return A dynamically allocated string that must be freed by the caller. On - * errors, the string "unknown user" is returned, i.e. this function never - * returns \p NULL. - * - * \sa getpwuid(3). + * Returns a dynamically allocated string that must be freed by the caller. On + * errors, the string "unknown" is returned. This function never returns NULL. */ __must_check __malloc char *dss_logname(void) { - struct passwd *pw = getpwuid(getuid()); - return dss_strdup(pw? pw->pw_name : "unknown_user"); + const char *logname = getenv("LOGNAME"); + if (!logname && !*logname) + logname = "unknown"; + return dss_strdup(logname); } /**