Commit | Line | Data |
---|---|---|
9336b784 | 1 | /*- |
2b71dc5c KB |
2 | * Copyright (c) 1990, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
9336b784 MT |
4 | * |
5 | * %sccs.include.redist.c% | |
6 | */ | |
7 | ||
8 | #if defined(LIBC_SCCS) && !defined(lint) | |
2b71dc5c | 9 | static char sccsid[] = "@(#)pty.c 8.1 (Berkeley) %G%"; |
9336b784 MT |
10 | #endif /* LIBC_SCCS and not lint */ |
11 | ||
896b9bf5 | 12 | #include <sys/cdefs.h> |
04923501 KB |
13 | #include <sys/types.h> |
14 | #include <sys/stat.h> | |
b662bbb5 | 15 | #include <sys/ioctl.h> |
896b9bf5 | 16 | #include <fcntl.h> |
b662bbb5 | 17 | #include <termios.h> |
896b9bf5 KB |
18 | #include <errno.h> |
19 | #include <unistd.h> | |
20 | #include <stdio.h> | |
21 | #include <string.h> | |
b662bbb5 | 22 | #include <grp.h> |
9336b784 | 23 | |
b662bbb5 | 24 | openpty(amaster, aslave, name, termp, winp) |
9336b784 MT |
25 | int *amaster, *aslave; |
26 | char *name; | |
b662bbb5 MT |
27 | struct termios *termp; |
28 | struct winsize *winp; | |
9336b784 | 29 | { |
0f3c4369 | 30 | static char line[] = "/dev/ptyXX"; |
896b9bf5 | 31 | register const char *cp1, *cp2; |
04923501 | 32 | register int master, slave, ttygid; |
b662bbb5 | 33 | struct group *gr; |
9336b784 | 34 | |
b662bbb5 MT |
35 | if ((gr = getgrnam("tty")) != NULL) |
36 | ttygid = gr->gr_gid; | |
37 | else | |
38 | ttygid = -1; | |
b662bbb5 | 39 | |
9336b784 MT |
40 | for (cp1 = "pqrs"; *cp1; cp1++) { |
41 | line[8] = *cp1; | |
42 | for (cp2 = "0123456789abcdef"; *cp2; cp2++) { | |
43 | line[9] = *cp2; | |
44 | if ((master = open(line, O_RDWR, 0)) == -1) { | |
696b89b6 | 45 | if (errno == ENOENT) |
b662bbb5 | 46 | return (-1); /* out of ptys */ |
9336b784 MT |
47 | } else { |
48 | line[5] = 't'; | |
f2f593c3 MT |
49 | (void) chown(line, getuid(), ttygid); |
50 | (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP); | |
b662bbb5 | 51 | (void) revoke(line); |
9336b784 MT |
52 | if ((slave = open(line, O_RDWR, 0)) != -1) { |
53 | *amaster = master; | |
54 | *aslave = slave; | |
55 | if (name) | |
56 | strcpy(name, line); | |
b662bbb5 MT |
57 | if (termp) |
58 | (void) tcsetattr(slave, | |
896b9bf5 | 59 | TCSAFLUSH, termp); |
b662bbb5 MT |
60 | if (winp) |
61 | (void) ioctl(slave, TIOCSWINSZ, | |
f064e9b2 | 62 | (char *)winp); |
9336b784 MT |
63 | return (0); |
64 | } | |
b662bbb5 | 65 | (void) close(master); |
9336b784 MT |
66 | line[5] = 'p'; |
67 | } | |
68 | } | |
69 | } | |
b662bbb5 | 70 | errno = ENOENT; /* out of ptys */ |
9336b784 MT |
71 | return (-1); |
72 | } | |
b662bbb5 MT |
73 | |
74 | forkpty(amaster, name, termp, winp) | |
75 | int *amaster; | |
76 | char *name; | |
77 | struct termios *termp; | |
78 | struct winsize *winp; | |
79 | { | |
80 | int master, slave, pid; | |
81 | ||
82 | if (openpty(&master, &slave, name, termp, winp) == -1) | |
83 | return (-1); | |
84 | switch (pid = fork()) { | |
85 | case -1: | |
86 | return (-1); | |
87 | case 0: | |
88 | /* | |
89 | * child | |
90 | */ | |
91 | (void) close(master); | |
92 | login_tty(slave); | |
93 | return (0); | |
94 | } | |
95 | /* | |
96 | * parent | |
97 | */ | |
98 | *amaster = master; | |
99 | (void) close(slave); | |
100 | return (pid); | |
101 | } |