1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <array>
18 #include <cstdint>
19 #include <cstring>
20 #include <memory>
21 #include <utility>
22 
23 #include "android-base/logging.h"
24 
25 #include "wifilogd/main_loop.h"
26 #include "wifilogd/protocol.h"
27 
28 namespace android {
29 namespace wifilogd {
30 
31 namespace {
32 constexpr auto kMainBufferSizeBytes = 128 * 1024;
33 // TODO(b/32840641): Tune the sleep time.
34 constexpr auto kTransientErrorSleepTimeNsec = 100 * 1000;  // 100 usec
35 }
36 
MainLoop(const std::string & socket_name)37 MainLoop::MainLoop(const std::string& socket_name)
38     : MainLoop(socket_name, std::make_unique<Os>(),
39                std::make_unique<CommandProcessor>(kMainBufferSizeBytes)) {}
40 
MainLoop(const std::string & socket_name,std::unique_ptr<Os> os,std::unique_ptr<CommandProcessor> command_processor)41 MainLoop::MainLoop(const std::string& socket_name, std::unique_ptr<Os> os,
42                    std::unique_ptr<CommandProcessor> command_processor)
43     : os_(std::move(os)), command_processor_(std::move(command_processor)) {
44   Os::Errno err;
45   std::tie(sock_fd_, err) = os_->GetControlSocket(socket_name);
46   if (err) {
47     PLOG(FATAL) << "Failed to get control socket";
48   }
49 }
50 
RunOnce()51 void MainLoop::RunOnce() {
52   std::array<uint8_t, protocol::kMaxMessageSize> input_buf;
53   size_t datagram_len;
54   Os::Errno err;
55   std::tie(datagram_len, err) =
56       os_->ReceiveDatagram(sock_fd_, input_buf.data(), input_buf.size());
57   if (err) {
58     ProcessError(err);
59     return;
60   }
61 
62   if (datagram_len > protocol::kMaxMessageSize) {
63     // TODO(b/32098735): Increment stats counter.
64     datagram_len = protocol::kMaxMessageSize;
65   }
66 
67   command_processor_->ProcessCommand(input_buf.data(), datagram_len,
68                                      Os::kInvalidFd);
69 }
70 
71 // Private methods below.
72 
ProcessError(Os::Errno err)73 void MainLoop::ProcessError(Os::Errno err) {
74   if (err == EINTR || err == ENOMEM) {
75     // TODO(b/32098735): Increment stats counter.
76     os_->Nanosleep(kTransientErrorSleepTimeNsec);
77     return;
78   }
79 
80   // Any other error is unexpected, and assumed to be non-recoverable.
81   // (If, e.g., our socket is in a bad state, then we won't be able to receive
82   // any new log messages.)
83   PLOG(FATAL) << "Unexpected error";
84 }
85 
86 }  // namespace wifilogd
87 }  // namespace android
88