summaryrefslogtreecommitdiffstats
path: root/fastos/src/vespa/fastos/unix_process.h
blob: a86474eccf70b610e9e5e1521325ecce31f84443 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
//************************************************************************
/**
 * Class definitions for FastOS_UNIX_Process.
 *
 * @author  Div, Oivind H. Danielsen
 */

#pragma once

#include "process.h"
#include "app.h"
#include <string>
#include <memory>
#include <future>

class FastOS_UNIX_RealProcess;
class FastOS_RingBuffer;

class FastOS_UNIX_Process : public FastOS_ProcessInterface
{
private:
    FastOS_UNIX_Process(const FastOS_UNIX_Process&);
    FastOS_UNIX_Process& operator=(const FastOS_UNIX_Process&);

    unsigned int _pid;
    bool _died;
    int _returnCode;
public:
    class DescriptorHandle
    {
    private:
        DescriptorHandle(const DescriptorHandle &);
        DescriptorHandle& operator=(const DescriptorHandle &);

    public:
        int _fd;
        bool _wantRead;
        bool _wantWrite;
        bool _canRead;
        bool _canWrite;
        int _pollIdx;
        std::unique_ptr<FastOS_RingBuffer> _readBuffer;
        std::unique_ptr<FastOS_RingBuffer> _writeBuffer;
        DescriptorHandle();
        ~DescriptorHandle();
        void CloseHandle();
    };
private:
    DescriptorHandle _descriptor[4];

    std::string _runDir;
    std::string _stdoutRedirName;
    std::string _stderrRedirName;
    bool _killed;

    FastOS_UNIX_ProcessStarter *GetProcessStarter () {
        return static_cast<FastOS_UNIX_Application *>(_app)->GetProcessStarter();
    }

    bool InternalWait (int *returnCode, int timeOutSeconds, bool *pollStillRunning);
public:
    enum DescriptorType
    {
        TYPE_STDOUT,
        TYPE_STDERR,
        TYPE_STDIN,
        TYPE_COUNT
    };

    enum Constants
    {
        TYPE_READCOUNT = 3
    };
    std::unique_ptr<std::promise<void>> _closing;
    FastOS_ProcessRedirectListener *GetListener (DescriptorType type)
    {
        if(type == TYPE_STDOUT)
            return _stdoutListener;
        else if(type == TYPE_STDERR)
            return _stderrListener;

        return nullptr;
    }

    void CloseListener (DescriptorType type)
    {
        if(type == TYPE_STDOUT)
        {
            if(_stdoutListener != nullptr)
            {
                _stdoutListener->OnReceiveData(nullptr, 0);
                _stdoutListener = nullptr;
            }
        }
        else if(type == TYPE_STDERR)
        {
            if(_stderrListener != nullptr)
            {
                _stderrListener->OnReceiveData(nullptr, 0);
                _stderrListener = nullptr;
            }
        }
    }

    FastOS_UNIX_Process (const char *cmdLine, bool pipeStdin = false,
                         FastOS_ProcessRedirectListener *stdoutListener = nullptr,
                         FastOS_ProcessRedirectListener *stderrListener = nullptr,
                         int bufferSize = 65535);
    ~FastOS_UNIX_Process ();
    bool CreateInternal (bool useShell);
    bool Create () override { return CreateInternal(false); }
    bool CreateWithShell () override  { return CreateInternal(true); }
    bool WriteStdin (const void *data, size_t length) override;
    bool Signal(int sig);
    bool Kill () override;
    bool Wait (int *returnCode, int timeOutSeconds = -1) override;
    bool PollWait (int *returnCode, bool *stillRunning) override;
    void SetProcessId (unsigned int pid) { _pid = pid; }
    unsigned int GetProcessId() override { return _pid; }
    void DeathNotification (int returnCode) {
        _returnCode = returnCode;
        _died = true;
    }
    bool GetDeathFlag () { return _died; }
    int BuildStreamMask (bool useShell);

    void CloseDescriptor (DescriptorType type)
    {
        _descriptor[type].CloseHandle();
    }

    void SetDescriptor (DescriptorType type, int descriptor)
    {
        _descriptor[type]._fd = descriptor;
    }

    DescriptorHandle &GetDescriptorHandle(DescriptorType type)
    {
        return _descriptor[type];
    }

    bool GetKillFlag () {return _killed; }

    const char *GetRunDir() const { return _runDir.c_str(); }
    const char *GetStdoutRedirName() const { return _stdoutRedirName.c_str(); }
    const char *GetStderrRedirName() const { return _stderrRedirName.c_str(); }
};


class FastOS_UNIX_RealProcess;
class FastOS_UNIX_ProcessStarter
{
private:
    FastOS_UNIX_ProcessStarter(const FastOS_UNIX_ProcessStarter&);
    FastOS_UNIX_ProcessStarter& operator=(const FastOS_UNIX_ProcessStarter&);

protected:
    FastOS_ApplicationInterface *_app;

    FastOS_UNIX_RealProcess *_processList;

    pid_t _pid;
    bool _closedProxyProcessFiles;
    bool _hasDirectChildren;

    void AddChildProcess (FastOS_UNIX_RealProcess *node);
    void RemoveChildProcess (FastOS_UNIX_RealProcess *node);

    void PollReapDirectChildren();

public:
    FastOS_UNIX_ProcessStarter (FastOS_ApplicationInterface *app);
    ~FastOS_UNIX_ProcessStarter ();

    void CloseProxiedChildDescs();
    void CloseProxyDescs(int stdinPipedDes, int stdoutPipedDes, int stderrPipedDes,
                         int handshakeDes0, int handshakeDes1);

    bool CreateProcess (FastOS_UNIX_Process *process, bool useShell,
                        bool pipeStdin, bool pipeStdout, bool pipeStderr);
    bool Wait (FastOS_UNIX_Process *process, int timeOutSeconds, bool *pollStillRunning);
};