1 // Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <signal.h>
9 #include <unistd.h>
10 #include <thread>
11 #include <condition_variable>
12 #include <mutex>
13 #include <iostream>
14 
15 #include <vsomeip/vsomeip.hpp>
16 #include <vsomeip/internal/logger.hpp>
17 
18 #ifdef USE_DLT
19 #include <dlt/dlt.h>
20 
21 #endif
22 
23 static std::shared_ptr<vsomeip::application> its_application;
24 
25 #ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
26 static vsomeip::routing_state_e routing_state = vsomeip::routing_state_e::RS_RUNNING;
27 static bool stop_application = false;
28 static bool stop_sighandler = false;
29 static std::condition_variable_any sighandler_condition;
30 static std::recursive_mutex sighandler_mutex;
31 #endif
32 
33 #ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
34 /*
35  * Handle signal to stop the daemon
36  */
routingmanagerd_stop(int _signal)37 void routingmanagerd_stop(int _signal) {
38     // Do not log messages in signal handler as this can cause deadlock in boost logger
39     switch (_signal) {
40     case SIGINT:
41     case SIGTERM:
42         stop_application = true;
43         break;
44 
45     case SIGUSR1:
46         routing_state = vsomeip::routing_state_e::RS_SUSPENDED;
47         break;
48 
49     case SIGUSR2:
50         routing_state = vsomeip::routing_state_e::RS_RESUMED;
51         break;
52 
53     default:
54         ;
55     };
56 
57     std::unique_lock<std::recursive_mutex> its_lock(sighandler_mutex);
58     sighandler_condition.notify_one();
59 }
60 #endif
61 
62 /*
63  * Create a vsomeip application object and start it.
64  */
routingmanagerd_process(bool _is_quiet)65 int routingmanagerd_process(bool _is_quiet) {
66 #ifdef USE_DLT
67     if (!_is_quiet)
68         DLT_REGISTER_APP(VSOMEIP_LOG_DEFAULT_APPLICATION_ID, VSOMEIP_LOG_DEFAULT_APPLICATION_NAME);
69 #else
70     (void)_is_quiet;
71 #endif
72 
73     std::shared_ptr<vsomeip::runtime> its_runtime
74         = vsomeip::runtime::get();
75 
76     if (!its_runtime) {
77         return -1;
78     }
79 
80     // Create the application object
81     its_application = its_runtime->create_application("routingmanagerd");
82 #ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
83     std::thread sighandler_thread([]() {
84         // Unblock signals for this thread only
85         sigset_t handler_mask;
86         sigemptyset(&handler_mask);
87         sigaddset(&handler_mask, SIGUSR1);
88         sigaddset(&handler_mask, SIGUSR2);
89         sigaddset(&handler_mask, SIGTERM);
90         sigaddset(&handler_mask, SIGINT);
91         sigaddset(&handler_mask, SIGSEGV);
92         sigaddset(&handler_mask, SIGABRT);
93         pthread_sigmask(SIG_UNBLOCK, &handler_mask, NULL);
94 
95         // Handle the following signals
96         signal(SIGINT, routingmanagerd_stop);
97         signal(SIGTERM, routingmanagerd_stop);
98         signal(SIGUSR1, routingmanagerd_stop);
99         signal(SIGUSR2, routingmanagerd_stop);
100 
101         while (!stop_sighandler) {
102             std::unique_lock<std::recursive_mutex> its_lock(sighandler_mutex);
103             sighandler_condition.wait(its_lock);
104             if (stop_application) {
105                 its_application->stop();
106                 return;
107             } else if (routing_state == vsomeip::routing_state_e::RS_RESUMED ||
108                     routing_state == vsomeip::routing_state_e::RS_SUSPENDED){
109                 VSOMEIP_INFO << "Received signal for setting routing_state to: 0x"
110                        << std::hex << static_cast<int>(routing_state );
111                 its_application->set_routing_state(routing_state);
112             }
113         }
114     });
115 #endif
116     if (its_application->init()) {
117         if (its_application->is_routing()) {
118             its_application->start();
119 #ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
120             sighandler_thread.join();
121 #endif
122             return 0;
123         }
124         VSOMEIP_ERROR << "routingmanagerd has not been configured as routing - abort";
125     }
126 #ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
127     std::unique_lock<std::recursive_mutex> its_lock(sighandler_mutex);
128     stop_sighandler = true;
129     sighandler_condition.notify_one();
130     sighandler_thread.join();
131 #endif
132     return -1;
133 }
134 
135 /*
136  * Parse command line options
137  * -h | --help          print usage information
138  * -d | --daemonize     start background processing by forking the process
139  * -q | --quiet         do _not_ use dlt logging
140  *
141  * and start processing.
142  */
main(int argc,char ** argv)143 int main(int argc, char **argv) {
144 #ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
145     // Block all signals
146     sigset_t mask;
147     sigfillset(&mask);
148     sigprocmask(SIG_SETMASK, &mask, NULL);
149 #endif
150     bool must_daemonize(false);
151     bool is_quiet(false);
152     if (argc > 1) {
153         for (int i = 0; i < argc; i++) {
154             std::string its_argument(argv[i]);
155             if (its_argument == "-d" || its_argument == "--daemonize") {
156                 must_daemonize = true;
157             } else if (its_argument == "-q" || its_argument == "--quiet") {
158                 is_quiet = true;
159             } else if (its_argument == "-h" || its_argument == "--help") {
160                 std::cout << "usage: "
161                         << argv[0] << " [-h|--help][-d|--daemonize][-q|--quiet]"
162                         << std::endl;
163                 return 0;
164             }
165         }
166     }
167 
168     /* Fork the process if processing shall be done in the background */
169     if (must_daemonize) {
170         pid_t its_process, its_signature;
171 
172         its_process = fork();
173 
174         if (its_process < 0) {
175             return EXIT_FAILURE;
176         }
177 
178         if (its_process > 0) {
179             return EXIT_SUCCESS;
180         }
181 
182         umask(0111);
183 
184         its_signature = setsid();
185         if (its_signature < 0) {
186             return EXIT_FAILURE;
187         }
188     }
189 
190     return routingmanagerd_process(is_quiet);
191 }
192