Blame


1 75024f0f 2004-03-26 devnull #include <u.h>
2 75024f0f 2004-03-26 devnull #include <sys/types.h>
3 75024f0f 2004-03-26 devnull #include <sys/ioctl.h>
4 75024f0f 2004-03-26 devnull #include <sys/stat.h>
5 75024f0f 2004-03-26 devnull #include <errno.h>
6 75024f0f 2004-03-26 devnull #include <fcntl.h>
7 75024f0f 2004-03-26 devnull #include <grp.h>
8 75024f0f 2004-03-26 devnull #include <stdlib.h>
9 75024f0f 2004-03-26 devnull #include <string.h>
10 75024f0f 2004-03-26 devnull #include <termios.h>
11 75024f0f 2004-03-26 devnull #include <unistd.h>
12 75024f0f 2004-03-26 devnull #include <libc.h>
13 75024f0f 2004-03-26 devnull #include "term.h"
14 75024f0f 2004-03-26 devnull
15 75024f0f 2004-03-26 devnull int myopenpty(int[], char*);
16 75024f0f 2004-03-26 devnull
17 75024f0f 2004-03-26 devnull int
18 75024f0f 2004-03-26 devnull getpts(int fd[], char *slave)
19 75024f0f 2004-03-26 devnull {
20 75024f0f 2004-03-26 devnull return myopenpty(fd, slave);
21 75024f0f 2004-03-26 devnull }
22 75024f0f 2004-03-26 devnull
23 75024f0f 2004-03-26 devnull int
24 75024f0f 2004-03-26 devnull childpty(int fd[], char *slave)
25 75024f0f 2004-03-26 devnull {
26 75024f0f 2004-03-26 devnull int sfd;
27 75024f0f 2004-03-26 devnull
28 75024f0f 2004-03-26 devnull close(fd[1]);
29 75024f0f 2004-03-26 devnull setsid();
30 75024f0f 2004-03-26 devnull sfd = open(slave, ORDWR);
31 75024f0f 2004-03-26 devnull if(sfd < 0)
32 75024f0f 2004-03-26 devnull sysfatal("open %s: %r\n", slave);
33 75024f0f 2004-03-26 devnull if(ioctl(sfd, TIOCSCTTY, 0) < 0)
34 75024f0f 2004-03-26 devnull fprint(2, "ioctl TIOCSCTTY: %r\n");
35 75024f0f 2004-03-26 devnull return sfd;
36 75024f0f 2004-03-26 devnull }
37 75024f0f 2004-03-26 devnull
38 75024f0f 2004-03-26 devnull
39 75024f0f 2004-03-26 devnull struct winsize ows;
40 75024f0f 2004-03-26 devnull
41 75024f0f 2004-03-26 devnull void
42 75024f0f 2004-03-26 devnull updatewinsize(int row, int col, int dx, int dy)
43 75024f0f 2004-03-26 devnull {
44 75024f0f 2004-03-26 devnull struct winsize ws;
45 75024f0f 2004-03-26 devnull
46 75024f0f 2004-03-26 devnull ws.ws_row = row;
47 75024f0f 2004-03-26 devnull ws.ws_col = col;
48 75024f0f 2004-03-26 devnull ws.ws_xpixel = dx;
49 75024f0f 2004-03-26 devnull ws.ws_ypixel = dy;
50 75024f0f 2004-03-26 devnull if(ws.ws_row != ows.ws_row || ws.ws_col != ows.ws_col)
51 75024f0f 2004-03-26 devnull if(ioctl(rcfd, TIOCSWINSZ, &ws) < 0)
52 75024f0f 2004-03-26 devnull fprint(2, "ioctl: %r\n");
53 75024f0f 2004-03-26 devnull ows = ws;
54 75024f0f 2004-03-26 devnull }
55 75024f0f 2004-03-26 devnull
56 75024f0f 2004-03-26 devnull
57 75024f0f 2004-03-26 devnull
58 75024f0f 2004-03-26 devnull
59 75024f0f 2004-03-26 devnull
60 75024f0f 2004-03-26 devnull
61 75024f0f 2004-03-26 devnull
62 75024f0f 2004-03-26 devnull
63 75024f0f 2004-03-26 devnull /*-
64 75024f0f 2004-03-26 devnull * Copyright (c) 1990, 1993, 1994
65 75024f0f 2004-03-26 devnull * The Regents of the University of California. All rights reserved.
66 75024f0f 2004-03-26 devnull *
67 75024f0f 2004-03-26 devnull * Redistribution and use in source and binary forms, with or without
68 75024f0f 2004-03-26 devnull * modification, are permitted provided that the following conditions
69 75024f0f 2004-03-26 devnull * are met:
70 75024f0f 2004-03-26 devnull * 1. Redistributions of source code must retain the above copyright
71 75024f0f 2004-03-26 devnull * notice, this list of conditions and the following disclaimer.
72 75024f0f 2004-03-26 devnull * 2. Redistributions in binary form must reproduce the above copyright
73 75024f0f 2004-03-26 devnull * notice, this list of conditions and the following disclaimer in the
74 75024f0f 2004-03-26 devnull * documentation and/or other materials provided with the distribution.
75 75024f0f 2004-03-26 devnull * 3. All advertising materials mentioning features or use of this software
76 75024f0f 2004-03-26 devnull * must display the following acknowledgement:
77 75024f0f 2004-03-26 devnull * This product includes software developed by the University of
78 75024f0f 2004-03-26 devnull * California, Berkeley and its contributors.
79 75024f0f 2004-03-26 devnull * 4. Neither the name of the University nor the names of its contributors
80 75024f0f 2004-03-26 devnull * may be used to endorse or promote products derived from this software
81 75024f0f 2004-03-26 devnull * without specific prior written permission.
82 75024f0f 2004-03-26 devnull *
83 75024f0f 2004-03-26 devnull * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
84 75024f0f 2004-03-26 devnull * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85 75024f0f 2004-03-26 devnull * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86 75024f0f 2004-03-26 devnull * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
87 75024f0f 2004-03-26 devnull * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88 75024f0f 2004-03-26 devnull * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
89 75024f0f 2004-03-26 devnull * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
90 75024f0f 2004-03-26 devnull * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
91 75024f0f 2004-03-26 devnull * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
92 75024f0f 2004-03-26 devnull * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 75024f0f 2004-03-26 devnull * SUCH DAMAGE.
94 75024f0f 2004-03-26 devnull */
95 75024f0f 2004-03-26 devnull
96 75024f0f 2004-03-26 devnull int
97 75024f0f 2004-03-26 devnull myopenpty(int fd[], char *name)
98 75024f0f 2004-03-26 devnull {
99 75024f0f 2004-03-26 devnull char pty[] = "/dev/ptyXX";
100 75024f0f 2004-03-26 devnull char *s, *t;
101 75024f0f 2004-03-26 devnull struct group *gr;
102 75024f0f 2004-03-26 devnull int ttygid;
103 75024f0f 2004-03-26 devnull
104 75024f0f 2004-03-26 devnull if((gr = getgrnam("tty")) != NULL)
105 75024f0f 2004-03-26 devnull ttygid = gr->gr_gid;
106 75024f0f 2004-03-26 devnull else
107 75024f0f 2004-03-26 devnull ttygid = -1;
108 75024f0f 2004-03-26 devnull
109 75024f0f 2004-03-26 devnull for(s="pqrstuvw"; *s; s++)
110 75024f0f 2004-03-26 devnull for(t="0123456789abcdef"; *t; t++){
111 75024f0f 2004-03-26 devnull pty[5] = 'p';
112 75024f0f 2004-03-26 devnull pty[8] = *s;
113 75024f0f 2004-03-26 devnull pty[9] = *t;
114 75024f0f 2004-03-26 devnull if((fd[1] = open(pty, O_RDWR)) < 0){
115 75024f0f 2004-03-26 devnull if(errno == ENOENT)
116 75024f0f 2004-03-26 devnull return -1;
117 75024f0f 2004-03-26 devnull }else{
118 75024f0f 2004-03-26 devnull pty[5] = 't';
119 75024f0f 2004-03-26 devnull chown(pty, getuid(), ttygid);
120 75024f0f 2004-03-26 devnull chmod(pty, 620);
121 75024f0f 2004-03-26 devnull revoke(pty);
122 75024f0f 2004-03-26 devnull if((fd[0] = open(pty, O_RDWR)) < 0){
123 75024f0f 2004-03-26 devnull close(fd[1]);
124 75024f0f 2004-03-26 devnull continue;
125 75024f0f 2004-03-26 devnull }
126 75024f0f 2004-03-26 devnull if(name)
127 75024f0f 2004-03-26 devnull strcpy(name, pty);
128 75024f0f 2004-03-26 devnull return 0;
129 75024f0f 2004-03-26 devnull }
130 75024f0f 2004-03-26 devnull }
131 75024f0f 2004-03-26 devnull errno = ENOENT;
132 75024f0f 2004-03-26 devnull return -1;
133 75024f0f 2004-03-26 devnull
134 75024f0f 2004-03-26 devnull }
135 75024f0f 2004-03-26 devnull