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

kdecore

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

kdecore

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

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |