]>
Commit | Line | Data |
---|---|---|
0200214b RM |
1 | /* Copyright (C) 1996 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. | |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
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 | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If | |
17 | not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
19 | ||
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> |
500590c7 MB |
26 | \f |
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 | { | |
43 | rv = ttyname_r (fd, buf, buf_len); | |
44 | ||
45 | if (rv < 0 || memchr (buf, '\0', buf_len)) | |
46 | /* We either got an error, or we succeeded and the returned name fit | |
47 | in the buffer. */ | |
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; | |
65 | errno = ENOMEM; | |
66 | break; | |
67 | } | |
68 | } | |
69 | ||
70 | if (rv == 0) | |
71 | *tty = buf; /* Return buffer to the user. */ | |
72 | else if (buf != *tty) | |
73 | free (buf); /* Free what we malloced when returning an error. */ | |
74 | ||
75 | return rv; | |
76 | } | |
77 | \f | |
1474b80f | 78 | void |
0200214b | 79 | login (const struct utmp *ut) |
1474b80f | 80 | { |
500590c7 MB |
81 | #ifdef PATH_MAX |
82 | char _tty[PATH_MAX + UT_LINESIZE]; | |
83 | #else | |
84 | char _tty[512 + UT_LINESIZE]; | |
85 | #endif | |
86 | char *tty = _tty; | |
0200214b RM |
87 | int found_tty; |
88 | const char *ttyp; | |
3e72eb70 | 89 | struct utmp_data data = { -1 }; |
0200214b RM |
90 | |
91 | /* Seek tty. */ | |
500590c7 | 92 | found_tty = tty_name (STDIN_FILENO, &tty, sizeof (_tty)); |
0200214b | 93 | if (found_tty < 0) |
500590c7 | 94 | found_tty = tty_name (STDOUT_FILENO, &tty, sizeof (_tty)); |
0200214b | 95 | if (found_tty < 0) |
500590c7 | 96 | found_tty = tty_name (STDERR_FILENO, &tty, sizeof (_tty)); |
0200214b RM |
97 | |
98 | if (found_tty >= 0) | |
99 | { | |
100 | /* Tell that we want to use the UTMP file. */ | |
101 | if (utmpname (_PATH_UTMP) != 0) | |
102 | { | |
103 | struct utmp tmp; | |
104 | struct utmp *old; | |
105 | ||
106 | /* Open UTMP file. */ | |
107 | setutent_r (&data); | |
108 | ||
109 | /* We only want to insert the name of the tty without path. */ | |
110 | ttyp = basename (tty); | |
111 | ||
112 | /* Position to record for this tty. */ | |
113 | #if _HAVE_UT_TYPE - 0 | |
114 | tmp.ut_type = USER_PROCESS; | |
115 | #endif | |
116 | strncpy (tmp.ut_line, ttyp, UT_LINESIZE); | |
117 | ||
118 | /* Read the record. */ | |
74e1a5ff | 119 | if (getutline_r (&tmp, &old, &data) >= 0) |
0200214b RM |
120 | { |
121 | #if _HAVE_UT_TYPE - 0 | |
122 | /* We have to fake the old entry because this `login' | |
123 | function does not fit well into the UTMP file | |
124 | handling scheme. */ | |
125 | old->ut_type = ut->ut_type; | |
126 | #endif | |
127 | pututline_r (ut, &data); | |
128 | } | |
74e1a5ff MB |
129 | else if (errno == ESRCH) |
130 | /* We didn't find anything. pututline_r will add UT at the end | |
131 | of the file in this case. */ | |
132 | pututline_r (ut, &data); | |
0200214b RM |
133 | |
134 | /* Close UTMP file. */ | |
135 | endutent_r (&data); | |
1474b80f | 136 | } |
500590c7 MB |
137 | |
138 | if (tty != _tty) | |
139 | free (tty); /* Free buffer malloced by tty_name. */ | |
0200214b RM |
140 | } |
141 | ||
142 | /* Update the WTMP file. Here we have to add a new entry. */ | |
143 | if (utmpname (_PATH_WTMP) != 0) | |
144 | { | |
145 | /* Open the WTMP file. */ | |
146 | setutent_r (&data); | |
147 | ||
148 | /* Position at end of file. */ | |
149 | data.loc_utmp = lseek (data.ut_fd, 0, SEEK_END); | |
150 | if (data.loc_utmp != -1) | |
151 | { | |
152 | #if _HAVE_UT_TYPE - 0 | |
153 | /* We have to fake the old entry because this `login' | |
154 | function does not fit well into the UTMP file handling | |
155 | scheme. */ | |
156 | data.ubuf.ut_type = ut->ut_type; | |
157 | #endif | |
158 | pututline_r (ut, &data); | |
1474b80f | 159 | } |
0200214b RM |
160 | |
161 | /* Close WTMP file. */ | |
162 | endutent_r (&data); | |
163 | } | |
1474b80f | 164 | } |