1 /*
2  * pty_init_slave: open slave side of terminal, clearing for use.
3  *
4  * Copyright 1995, 1996 by the Massachusetts Institute of Technology.
5  *
6  * Permission to use, copy, modify, and distribute this software and
7  * its documentation for any purpose and without fee is hereby
8  * granted, provided that the above copyright notice appear in all
9  * copies and that both that copyright notice and this permission
10  * notice appear in supporting documentation, and that the name of
11  * M.I.T. not be used in advertising or publicity pertaining to
12  * distribution of the software without specific, written prior
13  * permission.  Furthermore if you modify this software you must label
14  * your software as modified software and not distribute it in such a
15  * fashion that it might be confused with the original M.I.T. software.
16  * M.I.T. makes no representations about the suitability
17  * of this software for any purpose.  It is provided "as is" without
18  * express or implied warranty.
19  *
20  */
21 
22 #include "pty-int.h"
23 
24 /* * The following is an array of modules that should be pushed on the
25  *  stream.  See configure.in for caviats and notes about when this
26  *  array is used and not used.
27  */
28 #if defined(HAVE_STREAMS)&&(!defined(HAVE_LINE_PUSH))
29 static char *push_list[] = {
30 #ifdef PUSH_PTEM
31   "ptem",
32 #endif
33 #ifdef PUSH_LDTERM
34   "ldterm",
35 #endif
36 #ifdef PUSH_TTCOMPAT
37 "ttcompat",
38 #endif
39   0};
40 #endif /*HAVE_STREAMS but not HAVE_LINE_PUSH*/
41 
42 
43 
pty_initialize_slave(int fd)44 long pty_initialize_slave (int fd)
45 {
46 #if defined(POSIX_TERMIOS) && !defined(ultrix)
47     struct termios new_termio;
48 #else
49     struct sgttyb b;
50 #endif /* POSIX_TERMIOS */
51     int pid;
52 
53 #ifdef HAVE_STREAMS
54 #ifdef HAVE_LINE_PUSH
55         while (ioctl (fd, I_POP, 0) == 0); /*Clear out any old lined's*/
56 
57     if (line_push(fd) < 0)
58 	{
59 	    (void) close(fd); fd = -1;
60 	    return PTY_OPEN_SLAVE_LINE_PUSHFAIL;
61 	}
62 #else /*No line_push */
63     {
64        char **module = &push_list[0];
65       while (*module)
66 		if (ioctl(fd, I_PUSH, *(module++)) < 0)
67 		  	return PTY_OPEN_SLAVE_PUSH_FAIL;
68     }
69 
70 #endif /*LINE_PUSH*/
71 #endif /*HAVE_STREAMS*/
72 
73     /*
74 	 * Under Ultrix 3.0, the pgrp of the slave pty terminal
75 	 * needs to be set explicitly.  Why rlogind works at all
76 	 * without this on 4.3BSD is a mystery.
77 	 */
78 #ifdef GETPGRP_ONEARG
79     pid = getpgrp(getpid());
80 #else
81     pid = getpgrp();
82 #endif
83 
84 #ifdef TIOCSPGRP
85     ioctl(fd, TIOCSPGRP, &pid);
86 #endif
87 
88 
89 #if defined(POSIX_TERMIOS) && !defined(ultrix)
90 	tcsetpgrp(fd, pid);
91 	tcgetattr(fd,&new_termio);
92 	new_termio.c_cc[VMIN] = 1;
93 	new_termio.c_cc[VTIME] = 0;
94     tcsetattr(fd,TCSANOW,&new_termio);
95 #endif /* POSIX_TERMIOS */
96 
97     return 0;
98 }
99