• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kpty.cpp
1/*
2
3 This file is part of the KDE libraries
4 Copyright (C) 1997-2002 The Konsole Developers
5 Copyright (C) 2002 Waldo Bastian <bastian@kde.org>
6 Copyright (C) 2002-2003 Oswald Buddenhagen <ossi@kde.org>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22*/
23
24#include <config.h>
25
26#include "kpty.h"
27#include "tdeprocess.h"
28
29#ifdef __sgi
30#define __svr4__
31#endif
32
33#ifdef _AIX
34#define _ALL_SOURCE
35#endif
36
37// __USE_XOPEN isn't defined by default in ICC
38// (needed for ptsname(), grantpt() and unlockpt())
39#ifdef __INTEL_COMPILER
40# ifndef __USE_XOPEN
41# define __USE_XOPEN
42# endif
43#endif
44
45#include <sys/types.h>
46#include <sys/ioctl.h>
47#include <sys/time.h>
48#include <sys/resource.h>
49#include <sys/stat.h>
50#include <sys/param.h>
51
52#ifdef HAVE_SYS_STROPTS_H
53# include <sys/stropts.h> // Defines I_PUSH
54# define _NEW_TTY_CTRL
55#endif
56
57#include <errno.h>
58#include <fcntl.h>
59#include <time.h>
60#include <stdlib.h>
61#include <stdio.h>
62#include <string.h>
63#include <unistd.h>
64#include <grp.h>
65
66#if defined(HAVE_LIBUTIL_H)
67# include <libutil.h>
68# if (!defined(__FreeBSD__) || __FreeBSD_version < 900007)
69# define USE_LOGIN
70# endif
71#endif
72#if defined(HAVE_UTIL_H)
73# include <util.h>
74# define USE_LOGIN
75#endif
76
77#ifdef USE_LOGIN
78# include <utmp.h>
79#endif
80
81#ifdef HAVE_TERMIOS_H
82extern "C" {
83# include <termios.h>
84}
85#endif
86
87#ifdef HAVE_TERMIO_H
88/* needed at least on AIX */
89# include <termio.h>
90#endif
91
92#if defined(HAVE_TCGETATTR)
93# define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
94#elif defined(TIOCGETA)
95# define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
96#elif defined(TCGETS)
97# define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
98#else
99# error
100#endif
101
102#if defined(HAVE_TCSETATTR) && defined(TCSANOW)
103# define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode)
104#elif defined(TIOCSETA)
105# define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
106#elif defined(TCSETS)
107# define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
108#else
109# error
110#endif
111
112#if defined(HAVE_PTY_H)
113# include <pty.h>
114#endif
115
116#include <kdebug.h>
117#include <tdestandarddirs.h> // locate
118
119#ifndef CINTR
120#define CINTR 0x03
121#endif
122#ifndef CQUIT
123#define CQUIT 0x1c
124#endif
125#ifndef CERASE
126#define CERASE 0x7f
127#endif
128
129#define TTY_GROUP "tty"
130
132// private functions //
134
135#ifdef HAVE_UTEMPTER
136class TDEProcess_Utmp : public TDEProcess
137{
138public:
139 int commSetupDoneC()
140 {
141 dup2(cmdFd, 0);
142 dup2(cmdFd, 1);
143 dup2(cmdFd, 3);
144 return 1;
145 }
146 int cmdFd;
147};
148#endif
149
150#define BASE_CHOWN "kgrantpty"
151
152
153
155// private data //
157
158struct KPtyPrivate {
159 KPtyPrivate() :
160 xonXoff(false),
161 utf8(false),
162 masterFd(-1), slaveFd(-1)
163 {
164 memset(&winSize, 0, sizeof(winSize));
165 winSize.ws_row = 24;
166 winSize.ws_col = 80;
167 }
168
169 bool xonXoff : 1;
170 bool utf8 : 1;
171 int masterFd;
172 int slaveFd;
173 struct winsize winSize;
174
175 TQCString ttyName;
176};
177
179// public member functions //
181
182KPty::KPty()
183{
184 d = new KPtyPrivate;
185}
186
187KPty::~KPty()
188{
189 close();
190 delete d;
191}
192
193bool KPty::setPty(int pty_master)
194{
195 // a pty is already open
196 if(d->masterFd >= 0) {
197 kdWarning(175) << "KPty::setPty(): " << "d->masterFd >= 0" << endl;
198 return false;
199 }
200 d->masterFd = pty_master;
201 return _attachPty(pty_master);
202}
203
204bool KPty::_attachPty(int pty_master)
205{
206 if (d->slaveFd < 0 ) {
207
208 kdDebug(175) << "KPty::_attachPty(): " << pty_master << endl;
209#if defined(HAVE_PTSNAME)
210 char *ptsn = ptsname(d->masterFd);
211 if (ptsn) {
212 d->ttyName = ptsn;
213 } else {
214 ::close(d->masterFd);
215 d->masterFd = -1;
216 return false;
217 }
218#endif
219
220#if defined(HAVE_GRANTPT)
221 if (grantpt(d->masterFd)) {
222 return false;
223 }
224#else
225 struct stat st;
226 if (stat(d->ttyName.data(), &st))
227 return false; // this just cannot happen ... *cough* Yeah right, I just
228 // had it happen when pty #349 was allocated. I guess
229 // there was some sort of leak? I only had a few open.
230 if (((st.st_uid != getuid()) ||
231 (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
232 !chownpty(true))
233 {
234 kdWarning(175)
235 << "KPty::_attachPty(): " << "chownpty failed for device " << d->ttyName << endl
236 << "KPty::_attachPty(): " << "This means the communication can be eavesdropped." << endl;
237 }
238#endif
239
240#ifdef BSD
241 revoke(d->ttyName.data());
242#endif
243
244#ifdef HAVE_UNLOCKPT
245 unlockpt(d->masterFd);
246#endif
247
248 d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
249 if (d->slaveFd < 0)
250 {
251 kdWarning(175) << "KPty::_attachPty(): " << "Can't open slave pseudo teletype" << endl;
252 ::close(d->masterFd);
253 d->masterFd = -1;
254 return false;
255 }
256#ifdef HAVE_OPENPTY
257 // set screen size
258 ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
259#endif
260 }
261
262#if (defined(__svr4__) || defined(__sgi__))
263 // Solaris
264 ioctl(d->slaveFd, I_PUSH, "ptem");
265 ioctl(d->slaveFd, I_PUSH, "ldterm");
266#endif
267
268 // set xon/xoff & control keystrokes
269 // without the '::' some old system thinks, this declares
270 // the struct in this class, in this method, and fails to find
271 // the correct tc[gs]etattr
272 struct ::termios ttmode;
273
274 _tcgetattr(d->slaveFd, &ttmode);
275
276 if (!d->xonXoff)
277 ttmode.c_iflag &= ~(IXOFF | IXON);
278 else
279 ttmode.c_iflag |= (IXOFF | IXON);
280
281#ifdef IUTF8
282 if (!d->utf8)
283 ttmode.c_iflag &= ~IUTF8;
284 else
285 ttmode.c_iflag |= IUTF8;
286#endif
287
288 ttmode.c_cc[VINTR] = CINTR;
289 ttmode.c_cc[VQUIT] = CQUIT;
290 ttmode.c_cc[VERASE] = CERASE;
291
292 _tcsetattr(d->slaveFd, &ttmode);
293
294#ifndef HAVE_OPENPTY
295 // set screen size
296 ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
297#endif
298
299 fcntl(d->masterFd, F_SETFD, FD_CLOEXEC);
300 fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);
301
302 return true;
303}
304
305bool KPty::open()
306{
307 if (d->masterFd >= 0)
308 return true;
309
310#if defined(HAVE_OPENPTY)
311 char cpty[16];
312
313 if (openpty(&d->masterFd, &d->slaveFd, cpty, NULL, &d->winSize) == 0) {
314 d->ttyName = cpty;
315 } else {
316 kdWarning(175) << "Can't open slave pseudo teletype" << endl;
317 return false;
318 }
319#else
320
321 TQCString ptyName;
322
323 // Find a master pty that we can open ////////////////////////////////
324
325 // Because not all the pty animals are created equal, they want to
326 // be opened by several different methods.
327
328 // We try, as we know them, one by one.
329
330#if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
331#if defined(HAVE_GETPT)
332 d->masterFd = ::getpt();
333#elif defined(HAVE_POSIX_OPENPT)
334 d->masterFd = ::posix_openpt(O_RDWR);
335#elif defined(_AIX)
336 d->masterFd = ::open("/dev/ptc",O_RDWR);
337#else
338 d->masterFd = ::open("/dev/ptmx",O_RDWR);
339#endif
340 if (d->masterFd >= 0)
341 {
342 char *ptsn = ptsname(d->masterFd);
343 if (ptsn) {
344 grantpt(d->masterFd);
345 d->ttyName = ptsn;
346 goto gotpty;
347 } else {
348 ::close(d->masterFd);
349 d->masterFd = -1;
350 }
351 }
352#endif
353
354 // Linux device names, FIXME: Trouble on other systems?
355 for (const char* s3 = "pqrstuvwxyzabcdefghijklmno"; *s3; s3++)
356 {
357 for (const char* s4 = "0123456789abcdefghijklmnopqrstuvwxyz"; *s4; s4++)
358 {
359 ptyName.sprintf("/dev/pty%c%c", *s3, *s4);
360 d->ttyName.sprintf("/dev/tty%c%c", *s3, *s4);
361
362 d->masterFd = ::open(ptyName.data(), O_RDWR);
363 if (d->masterFd >= 0)
364 {
365#ifdef __sun
366 /* Need to check the process group of the pty.
367 * If it exists, then the slave pty is in use,
368 * and we need to get another one.
369 */
370 int pgrp_rtn;
371 if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
372 ::close(d->masterFd);
373 d->masterFd = -1;
374 continue;
375 }
376#endif /* sun */
377 if (!access(d->ttyName.data(),R_OK|W_OK)) // checks availability based on permission bits
378 {
379 if (!geteuid())
380 {
381 struct group* p = getgrnam(TTY_GROUP);
382 if (!p)
383 p = getgrnam("wheel");
384 gid_t gid = p ? p->gr_gid : getgid ();
385
386 chown(d->ttyName.data(), getuid(), gid);
387 chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
388 }
389 goto gotpty;
390 }
391 ::close(d->masterFd);
392 d->masterFd = -1;
393 }
394 }
395 }
396
397 kdWarning(175) << "KPty::open(): " << "Can't open a pseudo teletype" << endl;
398 return false;
399#endif
400
401 gotpty:
402 return _attachPty(d->masterFd);
403
404 return true;
405}
406
407void KPty::close()
408{
409 if (d->masterFd < 0)
410 return;
411 // don't bother resetting unix98 pty, it will go away after closing master anyway.
412 if (memcmp(d->ttyName.data(), "/dev/pts/", 9)) {
413 if (!geteuid()) {
414 struct stat st;
415 if (!stat(d->ttyName.data(), &st)) {
416 chown(d->ttyName.data(), 0, st.st_gid == getgid() ? 0 : -1);
417 chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
418 }
419 } else {
420 fcntl(d->masterFd, F_SETFD, 0);
421 chownpty(false);
422 }
423 }
424 ::close(d->slaveFd);
425 ::close(d->masterFd);
426 d->masterFd = d->slaveFd = -1;
427}
428
429void KPty::setCTty()
430{
431 // Setup job control //////////////////////////////////
432
433 // Become session leader, process group leader,
434 // and get rid of the old controlling terminal.
435 setsid();
436
437 // make our slave pty the new controlling terminal.
438#ifdef TIOCSCTTY
439 ioctl(d->slaveFd, TIOCSCTTY, 0);
440#else
441 // SVR4 hack: the first tty opened after setsid() becomes controlling tty
442 ::close(::open(d->ttyName, O_WRONLY, 0));
443#endif
444
445 // make our new process group the foreground group on the pty
446 int pgrp = getpid();
447#if defined(_POSIX_VERSION) || defined(__svr4__)
448 tcsetpgrp (d->slaveFd, pgrp);
449#elif defined(TIOCSPGRP)
450 ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp);
451#endif
452}
453
454void KPty::login(const char *user, const char *remotehost)
455{
456#ifdef HAVE_UTEMPTER
457 TDEProcess_Utmp utmp;
458 utmp.cmdFd = d->masterFd;
459 utmp << UTEMPTER_HELPER << "add";
460 if (remotehost)
461 utmp << remotehost;
462 utmp.start(TDEProcess::Block);
463 Q_UNUSED(user);
464 Q_UNUSED(remotehost);
465#elif defined(USE_LOGIN)
466 const char *str_ptr;
467 struct utmp l_struct;
468 memset(&l_struct, 0, sizeof(struct utmp));
469 // note: strncpy without terminators _is_ correct here. man 4 utmp
470
471 if (user)
472 strncpy(l_struct.ut_name, user, UT_NAMESIZE);
473
474 if (remotehost)
475 strncpy(l_struct.ut_host, remotehost, UT_HOSTSIZE);
476
477# ifndef __GLIBC__
478 str_ptr = d->ttyName.data();
479 if (!memcmp(str_ptr, "/dev/", 5))
480 str_ptr += 5;
481 strncpy(l_struct.ut_line, str_ptr, UT_LINESIZE);
482# endif
483
484 // Handle 64-bit time_t properly, where it may be larger
485 // than the integral type of ut_time.
486 {
487 time_t ut_time_temp;
488 time(&ut_time_temp);
489 l_struct.ut_time=ut_time_temp;
490 }
491
492 ::login(&l_struct);
493#else
494 Q_UNUSED(user);
495 Q_UNUSED(remotehost);
496#endif
497}
498
499void KPty::logout()
500{
501#ifdef HAVE_UTEMPTER
502 TDEProcess_Utmp utmp;
503 utmp.cmdFd = d->masterFd;
504 utmp << UTEMPTER_HELPER << "del";
505 utmp.start(TDEProcess::Block);
506#elif defined(USE_LOGIN)
507 const char *str_ptr = d->ttyName.data();
508 if (!memcmp(str_ptr, "/dev/", 5))
509 str_ptr += 5;
510# ifdef __GLIBC__
511 else {
512 const char *sl_ptr = strrchr(str_ptr, '/');
513 if (sl_ptr)
514 str_ptr = sl_ptr + 1;
515 }
516# endif
517 ::logout(str_ptr);
518#endif
519}
520
521void KPty::setWinSize(int lines, int columns)
522{
523 d->winSize.ws_row = (unsigned short)lines;
524 d->winSize.ws_col = (unsigned short)columns;
525 if (d->masterFd >= 0)
526 ioctl( d->masterFd, TIOCSWINSZ, (char *)&d->winSize );
527}
528
529void KPty::setXonXoff(bool useXonXoff)
530{
531 d->xonXoff = useXonXoff;
532 if (d->masterFd >= 0) {
533 // without the '::' some old system thinks, this declares
534 // the struct in this class, in this method, and fails to find
535 // the correct tc[gs]etattr
536 struct ::termios ttmode;
537
538 _tcgetattr(d->masterFd, &ttmode);
539
540 if (!useXonXoff)
541 ttmode.c_iflag &= ~(IXOFF | IXON);
542 else
543 ttmode.c_iflag |= (IXOFF | IXON);
544
545 _tcsetattr(d->masterFd, &ttmode);
546 }
547}
548
549void KPty::setUtf8Mode(bool useUtf8)
550{
551 d->utf8 = useUtf8;
552#ifdef IUTF8
553 if (d->masterFd >= 0) {
554 // without the '::' some old system thinks, this declares
555 // the struct in this class, in this method, and fails to find
556 // the correct tc[gs]etattr
557 struct ::termios ttmode;
558
559 _tcgetattr(d->masterFd, &ttmode);
560
561 if (!useUtf8)
562 ttmode.c_iflag &= ~IUTF8;
563 else
564 ttmode.c_iflag |= IUTF8;
565
566 _tcsetattr(d->masterFd, &ttmode);
567 }
568#endif
569}
570
571const char *KPty::ttyName() const
572{
573 return d->ttyName.data();
574}
575
576int KPty::masterFd() const
577{
578 return d->masterFd;
579}
580
581int KPty::slaveFd() const
582{
583 return d->slaveFd;
584}
585
586// private
587bool KPty::chownpty(bool grant)
588{
589#if !defined(__OpenBSD__) && !defined(__FreeBSD__)
590 TDEProcess proc;
591 proc << locate("exe", BASE_CHOWN) << (grant?"--grant":"--revoke") << TQString::number(d->masterFd);
592 return proc.start(TDEProcess::Block) && proc.normalExit() && !proc.exitStatus();
593#endif
594}
595
KPty::setPty
bool setPty(int pty_master)
Attach a existing pty master.
Definition: kpty.cpp:193
KPty::~KPty
~KPty()
Destructor:
Definition: kpty.cpp:187
KPty::setXonXoff
void setXonXoff(bool useXonXoff)
Set whether the pty should honor Xon/Xoff flow control.
Definition: kpty.cpp:529
KPty::setCTty
void setCTty()
Creates a new session and process group and makes this pty the controlling tty.
Definition: kpty.cpp:429
KPty::masterFd
int masterFd() const
Definition: kpty.cpp:576
KPty::setWinSize
void setWinSize(int lines, int columns)
Change the logical (screen) size of the pty.
Definition: kpty.cpp:521
KPty::login
void login(const char *user=0, const char *remotehost=0)
Creates an utmp entry for the tty.
Definition: kpty.cpp:454
KPty::open
bool open()
Create a pty master/slave pair.
Definition: kpty.cpp:305
KPty::setUtf8Mode
void setUtf8Mode(bool useUtf8)
Set the pty in utf8 mode on systems that support it.
Definition: kpty.cpp:549
KPty::logout
void logout()
Removes the utmp entry for this tty.
Definition: kpty.cpp:499
KPty::ttyName
const char * ttyName() const
Definition: kpty.cpp:571
KPty::KPty
KPty()
Constructor.
Definition: kpty.cpp:182
KPty::slaveFd
int slaveFd() const
Definition: kpty.cpp:581
KPty::close
void close()
Close the pty master/slave pair.
Definition: kpty.cpp:407
TDEProcess
Child process invocation, monitoring and control.
Definition: tdeprocess.h:131
TDEProcess::commSetupDoneC
virtual int commSetupDoneC()
Called right after a (successful) fork(), but before an exec() on the child process' side.
Definition: tdeprocess.cpp:982
TDEProcess::start
virtual bool start(RunMode runmode=NotifyOnExit, Communication comm=NoCommunication)
Starts the process.
Definition: tdeprocess.cpp:293
TDEProcess::exitStatus
int exitStatus() const
Returns the exit status of the process.
Definition: tdeprocess.cpp:611
TDEProcess::Block
@ Block
The application is suspended until the started process is finished.
Definition: tdeprocess.h:182
TDEProcess::normalExit
bool normalExit() const
Checks whether the process exited cleanly.
Definition: tdeprocess.cpp:589
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.