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
21 #include <not-cancel.h>
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>
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. */
36 __getlogin_r_loginuid (name
, namesize
)
40 int fd
= open_not_cancel_2 ("/proc/self/loginuid", O_RDONLY
);
44 /* We are reading a 32-bit number. 12 bytes are enough for the text
45 representation. If not, something is wrong. */
47 ssize_t n
= TEMP_FAILURE_RETRY (read_not_cancel (fd
, uidbuf
,
49 close_not_cancel_no_status (fd
);
54 || n
== sizeof (uidbuf
)
56 uid
= strtoul (uidbuf
, &endp
, 10),
57 endp
== uidbuf
|| *endp
!= '\0'))
61 char *buf
= alloca (buflen
);
62 bool use_malloc
= false;
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
);
74 char *newp
= realloc (use_malloc
? buf
: NULL
, buflen
);
84 if (res
!= 0 || tpwd
== NULL
)
90 size_t needed
= strlen (pwd
.pw_name
) + 1;
91 if (needed
> namesize
)
98 memcpy (name
, pwd
.pw_name
, needed
);
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
)
117 int res
= __getlogin_r_loginuid (name
, namesize
);
121 return getlogin_r_fd0 (name
, namesize
);
123 libc_hidden_def (getlogin_r
)