1 /*
2 * Copyright (c) 2021, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * The file implements the OTBR Agent.
32 */
33
34 #define OTBR_LOG_TAG "APP"
35
36 #ifdef HAVE_LIBSYSTEMD
37 #include <systemd/sd-daemon.h>
38 #endif
39
40 #include "agent/application.hpp"
41 #include "common/code_utils.hpp"
42 #include "common/mainloop_manager.hpp"
43 #include "utils/infra_link_selector.hpp"
44
45 namespace otbr {
46
47 std::atomic_bool Application::sShouldTerminate(false);
48 const struct timeval Application::kPollTimeout = {10, 0};
49
Application(const std::string & aInterfaceName,const std::vector<const char * > & aBackboneInterfaceNames,const std::vector<const char * > & aRadioUrls,bool aEnableAutoAttach)50 Application::Application(const std::string & aInterfaceName,
51 const std::vector<const char *> &aBackboneInterfaceNames,
52 const std::vector<const char *> &aRadioUrls,
53 bool aEnableAutoAttach)
54 : mInterfaceName(aInterfaceName)
55 #if __linux__
56 , mInfraLinkSelector(aBackboneInterfaceNames)
57 , mBackboneInterfaceName(mInfraLinkSelector.Select())
58 #else
59 , mBackboneInterfaceName(aBackboneInterfaceNames.empty() ? "" : aBackboneInterfaceNames.front())
60 #endif
61 , mNcp(mInterfaceName.c_str(), aRadioUrls, mBackboneInterfaceName, /* aDryRun */ false, aEnableAutoAttach)
62 #if OTBR_ENABLE_BORDER_AGENT
63 , mBorderAgent(mNcp)
64 #endif
65 #if OTBR_ENABLE_BACKBONE_ROUTER
66 , mBackboneAgent(mNcp, aInterfaceName, mBackboneInterfaceName)
67 #endif
68 #if OTBR_ENABLE_OPENWRT
69 , mUbusAgent(mNcp)
70 #endif
71 #if OTBR_ENABLE_REST_SERVER
72 , mRestWebServer(mNcp)
73 #endif
74 #if OTBR_ENABLE_DBUS_SERVER && OTBR_ENABLE_BORDER_AGENT
75 , mDBusAgent(mNcp, mBorderAgent.GetPublisher())
76 #endif
77 #if OTBR_ENABLE_VENDOR_SERVER
78 , mVendorServer(mNcp)
79 #endif
80 {
81 }
82
Init(void)83 void Application::Init(void)
84 {
85 mNcp.Init();
86
87 #if OTBR_ENABLE_BORDER_AGENT
88 mBorderAgent.Init();
89 #endif
90 #if OTBR_ENABLE_BACKBONE_ROUTER
91 mBackboneAgent.Init();
92 #endif
93 #if OTBR_ENABLE_OPENWRT
94 mUbusAgent.Init();
95 #endif
96 #if OTBR_ENABLE_REST_SERVER
97 mRestWebServer.Init();
98 #endif
99 #if OTBR_ENABLE_DBUS_SERVER
100 mDBusAgent.Init();
101 #endif
102 #if OTBR_ENABLE_VENDOR_SERVER
103 mVendorServer.Init();
104 #endif
105 }
106
Deinit(void)107 void Application::Deinit(void)
108 {
109 #if OTBR_ENABLE_BORDER_AGENT
110 mBorderAgent.Deinit();
111 #endif
112
113 mNcp.Deinit();
114 }
115
Run(void)116 otbrError Application::Run(void)
117 {
118 otbrError error = OTBR_ERROR_NONE;
119
120 otbrLogInfo("Thread Border Router started on AIL %s.", mBackboneInterfaceName);
121
122 #ifdef HAVE_LIBSYSTEMD
123 if (getenv("SYSTEMD_EXEC_PID") != nullptr)
124 {
125 otbrLogInfo("Notify systemd the service is ready.");
126
127 // Ignored return value as systemd recommends.
128 // See https://www.freedesktop.org/software/systemd/man/sd_notify.html
129 sd_notify(0, "READY=1");
130 }
131 #endif
132
133 #if OTBR_ENABLE_NOTIFY_UPSTART
134 if (getenv("UPSTART_JOB") != nullptr)
135 {
136 otbrLogInfo("Notify Upstart the service is ready.");
137 if (raise(SIGSTOP))
138 {
139 otbrLogWarning("Failed to notify Upstart.");
140 }
141 }
142 #endif
143
144 // allow quitting elegantly
145 signal(SIGTERM, HandleSignal);
146
147 while (!sShouldTerminate)
148 {
149 otbr::MainloopContext mainloop;
150 int rval;
151
152 mainloop.mMaxFd = -1;
153 mainloop.mTimeout = kPollTimeout;
154
155 FD_ZERO(&mainloop.mReadFdSet);
156 FD_ZERO(&mainloop.mWriteFdSet);
157 FD_ZERO(&mainloop.mErrorFdSet);
158
159 MainloopManager::GetInstance().Update(mainloop);
160
161 rval = select(mainloop.mMaxFd + 1, &mainloop.mReadFdSet, &mainloop.mWriteFdSet, &mainloop.mErrorFdSet,
162 &mainloop.mTimeout);
163
164 if (rval >= 0)
165 {
166 MainloopManager::GetInstance().Process(mainloop);
167
168 #if __linux__
169 {
170 const char *newInfraLink = mInfraLinkSelector.Select();
171
172 if (mBackboneInterfaceName != newInfraLink)
173 {
174 error = OTBR_ERROR_INFRA_LINK_CHANGED;
175 break;
176 }
177 }
178 #endif
179 }
180 else if (errno != EINTR)
181 {
182 error = OTBR_ERROR_ERRNO;
183 otbrLogErr("select() failed: %s", strerror(errno));
184 break;
185 }
186 }
187
188 return error;
189 }
190
HandleSignal(int aSignal)191 void Application::HandleSignal(int aSignal)
192 {
193 sShouldTerminate = true;
194 signal(aSignal, SIG_DFL);
195 }
196
197 } // namespace otbr
198