Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 376ce3e

Browse files
committed
Prefer $HOME when looking up the current user's home directory.
When we need to identify the home directory on non-Windows, first consult getenv("HOME"). If that's empty or unset, fall back on our previous method of checking the <pwd.h> database. Preferring $HOME allows the user to intentionally point at some other directory, and it seems to be in line with the behavior of most other utilities. However, we shouldn't rely on it completely, as $HOME is likely to be unset when running as a daemon. Anders Kaseorg Discussion: https://postgr.es/m/1634252654444.90107@mit.edu
1 parent 6867f96 commit 376ce3e

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

src/bin/psql/command.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -558,19 +558,25 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
558558
else
559559
{
560560
#ifndef WIN32
561-
struct passwd *pw;
562-
uid_t user_id = geteuid();
563-
564-
errno = 0; /* clear errno before call */
565-
pw = getpwuid(user_id);
566-
if (!pw)
561+
/* This should match get_home_path() */
562+
dir = getenv("HOME");
563+
if (dir == NULL || dir[0] == '\0')
567564
{
568-
pg_log_error("could not get home directory for user ID %ld: %s",
569-
(long) user_id,
570-
errno ? strerror(errno) : _("user does not exist"));
571-
exit(EXIT_FAILURE);
565+
uid_t user_id = geteuid();
566+
struct passwd *pw;
567+
568+
errno = 0; /* clear errno before call */
569+
pw = getpwuid(user_id);
570+
if (pw)
571+
dir = pw->pw_dir;
572+
else
573+
{
574+
pg_log_error("could not get home directory for user ID %ld: %s",
575+
(long) user_id,
576+
errno ? strerror(errno) : _("user does not exist"));
577+
success = false;
578+
}
572579
}
573-
dir = pw->pw_dir;
574580
#else /* WIN32 */
575581

576582
/*
@@ -581,7 +587,8 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
581587
#endif /* WIN32 */
582588
}
583589

584-
if (chdir(dir) == -1)
590+
if (success &&
591+
chdir(dir) < 0)
585592
{
586593
pg_log_error("\\%s: could not change directory to \"%s\": %m",
587594
cmd, dir);

src/interfaces/libpq/fe-connect.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7267,14 +7267,21 @@ bool
72677267
pqGetHomeDirectory(char *buf, int bufsize)
72687268
{
72697269
#ifndef WIN32
7270-
char pwdbuf[BUFSIZ];
7271-
struct passwd pwdstr;
7272-
struct passwd *pwd = NULL;
7270+
const char *home;
72737271

7274-
(void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
7275-
if (pwd == NULL)
7276-
return false;
7277-
strlcpy(buf, pwd->pw_dir, bufsize);
7272+
home = getenv("HOME");
7273+
if (home == NULL || home[0] == '\0')
7274+
{
7275+
char pwdbuf[BUFSIZ];
7276+
struct passwd pwdstr;
7277+
struct passwd *pwd = NULL;
7278+
7279+
(void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
7280+
if (pwd == NULL)
7281+
return false;
7282+
home = pwd->pw_dir;
7283+
}
7284+
strlcpy(buf, home, bufsize);
72787285
return true;
72797286
#else
72807287
char tmppath[MAX_PATH];

src/port/path.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -807,14 +807,25 @@ bool
807807
get_home_path(char *ret_path)
808808
{
809809
#ifndef WIN32
810-
char pwdbuf[BUFSIZ];
811-
struct passwd pwdstr;
812-
struct passwd *pwd = NULL;
810+
/*
811+
* We first consult $HOME. If that's unset, try to get the info from
812+
* <pwd.h>.
813+
*/
814+
const char *home;
813815

814-
(void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
815-
if (pwd == NULL)
816-
return false;
817-
strlcpy(ret_path, pwd->pw_dir, MAXPGPATH);
816+
home = getenv("HOME");
817+
if (home == NULL || home[0] == '\0')
818+
{
819+
char pwdbuf[BUFSIZ];
820+
struct passwd pwdstr;
821+
struct passwd *pwd = NULL;
822+
823+
(void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
824+
if (pwd == NULL)
825+
return false;
826+
home = pwd->pw_dir;
827+
}
828+
strlcpy(ret_path, home, MAXPGPATH);
818829
return true;
819830
#else
820831
char *tmppath;

0 commit comments

Comments
 (0)