summaryrefslogtreecommitdiff
path: root/extpipe.cpp
blob: eb057d1b9193c8315b93e806c6eaf0967852f3e2 (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
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#include <vdr/tools.h>
#include <vdr/thread.h>
#include "extpipe.h"



cExtPipe::cExtPipe(void)
{
    pid = -1;
    f_stderr = -1;
    f_stdout=  -1;
}

cExtPipe::~cExtPipe()
{
    int status;
    Close(status);
}

bool cExtPipe::Open(const char *Command)
{
    int fd_stdout[2];
    int fd_stderr[2];

    if (pipe(fd_stdout) < 0)
    {
        LOG_ERROR;
        return false;
    }
    if (pipe(fd_stderr) < 0)
    {
        close(fd_stdout[0]);
        close(fd_stdout[1]);
        LOG_ERROR;
        return false;
    }

    if ((pid = fork()) < 0)   // fork failed
    {
        LOG_ERROR;
        close(fd_stdout[0]);
        close(fd_stdout[1]);
        close(fd_stderr[0]);
        close(fd_stderr[1]);
        return false;
    }

    if (pid > 0)   // parent process
    {
        close(fd_stdout[1]); // close write fd, we need only read fd
        close(fd_stderr[1]); // close write fd, we need only read fd
        f_stdout = fd_stdout[0];
        f_stderr = fd_stderr[0];
        return true;
    }
    else   // child process
    {
        close(fd_stdout[0]); // close read fd, we need only write fd
        close(fd_stderr[0]); // close read fd, we need only write fd

        if (dup2(fd_stdout[1], STDOUT_FILENO) == -1)   // now redirect
        {
            LOG_ERROR;
            close(fd_stderr[1]);
            close(fd_stdout[1]);
            _exit(-1);
        }

        if (dup2(fd_stderr[1], STDERR_FILENO) == -1)   // now redirect
        {
            LOG_ERROR;
            close(fd_stderr[1]);
            close(fd_stdout[1]);
            _exit(-1);
        }

        int MaxPossibleFileDescriptors = getdtablesize();
        for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
            close(i); //close all dup'ed filedescriptors
        if (execl("/bin/sh", "sh", "-c", Command, NULL) == -1)
        {
            LOG_ERROR_STR(Command);
            close(fd_stderr[1]);
            close(fd_stdout[1]);
            _exit(-1);
        }
        _exit(0);
    }
}

int cExtPipe::Close(int &status)
{
    int ret = -1;

    if (f_stderr!=-1)
    {
        close(f_stderr);
        f_stderr = -1;
    }

    if (f_stdout!=-1)
    {
        close(f_stdout);
        f_stdout=-1;
    }

    if (pid > 0)
    {
        int i = 5;
        while (i > 0)
        {
            ret = waitpid(pid, &status, WNOHANG);
            if (ret < 0)
            {
                if (errno != EINTR && errno != ECHILD)
                {
                    LOG_ERROR;
                    break;
                }
            }
            else if (ret == pid)
                break;
            i--;
            cCondWait::SleepMs(100);
        }
        if (!i)
        {
            kill(pid, SIGKILL);
            ret = -1;
        }
        else if (ret == -1 || !WIFEXITED(status))
            ret = -1;
        pid = -1;
    }

    return ret;
}