xref: /openbsd-src/regress/sys/kern/pledge/generic/test_tty.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: test_tty.c,v 1.4 2015/10/30 07:24:20 semarie Exp $ */
2 /*
3  * Copyright (c) 2015 Sebastien Marie <semarie@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/ioctl.h>
19 #include <sys/termios.h>
20 #include <sys/ttycom.h>
21 
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 
29 void
30 test_request_tty()
31 {
32 	int fd = STDERR_FILENO;
33 	struct termios ts; /* sys/termios.h */
34 	struct winsize ws; /* sys/ttycom.h */
35 
36 	/* TODO: get a tty */
37 
38 	/* tests that need tty+proc (stdio for pledge(2) */
39 	if (pledge("stdio tty proc", NULL) == -1)
40 		_exit(errno);
41 
42 	/* TIOCSPGRP (tty+proc) */
43 	if ((tcsetpgrp(fd, 1) == -1) && (errno != ENOTTY))
44 		_exit(errno);
45 	errno = 0; /* discard error */
46 
47 	/* tests that only need tty (and stdio for calling ioctl(2)) */
48 	if (pledge("stdio tty", NULL) == -1)
49 		_exit(errno);
50 
51 
52 	/* TIOCGETA */
53 	if (ioctl(fd, TIOCGETA, &ts) == -1)
54 		_exit(errno);
55 
56 	/* TIOCGWINSZ */
57 	if (ioctl(fd, TIOCGWINSZ, &ws) == -1)
58 		_exit(errno);
59 
60 	/* TIOCSBRK */
61 	if ((ioctl(fd, TIOCSBRK, NULL) == -1) && (errno != ENOTTY))
62 		_exit(errno);
63 	errno = 0; /* discard error */
64 
65 	/* TIOCCDTR */
66 	if ((ioctl(fd, TIOCCDTR, NULL) == -1) && (errno != ENOTTY))
67 		_exit(errno);
68 	errno = 0; /* discard error */
69 
70 	/* TIOCSETA */
71 	if (tcsetattr(fd, TCSANOW, &ts) == -1)
72 		_exit(errno);
73 
74 	/* TIOCSETAW */
75 	if (tcsetattr(fd, TCSADRAIN, &ts) == -1)
76 		_exit(errno);
77 
78 	/* TIOCSETAF */
79 	if (tcsetattr(fd, TCSAFLUSH, &ts) == -1)
80 		_exit(errno);
81 }
82