Fix pathconf statvfs namespace (bug 18507).
[glibc.git] / sysdeps / unix / sysv / linux / getlogin_r.c
blob2c52b2183b1ce143432625d5e21c15005a02c0ee
1 /* Copyright (C) 2010-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <pwd.h>
19 #include <unistd.h>
20 #include <not-cancel.h>
22 #define STATIC static
23 static int getlogin_r_fd0 (char *name, size_t namesize);
24 #define getlogin_r getlogin_r_fd0
25 #include <sysdeps/unix/getlogin_r.c>
26 #undef getlogin_r
29 /* Try to determine login name from /proc/self/loginuid and return 0
30 if successful. If /proc/self/loginuid cannot be read return -1.
31 Otherwise return the error number. */
33 int
34 attribute_hidden
35 __getlogin_r_loginuid (name, namesize)
36 char *name;
37 size_t namesize;
39 int fd = open_not_cancel_2 ("/proc/self/loginuid", O_RDONLY);
40 if (fd == -1)
41 return -1;
43 /* We are reading a 32-bit number. 12 bytes are enough for the text
44 representation. If not, something is wrong. */
45 char uidbuf[12];
46 ssize_t n = TEMP_FAILURE_RETRY (read_not_cancel (fd, uidbuf,
47 sizeof (uidbuf)));
48 close_not_cancel_no_status (fd);
50 uid_t uid;
51 char *endp;
52 if (n <= 0
53 || n == sizeof (uidbuf)
54 || (uidbuf[n] = '\0',
55 uid = strtoul (uidbuf, &endp, 10),
56 endp == uidbuf || *endp != '\0'))
57 return -1;
59 size_t buflen = 1024;
60 char *buf = alloca (buflen);
61 bool use_malloc = false;
62 struct passwd pwd;
63 struct passwd *tpwd;
64 int result = 0;
65 int res;
67 while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) == ERANGE)
68 if (__libc_use_alloca (2 * buflen))
69 buf = extend_alloca (buf, buflen, 2 * buflen);
70 else
72 buflen *= 2;
73 char *newp = realloc (use_malloc ? buf : NULL, buflen);
74 if (newp == NULL)
76 result = ENOMEM;
77 goto out;
79 buf = newp;
80 use_malloc = true;
83 if (res != 0 || tpwd == NULL)
85 result = -1;
86 goto out;
89 size_t needed = strlen (pwd.pw_name) + 1;
90 if (needed > namesize)
92 __set_errno (ERANGE);
93 result = ERANGE;
94 goto out;
97 memcpy (name, pwd.pw_name, needed);
99 out:
100 if (use_malloc)
101 free (buf);
103 return result;
107 /* Return at most NAME_LEN characters of the login name of the user in NAME.
108 If it cannot be determined or some other error occurred, return the error
109 code. Otherwise return 0. */
112 getlogin_r (name, namesize)
113 char *name;
114 size_t namesize;
116 int res = __getlogin_r_loginuid (name, namesize);
117 if (res >= 0)
118 return res;
120 return getlogin_r_fd0 (name, namesize);
122 libc_hidden_def (getlogin_r)