Remove `.ctors' and `.dtors' output sections
[glibc.git] / sysdeps / unix / sysv / linux / getlogin_r.c
blob42041eeee0b01d5a3e1a2a4d9b817dc48481d60b
1 /* Copyright (C) 2010 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <pwd.h>
20 #include <unistd.h>
21 #include <not-cancel.h>
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
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. */
34 int
35 attribute_hidden
36 __getlogin_r_loginuid (name, namesize)
37 char *name;
38 size_t namesize;
40 int fd = open_not_cancel_2 ("/proc/self/loginuid", O_RDONLY);
41 if (fd == -1)
42 return -1;
44 /* We are reading a 32-bit number. 12 bytes are enough for the text
45 representation. If not, something is wrong. */
46 char uidbuf[12];
47 ssize_t n = TEMP_FAILURE_RETRY (read_not_cancel (fd, uidbuf,
48 sizeof (uidbuf)));
49 close_not_cancel_no_status (fd);
51 uid_t uid;
52 char *endp;
53 if (n <= 0
54 || n == sizeof (uidbuf)
55 || (uidbuf[n] = '\0',
56 uid = strtoul (uidbuf, &endp, 10),
57 endp == uidbuf || *endp != '\0'))
58 return -1;
60 size_t buflen = 1024;
61 char *buf = alloca (buflen);
62 bool use_malloc = false;
63 struct passwd pwd;
64 struct passwd *tpwd;
65 int result = 0;
66 int res;
68 while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) == ERANGE)
69 if (__libc_use_alloca (2 * buflen))
70 buf = extend_alloca (buf, buflen, 2 * buflen);
71 else
73 buflen *= 2;
74 char *newp = realloc (use_malloc ? buf : NULL, buflen);
75 if (newp == NULL)
77 result = ENOMEM;
78 goto out;
80 buf = newp;
81 use_malloc = true;
84 if (res != 0 || tpwd == NULL)
86 result = -1;
87 goto out;
90 size_t needed = strlen (pwd.pw_name) + 1;
91 if (needed > namesize)
93 __set_errno (ERANGE);
94 result = ERANGE;
95 goto out;
98 memcpy (name, pwd.pw_name, needed);
100 out:
101 if (use_malloc)
102 free (buf);
104 return result;
108 /* Return at most NAME_LEN characters of the login name of the user in NAME.
109 If it cannot be determined or some other error occurred, return the error
110 code. Otherwise return 0. */
113 getlogin_r (name, namesize)
114 char *name;
115 size_t namesize;
117 int res = __getlogin_r_loginuid (name, namesize);
118 if (res >= 0)
119 return res;
121 return getlogin_r_fd0 (name, namesize);
123 libc_hidden_def (getlogin_r)