summaryrefslogtreecommitdiff
path: root/daemon/vsomeipd.cpp
blob: d79b3005be851612fe92492cafec3933d35d0501 (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
// Copyright (C) 2015-2016 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 <iostream>

#include <vsomeip/vsomeip.hpp>
#include "../implementation/configuration/include/internal.hpp"
#include "../implementation/logging/include/logger.hpp"

#ifdef USE_DLT
#include <dlt/dlt.h>
#include "../implementation/logging/include/defines.hpp"
#endif

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

#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
/*
 * Handle signal to stop the daemon
 */
void vsomeipd_stop(int _signal) {
    if (_signal == SIGINT || _signal == SIGTERM)
        its_application->stop();
    if (_signal == SIGABRT) {
        VSOMEIP_INFO << "Suspending service discovery";
        its_application->set_routing_state(vsomeip::routing_state_e::RS_SUSPENDED);
        its_application->stop();
    }
}
#endif

/*
 * Create a vsomeip application object and start it.
 */
int vsomeipd_process(bool _is_quiet) {
#ifdef USE_DLT
    if (!_is_quiet)
        DLT_REGISTER_APP(VSOMEIP_LOG_DEFAULT_APPLICATION_ID, VSOMEIP_LOG_DEFAULT_APPLICATION_NAME);
#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(VSOMEIP_ROUTING);
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
    // Handle signals
    signal(SIGINT, vsomeipd_stop);
    signal(SIGTERM, vsomeipd_stop);
    signal(SIGABRT, vsomeipd_stop);
#endif
    if (its_application->init()) {
        if (its_application->is_routing()) {
            its_application->start();
            return 0;
        }
        VSOMEIP_ERROR << "vsomeipd has not been configured as routing - abort";
    }
    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) {
    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(0);

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

    return vsomeipd_process(is_quiet);
}