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>
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.
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.
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.
42
// __USE_XOPEN isn't defined by default in ICC
43
// (needed for ptsname(), grantpt() and unlockpt())
44
#ifdef __INTEL_COMPILER
50
#include <sys/types.h>
51
#include <sys/ioctl.h>
53
#include <sys/resource.h>
55
#include <sys/param.h>
57
#ifdef HAVE_SYS_STROPTS_H
58
# include <sys/stropts.h> // Defines I_PUSH
59
# define _NEW_TTY_CTRL
74
#elif defined(HAVE_UTIL_H)
84
/* for HP-UX (some versions) the extern C is needed, and for other
85
platforms it doesn't hurt */
93
/* needed at least on AIX */
98
#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
99
# define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
101
# if defined(_HPUX_SOURCE) || defined(__Lynx__) || defined (__CYGWIN__)
102
# define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
104
# define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
108
#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
109
# define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
111
# if defined(_HPUX_SOURCE) || defined(__CYGWIN__)
112
# define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode)
114
# define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
118
#if defined (_HPUX_SOURCE)
119
# define _TERMIOS_INCLUDED
123
#if defined(HAVE_PTY_H)
128
#include <kstandarddirs.h> // locate
140
#define TTY_GROUP "tty"
142
///////////////////////
143
// private functions //
144
///////////////////////
147
class KProcess_Utmp : public KProcess
161
#define BASE_CHOWN "kgrantpty"
173
masterFd(-1), slaveFd(-1)
175
memset(&winSize, 0, sizeof(winSize));
184
struct winsize winSize;
189
/////////////////////////////
190
// public member functions //
191
/////////////////////////////
206
if (d->masterFd >= 0)
211
// Find a master pty that we can open ////////////////////////////////
213
// Because not all the pty animals are created equal, they want to
214
// be opened by several different methods.
216
// We try, as we know them, one by one.
218
#if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
220
d->masterFd = ::open("/dev/ptc",O_RDWR);
222
d->masterFd = ::open("/dev/ptmx",O_RDWR);
224
if (d->masterFd >= 0)
226
char *ptsn = ptsname(d->masterFd);
228
grantpt(d->masterFd);
232
::close(d->masterFd);
238
// Linux device names, FIXME: Trouble on other systems?
239
for (const char* s3 = "pqrstuvwxyzabcdefghijklmno"; *s3; s3++)
241
for (const char* s4 = "0123456789abcdefghijklmnopqrstuvwxyz"; *s4; s4++)
243
ptyName.sprintf("/dev/pty%c%c", *s3, *s4);
244
d->ttyName.sprintf("/dev/tty%c%c", *s3, *s4);
246
d->masterFd = ::open(ptyName.data(), O_RDWR);
247
if (d->masterFd >= 0)
250
/* Need to check the process group of the pty.
251
* If it exists, then the slave pty is in use,
252
* and we need to get another one.
255
if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
256
::close(d->masterFd);
261
if (!access(d->ttyName.data(),R_OK|W_OK)) // checks availability based on permission bits
265
struct group* p = getgrnam(TTY_GROUP);
267
p = getgrnam("wheel");
268
gid_t gid = p ? p->gr_gid : getgid ();
270
chown(d->ttyName.data(), getuid(), gid);
271
chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
275
::close(d->masterFd);
281
kdWarning(175) << "Can't open a pseudo teletype" << endl;
286
if (stat(d->ttyName.data(), &st))
287
return false; // this just cannot happen ... *cough* Yeah right, I just
288
// had it happen when pty #349 was allocated. I guess
289
// there was some sort of leak? I only had a few open.
290
if (((st.st_uid != getuid()) ||
291
(st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) &&
295
<< "chownpty failed for device " << ptyName << "::" << d->ttyName
296
<< "\nThis means the communication can be eavesdropped." << endl;
300
revoke(d->ttyName.data());
304
unlockpt(d->masterFd);
307
d->slaveFd = ::open(d->ttyName.data(), O_RDWR | O_NOCTTY);
310
kdWarning(175) << "Can't open slave pseudo teletype" << endl;
311
::close(d->masterFd);
316
#if (defined(__svr4__) || defined(__sgi__))
318
ioctl(d->slaveFd, I_PUSH, "ptem");
319
ioctl(d->slaveFd, I_PUSH, "ldterm");
322
// set xon/xoff & control keystrokes
323
// without the '::' some version of HP-UX thinks, this declares
324
// the struct in this class, in this method, and fails to find
325
// the correct tc[gs]etattr
326
struct ::termios ttmode;
328
_tcgetattr(d->slaveFd, &ttmode);
331
ttmode.c_iflag &= ~(IXOFF | IXON);
333
ttmode.c_iflag |= (IXOFF | IXON);
337
ttmode.c_iflag &= ~IUTF8;
339
ttmode.c_iflag |= IUTF8;
342
ttmode.c_cc[VINTR] = CINTR;
343
ttmode.c_cc[VQUIT] = CQUIT;
344
ttmode.c_cc[VERASE] = CERASE;
346
_tcsetattr(d->slaveFd, &ttmode);
349
ioctl(d->slaveFd, TIOCSWINSZ, (char *)&d->winSize);
351
fcntl(d->masterFd, F_SETFD, FD_CLOEXEC);
352
fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC);
361
// don't bother resetting unix98 pty, it will go away after closing master anyway.
362
if (memcmp(d->ttyName.data(), "/dev/pts/", 9)) {
365
if (!stat(d->ttyName.data(), &st)) {
366
chown(d->ttyName.data(), 0, st.st_gid == getgid() ? 0 : -1);
367
chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
370
fcntl(d->masterFd, F_SETFD, 0);
375
::close(d->masterFd);
376
d->masterFd = d->slaveFd = -1;
381
// Setup job control //////////////////////////////////
383
// Become session leader, process group leader,
384
// and get rid of the old controlling terminal.
387
// make our slave pty the new controlling terminal.
389
ioctl(d->slaveFd, TIOCSCTTY, 0);
391
// SVR4 hack: the first tty opened after setsid() becomes controlling tty
392
::close(::open(d->ttyName, O_WRONLY, 0));
395
// make our new process group the foreground group on the pty
397
#if defined(_POSIX_VERSION) || defined(__svr4__)
398
tcsetpgrp (d->slaveFd, pgrp);
399
#elif defined(TIOCSPGRP)
400
ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp);
404
void KPty::login(const char *user, const char *remotehost)
408
utmp.cmdFd = d->masterFd;
409
utmp << "/usr/sbin/utempter" << "-a" << d->ttyName << "";
410
utmp.start(KProcess::Block);
412
Q_UNUSED(remotehost);
413
#elif defined(USE_LOGIN)
415
struct utmp l_struct;
416
memset(&l_struct, 0, sizeof(struct utmp));
417
// note: strncpy without terminators _is_ correct here. man 4 utmp
420
strncpy(l_struct.ut_name, user, UT_NAMESIZE);
423
strncpy(l_struct.ut_host, remotehost, UT_HOSTSIZE);
426
str_ptr = d->ttyName.data();
427
if (!memcmp(str_ptr, "/dev/", 5))
429
strncpy(l_struct.ut_line, str_ptr, UT_LINESIZE);
432
// Handle 64-bit time_t properly, where it may be larger
433
// than the integral type of ut_time.
437
l_struct.ut_time=ut_time_temp;
443
Q_UNUSED(remotehost);
451
utmp.cmdFd = d->masterFd;
452
utmp << "/usr/sbin/utempter" << "-d" << d->ttyName;
453
utmp.start(KProcess::Block);
454
#elif defined(USE_LOGIN)
455
const char *str_ptr = d->ttyName.data();
456
if (!memcmp(str_ptr, "/dev/", 5))
460
const char *sl_ptr = strrchr(str_ptr, '/');
462
str_ptr = sl_ptr + 1;
469
void KPty::setWinSize(int lines, int columns)
471
d->winSize.ws_row = (unsigned short)lines;
472
d->winSize.ws_col = (unsigned short)columns;
473
if (d->masterFd >= 0)
474
ioctl( d->masterFd, TIOCSWINSZ, (char *)&d->winSize );
477
void KPty::setXonXoff(bool useXonXoff)
479
d->xonXoff = useXonXoff;
480
if (d->masterFd >= 0) {
481
// without the '::' some version of HP-UX thinks, this declares
482
// the struct in this class, in this method, and fails to find
483
// the correct tc[gs]etattr
484
struct ::termios ttmode;
486
_tcgetattr(d->masterFd, &ttmode);
489
ttmode.c_iflag &= ~(IXOFF | IXON);
491
ttmode.c_iflag |= (IXOFF | IXON);
493
_tcsetattr(d->masterFd, &ttmode);
497
void KPty::setUtf8Mode(bool useUtf8)
501
if (d->masterFd >= 0) {
502
// without the '::' some version of HP-UX thinks, this declares
503
// the struct in this class, in this method, and fails to find
504
// the correct tc[gs]etattr
505
struct ::termios ttmode;
507
_tcgetattr(d->masterFd, &ttmode);
510
ttmode.c_iflag &= ~IUTF8;
512
ttmode.c_iflag |= IUTF8;
514
_tcsetattr(d->masterFd, &ttmode);
519
const char *KPty::ttyName() const
521
return d->ttyName.data();
524
int KPty::masterFd() const
529
int KPty::slaveFd() const
535
bool KPty::chownpty(bool grant)
538
proc << locate("exe", BASE_CHOWN) << (grant?"--grant":"--revoke") << QString::number(d->masterFd);
539
return proc.start(KProcess::Block) && proc.normalExit() && !proc.exitStatus();