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