summaryrefslogtreecommitdiff
path: root/regress/sys/kern/ptmget/ptmget.c
blob: 24fb09b730fc91921358a5ad477c4e364e4d8172 (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
/*	$OpenBSD: ptmget.c,v 1.2 2004/12/22 00:54:39 david Exp $ */
/*
 *	Written by Bob Beck <beck@openbsd.org> 2004 Public Domain.
 *	Basic test to ensure /dev/ptm works, and what it returns
 * 	can be used via tty(4);
 */
#include <err.h>
#include <fcntl.h>
#include <grp.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/types.h>
#include <sys/stat.h>

int
main(int argc, char *argv[])
{
	int fd;
	struct ptmget ptm;
	struct termios ti;
	struct stat sb;
	struct group *gr;
	gid_t ttygid;

	if ((gr = getgrnam("tty")) != NULL)
		ttygid = gr->gr_gid;
	else
		ttygid = 4;
	fd = open("/dev/ptm", O_RDWR, 0);
	if (fd == -1)
		err(1, "Can't open /dev/ptm");
	if ((ioctl(fd, PTMGET, &ptm) == -1))
		err(1, "ioctl PTMGET failed");
	if ((tcgetattr(ptm.sfd, &ti) == -1))
		err(1, "tcgetattr failed on slave");
	if ((tcgetattr(ptm.cfd, &ti) == -1))
		err(1, "tcgetattr failed on master");
	if ((ioctl(ptm.sfd, TIOCSTOP) == -1))
		err(1, "ioctl TIOCSTOP failed on slave");
	if ((ioctl(ptm.cfd, TIOCSTOP) == -1))
		err(1, "ioctl TIOCSTOP failed on master");
	bzero(&sb, sizeof(sb));
	if ((stat(ptm.sn, &sb) == -1))
		err(1, "can't stat slave %s", ptm.sn);
	if (sb.st_mode != (S_IFCHR | S_IWUSR | S_IRUSR | S_IWGRP))
		errx(1, "Bad mode %o on %s, should be %o", sb.st_mode,
		     ptm.sn, (S_IFCHR | S_IWUSR | S_IRUSR | S_IWGRP));
	if (sb.st_gid != ttygid)
		errx(1, "%s gid is %d not the tty group(%d)", ptm.sn,
		     sb.st_gid, ttygid);
	if (sb.st_uid != geteuid())
		errx(1, "%s owned by %d, not the current user (%d)", ptm.sn,
		     sb.st_uid, geteuid());
	return(0);
}