Annotation of src/usr.bin/tmux/pty.c, Revision 1.2
1.2 ! nicm 1: /* $OpenBSD: pty.c,v 1.1 2017/01/23 10:09:43 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20: #include <sys/ioctl.h>
21: #include <sys/time.h>
22: #include <sys/tty.h>
23:
24: #include <fcntl.h>
25: #include <string.h>
26: #include <termios.h>
27: #include <unistd.h>
28: #include <util.h>
29:
30: int pty_open(int *);
31: pid_t pty_fork(int, int *, char *, size_t, struct winsize *);
32:
33: int
34: pty_open(int *fd)
35: {
36: *fd = open(PATH_PTMDEV, O_RDWR|O_CLOEXEC);
37: if (*fd < 0)
38: return (-1);
39: return (0);
40: }
41:
42: pid_t
43: pty_fork(int ptmfd, int *fd, char *name, size_t namelen, struct winsize *ws)
44: {
45: struct ptmget ptm;
46: pid_t pid;
47:
1.2 ! nicm 48: if (ioctl(ptmfd, PTMGET, &ptm) == -1)
1.1 nicm 49: return (-1);
50:
51: strlcpy(name, ptm.sn, namelen);
52: ioctl(ptm.sfd, TIOCSWINSZ, ws);
53:
54: switch (pid = fork()) {
55: case -1:
56: close(ptm.cfd);
57: close(ptm.sfd);
58: return (-1);
59: case 0:
60: close(ptm.cfd);
61: login_tty(ptm.sfd);
62: return (0);
63: }
64: *fd = ptm.cfd;
65: close(ptm.sfd);
66: return (pid);
67: }