summaryrefslogtreecommitdiff
path: root/notes/posixmodule.c.diff
blob: 3bea1f9cba96e0701ec64fb3a273e377b6d66d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
*** Python-2.2.1.orig/Modules/posixmodule.c	Tue Mar 12 16:38:31 2002
--- Python-2.2.1/Modules/posixmodule.c	Tue May 21 01:16:29 2002
***************
*** 1904,1910 ****
  }
  #endif
  
! #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
  #ifdef HAVE_PTY_H
  #include <pty.h>
  #else
--- 1904,1913 ----
  }
  #endif
  
! #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(sun)
! #ifdef sun
! #include <sys/stropts.h>
! #endif
  #ifdef HAVE_PTY_H
  #include <pty.h>
  #else
***************
*** 1914,1920 ****
  #endif /* HAVE_PTY_H */
  #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
  
! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
  static char posix_openpty__doc__[] =
  "openpty() -> (master_fd, slave_fd)\n\
  Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
--- 1917,1923 ----
  #endif /* HAVE_PTY_H */
  #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
  
! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(sun)
  static char posix_openpty__doc__[] =
  "openpty() -> (master_fd, slave_fd)\n\
  Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
***************
*** 1925,1932 ****
  	int master_fd, slave_fd;
  #ifndef HAVE_OPENPTY
  	char * slave_name;
  #endif
! 
  	if (!PyArg_ParseTuple(args, ":openpty"))
  		return NULL;
  
--- 1928,1941 ----
  	int master_fd, slave_fd;
  #ifndef HAVE_OPENPTY
  	char * slave_name;
+ #ifdef sun
+         void *sig_saved;
  #endif
! #endif
! #if !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) && defined(sun)
!         extern char *ptsname();
! #endif
!         
  	if (!PyArg_ParseTuple(args, ":openpty"))
  		return NULL;
  
***************
*** 1933,1939 ****
  #ifdef HAVE_OPENPTY
  	if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
  		return posix_error();
! #else
  	slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
  	if (slave_name == NULL)
  		return posix_error();
--- 1942,1948 ----
  #ifdef HAVE_OPENPTY
  	if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
  		return posix_error();
! #elif HAVE__GETPTY
  	slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
  	if (slave_name == NULL)
  		return posix_error();
***************
*** 1941,1946 ****
--- 1950,1966 ----
  	slave_fd = open(slave_name, O_RDWR);
  	if (slave_fd < 0)
  		return posix_error();
+ #else
+         master_fd = open("/dev/ptmx", O_RDWR|O_NOCTTY);  /* open master */
+         sig_saved = signal(SIGCHLD, SIG_DFL);
+         grantpt(master_fd);                     /* change permission of   slave */
+         unlockpt(master_fd);                    /* unlock slave */
+         signal(SIGCHLD,sig_saved);
+         slave_name = ptsname(master_fd);         /* get name of slave */
+         slave_fd = open(slave_name, O_RDWR);    /* open slave */
+         ioctl(slave_fd, I_PUSH, "ptem");       /* push ptem */
+         ioctl(slave_fd, I_PUSH, "ldterm");     /* push ldterm*/
+         ioctl(slave_fd, I_PUSH, "ttcompat");     /* push ttcompat*/
  #endif /* HAVE_OPENPTY */
  
  	return Py_BuildValue("(ii)", master_fd, slave_fd);
***************
*** 1948,1954 ****
  }
  #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */
  
! #ifdef HAVE_FORKPTY
  static char posix_forkpty__doc__[] =
  "forkpty() -> (pid, master_fd)\n\
  Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
--- 1968,1974 ----
  }
  #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */
  
! #if defined(HAVE_FORKPTY) || defined(sun)
  static char posix_forkpty__doc__[] =
  "forkpty() -> (pid, master_fd)\n\
  Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
***************
*** 1959,1968 ****
--- 1979,2067 ----
  posix_forkpty(PyObject *self, PyObject *args)
  {
  	int master_fd, pid;
+ #if defined(sun)
+         int slave;
+ 	char * slave_name;
+         void *sig_saved;
+         int fd;
+ #endif
  	
  	if (!PyArg_ParseTuple(args, ":forkpty"))
  		return NULL;
+ #if defined(sun)
+         master_fd = open("/dev/ptmx", O_RDWR|O_NOCTTY);  /* open master */
+         sig_saved = signal(SIGCHLD, SIG_DFL);
+         grantpt(master_fd);                     /* change permission of   slave */
+         unlockpt(master_fd);                    /* unlock slave */
+         signal(SIGCHLD,sig_saved);
+         slave_name = ptsname(master_fd);         /* get name of slave */
+         slave = open(slave_name, O_RDWR);    /* open slave */
+         ioctl(slave, I_PUSH, "ptem");       /* push ptem */
+         ioctl(slave, I_PUSH, "ldterm");     /* push ldterm*/
+         ioctl(slave, I_PUSH, "ttcompat");     /* push ttcompat*/
+         if (master_fd < 0 || slave < 0)
+         {
+             return posix_error();
+         }
+ 	switch (pid = fork()) {
+ 	case -1:	
+             return posix_error();
+ 	case 0:
+             /* First disconnect from the old controlling tty. */
+ #ifdef TIOCNOTTY
+             fd = open("/dev/tty", O_RDWR | O_NOCTTY);
+             if (fd >= 0) {
+ 		(void) ioctl(fd, TIOCNOTTY, NULL);
+ 		close(fd);
+             }
+ #endif /* TIOCNOTTY */
+             if (setsid() < 0)
+ 		return posix_error();
+             
+             /*
+              * Verify that we are successfully disconnected from the controlling
+              * tty.
+              */
+             fd = open("/dev/tty", O_RDWR | O_NOCTTY);
+             if (fd >= 0) {
+ 		return posix_error();
+ 		close(fd);
+             }
+             /* Make it our controlling tty. */
+ #ifdef TIOCSCTTY
+             if (ioctl(slave, TIOCSCTTY, NULL) < 0)
+ 		return posix_error();
+ #endif /* TIOCSCTTY */
+             fd = open(slave_name, O_RDWR);
+             if (fd < 0) {
+ 		return posix_error();
+             } else {
+ 		close(fd);
+             }
+             /* Verify that we now have a controlling tty. */
+             fd = open("/dev/tty", O_WRONLY);
+             if (fd < 0)
+ 		return posix_error();
+             else {
+ 		close(fd);
+             }
+             (void) close(master_fd);
+             (void) dup2(slave, 0);
+             (void) dup2(slave, 1);
+             (void) dup2(slave, 2);
+             if (slave > 2)
+                 (void) close(slave);
+             pid = 0;
+             break;
+           defautlt:
+             /*
+              * parent
+              */
+             (void) close(slave);
+ 	}
+ #else
  	pid = forkpty(&master_fd, NULL, NULL, NULL);
+ #endif
  	if (pid == -1)
  		return posix_error();
  	if (pid == 0)
***************
*** 5607,5616 ****
  #ifdef HAVE_FORK
  	{"fork",	posix_fork, METH_VARARGS, posix_fork__doc__},
  #endif /* HAVE_FORK */
! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
  	{"openpty",	posix_openpty, METH_VARARGS, posix_openpty__doc__},
  #endif /* HAVE_OPENPTY || HAVE__GETPTY */
! #ifdef HAVE_FORKPTY
  	{"forkpty",	posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
  #endif /* HAVE_FORKPTY */
  #ifdef HAVE_GETEGID
--- 5706,5715 ----
  #ifdef HAVE_FORK
  	{"fork",	posix_fork, METH_VARARGS, posix_fork__doc__},
  #endif /* HAVE_FORK */
! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(sun)
  	{"openpty",	posix_openpty, METH_VARARGS, posix_openpty__doc__},
  #endif /* HAVE_OPENPTY || HAVE__GETPTY */
! #if defined(HAVE_FORKPTY) || defined(sun)
  	{"forkpty",	posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
  #endif /* HAVE_FORKPTY */
  #ifdef HAVE_GETEGID