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