4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #pragma weak _ptsname = ptsname
33 #pragma weak _grantpt = grantpt
34 #pragma weak _unlockpt = unlockpt
39 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/mkdev.h>
43 #include <sys/stream.h>
44 #include <sys/stropts.h>
46 #include <sys/signal.h>
59 #define PTSNAME "/dev/pts/" /* slave name */
60 #define PTLEN 32 /* slave name length */
61 #define DEFAULT_TTY_GROUP "tty" /* slave device group owner */
63 static void itoa(int, char *);
66 * Check that fd argument is a file descriptor of an opened master.
67 * Do this by sending an ISPTM ioctl message down stream. Ioctl()
68 * will fail if:(1) fd is not a valid file descriptor.(2) the file
69 * represented by fd does not understand ISPTM(not a master device).
70 * If we have a valid master, get its minor number via fstat().
71 * Concatenate it to PTSNAME and return it as the name of the slave
85 if (ioctl(fd
, I_STR
, &istr
) < 0 || fstat64(fd
, &status
) < 0)
88 return (minor(status
.st_rdev
));
97 if ((dev
= ptsdev(fd
)) == NODEV
)
100 sname
= tsdalloc(_T_PTSNAME
, PTLEN
, NULL
);
103 (void) strcpy(sname
, PTSNAME
);
104 itoa(dev
, sname
+ strlen(PTSNAME
));
107 * This lookup will create the /dev/pts node (if the corresponding
110 if (access(sname
, F_OK
) == 0)
117 * Send an ioctl down to the master device requesting the
118 * master/slave pair be unlocked.
123 struct strioctl istr
;
125 istr
.ic_cmd
= UNLKPT
;
130 if (ioctl(fd
, I_STR
, &istr
) < 0)
139 struct strioctl istr
;
141 struct group
*gr_name
;
143 /* validate the file descriptor before proceeding */
144 if (ptsdev(fd
) == NODEV
)
147 pto
.pto_ruid
= getuid();
149 gr_name
= getgrnam(DEFAULT_TTY_GROUP
);
151 pto
.pto_rgid
= gr_name
->gr_gid
;
153 pto
.pto_rgid
= getgid();
155 istr
.ic_cmd
= OWNERPT
;
156 istr
.ic_len
= sizeof (pt_own_t
);
158 istr
.ic_dp
= (char *)&pto
;
160 if (ioctl(fd
, I_STR
, &istr
) != 0) {
169 * Send an ioctl down to the master device requesting the master/slave pair
170 * be assigned to the given zone.
173 zonept(int fd
, zoneid_t zoneid
)
175 struct strioctl istr
;
177 istr
.ic_cmd
= ZONEPT
;
178 istr
.ic_len
= sizeof (zoneid
);
180 istr
.ic_dp
= (char *)&zoneid
;
182 if (ioctl(fd
, I_STR
, &istr
) != 0) {
190 itoa(int i
, char *ptr
)
204 *(--ptr
) = i
% 10 + '0';
211 * added for SUSv3 standard
213 * Open a pseudo-terminal device. External interface.
217 posix_openpt(int oflag
)
219 return (open("/dev/ptmx", oflag
));