1 /* Copyright (C) 1996-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
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. */
33 tty_name (int fd
, char **tty
, size_t buf_len
)
35 int rv
; /* Return value. 0 = success. */
36 char *buf
= *tty
; /* Buffer for ttyname, initially the user's. */
44 rv
= ttyname_r (fd
, buf
, buf_len
);
46 if (rv
!= 0 || memchr (buf
, '\0', buf_len
))
47 /* We either got an error, or we succeeded and the
48 returned name fit in the buffer. */
51 /* Try again with a longer buffer. */
52 buf_len
+= buf_len
; /* Double it */
55 /* No initial buffer; start out by mallocing one. */
56 buf_len
= 128; /* First time guess. */
59 /* We've already malloced another buffer at least once. */
60 new_buf
= realloc (buf
, buf_len
);
62 new_buf
= malloc (buf_len
);
73 *tty
= buf
; /* Return buffer to the user. */
75 free (buf
); /* Free what we malloced when returning an error. */
81 login (const struct utmp
*ut
)
84 char _tty
[PATH_MAX
+ UT_LINESIZE
];
86 char _tty
[512 + UT_LINESIZE
];
91 struct utmp copy
= *ut
;
93 /* Fill in those fields we supply. */
95 copy
.ut_type
= USER_PROCESS
;
98 copy
.ut_pid
= getpid ();
102 found_tty
= tty_name (STDIN_FILENO
, &tty
, sizeof (_tty
));
104 found_tty
= tty_name (STDOUT_FILENO
, &tty
, sizeof (_tty
));
106 found_tty
= tty_name (STDERR_FILENO
, &tty
, sizeof (_tty
));
110 /* We only want to insert the name of the tty without path.
111 But take care of name like /dev/pts/3. */
112 if (strncmp (tty
, "/dev/", 5) == 0)
113 ttyp
= tty
+ 5; /* Skip the "/dev/". */
115 ttyp
= basename (tty
);
117 /* Position to record for this tty. */
118 strncpy (copy
.ut_line
, ttyp
, UT_LINESIZE
);
120 /* Tell that we want to use the UTMP file. */
121 if (utmpname (_PATH_UTMP
) == 0)
123 /* Open UTMP file. */
126 /* Write the entry. */
129 /* Close UTMP file. */
134 free (tty
); /* Free buffer malloced by tty_name. */
137 /* We provide a default value so that the output does not contain
138 an random bytes in this field. */
139 strncpy (copy
.ut_line
, "???", UT_LINESIZE
);
141 /* Update the WTMP file. Here we have to add a new entry. */
142 updwtmp (_PATH_WTMP
, ©
);