1 /*
2 * Copyright (C) 2017 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 <signal.h>
18 #include <unistd.h>
19
20 #include <string>
21 #include <vector>
22
23 #include <gflags/gflags.h>
24 #include <glog/logging.h>
25
26 #include <common/libs/fs/shared_fd.h>
27 #include <common/libs/fs/shared_select.h>
28 #include <common/libs/strings/str_split.h>
29 #include <host/libs/config/cuttlefish_config.h>
30 #include "host/commands/kernel_log_monitor/kernel_log_server.h"
31
32 DEFINE_int32(log_server_fd, -1,
33 "A file descriptor representing a (UNIX) socket from which to "
34 "read the logs. If -1 is given the socket is created according to "
35 "the instance configuration");
36 DEFINE_string(subscriber_fds, "",
37 "A comma separated list of file descriptors (most likely pipes) to"
38 " send boot events to.");
39
SubscribersFromCmdline()40 std::vector<cvd::SharedFD> SubscribersFromCmdline() {
41 // Validate the parameter
42 std::string fd_list = FLAGS_subscriber_fds;
43 for (auto c: fd_list) {
44 if (c != ',' && (c < '0' || c > '9')) {
45 LOG(ERROR) << "Invalid file descriptor list: " << fd_list;
46 std::exit(1);
47 }
48 }
49
50 auto fds = cvd::StrSplit(FLAGS_subscriber_fds, ',');
51 std::vector<cvd::SharedFD> shared_fds;
52 for (auto& fd_str: fds) {
53 auto fd = std::stoi(fd_str);
54 auto shared_fd = cvd::SharedFD::Dup(fd);
55 close(fd);
56 shared_fds.push_back(shared_fd);
57 }
58
59 return shared_fds;
60 }
61
main(int argc,char ** argv)62 int main(int argc, char** argv) {
63 ::android::base::InitLogging(argv, android::base::StderrLogger);
64 google::ParseCommandLineFlags(&argc, &argv, true);
65
66 auto subscriber_fds = SubscribersFromCmdline();
67
68 // Disable default handling of SIGPIPE
69 struct sigaction new_action {
70 }, old_action{};
71 new_action.sa_handler = SIG_IGN;
72 sigaction(SIGPIPE, &new_action, &old_action);
73
74 auto config = vsoc::CuttlefishConfig::Get();
75 if (!config) {
76 LOG(ERROR) << "Unable to get config object";
77 return 1;
78 }
79
80 cvd::SharedFD server;
81 if (FLAGS_log_server_fd < 0) {
82 auto log_name = config->kernel_log_socket_name();
83 server = cvd::SharedFD::SocketLocalServer(log_name.c_str(), false,
84 SOCK_STREAM, 0666);
85 } else {
86 server = cvd::SharedFD::Dup(FLAGS_log_server_fd);
87 close(FLAGS_log_server_fd);
88 }
89
90 if (!server->IsOpen()) {
91 LOG(ERROR) << "Error opening log server: " << server->StrError();
92 return 2;
93 }
94
95 monitor::KernelLogServer klog{server, config->PerInstancePath("kernel.log"),
96 config->deprecated_boot_completed()};
97
98 for (auto subscriber_fd: subscriber_fds) {
99 if (subscriber_fd->IsOpen()) {
100 klog.SubscribeToBootEvents([subscriber_fd](monitor::BootEvent evt) {
101 int retval = subscriber_fd->Write(&evt, sizeof(evt));
102 if (retval < 0) {
103 if (subscriber_fd->GetErrno() != EPIPE) {
104 LOG(ERROR) << "Error while writing to pipe: "
105 << subscriber_fd->StrError();
106 }
107 subscriber_fd->Close();
108 return monitor::SubscriptionAction::CancelSubscription;
109 }
110 return monitor::SubscriptionAction::ContinueSubscription;
111 });
112 } else {
113 LOG(ERROR) << "Subscriber fd isn't valid: " << subscriber_fd->StrError();
114 // Don't return here, we still need to write the logs to a file
115 }
116 }
117
118 for (;;) {
119 cvd::SharedFDSet fd_read;
120 fd_read.Zero();
121
122 klog.BeforeSelect(&fd_read);
123
124 int ret = cvd::Select(&fd_read, nullptr, nullptr, nullptr);
125 if (ret <= 0) continue;
126
127 klog.AfterSelect(fd_read);
128 }
129
130 return 0;
131 }
132