1*dba3562dSLionel Sambuc /* $NetBSD: login_tty.c,v 1.13 2011/09/16 16:13:16 plunky Exp $ */
20c3983b2SBen Gras
30c3983b2SBen Gras /*-
40c3983b2SBen Gras * Copyright (c) 1990, 1993
50c3983b2SBen Gras * The Regents of the University of California. All rights reserved.
60c3983b2SBen Gras *
70c3983b2SBen Gras * Redistribution and use in source and binary forms, with or without
80c3983b2SBen Gras * modification, are permitted provided that the following conditions
90c3983b2SBen Gras * are met:
100c3983b2SBen Gras * 1. Redistributions of source code must retain the above copyright
110c3983b2SBen Gras * notice, this list of conditions and the following disclaimer.
120c3983b2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
130c3983b2SBen Gras * notice, this list of conditions and the following disclaimer in the
140c3983b2SBen Gras * documentation and/or other materials provided with the distribution.
150c3983b2SBen Gras * 3. Neither the name of the University nor the names of its contributors
160c3983b2SBen Gras * may be used to endorse or promote products derived from this software
170c3983b2SBen Gras * without specific prior written permission.
180c3983b2SBen Gras *
190c3983b2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
200c3983b2SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
210c3983b2SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
220c3983b2SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
230c3983b2SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
240c3983b2SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
250c3983b2SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260c3983b2SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
270c3983b2SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
280c3983b2SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
290c3983b2SBen Gras * SUCH DAMAGE.
300c3983b2SBen Gras */
310c3983b2SBen Gras
320c3983b2SBen Gras #include <sys/cdefs.h>
330c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
340c3983b2SBen Gras #if 0
350c3983b2SBen Gras static char sccsid[] = "@(#)login_tty.c 8.1 (Berkeley) 6/4/93";
360c3983b2SBen Gras #else
37*dba3562dSLionel Sambuc __RCSID("$NetBSD: login_tty.c,v 1.13 2011/09/16 16:13:16 plunky Exp $");
380c3983b2SBen Gras #endif
390c3983b2SBen Gras #endif /* LIBC_SCCS and not lint */
400c3983b2SBen Gras
410c3983b2SBen Gras #include <sys/param.h>
420c3983b2SBen Gras #include <sys/ioctl.h>
430c3983b2SBen Gras
440c3983b2SBen Gras #include <assert.h>
450c3983b2SBen Gras #include <errno.h>
460c3983b2SBen Gras #include <unistd.h>
470c3983b2SBen Gras #include <util.h>
480c3983b2SBen Gras
490c3983b2SBen Gras int
login_tty(int fd)500c3983b2SBen Gras login_tty(int fd)
510c3983b2SBen Gras {
520c3983b2SBen Gras
530c3983b2SBen Gras _DIAGASSERT(fd != -1);
540c3983b2SBen Gras
550c3983b2SBen Gras (void) setsid();
56*dba3562dSLionel Sambuc if (ioctl(fd, TIOCSCTTY, NULL) == -1)
570c3983b2SBen Gras return (-1);
580c3983b2SBen Gras (void) dup2(fd, STDIN_FILENO);
590c3983b2SBen Gras (void) dup2(fd, STDOUT_FILENO);
600c3983b2SBen Gras (void) dup2(fd, STDERR_FILENO);
610c3983b2SBen Gras if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO)
620c3983b2SBen Gras (void) close(fd);
630c3983b2SBen Gras return (0);
640c3983b2SBen Gras }
65