]>
Commit | Line | Data |
---|---|---|
f7a9f785 | 1 | /* Copyright (C) 2010-2016 Free Software Foundation, Inc. |
c8727fa6 UD |
2 | This file is part of the GNU C Library. |
3 | ||
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. | |
8 | ||
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. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
c8727fa6 UD |
17 | |
18 | #include <pwd.h> | |
19 | #include <unistd.h> | |
20 | #include <not-cancel.h> | |
21 | ||
22 | #define STATIC static | |
23 | static int getlogin_r_fd0 (char *name, size_t namesize); | |
5371d99e | 24 | #define __getlogin_r getlogin_r_fd0 |
c8727fa6 | 25 | #include <sysdeps/unix/getlogin_r.c> |
5371d99e | 26 | #undef __getlogin_r |
c8727fa6 UD |
27 | |
28 | ||
e35fcef8 AS |
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. */ | |
32 | ||
c8727fa6 UD |
33 | int |
34 | attribute_hidden | |
bd2260a2 | 35 | __getlogin_r_loginuid (char *name, size_t namesize) |
c8727fa6 UD |
36 | { |
37 | int fd = open_not_cancel_2 ("/proc/self/loginuid", O_RDONLY); | |
38 | if (fd == -1) | |
e35fcef8 | 39 | return -1; |
c8727fa6 | 40 | |
aa6436d6 UD |
41 | /* We are reading a 32-bit number. 12 bytes are enough for the text |
42 | representation. If not, something is wrong. */ | |
43 | char uidbuf[12]; | |
44 | ssize_t n = TEMP_FAILURE_RETRY (read_not_cancel (fd, uidbuf, | |
45 | sizeof (uidbuf))); | |
c8727fa6 UD |
46 | close_not_cancel_no_status (fd); |
47 | ||
48 | uid_t uid; | |
49 | char *endp; | |
50 | if (n <= 0 | |
aa6436d6 UD |
51 | || n == sizeof (uidbuf) |
52 | || (uidbuf[n] = '\0', | |
53 | uid = strtoul (uidbuf, &endp, 10), | |
54 | endp == uidbuf || *endp != '\0')) | |
e35fcef8 | 55 | return -1; |
c8727fa6 UD |
56 | |
57 | size_t buflen = 1024; | |
58 | char *buf = alloca (buflen); | |
59 | bool use_malloc = false; | |
60 | struct passwd pwd; | |
61 | struct passwd *tpwd; | |
63c4ed22 | 62 | int result = 0; |
c8727fa6 UD |
63 | int res; |
64 | ||
63c4ed22 | 65 | while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) == ERANGE) |
c8727fa6 | 66 | if (__libc_use_alloca (2 * buflen)) |
63c4ed22 | 67 | buf = extend_alloca (buf, buflen, 2 * buflen); |
c8727fa6 UD |
68 | else |
69 | { | |
70 | buflen *= 2; | |
71 | char *newp = realloc (use_malloc ? buf : NULL, buflen); | |
72 | if (newp == NULL) | |
73 | { | |
63c4ed22 UD |
74 | result = ENOMEM; |
75 | goto out; | |
c8727fa6 UD |
76 | } |
77 | buf = newp; | |
78 | use_malloc = true; | |
79 | } | |
80 | ||
c86434cc | 81 | if (res != 0 || tpwd == NULL) |
63c4ed22 UD |
82 | { |
83 | result = -1; | |
84 | goto out; | |
85 | } | |
c8727fa6 | 86 | |
5ae958d7 UD |
87 | size_t needed = strlen (pwd.pw_name) + 1; |
88 | if (needed > namesize) | |
89 | { | |
90 | __set_errno (ERANGE); | |
91 | result = ERANGE; | |
92 | goto out; | |
93 | } | |
94 | ||
95 | memcpy (name, pwd.pw_name, needed); | |
96 | ||
97 | out: | |
c8727fa6 UD |
98 | if (use_malloc) |
99 | free (buf); | |
100 | ||
5ae958d7 | 101 | return result; |
c8727fa6 UD |
102 | } |
103 | ||
104 | ||
aa6436d6 UD |
105 | /* Return at most NAME_LEN characters of the login name of the user in NAME. |
106 | If it cannot be determined or some other error occurred, return the error | |
107 | code. Otherwise return 0. */ | |
c8727fa6 UD |
108 | |
109 | int | |
bd2260a2 | 110 | __getlogin_r (char *name, size_t namesize) |
c8727fa6 | 111 | { |
63c4ed22 UD |
112 | int res = __getlogin_r_loginuid (name, namesize); |
113 | if (res >= 0) | |
114 | return res; | |
c8727fa6 UD |
115 | |
116 | return getlogin_r_fd0 (name, namesize); | |
117 | } | |
5371d99e JM |
118 | libc_hidden_def (__getlogin_r) |
119 | weak_alias (__getlogin_r, getlogin_r) | |
120 | libc_hidden_weak (getlogin_r) |