summaryrefslogtreecommitdiff
path: root/src/stable/process.cpp
blob: 63e306c2f6e8183973f532fd4e58110abdab3c17 (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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
 * Copyright (C) 2007-2013 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/process.h"
#include <unistd.h>
#include <stdarg.h>

#ifndef WIN32
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/select.h>
#include <errno.h>
#else
#include <windows.h> 
#include <tchar.h>
#include <wchar.h>
#endif


#include "bu/config.h"

Bu::Process::ProcData::ProcData() :
#ifdef WIN32
    hStdIn( NULL ),
    hStdOut( NULL ),
    hStdErr( NULL )
#else
    iStdIn( -1 ),
    iStdOut( -1 ),
    iStdErr( -1 )
#endif
{
}


Bu::Process::Process( Flags eFlags, const char *sName, char *const argv[] ) :
    iProcStatus( 0 ),
    bBlocking( true ),
    bStdOutEos( true ),
    bStdErrEos( true )
{
    gexec( eFlags, sName, argv );
}

Bu::Process::Process( Flags eFlags, const char *sName, const char *argv, ...) :
    iProcStatus( 0 ),
    bBlocking( true ),
    bStdOutEos( true ),
    bStdErrEos( true )
{
    int iCnt = 0;
    va_list ap;
    va_start( ap, argv );
    for(; va_arg( ap, const char *); iCnt++ ) { }
    va_end( ap );

    char const **list = new char const *[iCnt+2];
    va_start( ap, argv );
    list[0] = argv;
    for( int j = 1; j <= iCnt; j++ )
    {
        list[j] = va_arg( ap, const char *);
    }
    list[iCnt+1] = NULL;
    va_end( ap );

    gexec( eFlags, sName, (char *const *)list );
    delete[] list;
}

Bu::Process::Process( Flags eFlags, const Bu::Process::Options &opt, const char *sName, char *const argv[] ) :
    iProcStatus( 0 ),
    bBlocking( true ),
    bStdOutEos( true ),
    bStdErrEos( true ),
    opt( opt )
{
    gexec( eFlags, sName, argv );
}

Bu::Process::Process( Flags eFlags, const Bu::Process::Options &opt, const char *sName, const char *argv, ...) :
    iProcStatus( 0 ),
    bBlocking( true ),
    bStdOutEos( true ),
    bStdErrEos( true ),
    opt( opt )
{
    int iCnt = 0;
    va_list ap;
    va_start( ap, argv );
    for(; va_arg( ap, const char *); iCnt++ ) { }
    va_end( ap );

    char const **list = new char const *[iCnt+2];
    va_start( ap, argv );
    list[0] = argv;
    for( int j = 1; j <= iCnt; j++ )
    {
        list[j] = va_arg( ap, const char *);
    }
    list[iCnt+1] = NULL;
    va_end( ap );

    gexec( eFlags, sName, (char *const *)list );
    delete[] list;
}

Bu::Process::~Process()
{
    close();
}

void Bu::Process::wait()
{
    close();
}

void Bu::Process::gexec( Flags eFlags, const char *sName, char *const argv[] )
{
#ifdef WIN32
    bStdErrEos = true;
    SECURITY_ATTRIBUTES saAttr;
    HANDLE hChildStd_IN_Rd;
    HANDLE hChildStd_IN_Wr;
    HANDLE hChildStd_OUT_Rd;
    HANDLE hChildStd_OUT_Wr;
    PROCESS_INFORMATION piProcInfo;

    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    saAttr.bInheritHandle = TRUE;
    saAttr.lpSecurityDescriptor = NULL;

    // Create a pipe for the child process's STDOUT. 
    if ( ! CreatePipe(
                &hChildStd_OUT_Rd,
                &hChildStd_OUT_Wr,
                &saAttr, 0) ) 
        throw "StdoutRd CreatePipe"; 

    // Ensure the read handle to the pipe for STDOUT is not inherited.
    if ( ! SetHandleInformation(
                hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) )
        throw "Stdout SetHandleInformation"; 

    // Create a pipe for the child process's STDIN. 
    if (! CreatePipe(
                &hChildStd_IN_Rd,
                &hChildStd_IN_Wr,
                &saAttr, 0)) 
        throw "Stdin CreatePipe"; 

    // Ensure the write handle to the pipe for STDIN is not inherited. 
    if ( ! SetHandleInformation(
                hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
        throw "Stdin SetHandleInformation"; 

    //TCHAR szCmdline[] = TEXT( sName );
    //PROCESS_INFORMATION pd.piProcInfo; 
    STARTUPINFO siStartInfo;
    BOOL bSuccess = FALSE; 

    // Set up members of the PROCESS_INFORMATION structure. 
    ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );

    // Set up members of the STARTUPINFO structure. 
    // This structure specifies the STDIN and STDOUT handles for redirection.
    ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
    siStartInfo.cb = sizeof(STARTUPINFO); 
    siStartInfo.hStdError = hChildStd_OUT_Wr;
    siStartInfo.hStdOutput = hChildStd_OUT_Wr;
    siStartInfo.hStdInput = hChildStd_IN_Rd;
    siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

    Bu::String sCmd;
    for( char *const *arg = argv; *arg; arg++ )
    {
        if( arg != argv )
            sCmd += " ";
        sCmd += *arg;
    }

    // Create the child process. 
    bSuccess = CreateProcessA(
        sName, //sName,             //module name
        sCmd.getStr(), //(TCHAR *) sName, //szCmdline,     // command line 
        NULL,          // process security attributes 
        NULL,          // primary thread security attributes 
        TRUE,          // handles are inherited 
        CREATE_NO_WINDOW,             // creation flags 
        NULL,          // use parent's environment 
        NULL,          // use parent's current directory 
        &siStartInfo,  // STARTUPINFO pointer 
        &piProcInfo );  // receives PROCESS_INFORMATION 

    // If an error occurs, exit the application. 
    if ( ! bSuccess ) 
    {
        throw Bu::ExceptionBase("Error spawning child process.");
        return;
    }
    else 
    {
        // Close handles to the child process and its primary thread.
        // Some applications might keep these handles to monitor the status
        // of the child process, for example. 
        
        //CloseHandle(pData->pd.piProcInfo.hProcess);
        CloseHandle(piProcInfo.hThread);
        pd.hProcess = piProcInfo.hProcess;

        // Close the ends we can't use
        CloseHandle( hChildStd_OUT_Wr );
        CloseHandle( hChildStd_IN_Rd );
        pd.hStdIn = hChildStd_IN_Wr;
        pd.hStdOut = hChildStd_OUT_Rd;
    }
#else
    int iaStdIn[2];
    int iaStdOut[2];
    int iaStdErr[2];
    pipe( iaStdIn );
    if( eFlags & StdOut )
    {
        pipe( iaStdOut );
        pd.iStdOut = iaStdOut[0];
        bStdOutEos = false;
    }
    if( eFlags & StdErr )
    {
        pipe( iaStdErr );
        pd.iStdErr = iaStdErr[0];
        bStdErrEos = false;
    }

    pd.iStdIn = iaStdIn[1];

//  fcntl( pd.iStdOut, F_SETFL, fcntl( pd.iStdOut, F_GETFL, 0 )|O_NONBLOCK );

    pd.iPid = fork();
    if( pd.iPid == 0 )
    {
        ::close( iaStdIn[1] );
        dup2( iaStdIn[0], 0 );
        if( eFlags & StdOut )
        {
            ::close( iaStdOut[0] );
            dup2( iaStdOut[1], 1 );
        }
        if( eFlags & StdErr )
        {
            ::close( iaStdErr[0] );
            dup2( iaStdErr[1], 2 );
        }
        if( (opt.eFlags&Options::SetGid) )
        {
            setgid( opt.iGid );
        }
        if( (opt.eFlags&Options::SetUid) )
        {
            setuid( opt.iUid );
        }
        execvp( sName, argv );
        throw Bu::ExceptionBase("Hey, execvp failed!");
    }
    ::close( iaStdIn[0] );
    if( eFlags & StdOut )
        ::close( iaStdOut[1] );
    if( eFlags & StdErr )
        ::close( iaStdErr[1] );
#endif
}

void Bu::Process::close()
{
#ifdef WIN32
    if( pd.hStdIn )
    {
        CloseHandle( pd.hStdIn );
        CloseHandle( pd.hStdOut );

        pd.hStdIn = pd.hStdOut = pd.hStdErr = NULL;

        if( !TerminateProcess(pd.hProcess, 1) )
        {
            throw Bu::ExceptionBase("Error closing process.");
        }
        
        GetExitCodeProcess( pd.hProcess, (PDWORD)&iProcStatus );

        CloseHandle( pd.hProcess );
        pd.hProcess = NULL;
    }
#else
    if( pd.iPid )
    {
        if( pd.iStdIn > -1 )
            ::close( pd.iStdIn );
        if( pd.iStdOut > -1 )
            ::close( pd.iStdOut );
        if( pd.iStdErr > -1 )
            ::close( pd.iStdErr );
        waitpid( pd.iPid, &iProcStatus, 0 );
        pd.iPid = 0;
    }
#endif
}

void Bu::Process::closeStdIn()
{
#ifdef WIN32
    CloseHandle( pd.hStdIn );
    pd.hStdIn = NULL;
#else
    ::close( pd.iStdIn );
    pd.iStdIn = -1;
#endif
}

void Bu::Process::closeStdOut()
{
#ifdef WIN32
    CloseHandle( pd.hStdOut );
    pd.hStdOut = NULL;
#else
    ::close( pd.iStdOut );
    pd.iStdOut = -1;
#endif
}

Bu::size Bu::Process::read( void *pBuf, Bu::size nBytes )
{
#ifdef WIN32
    DWORD dwRead;
    DWORD lExitCode;
    BOOL bSuccess = FALSE;
    DWORD dwLen = (DWORD) nBytes;
    bSuccess = ReadFile(
            pd.hStdOut, (char *) pBuf,
            dwLen, &dwRead, NULL);
//  if( dwRead < dwLen )
    {
        bSuccess = GetExitCodeProcess( pd.hProcess, &lExitCode );
        if( lExitCode != STILL_ACTIVE )
        {
            bStdOutEos = true;
        }
    }
    return (int32_t) dwRead;
#else
    if( bStdOutEos )
        return 0;
    fd_set rfds;
    FD_ZERO( &rfds );
    FD_SET( pd.iStdOut, &rfds );
    struct timeval tv = {0, 0};
    if( ::bu_select( pd.iStdOut+1, &rfds, NULL, NULL, &tv ) < 0 )
        throw Bu::ExceptionBase( strerror( errno ) );
    if( FD_ISSET( pd.iStdOut, &rfds ) || bBlocking )
    {
        Bu::size nRead = TEMP_FAILURE_RETRY( ::read( pd.iStdOut, pBuf, nBytes ) );
        if( nRead == 0 )
        {
            bStdOutEos = true;
            checkClose();
            return 0;
        }
        if( nRead < 0 )
        {
            if( errno == EAGAIN )
                return 0;
            throw Bu::ExceptionBase( strerror( errno ) );
        }
        return nRead;
    }
    return 0;
#endif
}

Bu::size Bu::Process::readErr( void *pBuf, Bu::size nBytes )
{
#ifdef WIN32
    if( !pd.hStdErr )
        return 0;
    DWORD dwRead;
    BOOL bSuccess = FALSE;
    DWORD dwLen = (DWORD) nBytes;
    bSuccess = ReadFile(
            pd.hStdErr, (char *) pBuf,
            dwLen, &dwRead, NULL);
    return (int32_t) dwRead;
#else
    if( bStdErrEos )
        return 0;
    fd_set rfds;
    FD_ZERO( &rfds );
    FD_SET( pd.iStdErr, &rfds );
    struct timeval tv = {0, 0};
    if( ::bu_select( pd.iStdErr+1, &rfds, NULL, NULL, &tv ) < 0 )
        throw Bu::ExceptionBase( strerror( errno ) );
    if( FD_ISSET( pd.iStdErr, &rfds ) || bBlocking )
    {
        Bu::size nRead = TEMP_FAILURE_RETRY( ::read( pd.iStdErr, pBuf, nBytes ) );
        if( nRead == 0 )
        {
            bStdErrEos = true;
            checkClose();
            return 0;
        }
        if( nRead < 0 )
        {
            if( errno == EAGAIN )
                return 0;
            throw Bu::ExceptionBase( strerror( errno ) );
        }
        return nRead;
    }
    return 0;
#endif
}

Bu::size Bu::Process::write( const void *pBuf, Bu::size nBytes )
{
#ifdef WIN32
    DWORD dwWritten;
    BOOL bSuccess = FALSE;
    DWORD dwLen = (DWORD) nBytes;
    bSuccess = WriteFile( 
            pd.hStdIn, (const char *) pBuf,
            dwLen, &dwWritten, NULL );
    FlushFileBuffers( pd.hStdIn );
    return (int32_t) dwWritten;
#else
    return TEMP_FAILURE_RETRY( ::write( pd.iStdIn, pBuf, nBytes ) );
#endif
}

Bu::size Bu::Process::tell()
{
    return 0;
}

void Bu::Process::seek( Bu::size )
{
}

void Bu::Process::setPos( Bu::size )
{
}

void Bu::Process::setPosEnd( Bu::size )
{
}

bool Bu::Process::isEos()
{
#ifdef WIN32
    return (pd.hProcess == NULL);
#else
    return (pd.iPid == 0);
#endif
}

bool Bu::Process::isOpen()
{
    return !isEos();
}

void Bu::Process::flush()
{
}

bool Bu::Process::canRead()
{
    return true;
}

bool Bu::Process::canWrite()
{
    return true;
}

bool Bu::Process::isReadable()
{
    return true;
}

bool Bu::Process::isWritable()
{
    return true;
}

bool Bu::Process::isSeekable()
{
    return false;
}

bool Bu::Process::isBlocking()
{
    return true;
}

void Bu::Process::setBlocking( bool bBlocking )
{
#ifdef WIN32
#else
    if( bBlocking )
    {
        if( !bStdOutEos )
            fcntl( pd.iStdOut, F_SETFL, fcntl(pd.iStdOut,F_GETFL,0 )&(~O_NONBLOCK) );
        if( !bStdErrEos )
            fcntl( pd.iStdErr, F_SETFL, fcntl(pd.iStdErr,F_GETFL,0 )&(~O_NONBLOCK) );
    }
    else
    {
        if( !bStdOutEos )
            fcntl( pd.iStdOut, F_SETFL, fcntl( pd.iStdOut, F_GETFL, 0 )|O_NONBLOCK );
        if( !bStdErrEos )
            fcntl( pd.iStdErr, F_SETFL, fcntl( pd.iStdErr, F_GETFL, 0 )|O_NONBLOCK );
    }
    this->bBlocking = bBlocking;
#endif
}

void Bu::Process::setSize( Bu::size )
{
}

Bu::size Bu::Process::getBlockSize() const
{
    return 0;
}

Bu::size Bu::Process::getSize() const
{
    return 0;
}

Bu::String Bu::Process::getLocation() const
{
    return "";
}

void Bu::Process::select( bool &bStdOut, bool &bStdErr )
{
#ifdef WIN32
#else
    fd_set rfds;
    FD_ZERO( &rfds );
    if( !bStdOutEos )
        FD_SET( pd.iStdOut, &rfds );
    if( !bStdErrEos )
        FD_SET( pd.iStdErr, &rfds );
    if( ::bu_select( pd.iStdErr+1, &rfds, NULL, NULL, NULL ) < 0 )  
        throw Bu::ExceptionBase( strerror( errno ) );

    if( FD_ISSET( pd.iStdOut, &rfds ) )
        bStdOut = true;
    else
        bStdOut = false;

    if( FD_ISSET( pd.iStdErr, &rfds ) )
        bStdErr = true;
    else
        bStdErr = false;
#endif
}

bool Bu::Process::isRunning()
{
#ifdef WIN32
    DWORD lExitCode;
    GetExitCodeProcess( pd.hProcess, &lExitCode );
    if( lExitCode != STILL_ACTIVE )
        checkClose();
#else
    if( waitpid( pd.iPid, NULL, WNOHANG ) == pd.iPid )
        checkClose();
#endif
    return isOpen();
}

void Bu::Process::ignoreStdErr()
{
#ifdef WIN32
#else
    if( pd.iStdErr == -1 )
        return;
    ::close( pd.iStdErr );
    pd.iStdErr = -1;
    bStdErrEos = true;
#endif
}

pid_t Bu::Process::getPid()
{
#ifdef WIN32
    return 0;
#else
    return pd.iPid;
#endif
}

bool Bu::Process::childExited()
{
#ifdef WIN32
    return pd.hProcess != NULL;
#else
    return WIFEXITED( iProcStatus );
#endif
}

int Bu::Process::childExitStatus()
{
#ifdef WIN32
    return iProcStatus;
#else
    return WEXITSTATUS( iProcStatus );
#endif
}

bool Bu::Process::childSignaled()
{
#ifdef WIN32
    return false;
#else
    return WIFSIGNALED( iProcStatus );
#endif
}

int Bu::Process::childSignal()
{
#ifdef WIN32
    return 0;
#else
    return WTERMSIG( iProcStatus );
#endif
}

bool Bu::Process::childCoreDumped()
{
#ifdef WIN32
    return false;
#else
    return WCOREDUMP( iProcStatus );
#endif
}

void Bu::Process::checkClose()
{
    if( bStdOutEos && bStdErrEos )
    {
        close();
    }
}