]>
Commit | Line | Data |
---|---|---|
581c785b | 1 | /* Copyright (C) 1996-2022 Free Software Foundation, Inc. |
8a523922 | 2 | This file is part of the GNU C Library. |
0200214b | 3 | |
8a523922 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
0200214b | 8 | |
8a523922 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
0200214b | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
0200214b | 17 | |
1eb610d1 | 18 | #include <assert.h> |
0200214b RM |
19 | #include <errno.h> |
20 | #include <limits.h> | |
21 | #include <string.h> | |
1474b80f | 22 | #include <unistd.h> |
500590c7 | 23 | #include <stdlib.h> |
1474b80f | 24 | #include <utmp.h> |
734c60eb | 25 | #include <shlib-compat.h> |
76b87c03 | 26 | |
500590c7 MB |
27 | /* Return the result of ttyname in the buffer pointed to by TTY, which should |
28 | be of length BUF_LEN. If it is too long to fit in this buffer, a | |
29 | sufficiently long buffer is allocated using malloc, and returned in TTY. | |
30 | 0 is returned upon success, -1 otherwise. */ | |
31 | static int | |
32 | tty_name (int fd, char **tty, size_t buf_len) | |
33 | { | |
34 | int rv; /* Return value. 0 = success. */ | |
35 | char *buf = *tty; /* Buffer for ttyname, initially the user's. */ | |
0200214b | 36 | |
500590c7 MB |
37 | for (;;) |
38 | { | |
39 | char *new_buf; | |
40 | ||
41 | if (buf_len) | |
42 | { | |
734c60eb | 43 | rv = __ttyname_r (fd, buf, buf_len); |
500590c7 | 44 | |
f21acc89 | 45 | if (rv != 0 || memchr (buf, '\0', buf_len)) |
8a523922 UD |
46 | /* We either got an error, or we succeeded and the |
47 | returned name fit in the buffer. */ | |
500590c7 | 48 | break; |
1474b80f | 49 | |
500590c7 MB |
50 | /* Try again with a longer buffer. */ |
51 | buf_len += buf_len; /* Double it */ | |
52 | } | |
53 | else | |
54 | /* No initial buffer; start out by mallocing one. */ | |
55 | buf_len = 128; /* First time guess. */ | |
56 | ||
57 | if (buf != *tty) | |
58 | /* We've already malloced another buffer at least once. */ | |
59 | new_buf = realloc (buf, buf_len); | |
60 | else | |
61 | new_buf = malloc (buf_len); | |
62 | if (! new_buf) | |
63 | { | |
64 | rv = -1; | |
c4029823 | 65 | __set_errno (ENOMEM); |
500590c7 MB |
66 | break; |
67 | } | |
762a2918 | 68 | buf = new_buf; |
500590c7 MB |
69 | } |
70 | ||
71 | if (rv == 0) | |
8a523922 | 72 | *tty = buf; /* Return buffer to the user. */ |
500590c7 | 73 | else if (buf != *tty) |
8a523922 | 74 | free (buf); /* Free what we malloced when returning an error. */ |
500590c7 MB |
75 | |
76 | return rv; | |
77 | } | |
78 | \f | |
1474b80f | 79 | void |
734c60eb | 80 | __login (const struct utmp *ut) |
1474b80f | 81 | { |
500590c7 MB |
82 | #ifdef PATH_MAX |
83 | char _tty[PATH_MAX + UT_LINESIZE]; | |
84 | #else | |
85 | char _tty[512 + UT_LINESIZE]; | |
86 | #endif | |
87 | char *tty = _tty; | |
0200214b RM |
88 | int found_tty; |
89 | const char *ttyp; | |
2549e758 MB |
90 | struct utmp copy = *ut; |
91 | ||
92 | /* Fill in those fields we supply. */ | |
2549e758 | 93 | copy.ut_type = USER_PROCESS; |
2549e758 | 94 | copy.ut_pid = getpid (); |
0200214b RM |
95 | |
96 | /* Seek tty. */ | |
500590c7 | 97 | found_tty = tty_name (STDIN_FILENO, &tty, sizeof (_tty)); |
0200214b | 98 | if (found_tty < 0) |
500590c7 | 99 | found_tty = tty_name (STDOUT_FILENO, &tty, sizeof (_tty)); |
0200214b | 100 | if (found_tty < 0) |
500590c7 | 101 | found_tty = tty_name (STDERR_FILENO, &tty, sizeof (_tty)); |
0200214b RM |
102 | |
103 | if (found_tty >= 0) | |
104 | { | |
68185625 UD |
105 | /* We only want to insert the name of the tty without path. |
106 | But take care of name like /dev/pts/3. */ | |
107 | if (strncmp (tty, "/dev/", 5) == 0) | |
108 | ttyp = tty + 5; /* Skip the "/dev/". */ | |
109 | else | |
110 | ttyp = basename (tty); | |
2549e758 MB |
111 | |
112 | /* Position to record for this tty. */ | |
113 | strncpy (copy.ut_line, ttyp, UT_LINESIZE); | |
114 | ||
0200214b | 115 | /* Tell that we want to use the UTMP file. */ |
734c60eb | 116 | if (__utmpname (_PATH_UTMP) == 0) |
0200214b | 117 | { |
0200214b | 118 | /* Open UTMP file. */ |
734c60eb | 119 | __setutent (); |
0200214b | 120 | |
8a523922 | 121 | /* Write the entry. */ |
734c60eb | 122 | __pututline (©); |
0200214b RM |
123 | |
124 | /* Close UTMP file. */ | |
734c60eb | 125 | __endutent (); |
1474b80f | 126 | } |
500590c7 MB |
127 | |
128 | if (tty != _tty) | |
129 | free (tty); /* Free buffer malloced by tty_name. */ | |
0200214b | 130 | } |
1eb610d1 UD |
131 | else |
132 | /* We provide a default value so that the output does not contain | |
133 | an random bytes in this field. */ | |
134 | strncpy (copy.ut_line, "???", UT_LINESIZE); | |
0200214b RM |
135 | |
136 | /* Update the WTMP file. Here we have to add a new entry. */ | |
734c60eb | 137 | __updwtmp (_PATH_WTMP, ©); |
1474b80f | 138 | } |
734c60eb FW |
139 | versioned_symbol (libc, __login, login, GLIBC_2_34); |
140 | libc_hidden_ver (__login, login) | |
141 | ||
142 | #if OTHER_SHLIB_COMPAT (libutil, GLIBC_2_0, GLIBC_2_34) | |
143 | compat_symbol (libutil, __login, login, GLIBC_2_0); | |
144 | #endif |