summaryrefslogtreecommitdiffstats
path: root/tdesu/process.cpp
blob: 28e3bb110d79020482d5b92c26fd77edfb98b1c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/* vi: ts=8 sts=4 sw=4
 *
 * $Id$
 *
 * This file is part of the KDE project, module tdesu.
 * Copyright (C) 1999,2000 Geert Jansen <[email protected]>
 * 
 * This file contains code from TEShell.C of the KDE konsole. 
 * Copyright (c) 1997,1998 by Lars Doelle <[email protected]> 
 *
 * This is free software; you can use this library under the GNU Library 
 * General Public License, version 2. See the file "COPYING.LIB" for the 
 * exact licensing terms.
 *
 * process.cpp: Functionality to build a front end to password asking
 *  terminal programs.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <termios.h>
#include <signal.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/ioctl.h>

#if defined(__SVR4) && defined(sun)
#include <stropts.h>
#include <sys/stream.h>
#endif

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>                // Needed on some systems.
#endif

#include <tqglobal.h>
#include <tqcstring.h>
#include <tqfile.h>

#include <tdeconfig.h>
#include <kdebug.h>
#include <kstandarddirs.h>

#include "process.h"
#include "tdesu_pty.h"
#include "kcookie.h"

int PtyProcess::waitMS(int fd,int ms)
{
	struct timeval tv;
	tv.tv_sec = 0; 
	tv.tv_usec = 1000*ms;

	fd_set fds;
	FD_ZERO(&fds);
	FD_SET(fd,&fds);
	return select(fd+1, &fds, 0L, 0L, &tv);
}

/*
** Basic check for the existence of @p pid.
** Returns true iff @p pid is an extant process.
*/
bool PtyProcess::checkPid(pid_t pid)
{
	TDEConfig* config = TDEGlobal::config();
	config->setGroup("super-user-command");
	TQString superUserCommand = config->readEntry("super-user-command", DEFAULT_SUPER_USER_COMMAND);
	//sudo does not accept signals from user so we except it
	if (superUserCommand == "sudo") {
		return true;
	} else {
	return kill(pid,0) == 0;
	}
}

/*
** Check process exit status for process @p pid.
** On error (no child, no exit), return Error (-1).
** If child @p pid has exited, return its exit status,
** (which may be zero).
** If child @p has not exited, return NotExited (-2).
*/

int PtyProcess::checkPidExited(pid_t pid)
{
	int state, ret;
	ret = waitpid(pid, &state, WNOHANG);

	if (ret < 0) 
	{
		kdError(900) << k_lineinfo << "waitpid(): " << perror << "\n";
		return Error;
	}
	if (ret == pid) 
	{
		if (WIFEXITED(state))
			return WEXITSTATUS(state);
		return Killed;
	}

	return NotExited;
}


class PtyProcess::PtyProcessPrivate
{
public:
    QCStringList env;
};


PtyProcess::PtyProcess()
{
    m_bTerminal = false;
    m_bErase = false;
    m_pPTY = 0L;
    d = new PtyProcessPrivate;
}


int PtyProcess::init()
{
    delete m_pPTY;
    m_pPTY = new PTY();
    m_Fd = m_pPTY->getpt();
    if (m_Fd < 0)
        return -1;
    if ((m_pPTY->grantpt() < 0) || (m_pPTY->unlockpt() < 0)) 
    {
        kdError(900) << k_lineinfo << "Master setup failed.\n";
        m_Fd = -1;
        return -1;
    }
    m_TTY = m_pPTY->ptsname();
    m_Inbuf.resize(0);
    return 0;
}


PtyProcess::~PtyProcess()
{
    delete m_pPTY;
    delete d;
}

/** Set additinal environment variables. */
void PtyProcess::setEnvironment( const QCStringList &env )
{
    d->env = env;
}

const QCStringList& PtyProcess::environment() const
{
    return d->env;
}

/*
 * Read one line of input. The terminal is in canonical mode, so you always
 * read a line at at time, but it's possible to receive multiple lines in
 * one time.
 */

TQCString PtyProcess::readLine(bool block)
{
    int pos;
    TQCString ret;

    if (!m_Inbuf.isEmpty()) 
    {
        pos = m_Inbuf.find('\n');
        if (pos == -1) 
        {
            ret = m_Inbuf;
            m_Inbuf.resize(0);
        } else
        {
            ret = m_Inbuf.left(pos);
            m_Inbuf = m_Inbuf.mid(pos+1);
        }
        return ret;
    }

    int flags = fcntl(m_Fd, F_GETFL);
    if (flags < 0) 
    {
        kdError(900) << k_lineinfo << "fcntl(F_GETFL): " << perror << "\n";
        return ret;
    }
    int oflags = flags;
    if (block)
        flags &= ~O_NONBLOCK;
    else
        flags |= O_NONBLOCK;

    if ((flags != oflags) && (fcntl(m_Fd, F_SETFL, flags) < 0))
    {
       // We get an error here when the child process has closed 
       // the file descriptor already.
       return ret;
    }

    int nbytes;
    char buf[256];
    while (1) 
    {
        nbytes = read(m_Fd, buf, 255);
        if (nbytes == -1) 
        {
            if (errno == EINTR)
                continue;
            else break;
        }
        if (nbytes == 0)
            break;        // eof

        buf[nbytes] = '\000';
        m_Inbuf += buf;

        pos = m_Inbuf.find('\n');
        if (pos == -1) 
        {
            ret = m_Inbuf;
            m_Inbuf.resize(0);
        } else 
        {
            ret = m_Inbuf.left(pos);
            m_Inbuf = m_Inbuf.mid(pos+1);
        }
        break;
    }

    return ret;
}

TQCString PtyProcess::readAll(bool block)
{
    TQCString ret;

    if (!m_Inbuf.isEmpty()) 
    {
        // if there is still something in the buffer, we need not block.
        // we should still try to read any further output, from the fd, though.
        block = false;
        ret = m_Inbuf;
        m_Inbuf.resize(0);
    }

    int flags = fcntl(m_Fd, F_GETFL);
    if (flags < 0) 
    {
        kdError(900) << k_lineinfo << "fcntl(F_GETFL): " << perror << "\n";
        return ret;
    }
    int oflags = flags;
    if (block)
        flags &= ~O_NONBLOCK;
    else
        flags |= O_NONBLOCK;

    if ((flags != oflags) && (fcntl(m_Fd, F_SETFL, flags) < 0))
    {
       // We get an error here when the child process has closed 
       // the file descriptor already.
       return ret;
    }

    int nbytes;
    char buf[256];
    while (1) 
    {
        nbytes = read(m_Fd, buf, 255);
        if (nbytes == -1) 
        {
            if (errno == EINTR)
                continue;
            else break;
        }
        if (nbytes == 0)
            break;        // eof

        buf[nbytes] = '\000';
        ret += buf;
        break;
    }

    return ret;
}


void PtyProcess::writeLine(const TQCString &line, bool addnl)
{
    if (!line.isEmpty())
        write(m_Fd, line, line.length());
    if (addnl)
        write(m_Fd, "\n", 1);
}


void PtyProcess::unreadLine(const TQCString &line, bool addnl)
{
    TQCString tmp = line;
    if (addnl)
        tmp += '\n';
    if (!tmp.isEmpty())
        m_Inbuf.prepend(tmp);
}

/*
 * Fork and execute the command. This returns in the parent.
 */

int PtyProcess::exec(const TQCString &command, const QCStringList &args)
{
    kdDebug(900) << k_lineinfo << "Running `" << command << "'\n";

    if (init() < 0)
        return -1;

    // Open the pty slave before forking. See SetupTTY()
    int slave = open(m_TTY, O_RDWR);
    if (slave < 0) 
    {
        kdError(900) << k_lineinfo << "Could not open slave pty.\n";
        return -1;
    } 

    if ((m_Pid = fork()) == -1) 
    {
        kdError(900) << k_lineinfo << "fork(): " << perror << "\n";
        return -1;
    } 

    // Parent
    if (m_Pid) 
    {
        close(slave);
        return 0;
    }

    // Child
    if (SetupTTY(slave) < 0)
        _exit(1);

    for(QCStringList::ConstIterator it = d->env.begin();
        it != d->env.end(); it++)
    {
        putenv(const_cast<TQCString&>(*it).data());
    }
    unsetenv("TDE_FULL_SESSION");
    
    // set temporarily LC_ALL to C, for su (to be able to parse "Password:")
    const char* old_lc_all = getenv( "LC_ALL" );
    if( old_lc_all != NULL )
        setenv( "TDESU_LC_ALL", old_lc_all, 1 );
    else
        unsetenv( "TDESU_LC_ALL" );
    setenv("LC_ALL", "C", 1);

    // From now on, terminal output goes through the tty.

    TQCString path;
    if (command.contains('/'))
        path = command;
    else 
    {
        TQString file = TDEStandardDirs::findExe(command);
        if (file.isEmpty()) 
        {
            kdError(900) << k_lineinfo << command << " not found\n"; 
            _exit(1);
        } 
        path = TQFile::encodeName(file);
    }

    const char **argp = (const char **)malloc((args.count()+2)*sizeof(char *));
    int i = 0;
    argp[i++] = path;
    for (QCStringList::ConstIterator it=args.begin(); it!=args.end(); ++it)
        argp[i++] = *it;

    argp[i] = 0L;
        
    execv(path, (char * const *)argp);
    kdError(900) << k_lineinfo << "execv(\"" << path << "\"): " << perror << "\n";
    _exit(1);
    return -1; // Shut up compiler. Never reached.
}


/*
 * Wait until the terminal is set into no echo mode. At least one su 
 * (RH6 w/ Linux-PAM patches) sets noecho mode AFTER writing the Password: 
 * prompt, using TCSAFLUSH. This flushes the terminal I/O queues, possibly 
 * taking the password  with it. So we wait until no echo mode is set 
 * before writing the password.
 * Note that this is done on the slave fd. While Linux allows tcgetattr() on
 * the master side, Solaris doesn't.
 */

int PtyProcess::WaitSlave()
{
    int slave = open(m_TTY, O_RDWR);
    if (slave < 0) 
    {
        kdError(900) << k_lineinfo << "Could not open slave tty.\n";
        return -1;
    }

    kdDebug(900) << k_lineinfo << "Child pid " << m_Pid << endl;
    
    struct termios tio;
    while (1) 
    {
	if (!checkPid(m_Pid))
	{
		close(slave);
		return -1;
	}
        if (tcgetattr(slave, &tio) < 0) 
        {
            kdError(900) << k_lineinfo << "tcgetattr(): " << perror << "\n";
            close(slave);
            return -1;
        }
        if (tio.c_lflag & ECHO) 
        {
            kdDebug(900) << k_lineinfo << "Echo mode still on.\n";
	    waitMS(slave,100);
            continue;
        }
        break;
    }
    close(slave);
    return 0;
}


int PtyProcess::enableLocalEcho(bool enable)
{
    int slave = open(m_TTY, O_RDWR);
    if (slave < 0) 
    {
        kdError(900) << k_lineinfo << "Could not open slave tty.\n";
        return -1;
    }
    struct termios tio;
    if (tcgetattr(slave, &tio) < 0) 
    {
        kdError(900) << k_lineinfo << "tcgetattr(): " << perror << "\n";
        close(slave); return -1;
    }
    if (enable)
        tio.c_lflag |= ECHO;
    else
        tio.c_lflag &= ~ECHO;
    if (tcsetattr(slave, TCSANOW, &tio) < 0) 
    {
        kdError(900) << k_lineinfo << "tcsetattr(): " << perror << "\n";
        close(slave); return -1;
    }
    close(slave);
    return 0;
}


/*
 * Copy output to stdout until the child process exists, or a line of output
 * matches `m_Exit'.
 * We have to use waitpid() to test for exit. Merely waiting for EOF on the
 * pty does not work, because the target process may have children still
 * attached to the terminal.
 */

int PtyProcess::waitForChild()
{
    int retval = 1;

    fd_set fds;
    FD_ZERO(&fds);

    while (1) 
    {
        FD_SET(m_Fd, &fds);
        int ret = select(m_Fd+1, &fds, 0L, 0L, 0L);
        if (ret == -1) 
        {
            if (errno != EINTR) 
            {
                kdError(900) << k_lineinfo << "select(): " << perror << "\n";
                return -1;
            }
            ret = 0;
        }

        if (ret) 
        {
            TQCString output = readAll(false);
            bool lineStart = true;
            while (!output.isNull()) 
            {
                if (!m_Exit.isEmpty())
                {
                    // match exit string only at line starts
                    int pos = output.find(m_Exit.data());
                    if ((pos >= 0) && ((pos == 0 && lineStart) || (output.at (pos - 1) == '\n')))
                    {
                        kill(m_Pid, SIGTERM);
                    }
                }
                if (m_bTerminal) 
                {
                    fputs(output, stdout);
                    fflush(stdout);
                }
                lineStart = output.at( output.length() - 1 ) == '\n';
                output = readAll(false);
            }
        }

	ret = checkPidExited(m_Pid);
	if (ret == Error)
	{
		if (errno == ECHILD) retval = 0;
		else retval = 1;
		break;
	}
	else if (ret == Killed)
	{
		retval = 0;
		break;
	}
	else if (ret == NotExited)
	{
		// keep checking
	}
	else
	{
		retval = ret;
		break;
	}
    }
    return retval;
}
   
/*
 * SetupTTY: Creates a new session. The filedescriptor "fd" should be
 * connected to the tty. It is closed after the tty is reopened to make it
 * our controlling terminal. This way the tty is always opened at least once
 * so we'll never get EIO when reading from it.
 */

int PtyProcess::SetupTTY(int fd)
{    
    // Reset signal handlers
    for (int sig = 1; sig < NSIG; sig++)
        signal(sig, SIG_DFL);
    signal(SIGHUP, SIG_IGN);

    // Close all file handles
    struct rlimit rlp;
    getrlimit(RLIMIT_NOFILE, &rlp);
    for (int i = 0; i < (int)rlp.rlim_cur; i++)
        if (i != fd) close(i); 

    // Create a new session.
    setsid();

    // Open slave. This will make it our controlling terminal
    int slave = open(m_TTY, O_RDWR);
    if (slave < 0) 
    {
        kdError(900) << k_lineinfo << "Could not open slave side: " << perror << "\n";
        return -1;
    }
    close(fd);

#if defined(__SVR4) && defined(sun)

    // Solaris STREAMS environment.
    // Push these modules to make the stream look like a terminal.
    ioctl(slave, I_PUSH, "ptem");
    ioctl(slave, I_PUSH, "ldterm");

#endif

#ifdef TIOCSCTTY
    ioctl(slave, TIOCSCTTY, NULL);
#endif

    // Connect stdin, stdout and stderr
    dup2(slave, 0); dup2(slave, 1); dup2(slave, 2);
    if (slave > 2) 
        close(slave);

    // Disable OPOST processing. Otherwise, '\n' are (on Linux at least)
    // translated to '\r\n'.
    struct termios tio;
    if (tcgetattr(0, &tio) < 0) 
    {
        kdError(900) << k_lineinfo << "tcgetattr(): " << perror << "\n";
        return -1;
    }
    tio.c_oflag &= ~OPOST;
    if (tcsetattr(0, TCSANOW, &tio) < 0) 
    {
        kdError(900) << k_lineinfo << "tcsetattr(): " << perror << "\n";
        return -1;
    }

    return 0;
}

void PtyProcess::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }