summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/gen/pt.c
blob: 3372f7133c820b3cfb871bb6d6843c4f2cb9f92f (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved	*/

#pragma weak _ptsname = ptsname
#pragma weak _grantpt = grantpt
#pragma weak _unlockpt = unlockpt

#include "lint.h"
#include "libc.h"
#include "mtlib.h"
#include <sys/types.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/mkdev.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ptms.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <spawn.h>
#include <grp.h>
#include "tsd.h"

#define	PTSNAME "/dev/pts/"		/* slave name */
#define	PTLEN   32			/* slave name length */
#define	DEFAULT_TTY_GROUP	"tty"	/* slave device group owner */

static void itoa(int, char *);

/*
 *  Check that fd argument is a file descriptor of an opened master.
 *  Do this by sending an ISPTM ioctl message down stream. Ioctl()
 *  will fail if:(1) fd is not a valid file descriptor.(2) the file
 *  represented by fd does not understand ISPTM(not a master device).
 *  If we have a valid master, get its minor number via fstat().
 *  Concatenate it to PTSNAME and return it as the name of the slave
 *  device.
 */
static dev_t
ptsdev(int fd)
{
	struct stat64 status;
	struct strioctl istr;

	istr.ic_cmd = ISPTM;
	istr.ic_len = 0;
	istr.ic_timout = 0;
	istr.ic_dp = NULL;

	if (ioctl(fd, I_STR, &istr) < 0 || fstat64(fd, &status) < 0)
		return (NODEV);

	return (minor(status.st_rdev));
}

char *
ptsname(int fd)
{
	dev_t dev;
	char *sname;

	if ((dev = ptsdev(fd)) == NODEV)
		return (NULL);

	sname = tsdalloc(_T_PTSNAME, PTLEN, NULL);
	if (sname == NULL)
		return (NULL);
	(void) strcpy(sname, PTSNAME);
	itoa(dev, sname + strlen(PTSNAME));

	/*
	 * This lookup will create the /dev/pts node (if the corresponding
	 * pty exists.
	 */
	if (access(sname, F_OK) ==  0)
		return (sname);

	return (NULL);
}

/*
 * Send an ioctl down to the master device requesting the
 * master/slave pair be unlocked.
 */
int
unlockpt(int fd)
{
	struct strioctl istr;

	istr.ic_cmd = UNLKPT;
	istr.ic_len = 0;
	istr.ic_timout = 0;
	istr.ic_dp = NULL;

	if (ioctl(fd, I_STR, &istr) < 0)
		return (-1);

	return (0);
}

/*
 * XPG4v2 requires that open of a slave pseudo terminal device
 * provides the process with an interface that is identical to
 * the terminal interface.
 *
 * To satisfy this, in strict XPG4v2 mode, this routine also sends
 * a message down the stream that sets a flag in the kernel module
 * so that additional actions are performed when opening an
 * associated slave PTY device. When this happens, modules are
 * automatically pushed onto the stream to provide terminal
 * semantics and those modules are then informed that they should
 * behave in strict XPG4v2 mode which modifies their behaviour. In
 * particular, in strict XPG4v2 mode, empty blocks will be sent up
 * the master side of the stream rather than being suppressed.
 *
 * Most applications do not expect this behaviour so it is only
 * enabled for programs compiled in strict XPG4v2 mode (see
 * stdlib.h).
 */
int
__unlockpt_xpg4(int fd)
{
	int ret;

	if ((ret = unlockpt(fd)) == 0) {
		struct strioctl istr;

		istr.ic_cmd = PTSSTTY;
		istr.ic_len = 0;
		istr.ic_timout = 0;
		istr.ic_dp = NULL;

		if (ioctl(fd, I_STR, &istr) < 0)
			ret = -1;
	}

	return (ret);
}

int
grantpt(int fd)
{
	struct strioctl istr;
	pt_own_t pto;
	struct group *gr_name;

	/* validate the file descriptor before proceeding */
	if (ptsdev(fd) == NODEV)
		return (-1);

	pto.pto_ruid = getuid();

	gr_name = getgrnam(DEFAULT_TTY_GROUP);
	if (gr_name)
		pto.pto_rgid = gr_name->gr_gid;
	else
		pto.pto_rgid = getgid();

	istr.ic_cmd = OWNERPT;
	istr.ic_len = sizeof (pt_own_t);
	istr.ic_timout = 0;
	istr.ic_dp = (char *)&pto;

	if (ioctl(fd, I_STR, &istr) != 0) {
		errno = EACCES;
		return (-1);
	}

	return (0);
}

/*
 * Send an ioctl down to the master device requesting the master/slave pair
 * be assigned to the given zone.
 */
int
zonept(int fd, zoneid_t zoneid)
{
	struct strioctl istr;

	istr.ic_cmd = ZONEPT;
	istr.ic_len = sizeof (zoneid);
	istr.ic_timout = 0;
	istr.ic_dp = (char *)&zoneid;

	if (ioctl(fd, I_STR, &istr) != 0) {
		return (-1);
	}
	return (0);
}


static void
itoa(int i, char *ptr)
{
	int dig = 0;
	int tempi;

	tempi = i;
	do {
		dig++;
		tempi /= 10;
	} while (tempi);

	ptr += dig;
	*ptr = '\0';
	while (--dig >= 0) {
		*(--ptr) = i % 10 + '0';
		i /= 10;
	}
}


/*
 * added for SUSv3 standard
 *
 * Open a pseudo-terminal device.  External interface.
 */

int
posix_openpt(int oflag)
{
	return (open("/dev/ptmx", oflag));
}