2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 #ifdef VERBOSE_REV_INFO
25 static char rcs_id[] = "$TOG: TermPrimGetPty-clone.c /main/7 1998/04/03 17:11:08 mgreess $";
26 #endif /* VERBOSE_REV_INFO */
30 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
31 * (c) Copyright 1993, 1994 International Business Machines Corp. *
32 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
33 * (c) Copyright 1993, 1994 Novell, Inc. *
36 #include "TermPrimDebug.h"
37 #include "TermHeader.h"
43 #ifdef HP_ARCHITECTURE
44 # define X_INCLUDE_GRP_H
45 #endif /* HP_ARCHITECTURE */
46 #define X_INCLUDE_UNISTD_H
47 #define XOS_USE_XT_LOCKING
48 #include <X11/Xos_r.h>
51 # define PTY_CLONE_DEVICE "/dev/ptc"
52 #elif defined(__linux__)
53 # define PTY_CLONE_DEVICE "/dev/ptyc"
57 GetPty(char **ptySlave, char **ptyMaster)
63 *ptyMaster = malloc(strlen(PTY_CLONE_DEVICE) + 1);
64 (void) strcpy(*ptyMaster, PTY_CLONE_DEVICE);
66 if (isDebugFSet('p', 10)) {
73 if ((ptyFd = open(*ptyMaster, O_RDWR, 0))) {
74 _Xttynameparams tty_buf;
75 #if defined(__linux__)
76 if (c = _XTtyname(ptyFd)) {
78 if (c = _XTtyname(ptyFd, tty_buf)) {
80 *ptySlave = malloc(strlen(c) + 1);
81 (void) strcpy(*ptySlave, c);
83 if ((ttyFd = open(*ptySlave, O_RDWR | O_NOCTTY, 0)) < 0) {
85 (void) perror(*ptySlave);
90 /* close off the pty slave... */
93 /* fix the owner, mode, and group... */
94 #ifdef HP_ARCHITECTURE
98 _Xgetgrparams grp_buf;
100 if (grp = _XGetgrnam("tty", grp_buf)) {
106 (void) chown(*ptySlave, getuid(), gid);
107 (void) chmod(*ptySlave, 0620);
109 #else /* HP_ARCHITECTURE */
110 (void) chown(*ptySlave, getuid(), getgid());
111 (void) chmod(*ptySlave, 0622);
112 #endif /* HP_ARCHITECTURE */
114 /* pty master and slave names are already set. Return
115 * the file descriptor...
121 /* ttyname on the pty master failed. This should not happen!... */
122 (void) perror("ttyname");
126 (void) perror(*ptyMaster);
131 /* this is a public wrapper around the previous function that runs the
132 * previous function setuid root...
135 _DtTermPrimGetPty(char **ptySlave, char **ptyMaster)
139 /* this function needs to be suid root... */
140 (void) _DtTermPrimToggleSuidRoot(True);
141 retValue = GetPty(ptySlave, ptyMaster);
142 /* we now need to turn off setuid root... */
143 (void) _DtTermPrimToggleSuidRoot(False);
149 SetupPty(char *ptySlave, int ptyFd)
151 #ifdef HP_ARCHITECTURE
155 _Xgetgrparams grp_buf;
157 if (grp = _XGetgrnam("tty", grp_buf)) {
163 (void) chown(ptySlave, getuid(), gid);
164 (void) chmod(ptySlave, 0620);
166 #else /* HP_ARCHITECTURE */
167 (void) chown(ptySlave, getuid(), getgid());
168 (void) chmod(ptySlave, 0622);
169 #endif /* HP_ARCHITECTURE */
173 _DtTermPrimSetupPty(char *ptySlave, int ptyFd)
177 /* this function needs to be suid root... */
178 (void) _DtTermPrimToggleSuidRoot(True);
179 retValue = SetupPty(ptySlave, ptyFd);
180 /* we now need to turn off setuid root... */
181 (void) _DtTermPrimToggleSuidRoot(False);
187 ReleasePty(char *ptySlave)
189 (void) chown(ptySlave, 0, 0);
190 (void) chmod(ptySlave, 0666);
194 _DtTermPrimReleasePty(char *ptySlave)
196 /* this function needs to be suid root... */
197 (void) _DtTermPrimToggleSuidRoot(True);
198 (void) ReleasePty(ptySlave);
199 /* we now need to turn off setuid root... */
200 (void) _DtTermPrimToggleSuidRoot(False);
204 _DtTermPrimPtyCleanup()