1 /* $OpenBSD: test_tty.c,v 1.5 2017/02/21 15:46:25 tb 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 #include <util.h>
28
29
30 void
test_request_tty()31 test_request_tty()
32 {
33 int amaster, fd;
34 struct termios ts; /* sys/termios.h */
35 struct winsize ws; /* sys/ttycom.h */
36
37 /* get a tty */
38 if (openpty(&amaster, &fd, NULL, NULL, NULL) == -1)
39 _exit(errno);
40 close(amaster);
41
42 /* tests that need tty+proc (stdio for pledge(2) */
43 if (pledge("stdio tty proc", NULL) == -1)
44 _exit(errno);
45
46 /* TIOCSPGRP (tty+proc) */
47 if ((tcsetpgrp(fd, 1) == -1) && (errno != ENOTTY))
48 _exit(errno);
49 errno = 0; /* discard error */
50
51 /* tests that only need tty (and stdio for calling ioctl(2)) */
52 if (pledge("stdio tty", NULL) == -1)
53 _exit(errno);
54
55
56 /* TIOCGETA */
57 if (ioctl(fd, TIOCGETA, &ts) == -1)
58 _exit(errno);
59
60 /* TIOCGWINSZ */
61 if (ioctl(fd, TIOCGWINSZ, &ws) == -1)
62 _exit(errno);
63
64 /* TIOCSBRK */
65 if ((ioctl(fd, TIOCSBRK, NULL) == -1) && (errno != ENOTTY))
66 _exit(errno);
67 errno = 0; /* discard error */
68
69 /* TIOCCDTR */
70 if ((ioctl(fd, TIOCCDTR, NULL) == -1) && (errno != ENOTTY))
71 _exit(errno);
72 errno = 0; /* discard error */
73
74 /* TIOCSETA */
75 if (tcsetattr(fd, TCSANOW, &ts) == -1)
76 _exit(errno);
77
78 /* TIOCSETAW */
79 if (tcsetattr(fd, TCSADRAIN, &ts) == -1)
80 _exit(errno);
81
82 /* TIOCSETAF */
83 if (tcsetattr(fd, TCSAFLUSH, &ts) == -1)
84 _exit(errno);
85 }
86