aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/options/linux/generic/pty-stubs.cpp
blob: 8d95027c32a7e796fcc367d416353a80762c61b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include <asm/ioctls.h>
#include <bits/ensure.h>
#include <errno.h>
#include <fcntl.h>
#include <pty.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

#include <mlibc/debug.hpp>
#include <mlibc/linux-sysdeps.hpp>

int openpty(int *mfd, int *sfd, char *name, const struct termios *ios, const struct winsize *win) {
	int ptmx_fd;
	if(int e = mlibc::sys_open("/dev/ptmx", O_RDWR | O_NOCTTY, 0, &ptmx_fd); e) {
		errno = e;
		goto fail;
	}

	char spath[32];
	if(!name)
		name = spath;
	if(ptsname_r(ptmx_fd, name, 32))
		goto fail;

	int pts_fd;
	unlockpt(ptmx_fd);
	if(int e = mlibc::sys_open(name, O_RDWR | O_NOCTTY, 0, &pts_fd); e) {
		errno = e;
		goto fail;
	}

	if(ios)
		tcsetattr(ptmx_fd, TCSAFLUSH, ios);

	if(win)
		ioctl(ptmx_fd, TIOCSWINSZ, (void*)win);

	*mfd = ptmx_fd;
	*sfd = pts_fd;
	return 0;

fail:
	mlibc::sys_close(ptmx_fd);
	return -1;
}

int login_tty(int fd) {
	if(setsid() == -1)
		return -1;
	if(ioctl(fd, TIOCSCTTY, 0))
		return -1;

	MLIBC_CHECK_OR_ENOSYS(mlibc::sys_dup2, -1);
	if(int e = mlibc::sys_dup2(fd, 0, STDIN_FILENO); e) {
		errno = e;
		return -1;
	}
	if(int e = mlibc::sys_dup2(fd, 0, STDOUT_FILENO); e) {
		errno = e;
		return -1;
	}
	if(int e = mlibc::sys_dup2(fd, 0, STDERR_FILENO); e) {
		errno = e;
		return -1;
	}

	if(int e = mlibc::sys_close(fd); e) {
		errno = e;
		return -1;
	}
	return 0;
}

int forkpty(int *mfd, char *name, const struct termios *ios, const struct winsize *win) {
	int sfd;
	if(openpty(mfd, &sfd, name, ios, win))
		return -1;

	pid_t child;
	MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fork, -1);
	if(int e = mlibc::sys_fork(&child); e) {
		errno = e;
		return -1;
	}

	if(!child) {
		if(login_tty(sfd))
			mlibc::panicLogger() << "mlibc: TTY login fail in forkpty() child" << frg::endlog;
	}else{
		if(int e = mlibc::sys_close(sfd); e) {
			errno = e;
			return -1;
		}
	}

	return child;
}