summaryrefslogtreecommitdiff
path: root/regress/sys/kern/pledge/generic/pty.c
blob: 3651713b64061b52b405979a645255d4516dc553 (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
102
103
/*	$OpenBSD: pty.c,v 1.1 2024/06/03 08:02:22 anton Exp $	*/

#include <sys/ioctl.h>

#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#include "pty.h"

int
pty_open(struct pty *pty)
{
	int master, slave;

	master = posix_openpt(O_RDWR);
	if (master == -1) {
		warn("posix_openpt");
		return 1;
	}
	if (grantpt(master) == -1) {
		warn("grantpt");
		return 1;
	}
	if (unlockpt(master) == -1) {
		warn("unlockpt");
		return 1;
	}

	slave = open(ptsname(master), O_RDWR);
	if (slave == -1) {
		warn("%s", ptsname(master));
		return 1;
	}

	pty->master = master;
	pty->slave = slave;
	return 0;
}

void
pty_close(struct pty *pty)
{
	close(pty->slave);
	close(pty->master);
}

/*
 * Disconnect the controlling tty, if present.
 */
int
pty_detach(struct pty *pty)
{
	int fd;

	fd = open("/dev/tty", O_RDWR | O_NOCTTY);
	if (fd >= 0) {
		(void)ioctl(fd, TIOCNOTTY, NULL);
		close(fd);
	}
	return 0;
}

/*
 * Connect the slave as the controlling tty.
 */
int
pty_attach(struct pty *pty)
{
	if (ioctl(pty->slave, TIOCSCTTY, NULL) == -1) {
		warn("TIOCSCTTY");
		return 1;
	}
	return 0;
}

int
pty_drain(struct pty *pty)
{
	for (;;) {
		char *buf = &pty->buf.storage[pty->buf.len];
		size_t bufsize = sizeof(pty->buf.storage) - pty->buf.len;
		ssize_t n;

		n = read(pty->master, buf, bufsize);
		if (n == -1) {
			warn("read");
			return 1;
		}
		if (n == 0)
			break;

		/* Ensure space for NUL-terminator. */
		if ((size_t)n >= bufsize) {
			warnx("pty buffer too small");
			return 1;
		}
		pty->buf.len += n;
	}

	return 0;
}