xref: /illumos-gate/usr/src/test/libc-tests/tests/common/openpty.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2022 Oxide Computer Company
14  */
15 
16 #include <sys/types.h>
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <err.h>
21 #include <stdbool.h>
22 #include <sys/stropts.h>
23 
24 /*
25  * This is named this way with the hope that it'll be replaced someday by
26  * openpty.
27  */
28 bool
29 openpty(int *mfdp, int *sfdp)
30 {
31 	int sfd;
32 	int mfd = posix_openpt(O_RDWR | O_NOCTTY);
33 	const char *name;
34 
35 	if (mfd < 0) {
36 		warn("failed to open a pseudo-terminal");
37 		return (false);
38 	}
39 
40 	if (grantpt(mfd) != 0 || unlockpt(mfd) != 0) {
41 		warn("failed to grant and unlock the manager fd");
42 		(void) close(mfd);
43 		return (false);
44 	}
45 
46 	name = ptsname(mfd);
47 	if (name == NULL) {
48 		warnx("failed to get ptsname for fd %d", mfd);
49 		(void) close(mfd);
50 		return (false);
51 	}
52 
53 	sfd = open(name, O_RDWR | O_NOCTTY);
54 	if (sfd < 0) {
55 		warn("failed to open pty %s", name);
56 		(void) close(mfd);
57 		return (false);
58 	}
59 
60 	if (ioctl(sfd, __I_PUSH_NOCTTY, "ptem") < 0 ||
61 	    ioctl(sfd, __I_PUSH_NOCTTY, "ldterm") < 0) {
62 		warn("failed to push streams modules");
63 		(void) close(mfd);
64 		(void) close(sfd);
65 	}
66 
67 	*sfdp = sfd;
68 	*mfdp = mfd;
69 	return (true);
70 }
71