summaryrefslogtreecommitdiffstats
path: root/fastos/src/vespa/fastos/unix_app.cpp
blob: baf7960dc1dc608c80f2a2dfff4a2b73caf7d54e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
******************************************************************************
* @author  Oivind H. Danielsen
* @date    Creation date: 2000-01-18
* @file
* Implementation of FastOS_UNIX_Application methods.
*****************************************************************************/

#include "app.h"
#include "time.h"
#include "process.h"
#include "unix_ipc.h"
#include <unistd.h>
#include <csignal>
#include <getopt.h>


FastOS_UNIX_Application::FastOS_UNIX_Application ()
    : _processStarter(nullptr),
      _ipcHelper(nullptr)
{
}

FastOS_UNIX_Application::~FastOS_UNIX_Application() = default;

extern "C"
{
extern char **environ;
};

int
FastOS_UNIX_Application::GetOpt (const char *optionsString,
            const char* &optionArgument,
            int &optionIndex)
{
    int rc = getopt(_argc, _argv, optionsString);
    optionArgument = optarg;
    optionIndex = optind;
    return rc;
}

int
FastOS_UNIX_Application::GetOptLong(const char *optionsString,
               const char* &optionArgument,
               int &optionIndex,
               const struct option *longopts,
               int *longindex)
{
    int rc = getopt_long(_argc, _argv, optionsString,
                         longopts,
                         longindex);

    optionArgument = optarg;
    optionIndex = optind;
    return rc;
}

void
FastOS_UNIX_Application::resetOptIndex(int optionIndex)
{
    optind = optionIndex;
}

bool FastOS_UNIX_Application::PreThreadInit ()
{
    bool rc = true;
    if (FastOS_ApplicationInterface::PreThreadInit()) {
        // Ignore SIGPIPE
        struct sigaction act;
        act.sa_handler = SIG_IGN;
        sigemptyset(&act.sa_mask);
        act.sa_flags = 0;
        sigaction(SIGPIPE, &act, nullptr);

        if (useProcessStarter()) {
            _processStarter = new FastOS_UNIX_ProcessStarter(this);
        }
    } else {
        rc = false;
        fprintf(stderr, "FastOS_ApplicationInterface::PreThreadInit failed\n");
    }
    return rc;
}

bool FastOS_UNIX_Application::Init ()
{
    bool rc = false;

    if(FastOS_ApplicationInterface::Init())
    {
        int ipcDescriptor = -1;

        if (useIPCHelper()) {
            _ipcHelper = new FastOS_UNIX_IPCHelper(this, ipcDescriptor);
            GetThreadPool()->NewThread(_ipcHelper);
        }

        rc = true;
    }

    return rc;
}

void FastOS_UNIX_Application::Cleanup ()
{
    if(_ipcHelper != nullptr)
        _ipcHelper->Exit();

    if (_processStarter != nullptr) {
        {
            std::unique_lock<std::mutex> guard;
            if (_processListMutex) {
                guard = getProcessGuard();
            }
        }
        delete _processStarter;
        _processStarter = nullptr;
    }

    FastOS_ApplicationInterface::Cleanup();
}

FastOS_UNIX_ProcessStarter *
FastOS_UNIX_Application::GetProcessStarter ()
{
    return _processStarter;
}

void FastOS_UNIX_Application::
AddToIPCComm (FastOS_UNIX_Process *process)
{
    if(_ipcHelper != nullptr)
        _ipcHelper->AddProcess(process);
}

void FastOS_UNIX_Application::
RemoveFromIPCComm (FastOS_UNIX_Process *process)
{
    if(_ipcHelper != nullptr)
        _ipcHelper->RemoveProcess(process);
}