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.24 by ayin, Sun Sep 10 17:59:21 2006 UTC vs.
Revision 1.33 by ayin, Thu Dec 13 14:42:33 2007 UTC

63 63
64/* ------------------------------------------------------------------------- * 64/* ------------------------------------------------------------------------- *
65 * GET PSEUDO TELETYPE - MASTER AND SLAVE * 65 * GET PSEUDO TELETYPE - MASTER AND SLAVE *
66 * ------------------------------------------------------------------------- */ 66 * ------------------------------------------------------------------------- */
67/* 67/*
68 * Returns pty file descriptor, or -1 on failure 68 * Returns pty file descriptor, or -1 on failure
69 * If successful, ttydev is set to the name of the slave device. 69 * 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 70 * fd_tty _may_ also be set to an open fd to the slave device
71 */ 71 */
72#if defined(UNIX98_PTY) 72#if defined(UNIX98_PTY)
73
73static int 74 static int
74get_pty (int *fd_tty, char **ttydev) 75 get_pty (int *fd_tty, char **ttydev)
75{ 76 {
76 int pfd; 77 int pfd;
77 78
78# if defined(HAVE_GETPT) 79# if defined(HAVE_GETPT)
79 pfd = getpt(); 80 pfd = getpt();
80# elif defined(HAVE_POSIX_OPENPT) 81# elif defined(HAVE_POSIX_OPENPT)
81 pfd = posix_openpt (O_RDWR); 82 pfd = posix_openpt (O_RDWR);
82# else 83# else
83 pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0); 84 pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0);
84# endif 85# endif
86
85 if (pfd >= 0) 87 if (pfd >= 0)
86 { 88 {
87 if (grantpt (pfd) == 0 /* change slave permissions */ 89 if (grantpt (pfd) == 0 /* change slave permissions */
88 && unlockpt (pfd) == 0) 90 && unlockpt (pfd) == 0)
91 {
89 { /* slave now unlocked */ 92 /* slave now unlocked */
90 *ttydev = strdup (ptsname (pfd)); /* get slave's name */ 93 *ttydev = strdup (ptsname (pfd)); /* get slave's name */
91 return pfd; 94 return pfd;
92 } 95 }
93 96
94 close (pfd); 97 close (pfd);
95 } 98 }
96 99
97 return -1; 100 return -1;
98} 101 }
102
99#elif defined(HAVE_OPENPTY) 103#elif defined(HAVE_OPENPTY)
104
100static int 105 static int
101get_pty (int *fd_tty, char **ttydev) 106 get_pty (int *fd_tty, char **ttydev)
102{ 107 {
103 int pfd; 108 int pfd;
104 int res; 109 int res;
105 char tty_name[32]; 110
106
107 res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); 111 res = openpty (&pfd, fd_tty, NULL, NULL, NULL);
112
108 if (res != -1) 113 if (res != -1)
109 { 114 {
110 *ttydev = strdup (tty_name); 115 *ttydev = strdup (ttyname (*fd_tty));
111 return pfd; 116 return pfd;
112 } 117 }
113 118
114 return -1; 119 return -1;
115} 120 }
121
116#elif defined(HAVE__GETPTY) 122#elif defined(HAVE__GETPTY)
123
117static int 124 static int
118get_pty (int *fd_tty, char **ttydev) 125 get_pty (int *fd_tty, char **ttydev)
119{ 126 {
120 int pfd; 127 int pfd;
128 char *slave;
121 129
122 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); 130 slave = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
123 if (*ttydev != NULL) 131
132 if (slave != NULL)
133 {
134 *ttydev = strdup (slave);
124 return pfd; 135 return pfd;
136 }
125 137
126 return -1; 138 return -1;
127} 139 }
140
128#elif defined(HAVE_DEV_PTC) 141#elif defined(HAVE_DEV_PTC)
142
129static int 143 static int
130get_pty (int *fd_tty, char **ttydev) 144 get_pty (int *fd_tty, char **ttydev)
131{ 145 {
132 int pfd; 146 int pfd;
133 147
134 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) 148 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
135 { 149 {
136 *ttydev = strdup (ttyname (pfd)); 150 *ttydev = strdup (ttyname (pfd));
137 return pfd; 151 return pfd;
138 } 152 }
139 153
140 return -1; 154 return -1;
141} 155 }
156
142#elif defined(HAVE_DEV_CLONE) 157#elif defined(HAVE_DEV_CLONE)
158
143static int 159 static int
144get_pty (int *fd_tty, char **ttydev) 160 get_pty (int *fd_tty, char **ttydev)
145{ 161 {
146 int pfd; 162 int pfd;
147 163
148 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) 164 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0)
149 { 165 {
150 *ttydev = strdup (ptsname (pfd)); 166 *ttydev = strdup (ptsname (pfd));
151 return pfd; 167 return pfd;
152 } 168 }
153 169
154 return -1; 170 return -1;
155} 171 }
172
156#else 173#else
174
157/* Based on the code in openssh/openbsd-compat/bsd-openpty.c */ 175 /* Based on the code in openssh/openbsd-compat/bsd-openpty.c */
158static int 176 static int
159get_pty (int *fd_tty, char **ttydev) 177 get_pty (int *fd_tty, char **ttydev)
160{ 178 {
161 int pfd; 179 int pfd;
162 int i; 180 int i;
163 char pty_name[32]; 181 char pty_name[32];
164 char tty_name[32]; 182 char tty_name[32];
165 const char *majors = "pqrstuvwxyzabcde"; 183 const char *majors = "pqrstuvwxyzabcde";
166 const char *minors = "0123456789abcdef"; 184 const char *minors = "0123456789abcdef";
185
167 for (i = 0; i < 256; i++) 186 for (i = 0; i < 256; i++)
168 { 187 {
169 snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); 188 snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]);
170 snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); 189 snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]);
190
171 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) 191 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
172 { 192 {
173 snprintf(pty_name, 32, "/dev/ptyp%d", i); 193 snprintf(pty_name, 32, "/dev/ptyp%d", i);
174 snprintf(tty_name, 32, "/dev/ttyp%d", i); 194 snprintf(tty_name, 32, "/dev/ttyp%d", i);
175 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) 195 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
176 continue; 196 continue;
177 } 197 }
198
178 if (access (tty_name, R_OK | W_OK) == 0) 199 if (access (tty_name, R_OK | W_OK) == 0)
179 { 200 {
180 *ttydev = strdup (tty_name); 201 *ttydev = strdup (tty_name);
181 return pfd; 202 return pfd;
182 } 203 }
183 204
184 close (pfd); 205 close (pfd);
185 } 206 }
186 207
187 return -1; 208 return -1;
188} 209 }
210
189#endif 211#endif
190 212
191/*----------------------------------------------------------------------*/ 213/*----------------------------------------------------------------------*/
192/* 214/*
193 * Returns tty file descriptor, or -1 on failure 215 * Returns tty file descriptor, or -1 on failure
194 */ 216 */
195static int 217static int
196get_tty (char *ttydev) 218get_tty (char *ttydev)
197{ 219{
198 return open (ttydev, O_RDWR | O_NOCTTY, 0); 220 return open (ttydev, O_RDWR | O_NOCTTY, 0);
203 * Make our tty a controlling tty so that /dev/tty points to us 225 * Make our tty a controlling tty so that /dev/tty points to us
204 */ 226 */
205static int 227static int
206control_tty (int fd_tty) 228control_tty (int fd_tty)
207{ 229{
230 int fd;
231
208 setsid (); 232 setsid ();
209 233
210#if defined(HAVE_DEV_PTMX) && defined(I_PUSH) 234#if defined(HAVE_DEV_PTMX) && defined(I_PUSH)
211 /* 235 /*
212 * Push STREAMS modules: 236 * Push STREAMS modules:
233 ioctl (fd_tty, I_PUSH, "ldterm"); 257 ioctl (fd_tty, I_PUSH, "ldterm");
234 ioctl (fd_tty, I_PUSH, "ttcompat"); 258 ioctl (fd_tty, I_PUSH, "ttcompat");
235 } 259 }
236#endif 260#endif
237 261
262#ifdef TIOCSCTTY
238 ioctl (fd_tty, TIOCSCTTY, NULL); 263 ioctl (fd_tty, TIOCSCTTY, NULL);
264#else
265 fd = open (ttyname (fd_tty), O_RDWR);
266 if (fd >= 0)
267 close (fd);
268#endif
239 269
240 int fd = open ("/dev/tty", O_WRONLY); 270 fd = open ("/dev/tty", O_WRONLY);
241 if (fd < 0) 271 if (fd < 0)
242 return -1; /* fatal */ 272 return -1; /* fatal */
243 273
244 close (fd); 274 close (fd);
245 275
297 { 327 {
298#ifdef TTY_GID_SUPPORT 328#ifdef TTY_GID_SUPPORT
299 struct group *gr = getgrnam ("tty"); 329 struct group *gr = getgrnam ("tty");
300 330
301 if (gr) 331 if (gr)
332 {
302 { /* change group ownership of tty to "tty" */ 333 /* change group ownership of tty to "tty" */
303 mode = S_IRUSR | S_IWUSR | S_IWGRP; 334 mode = S_IRUSR | S_IWUSR | S_IWGRP;
304 gid = gr->gr_gid; 335 gid = gr->gr_gid;
305 } 336 }
306 else 337 else
307#endif /* TTY_GID_SUPPORT */ 338#endif /* TTY_GID_SUPPORT */
308 { 339 {
309 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; 340 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
310 gid = 0; 341 gid = 0;
311 } 342 }
312 } 343 }
329} 360}
330 361
331void 362void
332ptytty_unix::put () 363ptytty_unix::put ()
333{ 364{
365 if (name)
366 {
334 chmod (name, RESTORE_TTY_MODE); 367 chmod (name, RESTORE_TTY_MODE);
335 chown (name, 0, ttyconf.gid); 368 chown (name, 0, ttyconf.gid);
369 }
336 370
337 close_tty (); 371 close_tty ();
338 372
339 if (pty >= 0) 373 if (pty >= 0)
340 close (pty); 374 close (pty);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines