Debian Codegraph @

lib/kpty.cpp

raw symbols SCIP
1 /*
2
3 This file is part of the KDE libraries
4 Copyright (C) 2002 Waldo Bastian <[email protected]>
5 Copyright (C) 2002-2003,2007 Oswald Buddenhagen <[email protected]>
6
7 Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
23 */
24
25 #include "kpty_p.h"cxx . . $ `<file>/lib/kpty_p.h`/
26
27 #include <QtDebug>
28
29 #if defined(__FreeBSD__) || defined(__DragonFly__)
30 #define HAVE_LOGIN
31 #define HAVE_LIBUTIL_H
32 #endif
33
34 #if defined(__OpenBSD__)
35 #define HAVE_LOGIN
36 #define HAVE_UTIL_H
37 #endif
38
39 #if defined(__NetBSD__)
40 #define HAVE_LOGIN
41 #define HAVE_UTIL_H
42 #define HAVE_OPENPTY
43 #endif
44
45 #if defined(__APPLE__)
46 #define HAVE_OPENPTY
47 #define HAVE_UTIL_H
48 #endif
49
50 #ifdef __sgi
51 #define __svr4__
52 #endif
53
54 #ifdef __osf__
55 #define _OSF_SOURCE
56 #include <float.h>
57 #endif
58
59 #ifdef _AIX
60 #define _ALL_SOURCE
61 #endif
62
63 // __USE_XOPEN isn't defined by default in ICC
64 // (needed for ptsname(), grantpt() and unlockpt())
65 #ifdef __INTEL_COMPILER
66 # ifndef __USE_XOPEN
67 # define __USE_XOPEN
68 # endif
69 #endif
70
71 #include <sys/types.h>
72 #include <sys/ioctl.h>
73 #include <sys/time.h>
74 #include <sys/resource.h>
75 #include <sys/stat.h>
76 #include <sys/param.h>
77
78 #include <cerrno>
79 #include <fcntl.h>
80 #include <ctime>
81 #include <cstdlib>
82 #include <cstdio>
83 #include <cstring>
84 #include <unistd.h>
85 #include <grp.h>
86
87 #if defined(HAVE_PTY_H)
88 # include <pty.h>
89 #endif
90
91 #ifdef HAVE_LIBUTIL_H
92 # include <libutil.h>
93 #elif defined(HAVE_UTIL_H)
94 # include <util.h>
95 #endif
96
97 #ifdef HAVE_UTEMPTER
98 extern "C" {
99 # include <utempter.h>
100 }
101 #else
102 # include <utmp.h>
103 # ifdef HAVE_UTMPX
104 # include <utmpx.h>
105 # endif
106 # if !defined(_PATH_UTMPX) && defined(_UTMPX_FILE)
107 # define _PATH_UTMPX _UTMPX_FILE
108 # endif
109 # ifdef HAVE_UPDWTMPX
110 # if !defined(_PATH_WTMPX) && defined(_WTMPX_FILE)
111 # define _PATH_WTMPX _WTMPX_FILE
112 # endif
113 # endif
114 #endif
115
116 /* for HP-UX (some versions) the extern C is needed, and for other
117 platforms it doesn't hurt */
118 extern "C" {
119 #include <termios.h>
120 #if defined(HAVE_TERMIO_H)
121 # include <termio.h> // struct winsize on some systems
122 #endif
123 }
124
125 #if defined (_HPUX_SOURCE)
126 # define _TERMIOS_INCLUDED
127 # include <bsdtty.h>
128 #endif
129
130 #ifdef HAVE_SYS_STROPTS_H
131 # include <sys/stropts.h> // Defines I_PUSH
132 # define _NEW_TTY_CTRL
133 #endif
134
135 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
136 # define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
137 #else
138 # if defined(_HPUX_SOURCE) || defined(__Lynx__) || defined (__CYGWIN__) || defined(__GNU__)
139 # define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
140 # else
141 # define _tcgetattrcxx . . $ `lib/kpty.cpp:141:11`!(fd, ttmode) ioctl(fd, TCGETScxx . . $ `<external>/123502480044527469/ioctls.h:20:9`!, (char *)ttmode)
142 # endif
143 #endif
144
145 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
146 # define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
147 #else
148 # if defined(_HPUX_SOURCE) || defined(__CYGWIN__) || defined(__GNU__)
149 # define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode)
150 # else
151 # define _tcsetattrcxx . . $ `lib/kpty.cpp:151:11`!(fd, ttmode) ioctl(fd, TCSETScxx . . $ `<external>/123502480044527469/ioctls.h:21:9`!, (char *)ttmode)
152 # endif
153 #endif
154
155 //#include <kdebug.h>
156 //#include <kstandarddirs.h> // findExe
157
158 // not defined on HP-UX for example
159 #ifndef CTRLcxx . . $ `/usr/include/x86_64-linux-gnu/sys/ttydefaults.h:55:9`!
160 # define CTRL(x) ((x) & 037)
161 #endif
162
163 #define TTY_GROUPcxx . . $ `lib/kpty.cpp:163:9`! "tty"
164
165 ///////////////////////
166 // private functions //
167 ///////////////////////
168
169 //////////////////
170 // private data //
171 //////////////////
172
173 KPtyPrivatecxx . . $ KPtyPrivate#::KPtyPrivatecxx . . $ KPtyPrivate#KPtyPrivate(24a2b0b8e23ae922).(KPtycxx . . $ KPty#* parent) :
174 masterFdcxx . . $ KPtyPrivate#masterFd.(-1), slaveFdcxx . . $ KPtyPrivate#slaveFd.(-1), ownMastercxx . . $ KPtyPrivate#ownMaster.(true), q_ptrcxx . . $ KPtyPrivate#q_ptr.(parent)
175 {
176 }
177
178 KPtyPrivatecxx . . $ KPtyPrivate#::~cxx . . $ KPtyPrivate#`~KPtyPrivate`(ced63f7c635d850d).KPtyPrivatecxx . . $ KPtyPrivate#()
179 {
180 }
181
182 bool KPtyPrivatecxx . . $ KPtyPrivate#::chownptycxx . . $ KPtyPrivate#chownpty(efaadfb7dc14930a).(bool)
183 {
184 // return !QProcess::execute(KStandardDirs::findExe("kgrantpty"),
185 // QStringList() << (grant?"--grant":"--revoke") << QString::number(masterFd));
186 return true;
187 }
188
189 /////////////////////////////
190 // public member functions //
191 /////////////////////////////
192
193 KPtycxx . . $ KPty#::KPtycxx . . $ KPty#KPty(49f6e7a06ebc5aa8).() :
194 d_ptrcxx . . $ KPty#d_ptr.(new KPtyPrivatecxx . . $ KPtyPrivate#KPtyPrivate(24a2b0b8e23ae922).(this))
195 {
196 }
197
198 KPtycxx . . $ KPty#::KPtycxx . . $ KPty#KPty(a32c7870752f5a60).(KPtyPrivatecxx . . $ KPtyPrivate# *d) :
199 d_ptrcxx . . $ KPty#d_ptr.(d)
200 {
201 d_ptrcxx . . $ KPty#d_ptr.->q_ptrcxx . . $ KPtyPrivate#q_ptr. = this;
202 }
203
204 KPtycxx . . $ KPty#::~cxx . . $ KPty#`~KPty`(ced63f7c635d850d).KPtycxx . . $ KPty#()
205 {
206 closecxx . . $ KPty#close(49f6e7a06ebc5aa8).();
207 delete d_ptrcxx . . $ KPty#d_ptr.;
208 }
209
210 bool KPtycxx . . $ KPty#::opencxx . . $ KPty#open(c9857a73d68e59ba).()
211 {
212 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(KPty);
213
214 if (d->masterFdcxx . . $ KPtyPrivate#masterFd. >= 0)
215 return true;
216
217 d->ownMastercxx . . $ KPtyPrivate#ownMaster. = true;
218
219 QByteArraycxx . . $ QByteArray# ptyNamecxx . . $ QByteArray#QByteArray(ced63f7c635d850d).;
220
221 // Find a master pty that we can open ////////////////////////////////
222
223 // Because not all the pty animals are created equal, they want to
224 // be opened by several different methods.
225
226 // We try, as we know them, one by one.
227
228 #ifdef HAVE_OPENPTY
229
230 char ptsn[PATH_MAX];
231 if (::openpty( &d->masterFd, &d->slaveFd, ptsn, 0, 0)) {
232 d->masterFd = -1;
233 d->slaveFd = -1;
234 qWarning() << "Can't open a pseudo teletype";
235 return false;
236 }
237 d->ttyName = ptsn;
238
239 #else
240
241 #ifdef HAVE__GETPTY // irix
242
243 char *ptsn = _getpty(&d->masterFd, O_RDWR|O_NOCTTY, S_IRUSR|S_IWUSR, 0);
244 if (ptsn) {
245 d->ttyName = ptsn;
246 goto grantedpt;
247 }
248
249 #elif defined(HAVE_PTSNAME) || defined(TIOCGPTNcxx . . $ `<external>/123502480044527469/ioctls.h:69:9`!)
250
251 #ifdef HAVE_POSIX_OPENPTcxx . . $ `<built-in>:480:9`!
252 d->masterFdcxx . . $ KPtyPrivate#masterFd. = ::posix_openpt(O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!|O_NOCTTYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:53:10`!);
253 #elif defined(HAVE_GETPT)
254 d->masterFd = ::getpt();
255 #elif defined(PTM_DEVICE)
256 d->masterFd = ::open(PTM_DEVICE, O_RDWR|O_NOCTTY);
257 #else
258 # error No method to open a PTY master detected.
259 #endif
260 if (d->masterFdcxx . . $ KPtyPrivate#masterFd. >= 0) {
261 #ifdef HAVE_PTSNAME
262 char *ptsn = ptsname(d->masterFd);
263 if (ptsn) {
264 d->ttyName = ptsn;
265 #else
266 int ptyno;
267 if (ioctl(d->masterFdcxx . . $ KPtyPrivate#masterFd., TIOCGPTNcxx . . $ `<external>/123502480044527469/ioctls.h:69:9`!, &ptyno) != -1) {
268 d->ttyNamecxx . . $ KPtyPrivate#ttyName. =cxx . . $ QByteArray#`operator=`(acee8257ca81216f). QByteArraycxx . . $ QByteArray#QByteArray(34d26762b42c433a).("/dev/pts/") +cxx . . $ operator+(49e2485dc0bda598). QByteArraycxx . . $ QByteArray#::numbercxx . . $ QByteArray#number(4869bc8897d11479).(ptyno);
269 #endif
270 #ifdef HAVE_GRANTPT
271 if (!grantpt(d->masterFd)) {
272 goto grantedpt;
273 }
274 #else
275
276 goto gotpty;
277 #endif
278 }
279 ::close(d->masterFdcxx . . $ KPtyPrivate#masterFd.);
280 d->masterFdcxx . . $ KPtyPrivate#masterFd. = -1;
281 }
282 #endif // HAVE_PTSNAME || TIOCGPTN
283
284 // Linux device names, FIXME: Trouble on other systems?
285 for (const char * s3 = "pqrstuvwxyzabcde"; *s3; s3++) {
286 for (const char * s4 = "0123456789abcdef"; *s4; s4++) {
287 ptyName =cxx . . $ QByteArray#`operator=`(acee8257ca81216f). QByteArrayLiteralcxx . . $ `<external>/11057416899157294643/qbytearray.h:146:11`!("/dev/pty") +cxx . . $ operator+(3dec5636b72f978c). *s3 +cxx . . $ operator+(3dec5636b72f978c). *s4;
288 d->ttyNamecxx . . $ KPtyPrivate#ttyName. =cxx . . $ QByteArray#`operator=`(acee8257ca81216f). QByteArrayLiteralcxx . . $ `<external>/11057416899157294643/qbytearray.h:146:11`!("/dev/tty") +cxx . . $ operator+(3dec5636b72f978c). *s3 +cxx . . $ operator+(3dec5636b72f978c). *s4;
289
290 d->masterFdcxx . . $ KPtyPrivate#masterFd. = ::open(ptyName.datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!);
291 if (d->masterFdcxx . . $ KPtyPrivate#masterFd. >= 0) {
292 #ifdef Q_OS_SOLARIS
293 /* Need to check the process group of the pty.
294 * If it exists, then the slave pty is in use,
295 * and we need to get another one.
296 */
297 int pgrp_rtn;
298 if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) != -1 || errno != EIO) {
299 ::close(d->masterFd);
300 d->masterFd = -1;
301 continue;
302 }
303 #endif /* Q_OS_SOLARIS */
304 if (!access(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(),R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`!|W_OKcxx . . $ `<external>/12628407106812493757/unistd.h:282:9`!)) { // checks availability based on permission bits
305 if (!geteuid()) {
306 struct group * p = getgrnam(TTY_GROUPcxx . . $ `lib/kpty.cpp:163:9`!);
307 if (!p) {
308 p = getgrnam("wheel");
309 }
310 gid_t gid = p ? p->gr_gid : getgid ();
311
312 if (!chown(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), getuid(), gid)) {
313 chmod(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), S_IRUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:105:10`!|S_IWUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:106:10`!|S_IWGRPcxx . . $ `<external>/14710646456994434043/fcntl.h:112:10`!);
314 }
315 }
316 goto gotpty;
317 }
318 ::close(d->masterFdcxx . . $ KPtyPrivate#masterFd.);
319 d->masterFdcxx . . $ KPtyPrivate#masterFd. = -1;
320 }
321 }
322 }
323
324 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "Can't open a pseudo teletype";
325 return false;
326
327 gotpty:
328 struct stat st;
329 if (stat(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), &st)) {
330 return false; // this just cannot happen ... *cough* Yeah right, I just
331 // had it happen when pty #349 was allocated. I guess
332 // there was some sort of leak? I only had a few open.
333 }
334 if (((st.st_uid != getuid()) ||
335 (st.st_mode & (S_IRGRPcxx . . $ `<external>/14710646456994434043/fcntl.h:111:10`!|S_IXGRPcxx . . $ `<external>/14710646456994434043/fcntl.h:113:10`!|S_IROTHcxx . . $ `<external>/14710646456994434043/fcntl.h:117:10`!|S_IWOTHcxx . . $ `<external>/14710646456994434043/fcntl.h:118:10`!|S_IXOTHcxx . . $ `<external>/14710646456994434043/fcntl.h:119:10`!))) &&
336 !d->chownptycxx . . $ KPtyPrivate#chownpty(efaadfb7dc14930a).(true)) {
337 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!()
338 <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "chownpty failed for device " <<cxx . . $ QDebug#`operator<<`(1387ba1672043913). ptyName <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "::" <<cxx . . $ QDebug#`operator<<`(1387ba1672043913). d->ttyNamecxx . . $ KPtyPrivate#ttyName.
339 <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "\nThis means the communication can be eavesdropped.\n";
340 }
341
342 #if defined (HAVE__GETPTY) || defined (HAVE_GRANTPT)
343 grantedpt:
344 #endif
345
346 #ifdef HAVE_REVOKE
347 revoke(d->ttyName.data());
348 #endif
349
350 #ifdef HAVE_UNLOCKPT
351 unlockpt(d->masterFd);
352 #elif defined(TIOCSPTLCKcxx . . $ `<external>/123502480044527469/ioctls.h:70:9`!)
353 int flag = 0;
354 ioctl(d->masterFdcxx . . $ KPtyPrivate#masterFd., TIOCSPTLCKcxx . . $ `<external>/123502480044527469/ioctls.h:70:9`!, &flag);
355 #endif
356
357 d->slaveFdcxx . . $ KPtyPrivate#slaveFd. = ::open(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_NOCTTYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:53:10`!);
358 if (d->slaveFdcxx . . $ KPtyPrivate#slaveFd. < 0) {
359 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "Can't open slave pseudo teletype";
360 ::close(d->masterFdcxx . . $ KPtyPrivate#masterFd.);
361 d->masterFdcxx . . $ KPtyPrivate#masterFd. = -1;
362 return false;
363 }
364
365 #if (defined(__svr4__) || defined(__sgi__))
366 // Solaris
367 ioctl(d->slaveFd, I_PUSH, "ptem");
368 ioctl(d->slaveFd, I_PUSH, "ldterm");
369 #endif
370
371 #endif /* HAVE_OPENPTY */
372
373 fcntl(d->masterFdcxx . . $ KPtyPrivate#masterFd., F_SETFDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:167:9`!, FD_CLOEXECcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:219:9`!);
374 fcntl(d->slaveFdcxx . . $ KPtyPrivate#slaveFd., F_SETFDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:167:9`!, FD_CLOEXECcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:219:9`!);
375
376 return true;
377 }
378
379 bool KPtycxx . . $ KPty#::opencxx . . $ KPty#open(83c267b20bac841a).(int fd)
380 {
381 #if !defined(HAVE_PTSNAME) && !defined(TIOCGPTNcxx . . $ `<external>/123502480044527469/ioctls.h:69:9`!)
382 qWarning() << "Unsupported attempt to open pty with fd" << fd;
383 return false;
384 #else
385 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(KPty);
386
387 if (d->masterFdcxx . . $ KPtyPrivate#masterFd. >= 0) {
388 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "Attempting to open an already open pty";
389 return false;
390 }
391
392 d->ownMastercxx . . $ KPtyPrivate#ownMaster. = false;
393
394 # ifdef HAVE_PTSNAME
395 char *ptsn = ptsname(fd);
396 if (ptsn) {
397 d->ttyName = ptsn;
398 # else
399 int ptyno;
400 if (ioctl(fd, TIOCGPTNcxx . . $ `<external>/123502480044527469/ioctls.h:69:9`!, &ptyno) != -1) {
401 const size_t sz = 32;
402 char buf[sz];
403 const size_t r = snprintf(buf, sz, "/dev/pts/%d", ptyno);
404 if (sz <= r) {
405 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!("KPty::open: Buffer too small\n");
406 }
407 d->ttyNamecxx . . $ KPtyPrivate#ttyName. =cxx . . $ QByteArray#`operator=`(715145f812399bcb). buf;
408 # endif
409 } else {
410 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "Failed to determine pty slave device for fd" <<cxx . . $ QDebug#`operator<<`(6e3b7ae53f380c3). fd;
411 return false;
412 }
413
414 d->masterFdcxx . . $ KPtyPrivate#masterFd. = fd;
415 if (!openSlavecxx . . $ KPty#openSlave(c9857a73d68e59ba).()) {
416
417 d->masterFdcxx . . $ KPtyPrivate#masterFd. = -1;
418 return false;
419 }
420
421 return true;
422 #endif
423 }
424
425 void KPtycxx . . $ KPty#::closeSlavecxx . . $ KPty#closeSlave(49f6e7a06ebc5aa8).()
426 {
427 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(KPty);
428
429 if (d->slaveFdcxx . . $ KPtyPrivate#slaveFd. < 0) {
430 return;
431 }
432 ::close(d->slaveFdcxx . . $ KPtyPrivate#slaveFd.);
433 d->slaveFdcxx . . $ KPtyPrivate#slaveFd. = -1;
434 }
435
436 bool KPtycxx . . $ KPty#::openSlavecxx . . $ KPty#openSlave(c9857a73d68e59ba).()
437 {
438 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(KPty);
439
440 if (d->slaveFdcxx . . $ KPtyPrivate#slaveFd. >= 0)
441 return true;
442 if (d->masterFdcxx . . $ KPtyPrivate#masterFd. < 0) {
443 qDebugcxx . . $ `<external>/10450329605257866924/qlogging.h:164:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "Attempting to open pty slave while master is closed";
444 return false;
445 }
446 //d->slaveFd = KDE_open(d->ttyName.data(), O_RDWR | O_NOCTTY);
447 d->slaveFdcxx . . $ KPtyPrivate#slaveFd. = ::open(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_NOCTTYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:53:10`!);
448 if (d->slaveFdcxx . . $ KPtyPrivate#slaveFd. < 0) {
449 qDebugcxx . . $ `<external>/10450329605257866924/qlogging.h:164:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "Can't open slave pseudo teletype";
450 return false;
451 }
452 fcntl(d->slaveFdcxx . . $ KPtyPrivate#slaveFd., F_SETFDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:167:9`!, FD_CLOEXECcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:219:9`!);
453 return true;
454 }
455
456 void KPtycxx . . $ KPty#::closecxx . . $ KPty#close(49f6e7a06ebc5aa8).()
457 {
458 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(KPty);
459
460 if (d->masterFdcxx . . $ KPtyPrivate#masterFd. < 0) {
461 return;
462 }
463 closeSlavecxx . . $ KPty#closeSlave(49f6e7a06ebc5aa8).();
464 // don't bother resetting unix98 pty, it will go away after closing master anyway.
465 if (memcmp(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), "/dev/pts/", 9)) {
466 if (!geteuid()) {
467 struct stat st;
468 if (!stat(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), &st)) {
469 chown(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), 0, st.st_gid == getgid() ? 0 : -1);
470 chmod(d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).(), S_IRUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:105:10`!|S_IWUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:106:10`!|S_IRGRPcxx . . $ `<external>/14710646456994434043/fcntl.h:111:10`!|S_IWGRPcxx . . $ `<external>/14710646456994434043/fcntl.h:112:10`!|S_IROTHcxx . . $ `<external>/14710646456994434043/fcntl.h:117:10`!|S_IWOTHcxx . . $ `<external>/14710646456994434043/fcntl.h:118:10`!);
471 }
472 } else {
473 fcntl(d->masterFdcxx . . $ KPtyPrivate#masterFd., F_SETFDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:167:9`!, 0);
474 d->chownptycxx . . $ KPtyPrivate#chownpty(efaadfb7dc14930a).(false);
475 }
476 }
477 ::close(d->masterFdcxx . . $ KPtyPrivate#masterFd.);
478 d->masterFdcxx . . $ KPtyPrivate#masterFd. = -1;
479 }
480
481 void KPtycxx . . $ KPty#::setCTtycxx . . $ KPty#setCTty(49f6e7a06ebc5aa8).()
482 {
483 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(KPty);
484
485 // Setup job control //////////////////////////////////
486
487 // Become session leader, process group leader,
488 // and get rid of the old controlling terminal.
489 setsid();
490
491 // make our slave pty the new controlling terminal.
492 #ifdef TIOCSCTTYcxx . . $ `<external>/123502480044527469/ioctls.h:33:9`!
493 ioctl(d->slaveFdcxx . . $ KPtyPrivate#slaveFd., TIOCSCTTYcxx . . $ `<external>/123502480044527469/ioctls.h:33:9`!, 0);
494 #else
495 // __svr4__ hack: the first tty opened after setsid() becomes controlling tty
496 ::close(::open(d->ttyName, O_WRONLY, 0));
497 #endif
498
499 // make our new process group the foreground group on the pty
500 int pgrp = getpid();
501 #if defined(_POSIX_VERSIONcxx . . $ `<external>/12628407106812493757/unistd.h:34:10`!) || defined(__svr4__)
502 tcsetpgrp(d->slaveFdcxx . . $ KPtyPrivate#slaveFd., pgrp);
503 #elif defined(TIOCSPGRP)
504 ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp);
505 #endif
506 }
507
508 void KPtycxx . . $ KPty#::logincxx . . $ KPty#login(2fa786934fe80e24).(const char * user, const char * remotehost)
509 {
510 #ifdef HAVE_UTEMPTER
511 Q_D(KPty);
512
513 addToUtmp(d->ttyName.constData(), remotehost, d->masterFd);
514 Q_UNUSED(user);
515 #else
516 # ifdef HAVE_UTMPX
517 struct utmpx l_struct;
518 # else
519 struct utmp l_struct;
520 # endif
521 memset(&l_struct, 0, sizeof(l_struct));
522 // note: strncpy without terminators _is_ correct here. man 4 utmp
523
524 if (user) {
525 # ifdef HAVE_UTMPX
526 strncpy(l_struct.ut_user, user, sizeof(l_struct.ut_user));
527 # else
528 strncpy(l_struct.ut_namecxx . . $ `<external>/7534489640210462991/utmp.h:92:9`!, user, sizeof(l_struct.ut_namecxx . . $ `<external>/7534489640210462991/utmp.h:92:9`!));
529 # endif
530 }
531
532 if (remotehost) {
533 strncpy(l_struct.ut_host, remotehost, sizeof(l_struct.ut_host));
534 # ifdef HAVE_STRUCT_UTMP_UT_SYSLEN
535 l_struct.ut_syslen = qMin(strlen(remotehost), sizeof(l_struct.ut_host));
536 # endif
537 }
538
539 # ifndef __GLIBC__cxx . . $ `<external>/18206667465210621810/features.h:514:9`!
540 Q_D(KPty);
541 const char * str_ptr = d->ttyName.data();
542 if (!memcmp(str_ptr, "/dev/", 5)) {
543 str_ptr += 5;
544 }
545 strncpy(l_struct.ut_line, str_ptr, sizeof(l_struct.ut_line));
546 # ifdef HAVE_STRUCT_UTMP_UT_ID
547 strncpy(l_struct.ut_id,
548 str_ptr + strlen(str_ptr) - sizeof(l_struct.ut_id),
549 sizeof(l_struct.ut_id));
550 # endif
551 # endif
552
553 # ifdef HAVE_UTMPX
554 gettimeofday(&l_struct.ut_tv, 0);
555 # else
556 l_struct.ut_timecxx . . $ `<external>/7534489640210462991/utmp.h:96:10`! = time(nullptr);
557 # endif
558
559 # ifdef HAVE_LOGIN
560 # ifdef HAVE_LOGINX
561 ::loginx(&l_struct);
562 # else
563 ::login(&l_struct);
564 # endif
565 # else
566 # ifdef HAVE_STRUCT_UTMP_UT_TYPE
567 l_struct.ut_type = USER_PROCESS;
568 # endif
569 # ifdef HAVE_STRUCT_UTMP_UT_PID
570 l_struct.ut_pid = getpid();
571 # ifdef HAVE_STRUCT_UTMP_UT_SESSION
572 l_struct.ut_session = getsid(0);
573 # endif
574 # endif
575 # ifdef HAVE_UTMPX
576 utmpxname(_PATH_UTMPX);
577 setutxent();
578 pututxline(&l_struct);
579 endutxent();
580 # ifdef HAVE_UPDWTMPX
581 updwtmpx(_PATH_WTMPX, &l_struct);
582 # endif
583 # else
584 utmpname(_PATH_UTMPcxx . . $ `<external>/11492533085529128233/paths.h:64:9`!);
585 setutent();
586 pututline(&l_struct);
587 endutent();
588 updwtmp(_PATH_WTMPcxx . . $ `<external>/11492533085529128233/paths.h:66:9`!, &l_struct);
589 # endif
590 # endif
591 #endif
592 }
593
594 void KPtycxx . . $ KPty#::logoutcxx . . $ KPty#logout(49f6e7a06ebc5aa8).()
595 {
596 #ifdef HAVE_UTEMPTER
597 Q_D(KPty);
598
599 removeLineFromUtmp(d->ttyName.constData(), d->masterFd);
600 #else
601 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(KPty);
602
603 const char *str_ptr = d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(41208ce6ca65dd58).();
604 if (!memcmp(str_ptr, "/dev/", 5)) {
605 str_ptr += 5;
606 }
607 # ifdef __GLIBC__cxx . . $ `<external>/18206667465210621810/features.h:514:9`!
608 else {
609 const char * sl_ptr = strrchr(str_ptr, '/');
610 if (sl_ptr) {
611 str_ptr = sl_ptr + 1;
612 }
613 }
614 # endif
615 # ifdef HAVE_LOGIN
616 # ifdef HAVE_LOGINX
617 ::logoutx(str_ptr, 0, DEAD_PROCESS);
618 # else
619 ::logout(str_ptr);
620 # endif
621 # else
622 # ifdef HAVE_UTMPX
623 struct utmpx l_struct, *ut;
624 # else
625 struct utmp l_struct, *ut;
626 # endif
627 memset(&l_struct, 0, sizeof(l_struct));
628
629 strncpy(l_struct.ut_line, str_ptr, sizeof(l_struct.ut_line));
630
631 # ifdef HAVE_UTMPX
632 utmpxname(_PATH_UTMPX);
633 setutxent();
634 if ((ut = getutxline(&l_struct))) {
635 # else
636 utmpname(_PATH_UTMPcxx . . $ `<external>/11492533085529128233/paths.h:64:9`!);
637 setutent();
638 if ((ut = getutline(&l_struct))) {
639 # endif
640 # ifdef HAVE_UTMPX
641 memset(ut->ut_user, 0, sizeof(*ut->ut_user));
642 # else
643 memset(ut->ut_namecxx . . $ `<external>/7534489640210462991/utmp.h:92:9`!, 0, sizeof(*ut->ut_namecxx . . $ `<external>/7534489640210462991/utmp.h:92:9`!));
644 # endif
645 memset(ut->ut_host, 0, sizeof(*ut->ut_host));
646 # ifdef HAVE_STRUCT_UTMP_UT_SYSLEN
647 ut->ut_syslen = 0;
648 # endif
649 # ifdef HAVE_STRUCT_UTMP_UT_TYPE
650 ut->ut_type = DEAD_PROCESS;
651 # endif
652 # ifdef HAVE_UTMPX
653 gettimeofday(&ut->ut_tv, 0);
654 pututxline(ut);
655 }
656 endutxent();
657 # else
658 ut->ut_timecxx . . $ `<external>/7534489640210462991/utmp.h:96:10`! = time(nullptr);
659 pututline(ut);
660 }
661 endutent();
662 # endif
663 # endif
664 #endif
665 }
666
667 // XXX Supposedly, tc[gs]etattr do not work with the master on Solaris.
668 // Please verify.
669
670 bool KPtycxx . . $ KPty#::tcGetAttrcxx . . $ KPty#tcGetAttr(5fad475f32619bec).(struct ::termioscxx . . $ termios# * ttmode) const
671 {
672 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(const KPty);
673
674 return _tcgetattrcxx . . $ `lib/kpty.cpp:141:11`!(d->masterFd, ttmode) == 0;
675 }
676
677 bool KPtycxx . . $ KPty#::tcSetAttrcxx . . $ KPty#tcSetAttr(98b4f1f88d9dab40).(struct ::termioscxx . . $ termios# * ttmode)
678 {
679 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(KPty);
680
681 return _tcsetattrcxx . . $ `lib/kpty.cpp:151:11`!(d->masterFd, ttmode) == 0;
682 }
683
684 bool KPtycxx . . $ KPty#::setWinSizecxx . . $ KPty#setWinSize(cf9ef193275fa6d8).(int lines, int columns)
685 {
686 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(KPty);
687
688 struct winsize winSize;
689 memset(&winSize, 0, sizeof(winSize));
690 winSize.ws_row = (unsigned short)lines;
691 winSize.ws_col = (unsigned short)columns;
692 return ioctl(d->masterFdcxx . . $ KPtyPrivate#masterFd., TIOCSWINSZcxx . . $ `<external>/123502480044527469/ioctls.h:39:9`!, (char *)&winSize) != -1;
693 }
694
695 bool KPtycxx . . $ KPty#::setEchocxx . . $ KPty#setEcho(efaadfb7dc14930a).(bool echo)
696 {
697 struct ::termioscxx . . $ termios# ttmode;
698 if (!tcGetAttrcxx . . $ KPty#tcGetAttr(5fad475f32619bec).(&ttmode)) {
699 return false;
700 }
701 if (!echo) {
702 ttmode.c_lflagcxx . . $ termios#c_lflag. &= ~ECHOcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:29:9`!;
703 } else {
704 ttmode.c_lflagcxx . . $ termios#c_lflag. |= ECHOcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:29:9`!;
705 }
706 return tcSetAttrcxx . . $ KPty#tcSetAttr(98b4f1f88d9dab40).(&ttmode);
707 }
708
709 const char * KPtycxx . . $ KPty#::ttyNamecxx . . $ KPty#ttyName(a9f41ea0e82d88cf).() const
710 {
711 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(const KPty);
712
713 return d->ttyNamecxx . . $ KPtyPrivate#ttyName..datacxx . . $ QByteArray#data(a9f41ea0e82d88cf).();
714 }
715
716 int KPtycxx . . $ KPty#::masterFdcxx . . $ KPty#masterFd(455f465bc33b4cdf).() const
717 {
718 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(const KPty);
719
720 return d->masterFdcxx . . $ KPtyPrivate#masterFd.;
721 }
722
723 int KPtycxx . . $ KPty#::slaveFdcxx . . $ KPty#slaveFd(455f465bc33b4cdf).() const
724 {
725 Q_Dcxx . . $ `<external>/15045578679651060896/qglobal.h:1177:9`!(const KPty);
726
727 return d->slaveFdcxx . . $ KPtyPrivate#slaveFd.;
728 }