]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/getlogin_r.c
Fix reading loginuid file in getlogin{,_r}.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / getlogin_r.c
1 /* Copyright (C) 2010 Free Software Foundation, Inc.
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
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18
19 #include <pwd.h>
20 #include <unistd.h>
21 #include <not-cancel.h>
22
23 #define STATIC static
24 static int getlogin_r_fd0 (char *name, size_t namesize);
25 #define getlogin_r getlogin_r_fd0
26 #include <sysdeps/unix/getlogin_r.c>
27 #undef getlogin_r
28
29
30 int
31 attribute_hidden
32 __getlogin_r_loginuid (name, namesize)
33 char *name;
34 size_t namesize;
35 {
36 int fd = open_not_cancel_2 ("/proc/self/loginuid", O_RDONLY);
37 if (fd == -1)
38 return 1;
39
40 /* We are reading a 32-bit number. 12 bytes are enough for the text
41 representation. If not, something is wrong. */
42 char uidbuf[12];
43 ssize_t n = TEMP_FAILURE_RETRY (read_not_cancel (fd, uidbuf,
44 sizeof (uidbuf)));
45 close_not_cancel_no_status (fd);
46
47 uid_t uid;
48 char *endp;
49 if (n <= 0
50 || n == sizeof (uidbuf)
51 || (uidbuf[n] = '\0',
52 uid = strtoul (uidbuf, &endp, 10),
53 endp == uidbuf || *endp != '\0'))
54 return 1;
55
56 size_t buflen = 1024;
57 char *buf = alloca (buflen);
58 bool use_malloc = false;
59 struct passwd pwd;
60 struct passwd *tpwd;
61 int res;
62
63 while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) != 0)
64 if (__libc_use_alloca (2 * buflen))
65 extend_alloca (buf, buflen, 2 * buflen);
66 else
67 {
68 buflen *= 2;
69 char *newp = realloc (use_malloc ? buf : NULL, buflen);
70 if (newp == NULL)
71 {
72 fail:
73 if (use_malloc)
74 free (buf);
75 return 1;
76 }
77 buf = newp;
78 use_malloc = true;
79 }
80
81 if (tpwd == NULL)
82 goto fail;
83
84 strncpy (name, pwd.pw_name, namesize - 1);
85 name[namesize - 1] = '\0';
86
87 if (use_malloc)
88 free (buf);
89
90 return 0;
91 }
92
93
94 /* Return at most NAME_LEN characters of the login name of the user in NAME.
95 If it cannot be determined or some other error occurred, return the error
96 code. Otherwise return 0. */
97
98 int
99 getlogin_r (name, namesize)
100 char *name;
101 size_t namesize;
102 {
103 if (__getlogin_r_loginuid (name, namesize) == 0)
104 return 0;
105
106 return getlogin_r_fd0 (name, namesize);
107 }
108 libc_hidden_def (getlogin_r)