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

Comparing libptytty/src/ptytty.C (file contents):
Revision 1.25 by root, Tue Oct 3 11:19:49 2006 UTC vs.
Revision 1.34 by ayin, Fri Dec 14 09:51:32 2007 UTC

32#include <cstdlib> 32#include <cstdlib>
33#include <cstring> 33#include <cstring>
34#include <csignal> 34#include <csignal>
35 35
36#include <sys/types.h> 36#include <sys/types.h>
37#include <sys/stat.h>
37#include <unistd.h> 38#include <unistd.h>
38#include <fcntl.h> 39#include <fcntl.h>
39 40
40#ifdef HAVE_SYS_IOCTL_H 41#ifdef HAVE_SYS_IOCTL_H
41# include <sys/ioctl.h> 42# include <sys/ioctl.h>
63 64
64/* ------------------------------------------------------------------------- * 65/* ------------------------------------------------------------------------- *
65 * GET PSEUDO TELETYPE - MASTER AND SLAVE * 66 * GET PSEUDO TELETYPE - MASTER AND SLAVE *
66 * ------------------------------------------------------------------------- */ 67 * ------------------------------------------------------------------------- */
67/* 68/*
68 * Returns pty file descriptor, or -1 on failure 69 * Returns pty file descriptor, or -1 on failure
69 * If successful, ttydev is set to the name of the slave device. 70 * If successful, ttydev is set to the name of the slave device.
70 * fd_tty _may_ also be set to an open fd to the slave device 71 * fd_tty _may_ also be set to an open fd to the slave device
71 */ 72 */
72#if defined(UNIX98_PTY) 73#if defined(UNIX98_PTY)
73 74
86 87
87 if (pfd >= 0) 88 if (pfd >= 0)
88 { 89 {
89 if (grantpt (pfd) == 0 /* change slave permissions */ 90 if (grantpt (pfd) == 0 /* change slave permissions */
90 && unlockpt (pfd) == 0) 91 && unlockpt (pfd) == 0)
92 {
91 { /* slave now unlocked */ 93 /* slave now unlocked */
92 *ttydev = strdup (ptsname (pfd)); /* get slave's name */ 94 *ttydev = strdup (ptsname (pfd)); /* get slave's name */
93 return pfd; 95 return pfd;
94 } 96 }
95 97
96 close (pfd); 98 close (pfd);
104 static int 106 static int
105 get_pty (int *fd_tty, char **ttydev) 107 get_pty (int *fd_tty, char **ttydev)
106 { 108 {
107 int pfd; 109 int pfd;
108 int res; 110 int res;
109 char tty_name[32]; 111
110
111 res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); 112 res = openpty (&pfd, fd_tty, NULL, NULL, NULL);
112 113
113 if (res != -1) 114 if (res != -1)
114 { 115 {
115 *ttydev = strdup (tty_name); 116 *ttydev = strdup (ttyname (*fd_tty));
116 return pfd; 117 return pfd;
117 } 118 }
118 119
119 return -1; 120 return -1;
120 } 121 }
123 124
124 static int 125 static int
125 get_pty (int *fd_tty, char **ttydev) 126 get_pty (int *fd_tty, char **ttydev)
126 { 127 {
127 int pfd; 128 int pfd;
129 char *slave;
128 130
129 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); 131 slave = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
130 132
131 if (*ttydev != NULL) 133 if (slave != NULL)
134 {
135 *ttydev = strdup (slave);
132 return pfd; 136 return pfd;
137 }
133 138
134 return -1; 139 return -1;
135 } 140 }
136 141
137#elif defined(HAVE_DEV_PTC) 142#elif defined(HAVE_DEV_PTC)
206 211
207#endif 212#endif
208 213
209/*----------------------------------------------------------------------*/ 214/*----------------------------------------------------------------------*/
210/* 215/*
211 * Returns tty file descriptor, or -1 on failure 216 * Returns tty file descriptor, or -1 on failure
212 */ 217 */
213static int 218static int
214get_tty (char *ttydev) 219get_tty (char *ttydev)
215{ 220{
216 return open (ttydev, O_RDWR | O_NOCTTY, 0); 221 return open (ttydev, O_RDWR | O_NOCTTY, 0);
221 * Make our tty a controlling tty so that /dev/tty points to us 226 * Make our tty a controlling tty so that /dev/tty points to us
222 */ 227 */
223static int 228static int
224control_tty (int fd_tty) 229control_tty (int fd_tty)
225{ 230{
231 int fd;
232
226 setsid (); 233 setsid ();
227 234
228#if defined(HAVE_DEV_PTMX) && defined(I_PUSH) 235#if defined(HAVE_DEV_PTMX) && defined(I_PUSH)
229 /* 236 /*
230 * Push STREAMS modules: 237 * Push STREAMS modules:
251 ioctl (fd_tty, I_PUSH, "ldterm"); 258 ioctl (fd_tty, I_PUSH, "ldterm");
252 ioctl (fd_tty, I_PUSH, "ttcompat"); 259 ioctl (fd_tty, I_PUSH, "ttcompat");
253 } 260 }
254#endif 261#endif
255 262
263#ifdef TIOCSCTTY
256 ioctl (fd_tty, TIOCSCTTY, NULL); 264 ioctl (fd_tty, TIOCSCTTY, NULL);
265#else
266 fd = open (ttyname (fd_tty), O_RDWR);
267 if (fd >= 0)
268 close (fd);
269#endif
257 270
258 int fd = open ("/dev/tty", O_WRONLY); 271 fd = open ("/dev/tty", O_WRONLY);
259 if (fd < 0) 272 if (fd < 0)
260 return -1; /* fatal */ 273 return -1; /* fatal */
261 274
262 close (fd); 275 close (fd);
263 276
315 { 328 {
316#ifdef TTY_GID_SUPPORT 329#ifdef TTY_GID_SUPPORT
317 struct group *gr = getgrnam ("tty"); 330 struct group *gr = getgrnam ("tty");
318 331
319 if (gr) 332 if (gr)
333 {
320 { /* change group ownership of tty to "tty" */ 334 /* change group ownership of tty to "tty" */
321 mode = S_IRUSR | S_IWUSR | S_IWGRP; 335 mode = S_IRUSR | S_IWUSR | S_IWGRP;
322 gid = gr->gr_gid; 336 gid = gr->gr_gid;
323 } 337 }
324 else 338 else
325#endif /* TTY_GID_SUPPORT */ 339#endif /* TTY_GID_SUPPORT */
326 { 340 {
327 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; 341 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
328 gid = 0; 342 gid = 0;
329 } 343 }
330 } 344 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines