Annotation of libaitio/src/pty.c, revision 1.1.2.7
1.1.2.1 misho 1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.1.2.7 ! misho 6: * $Id: pty.c,v 1.1.2.6 2011/09/21 13:25:19 misho Exp $
1.1.2.1 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
49: /*
1.1.2.3 misho 50: * ioAllocPTY() Allocate new PTY and TTY
1.1.2.1 misho 51: * @ptyfd = master fd, pty
52: * @ttyfd = slave fd, tty
53: * @name = tty device name if not null
54: * @namesiz = name length, must be above 63 bytes.
55: * @term = termios for terminal
56: * @winz = winsize for terminal
57: * return: -1 error or 0 ok
58: */
59: inline int
60: ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz,
61: struct termios * __restrict term, struct winsize * __restrict winz)
62: {
63: assert(ptyfd && ttyfd);
64: if (!ptyfd || !ttyfd || (name && namesiz < 64)) {
65: io_SetErr(EINVAL, "Error:: invalid arguments ...");
66: return -1;
67: }
68:
69: memset(name, 0, namesiz);
70: if (openpty(ptyfd, ttyfd, name, term, winz) == -1) {
71: LOGERR;
72: return -1;
73: }
74:
75: return 0;
76: }
77:
78: /*
1.1.2.7 ! misho 79: * ioFreeTTY() Release PTY and TTY device
! 80: * @ptyfd = master fd, pty
1.1.2.1 misho 81: * @ttyname = tty filename
82: * return: none
83: */
84: inline void
1.1.2.7 ! misho 85: ioFreeTTY(int ptyfd, const char *ttyname)
1.1.2.1 misho 86: {
87: assert(ttyname);
88: if (!ttyname)
89: return;
90:
1.1.2.7 ! misho 91: close(ptyfd);
1.1.2.1 misho 92: chown(ttyname, (uid_t) 0, (gid_t) 0);
93: chmod(ttyname, (mode_t) 0666);
94: }
1.1.2.2 misho 95:
96: /*
97: * ioChgWinPTY() Change window size of PTY
98: * @ptyfd = master fd, pty
99: * @row = row
100: * @col = col
101: * @xpxl = x pixels
102: * @ypxl = y pixels
103: * return: -1 error or 0 ok
104: */
105: inline int
106: ioChgWinPTY(int ptyfd, u_short row, u_short col, u_short xpxl, u_short ypxl)
107: {
108: struct winsize w;
109:
110: w.ws_row = row;
111: w.ws_col = col;
112: w.ws_xpixel = xpxl;
113: w.ws_ypixel = ypxl;
114:
115: if (ioctl(ptyfd, TIOCSWINSZ, &w) == -1) {
116: LOGERR;
117: return -1;
118: }
119:
120: return 0;
121: }
122:
123: /*
1.1.2.3 misho 124: * ioSetOwnerTTY() Set owner to TTY
1.1.2.2 misho 125: * @ttyname = tty filename
126: * @UID = uid
127: * @GID = gid
128: * return: -1 error or 0 ok
129: */
130: int
1.1.2.3 misho 131: ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID)
1.1.2.2 misho 132: {
133: struct group *grp;
134: gid_t gid;
135: mode_t mode;
136: struct stat st;
137:
138: assert(ttyname);
139: if (!ttyname) {
140: io_SetErr(EINVAL, "Error:: invalid arguments ...");
141: return -1;
142: }
143:
144: grp = getgrnam("tty");
145: if (!grp) {
146: gid = GID;
147: mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
148: } else {
149: gid = grp->gr_gid;
150: mode = S_IRUSR | S_IWUSR | S_IWGRP;
151: }
152:
153: if (stat(ttyname, &st) == -1) {
154: LOGERR;
155: return -1;
156: }
157:
158: if (st.st_uid != UID || st.st_gid != gid)
159: if (chown(ttyname, UID, gid) == -1) {
160: LOGERR;
161: return -1;
162: }
163: if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode)
164: if (chmod(ttyname, mode) == -1) {
165: LOGERR;
166: return -1;
167: }
168:
169: return 0;
170: }
1.1.2.3 misho 171:
172: /*
173: * ioSetSidTTY() Makes the process's controlling TTY and sets it to sane modes.
174: * @ttyfd = slave fd, tty
175: * @ttyname = tty filename
176: * return: -1 error or 0 ok
177: */
178: int
179: ioSetSidTTY(int *ttyfd, const char *ttyname)
180: {
181: int fd;
182:
183: /* First disconnect from the old controlling tty. */
184: #ifdef TIOCNOTTY
185: fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
186: if (fd >= 0) {
187: ioctl(fd, TIOCNOTTY, NULL);
188: close(fd);
189: }
190: #endif
191: setsid();
192:
193: /* Verify that we are successfully disconnected from the controlling tty. */
194: fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
195: if (fd >= 0) {
196: io_SetErr(ENXIO, "Error:: Failed to disconnect from controlling tty.");
197: close(fd);
198: return -1;
199: }
200: /* Make it our controlling tty. */
201: #ifdef TIOCSCTTY
202: if (ioctl(*ttyfd, TIOCSCTTY, NULL) == -1) {
203: LOGERR;
204: return -1;
205: }
206: #endif
207: fd = open(ttyname, O_RDWR);
208: if (fd == -1) {
209: LOGERR;
210: return -1;
211: } else
212: close(fd);
213:
214: /* Verify that we now have a controlling tty. */
215: fd = open(_PATH_TTY, O_WRONLY);
216: if (fd == -1) {
217: LOGERR;
218: return -1;
219: } else
220: close(fd);
221:
1.1.2.5 misho 222: /* redirect standart handles to tty */
223: dup2(*ttyfd, STDIN_FILENO);
224: dup2(*ttyfd, STDOUT_FILENO);
225: dup2(*ttyfd, STDERR_FILENO);
226: if (*ttyfd > 2)
227: close(*ttyfd);
228:
229: /* NOW TTY IS READY! */
1.1.2.3 misho 230: return 0;
231: }
1.1.2.4 misho 232:
233: /*
234: * ioSetRAWMode() Enter into RAW mode
235: * @fd = tty fd
236: * @otio = saved old termios for later restore if !=NULL
237: * return: -1 error or 0 ok
238: */
239: inline int
240: ioSetRAWMode(int fd, struct termios *otio)
241: {
242: struct termios tio;
243:
244: if (tcgetattr(fd, &tio) == -1) {
245: LOGERR;
246: return -1;
247: }
248: if (otio)
249: *otio = tio;
250:
251: tio.c_iflag |= IGNPAR;
252: tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
253: #ifdef IUCLC
254: tio.c_iflag &= ~IUCLC;
255: #endif
256: tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
257: #ifdef IEXTEN
258: tio.c_lflag &= ~IEXTEN;
259: #endif
260: tio.c_oflag &= ~OPOST;
261: tio.c_cc[VMIN] = 1;
262: tio.c_cc[VTIME] = 0;
263:
264: if (tcsetattr(fd, TCSADRAIN, &tio) == -1) {
265: LOGERR;
266: return -1;
267: }
268:
269: return 0;
270: }
271:
272: /*
273: * ioRestoreMode() Restore termios to tty fd
274: * @fd = tty fd
275: * @tio = termios structure for restore
276: * return: -1 error or 0 ok
277: */
278: inline int
279: ioRestoreMode(int fd, struct termios tio)
280: {
281: if (tcsetattr(fd, TCSADRAIN, &tio) == -1) {
282: LOGERR;
283: return -1;
284: }
285:
286: return 0;
287: }
1.1.2.6 misho 288:
289: /*
290: * ioForkPTY() Fork new process with session leader and new TTY
291: * @ptyfd = master fd, pty
292: * @name = tty device name if not null
293: * @namesiz = name length, must be above 63 bytes.
294: * @term = termios for terminal
295: * @winz = winsize for terminal
296: * @otio = old termios structure for restore
297: * return: -1 error, 0 child process or >0 parent: pid of child
298: */
299: pid_t
300: ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term,
301: struct winsize * __restrict winz, struct termios * __restrict otio)
302: {
303: int ttyfd;
304: pid_t pid;
305:
306: if (ioAllocPTY(ptyfd, &ttyfd, name, namesiz, term, winz))
307: return -1;
308:
309: switch ((pid = fork())) {
310: case -1:
311: LOGERR;
312: return -1;
313: case 0:
314: if (ioSetOwnerTTY(name, getuid(), getgid()) == -1) {
1.1.2.7 ! misho 315: ioFreeTTY(*ptyfd, name);
1.1.2.6 misho 316: return -1;
317: }
318: if (ioSetSidTTY(&ttyfd, name) == -1) {
1.1.2.7 ! misho 319: ioFreeTTY(*ptyfd, name);
1.1.2.6 misho 320: return -1;
321: }
1.1.2.7 ! misho 322: close(*ptyfd);
1.1.2.6 misho 323:
324: /* CHILD */
325: break;
326: default:
327: close(ttyfd);
328:
329: ioSetRAWMode(*ptyfd, otio);
330:
331: /* PARENT */
332: break;
333: }
334:
335: return pid;
336: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>