summaryrefslogtreecommitdiff
path: root/examples/routingmanagerd/routingmanagerd.cpp
blob: 7290808ff9b3263ef4fb3d47c5b57856744f358b (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
// Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <iostream>

#include <vsomeip/vsomeip.hpp>
#include <vsomeip/internal/logger.hpp>

#ifdef USE_DLT
#ifndef ANDROID
#include <dlt/dlt.h>
#endif
#endif

static std::shared_ptr<vsomeip::application> its_application;

#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
static vsomeip::routing_state_e routing_state = vsomeip::routing_state_e::RS_RUNNING;
static bool stop_application = false;
static bool stop_sighandler = false;
static std::condition_variable_any sighandler_condition;
static std::recursive_mutex sighandler_mutex;
#endif

#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
/*
 * Handle signal to stop the daemon
 */
void routingmanagerd_stop(int _signal) {
    // Do not log messages in signal handler as this can cause deadlock in boost logger
    switch (_signal) {
    case SIGINT:
    case SIGTERM:
        stop_application = true;
        break;

    case SIGUSR1:
        routing_state = vsomeip::routing_state_e::RS_SUSPENDED;
        break;

    case SIGUSR2:
        routing_state = vsomeip::routing_state_e::RS_RESUMED;
        break;

    default:
        ;
    };

    std::unique_lock<std::recursive_mutex> its_lock(sighandler_mutex);
    sighandler_condition.notify_one();
}
#endif

/*
 * Create a vsomeip application object and start it.
 */
int routingmanagerd_process(bool _is_quiet) {
#ifdef USE_DLT
#ifndef ANDROID
    if (!_is_quiet)
        DLT_REGISTER_APP(VSOMEIP_LOG_DEFAULT_APPLICATION_ID, VSOMEIP_LOG_DEFAULT_APPLICATION_NAME);
#endif
#else
    (void)_is_quiet;
#endif

    std::shared_ptr<vsomeip::runtime> its_runtime
        = vsomeip::runtime::get();

    if (!its_runtime) {
        return -1;
    }

    // Create the application object
    its_application = its_runtime->create_application("routingmanagerd");
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
    std::thread sighandler_thread([]() {
        // Unblock signals for this thread only
        sigset_t handler_mask;
        sigemptyset(&handler_mask);
        sigaddset(&handler_mask, SIGUSR1);
        sigaddset(&handler_mask, SIGUSR2);
        sigaddset(&handler_mask, SIGTERM);
        sigaddset(&handler_mask, SIGINT);
        sigaddset(&handler_mask, SIGSEGV);
        sigaddset(&handler_mask, SIGABRT);
        pthread_sigmask(SIG_UNBLOCK, &handler_mask, NULL);

        // Handle the following signals
        signal(SIGINT, routingmanagerd_stop);
        signal(SIGTERM, routingmanagerd_stop);
        signal(SIGUSR1, routingmanagerd_stop);
        signal(SIGUSR2, routingmanagerd_stop);

        while (!stop_sighandler) {
            std::unique_lock<std::recursive_mutex> its_lock(sighandler_mutex);
            sighandler_condition.wait(its_lock);
            if (stop_application) {
                its_application->stop();
                return;
            } else if (routing_state == vsomeip::routing_state_e::RS_RESUMED ||
                    routing_state == vsomeip::routing_state_e::RS_SUSPENDED){
                VSOMEIP_INFO << "Received signal for setting routing_state to: 0x"
                       << std::hex << static_cast<int>(routing_state );
                its_application->set_routing_state(routing_state);
            }
        }
    });
#endif
    if (its_application->init()) {
        if (its_application->is_routing()) {
            its_application->start();
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
            sighandler_thread.join();
#endif
            return 0;
        }
        VSOMEIP_ERROR << "routingmanagerd has not been configured as routing - abort";
    }
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
    std::unique_lock<std::recursive_mutex> its_lock(sighandler_mutex);
    stop_sighandler = true;
    sighandler_condition.notify_one();
    sighandler_thread.join();
#endif
    return -1;
}

/*
 * Parse command line options
 * -h | --help          print usage information
 * -d | --daemonize     start background processing by forking the process
 * -q | --quiet         do _not_ use dlt logging
 *
 * and start processing.
 */
int main(int argc, char **argv) {
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
    // Block all signals
    sigset_t mask;
    sigfillset(&mask);
    sigprocmask(SIG_SETMASK, &mask, NULL);
#endif
    bool must_daemonize(false);
    bool is_quiet(false);
    if (argc > 1) {
        for (int i = 0; i < argc; i++) {
            std::string its_argument(argv[i]);
            if (its_argument == "-d" || its_argument == "--daemonize") {
                must_daemonize = true;
            } else if (its_argument == "-q" || its_argument == "--quiet") {
                is_quiet = true;
            } else if (its_argument == "-h" || its_argument == "--help") {
                std::cout << "usage: "
                        << argv[0] << " [-h|--help][-d|--daemonize][-q|--quiet]"
                        << std::endl;
                return 0;
            }
        }
    }

    /* Fork the process if processing shall be done in the background */
    if (must_daemonize) {
        pid_t its_process, its_signature;

        its_process = fork();

        if (its_process < 0) {
            return EXIT_FAILURE;
        }

        if (its_process > 0) {
            return EXIT_SUCCESS;
        }

        umask(0111);

        its_signature = setsid();
        if (its_signature < 0) {
            return EXIT_FAILURE;
        }
    }

    return routingmanagerd_process(is_quiet);
}