ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/ptytty.C
(Generate patch)

Comparing rxvt-unicode/src/ptytty.C (file contents):
Revision 1.43 by root, Tue Jan 17 16:50:42 2006 UTC vs.
Revision 1.48 by ayin, Thu Jan 19 10:44:44 2006 UTC

64/* 64/*
65 * Returns pty file descriptor, or -1 on failure 65 * Returns pty file descriptor, or -1 on failure
66 * If successful, ttydev is set to the name of the slave device. 66 * If successful, ttydev is set to the name of the slave device.
67 * fd_tty _may_ also be set to an open fd to the slave device 67 * fd_tty _may_ also be set to an open fd to the slave device
68 */ 68 */
69static int 69static inline int
70get_pty (int *fd_tty, char **ttydev) 70get_pty_streams (int *fd_tty, char **ttydev)
71{ 71{
72#ifdef NO_SETOWNER_TTYDEV
72 int pfd; 73 int pfd;
73 74
75# if defined(PTYS_ARE_GETPT)
76 pfd = getpt();
77# elif defined(PTYS_ARE_POSIX)
78 pfd = posix_openpt (O_RDWR);
79# else
80 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0);
81# endif
82 if (pfd >= 0)
83 {
84 if (grantpt (pfd) == 0 /* change slave permissions */
85 && unlockpt (pfd) == 0)
86 { /* slave now unlocked */
87 *ttydev = strdup (ptsname (pfd)); /* get slave's name */
88 return pfd;
89 }
90
91 close (pfd);
92 }
93#endif
94
95 return -1;
96}
97
98static inline int
99get_pty_openpty (int *fd_tty, char **ttydev)
100{
74#ifdef PTYS_ARE_OPENPTY 101#ifdef PTYS_ARE_OPENPTY
102 int pfd;
103 int res;
75 char tty_name[sizeof "/dev/pts/????\0"]; 104 char tty_name[sizeof "/dev/pts/????\0"];
76 105
77 int res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); 106 res = openpty (&pfd, fd_tty, tty_name, NULL, NULL);
78
79 if (res != -1) 107 if (res != -1)
80 { 108 {
81 *ttydev = strdup (tty_name); 109 *ttydev = strdup (tty_name);
82 return pfd; 110 return pfd;
83 } 111 }
84#endif 112#endif
85 113
114 return -1;
115}
116
117static inline int
118get_pty__getpty (int *fd_tty, char **ttydev)
119{
86#ifdef PTYS_ARE__GETPTY 120#ifdef PTYS_ARE__GETPTY
121 int pfd;
122
87 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); 123 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
88 if (*ttydev != NULL) 124 if (*ttydev != NULL)
89 return pfd; 125 return pfd;
90#endif 126#endif
91 127
92#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) 128 return -1;
93# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX) 129}
94 130
95 { 131static inline int
96# ifdef PTYS_ARE_GETPT 132get_pty_ptc (int *fd_tty, char **ttydev)
97 pfd = getpt(); 133{
98# else
99# ifdef PTYS_ARE_POSIX
100 pfd = posix_openpt (O_RDWR);
101# else
102 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0);
103# endif
104# endif
105
106 if (pfd >= 0)
107 {
108 if (grantpt (pfd) == 0 /* change slave permissions */
109 && unlockpt (pfd) == 0)
110 { /* slave now unlocked */
111 *ttydev = strdup (ptsname (pfd)); /* get slave's name */
112 return pfd;
113 }
114 close (pfd);
115 }
116 }
117# endif
118#endif
119
120#ifdef PTYS_ARE_PTC 134#ifdef PTYS_ARE_PTC
135 int pfd;
136
121 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) 137 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
122 { 138 {
123 *ttydev = strdup (ttyname (pfd)); 139 *ttydev = strdup (ttyname (pfd));
124 return pfd; 140 return pfd;
125 } 141 }
126#endif 142#endif
127 143
144 return -1;
145}
146
147static inline int
148get_pty_clone (int *fd_tty, char **ttydev)
149{
128#ifdef PTYS_ARE_CLONE 150#ifdef PTYS_ARE_CLONE
151 int pfd;
152
129 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) 153 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0)
130 { 154 {
131 *ttydev = strdup (ptsname (pfd)); 155 *ttydev = strdup (ptsname (pfd));
132 return pfd; 156 return pfd;
133 } 157 }
134#endif 158#endif
135 159
160 return -1;
161}
162
163static inline int
164get_pty_numeric (int *fd_tty, char **ttydev)
165{
136#ifdef PTYS_ARE_NUMERIC 166#ifdef PTYS_ARE_NUMERIC
137 { 167 int pfd;
138 int idx; 168 int idx;
139 char *c1, *c2; 169 char *c1, *c2;
140 char pty_name[] = "/dev/ptyp???"; 170 char pty_name[] = "/dev/ptyp???";
141 char tty_name[] = "/dev/ttyp???"; 171 char tty_name[] = "/dev/ttyp???";
142 172
143 c1 = &(pty_name[sizeof (pty_name) - 4]); 173 c1 = &(pty_name[sizeof (pty_name) - 4]);
144 c2 = &(tty_name[sizeof (tty_name) - 4]); 174 c2 = &(tty_name[sizeof (tty_name) - 4]);
175
145 for (idx = 0; idx < 256; idx++) 176 for (idx = 0; idx < 256; idx++)
146 { 177 {
147 sprintf (c1, "%d", idx); 178 sprintf (c1, "%d", idx);
148 sprintf (c2, "%d", idx); 179 sprintf (c2, "%d", idx);
180
149 if (access (tty_name, F_OK) < 0) 181 if (access (tty_name, F_OK) < 0)
150 { 182 {
151 idx = 256; 183 idx = 256;
152 break; 184 break;
153 } 185 }
154 186
155 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) 187 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
156 { 188 {
157 if (access (tty_name, R_OK | W_OK) == 0) 189 if (access (tty_name, R_OK | W_OK) == 0)
158 { 190 {
159 *ttydev = strdup (tty_name); 191 *ttydev = strdup (tty_name);
160 return pfd; 192 return pfd;
161 } 193 }
162 194
163 close (pfd); 195 close (pfd);
164 } 196 }
165 } 197 }
166 }
167#endif 198#endif
168 199
200 return -1;
201}
202
203static inline int
204get_pty_searched (int *fd_tty, char **ttydev)
205{
169#ifdef PTYS_ARE_SEARCHED 206#ifdef PTYS_ARE_SEARCHED
170 {
171 const char *c1, *c2;
172 char pty_name[] = "/dev/pty??";
173 char tty_name[] = "/dev/tty??";
174
175# ifndef PTYCHAR1 207# ifndef PTYCHAR1
176# define PTYCHAR1 "pqrstuvwxyz" 208# define PTYCHAR1 "pqrstuvwxyz"
177# endif 209# endif
178# ifndef PTYCHAR2 210# ifndef PTYCHAR2
179# define PTYCHAR2 "0123456789abcdef" 211# define PTYCHAR2 "0123456789abcdef"
180# endif 212# endif
213 int pfd;
214 const char *c1, *c2;
215 char pty_name[] = "/dev/pty??";
216 char tty_name[] = "/dev/tty??";
181 217
182 for (c1 = PTYCHAR1; *c1; c1++) 218 for (c1 = PTYCHAR1; *c1; c1++)
183 { 219 {
184 pty_name[ (sizeof (pty_name) - 3)] = 220 pty_name[ (sizeof (pty_name) - 3)] =
185 tty_name[ (sizeof (pty_name) - 3)] = *c1; 221 tty_name[ (sizeof (pty_name) - 3)] = *c1;
222
186 for (c2 = PTYCHAR2; *c2; c2++) 223 for (c2 = PTYCHAR2; *c2; c2++)
187 { 224 {
188 pty_name[ (sizeof (pty_name) - 2)] = 225 pty_name[ (sizeof (pty_name) - 2)] =
189 tty_name[ (sizeof (pty_name) - 2)] = *c2; 226 tty_name[ (sizeof (pty_name) - 2)] = *c2;
227
190 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) 228 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
191 { 229 {
192 if (access (tty_name, R_OK | W_OK) == 0) 230 if (access (tty_name, R_OK | W_OK) == 0)
193 { 231 {
194 *ttydev = strdup (tty_name); 232 *ttydev = strdup (tty_name);
195 return pfd; 233 return pfd;
196 } 234 }
197 235
198 close (pfd); 236 close (pfd);
199 } 237 }
200 } 238 }
201 } 239 }
202 }
203#endif 240#endif
241
242 return -1;
243}
244
245static int
246get_pty (int *fd_tty, char **ttydev)
247{
248 int pfd;
249
250 if ((pfd = get_pty_streams (fd_tty, ttydev)) != -1
251 || (pfd = get_pty_openpty (fd_tty, ttydev)) != -1
252 || (pfd = get_pty__getpty (fd_tty, ttydev)) != -1
253 || (pfd = get_pty_ptc (fd_tty, ttydev)) != -1
254 || (pfd = get_pty_clone (fd_tty, ttydev)) != -1
255 || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1
256 || (pfd = get_pty_searched (fd_tty, ttydev)) != -1)
257 return pfd;
204 258
205 return -1; 259 return -1;
206} 260}
207 261
208/*----------------------------------------------------------------------*/ 262/*----------------------------------------------------------------------*/
220 * Make our tty a controlling tty so that /dev/tty points to us 274 * Make our tty a controlling tty so that /dev/tty points to us
221 */ 275 */
222static int 276static int
223control_tty (int fd_tty) 277control_tty (int fd_tty)
224{ 278{
225 int fd;
226
227 /* ---------------------------------------- */
228 setsid (); 279 setsid ();
229 280
230 /* ---------------------------------------- */
231# if defined(PTYS_ARE_PTMX) && defined(I_PUSH) 281#if defined(PTYS_ARE_PTMX) && defined(I_PUSH)
232 /* 282 /*
233 * Push STREAMS modules: 283 * Push STREAMS modules:
234 * ptem: pseudo-terminal hardware emulation module. 284 * ptem: pseudo-terminal hardware emulation module.
235 * ldterm: standard terminal line discipline. 285 * ldterm: standard terminal line discipline.
236 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. 286 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
244 * documentation is really unclear about whether it is any close () on 294 * documentation is really unclear about whether it is any close () on
245 * the master side or the last close () - i.e. a proper STREAMS dismantling 295 * the master side or the last close () - i.e. a proper STREAMS dismantling
246 * close () - on the master side which causes a hang up to be sent 296 * close () - on the master side which causes a hang up to be sent
247 * through - Geoff Wing 297 * through - Geoff Wing
248 */ 298 */
249# ifdef HAVE_ISASTREAM 299# ifdef HAVE_ISASTREAM
250 if (isastream (fd_tty) == 1) 300 if (isastream (fd_tty) == 1)
251# endif 301# endif
252 { 302 {
253 ioctl (fd_tty, I_PUSH, "ptem"); 303 ioctl (fd_tty, I_PUSH, "ptem");
254 ioctl (fd_tty, I_PUSH, "ldterm"); 304 ioctl (fd_tty, I_PUSH, "ldterm");
255 ioctl (fd_tty, I_PUSH, "ttcompat"); 305 ioctl (fd_tty, I_PUSH, "ttcompat");
256 } 306 }
257# endif 307#endif
258 /* ---------------------------------------- */ 308
259 fd = ioctl (fd_tty, TIOCSCTTY, NULL); 309 ioctl (fd_tty, TIOCSCTTY, NULL);
260 /* ---------------------------------------- */ 310
261 fd = open ("/dev/tty", O_WRONLY); 311 int fd = open ("/dev/tty", O_WRONLY);
262 if (fd < 0) 312 if (fd < 0)
263 return -1; /* fatal */ 313 return -1; /* fatal */
314
264 close (fd); 315 close (fd);
265 /* ---------------------------------------- */
266 316
267 return 0; 317 return 0;
268} 318}
269 319
270void 320void
329 } 379 }
330 else 380 else
331#endif /* TTY_GID_SUPPORT */ 381#endif /* TTY_GID_SUPPORT */
332 { 382 {
333 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; 383 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
334 gid = getgid (); 384 gid = 0;
335 } 385 }
336 } 386 }
337} ttyconf; 387} ttyconf;
338 388
339///////////////////////////////////////////////////////////////////////////// 389/////////////////////////////////////////////////////////////////////////////
344 if (!name || !*name) 394 if (!name || !*name)
345 return; 395 return;
346 396
347 if (action == SAVE) 397 if (action == SAVE)
348 { 398 {
349# ifndef RESET_TTY_TO_COMMON_DEFAULTS 399 chown (name, getuid (), ttyconf.gid); /* fail silently */
350 /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */ 400 chmod (name, ttyconf.mode);
351 if (lstat (name, &savestat) < 0) /* you lose out */ 401# ifdef HAVE_REVOKE
352 ; 402 revoke (name);
353 else
354# endif 403# endif
355 {
356 saved = true;
357 chown (name, getuid (), ttyconf.gid); /* fail silently */
358 chmod (name, ttyconf.mode);
359# ifdef HAVE_REVOKE
360 revoke (name);
361# endif
362 }
363 } 404 }
364 else 405 else
365 { /* action == RESTORE */ 406 { /* action == RESTORE */
366# ifndef RESET_TTY_TO_COMMON_DEFAULTS 407 chmod (name, RESTORE_TTY_MODE);
367 if (saved)
368 {
369 chmod (name, savestat.st_mode);
370 chown (name, savestat.st_uid, savestat.st_gid);
371 }
372# else
373 chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
374 chown (name, 0, 0); 408 chown (name, 0, ttyconf.gid);
375# endif
376
377 } 409 }
378} 410}
379#endif 411#endif
380 412
381rxvt_ptytty_unix::rxvt_ptytty_unix () 413rxvt_ptytty_unix::rxvt_ptytty_unix ()
382{ 414{
383 pty = tty = -1; 415 pty = tty = -1;
384 name = 0; 416 name = 0;
385#ifndef NO_SETOWNER_TTYDEV
386 saved = false;
387#endif
388#if UTMP_SUPPORT 417#if UTMP_SUPPORT
389 cmd_pid = 0; 418 cmd_pid = 0;
390#endif 419#endif
391} 420}
392 421
587 close (sv[1]); 616 close (sv[1]);
588 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 617 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
589 } 618 }
590 else 619 else
591 { 620 {
592 setgid (getegid ());
593 setuid (geteuid ());
594
595 // server, pty-helper 621 // server, pty-helper
596 sock_fd = sv[1]; 622 sock_fd = sv[1];
597 623
598 for (int fd = 0; fd < 1023; fd++) 624 for (int fd = 0; fd < 1023; fd++)
599 if (fd != sock_fd && fd != 1) 625 if (fd != sock_fd)
600 close (fd); 626 close (fd);
601 627
602 serve (); 628 serve ();
603 _exit (EXIT_SUCCESS); 629 _exit (EXIT_SUCCESS);
604 } 630 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines