Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * thread.c
4 : *
5 : * Prototypes and macros around system calls, used to help make
6 : * threaded libraries reentrant and safe to use from threaded applications.
7 : *
8 : * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
9 : *
10 : * src/port/thread.c
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 :
15 : #include "c.h"
16 :
17 : #include <pwd.h>
18 :
19 :
20 : /*
21 : * Historically, the code in this module had to deal with operating systems
22 : * that lacked getpwuid_r().
7178 bruce 23 EUB : */
6797 24 :
453 tgl 25 : #ifndef WIN32
26 :
27 : /*
28 : * pg_get_user_name - get the name of the user with the given ID
29 : *
30 : * On success, the user name is returned into the buffer (of size buflen),
31 : * and "true" is returned. On failure, a localized error message is
32 : * returned into the buffer, and "false" is returned.
33 : */
34 : bool
453 tgl 35 GBC 8660 : pg_get_user_name(uid_t user_id, char *buffer, size_t buflen)
7239 bruce 36 EUB : {
37 : char pwdbuf[BUFSIZ];
38 : struct passwd pwdstr;
453 tgl 39 GIC 8660 : struct passwd *pw = NULL;
453 tgl 40 EUB : int pwerr;
7148 bruce 41 :
259 tmunro 42 GNC 8660 : pwerr = getpwuid_r(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
453 tgl 43 GBC 8660 : if (pw != NULL)
44 : {
453 tgl 45 GIC 8660 : strlcpy(buffer, pw->pw_name, buflen);
46 8660 : return true;
47 : }
453 tgl 48 UIC 0 : if (pwerr != 0)
49 0 : snprintf(buffer, buflen,
50 0 : _("could not look up local user ID %d: %s"),
51 : (int) user_id,
52 : strerror_r(pwerr, pwdbuf, sizeof(pwdbuf)));
53 : else
54 0 : snprintf(buffer, buflen,
55 0 : _("local user with ID %d does not exist"),
56 : (int) user_id);
57 0 : return false;
58 : }
59 :
60 : /*
61 : * pg_get_user_home_dir - get the home directory of the user with the given ID
62 : *
63 : * On success, the directory path is returned into the buffer (of size buflen),
64 : * and "true" is returned. On failure, a localized error message is
65 : * returned into the buffer, and "false" is returned.
66 : *
67 : * Note that this does not incorporate the common behavior of checking
68 : * $HOME first, since it's independent of which user_id is queried.
69 : */
70 : bool
71 0 : pg_get_user_home_dir(uid_t user_id, char *buffer, size_t buflen)
72 : {
73 : char pwdbuf[BUFSIZ];
74 : struct passwd pwdstr;
75 0 : struct passwd *pw = NULL;
76 : int pwerr;
77 :
259 tmunro 78 UNC 0 : pwerr = getpwuid_r(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
453 tgl 79 UIC 0 : if (pw != NULL)
80 : {
81 0 : strlcpy(buffer, pw->pw_dir, buflen);
82 0 : return true;
83 : }
84 0 : if (pwerr != 0)
85 0 : snprintf(buffer, buflen,
86 0 : _("could not look up local user ID %d: %s"),
87 : (int) user_id,
88 : strerror_r(pwerr, pwdbuf, sizeof(pwdbuf)));
89 : else
90 0 : snprintf(buffer, buflen,
91 0 : _("local user with ID %d does not exist"),
92 : (int) user_id);
93 0 : return false;
94 : }
95 :
96 : #endif /* !WIN32 */
|