]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 2010-2019 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> | |
6d7aa2b5 | 21 | #include <scratch_buffer.h> |
c8727fa6 UD |
22 | |
23 | #define STATIC static | |
24 | static int getlogin_r_fd0 (char *name, size_t namesize); | |
5371d99e | 25 | #define __getlogin_r getlogin_r_fd0 |
c8727fa6 | 26 | #include <sysdeps/unix/getlogin_r.c> |
5371d99e | 27 | #undef __getlogin_r |
c8727fa6 UD |
28 | |
29 | ||
e35fcef8 AS |
30 | /* Try to determine login name from /proc/self/loginuid and return 0 |
31 | if successful. If /proc/self/loginuid cannot be read return -1. | |
32 | Otherwise return the error number. */ | |
33 | ||
c8727fa6 UD |
34 | int |
35 | attribute_hidden | |
bd2260a2 | 36 | __getlogin_r_loginuid (char *name, size_t namesize) |
c8727fa6 | 37 | { |
c2284574 | 38 | int fd = __open_nocancel ("/proc/self/loginuid", O_RDONLY); |
c8727fa6 | 39 | if (fd == -1) |
e35fcef8 | 40 | return -1; |
c8727fa6 | 41 | |
aa6436d6 UD |
42 | /* We are reading a 32-bit number. 12 bytes are enough for the text |
43 | representation. If not, something is wrong. */ | |
44 | char uidbuf[12]; | |
a748eb31 | 45 | ssize_t n = TEMP_FAILURE_RETRY (__read_nocancel (fd, uidbuf, |
aa6436d6 | 46 | sizeof (uidbuf))); |
c181840c | 47 | __close_nocancel_nostatus (fd); |
c8727fa6 UD |
48 | |
49 | uid_t uid; | |
50 | char *endp; | |
51 | if (n <= 0 | |
aa6436d6 UD |
52 | || n == sizeof (uidbuf) |
53 | || (uidbuf[n] = '\0', | |
54 | uid = strtoul (uidbuf, &endp, 10), | |
55 | endp == uidbuf || *endp != '\0')) | |
e35fcef8 | 56 | return -1; |
c8727fa6 | 57 | |
cc8a1620 JH |
58 | /* If there is no login uid, linux sets /proc/self/loginid to the sentinel |
59 | value of, (uid_t) -1, so check if that value is set and return early to | |
60 | avoid making unneeded nss lookups. */ | |
61 | if (uid == (uid_t) -1) | |
62 | { | |
63 | __set_errno (ENXIO); | |
64 | return ENXIO; | |
65 | } | |
66 | ||
c8727fa6 UD |
67 | struct passwd pwd; |
68 | struct passwd *tpwd; | |
63c4ed22 | 69 | int result = 0; |
c8727fa6 | 70 | int res; |
6d7aa2b5 FW |
71 | struct scratch_buffer tmpbuf; |
72 | scratch_buffer_init (&tmpbuf); | |
c8727fa6 | 73 | |
6d7aa2b5 FW |
74 | while ((res = __getpwuid_r (uid, &pwd, |
75 | tmpbuf.data, tmpbuf.length, &tpwd)) == ERANGE) | |
76 | { | |
77 | if (!scratch_buffer_grow (&tmpbuf)) | |
78 | { | |
79 | result = ENOMEM; | |
80 | goto out; | |
81 | } | |
82 | } | |
c8727fa6 | 83 | |
c86434cc | 84 | if (res != 0 || tpwd == NULL) |
63c4ed22 UD |
85 | { |
86 | result = -1; | |
87 | goto out; | |
88 | } | |
c8727fa6 | 89 | |
5ae958d7 UD |
90 | size_t needed = strlen (pwd.pw_name) + 1; |
91 | if (needed > namesize) | |
92 | { | |
93 | __set_errno (ERANGE); | |
94 | result = ERANGE; | |
95 | goto out; | |
96 | } | |
97 | ||
98 | memcpy (name, pwd.pw_name, needed); | |
99 | ||
100 | out: | |
6d7aa2b5 | 101 | scratch_buffer_free (&tmpbuf); |
5ae958d7 | 102 | return result; |
c8727fa6 UD |
103 | } |
104 | ||
105 | ||
aa6436d6 UD |
106 | /* Return at most NAME_LEN characters of the login name of the user in NAME. |
107 | If it cannot be determined or some other error occurred, return the error | |
108 | code. Otherwise return 0. */ | |
c8727fa6 UD |
109 | |
110 | int | |
bd2260a2 | 111 | __getlogin_r (char *name, size_t namesize) |
c8727fa6 | 112 | { |
63c4ed22 UD |
113 | int res = __getlogin_r_loginuid (name, namesize); |
114 | if (res >= 0) | |
115 | return res; | |
c8727fa6 UD |
116 | |
117 | return getlogin_r_fd0 (name, namesize); | |
118 | } | |
5371d99e JM |
119 | libc_hidden_def (__getlogin_r) |
120 | weak_alias (__getlogin_r, getlogin_r) | |
121 | libc_hidden_weak (getlogin_r) |