2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <os/assumes.h>
25 #include <os/once_private.h>
29 #include <sys/ioctl.h>
35 * posix_openpt call for cloning pty implementation.
40 posix_openpt(int flags
)
42 int fd
= open("/dev/ptmx", flags
);
49 * grantpt call for cloning pty implementation.
51 * Change UID and GID of slave pty associated with master pty whose
52 * fd is provided, to the real UID and real GID of the calling thread.
57 return ioctl(fd
, TIOCPTYGRANT
);
60 // defined by TIOCPTYGNAME
61 #define PTSNAME_MAX_SIZE 128
63 static pthread_key_t ptsname_buffer_specific_key
;
64 static os_once_t ptsname_once
;
67 ptsname_once_init(void *ctx __unused
)
69 int ret
= pthread_key_create(&ptsname_buffer_specific_key
, free
);
74 * ptsname call for cloning pty implementation.
76 * NOTE: Returns a pointer to a static buffer, which will be overwritten on
82 os_once(&ptsname_once
, NULL
, ptsname_once_init
);
83 char *ptsnamebuf
= pthread_getspecific(ptsname_buffer_specific_key
);
85 if (ptsnamebuf
== NULL
) {
86 ptsnamebuf
= malloc(PTSNAME_MAX_SIZE
);
87 os_assert(ptsnamebuf
);
89 int error
= pthread_setspecific(ptsname_buffer_specific_key
, ptsnamebuf
);
90 os_assert_zero(error
);
93 int error
= ptsname_r(fd
, ptsnamebuf
, PTSNAME_MAX_SIZE
);
95 return error
? NULL
: ptsnamebuf
;
99 ptsname_r(int fd
, char *buffer
, size_t buflen
)
103 char ptsnamebuf
[PTSNAME_MAX_SIZE
];
110 error
= ioctl(fd
, TIOCPTYGNAME
, ptsnamebuf
);
118 * POSIX: Handle device rename test case, which is expected
119 * to fail if the pty has been renamed.
121 error
= stat(ptsnamebuf
, &sbuf
);
126 size_t len
= strlen(ptsnamebuf
) + 1;
132 memcpy(buffer
, ptsnamebuf
, len
);
138 * unlockpt call for cloning pty implementation.
140 * Unlock the slave pty associated with the master to which fd refers.
145 return ioctl(fd
, TIOCPTYUNLK
);