blob: d7e8fdf9d38e4e7722033e0f90d33f383d971d1f [file] [log] [blame]
Jorge E. Moreira81afca12018-07-26 16:48:49 -07001/*
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>
Jorge E. Moreiraa57b7ba2019-05-02 17:01:44 -070018#include <unistd.h>
19
20#include <string>
21#include <vector>
Jorge E. Moreira81afca12018-07-26 16:48:49 -070022
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -080023#include <android-base/strings.h>
Jorge E. Moreira81afca12018-07-26 16:48:49 -070024#include <gflags/gflags.h>
25#include <glog/logging.h>
26
27#include <common/libs/fs/shared_fd.h>
28#include <common/libs/fs/shared_select.h>
29#include <host/libs/config/cuttlefish_config.h>
Jorge E. Moreira66e9a1a2018-07-26 17:17:47 -070030#include "host/commands/kernel_log_monitor/kernel_log_server.h"
Jorge E. Moreira81afca12018-07-26 16:48:49 -070031
Jorge E. Moreirab1ec4352019-06-04 11:43:08 -070032DEFINE_int32(log_pipe_fd, -1,
Jorge E. Moreira81afca12018-07-26 16:48:49 -070033 "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");
Jorge E. Moreiraa57b7ba2019-05-02 17:01:44 -070036DEFINE_string(subscriber_fds, "",
37 "A comma separated list of file descriptors (most likely pipes) to"
38 " send boot events to.");
39
40std::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
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -080050 auto fds = android::base::Split(FLAGS_subscriber_fds, ",");
Jorge E. Moreiraa57b7ba2019-05-02 17:01:44 -070051 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}
Jorge E. Moreira81afca12018-07-26 16:48:49 -070061
62int main(int argc, char** argv) {
63 ::android::base::InitLogging(argv, android::base::StderrLogger);
64 google::ParseCommandLineFlags(&argc, &argv, true);
65
Jorge E. Moreiraa57b7ba2019-05-02 17:01:44 -070066 auto subscriber_fds = SubscribersFromCmdline();
67
Jorge E. Moreira81afca12018-07-26 16:48:49 -070068 // 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 }
Cody Schuffeleneb242c42019-12-13 15:16:50 -080079 auto instance = config->ForDefaultInstance();
Jorge E. Moreira81afca12018-07-26 16:48:49 -070080
Jorge E. Moreirab1ec4352019-06-04 11:43:08 -070081 cvd::SharedFD pipe;
82 if (FLAGS_log_pipe_fd < 0) {
Cody Schuffeleneb242c42019-12-13 15:16:50 -080083 auto log_name = instance.kernel_log_pipe_name();
Jorge E. Moreirab1ec4352019-06-04 11:43:08 -070084 pipe = cvd::SharedFD::Open(log_name.c_str(), O_RDONLY);
Jorge E. Moreira81afca12018-07-26 16:48:49 -070085 } else {
Jorge E. Moreirab1ec4352019-06-04 11:43:08 -070086 pipe = cvd::SharedFD::Dup(FLAGS_log_pipe_fd);
87 close(FLAGS_log_pipe_fd);
Jorge E. Moreira81afca12018-07-26 16:48:49 -070088 }
89
Jorge E. Moreirab1ec4352019-06-04 11:43:08 -070090 if (!pipe->IsOpen()) {
91 LOG(ERROR) << "Error opening log pipe: " << pipe->StrError();
Jorge E. Moreira81afca12018-07-26 16:48:49 -070092 return 2;
93 }
94
Cody Schuffeleneb242c42019-12-13 15:16:50 -080095 monitor::KernelLogServer klog{pipe, instance.PerInstancePath("kernel.log"),
Jorge E. Moreira81afca12018-07-26 16:48:49 -070096 config->deprecated_boot_completed()};
97
Jorge E. Moreiraa57b7ba2019-05-02 17:01:44 -070098 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));
Jorge E. Moreira81afca12018-07-26 16:48:49 -0700102 if (retval < 0) {
Jorge E. Moreiraa57b7ba2019-05-02 17:01:44 -0700103 if (subscriber_fd->GetErrno() != EPIPE) {
Jorge E. Moreira81afca12018-07-26 16:48:49 -0700104 LOG(ERROR) << "Error while writing to pipe: "
Jorge E. Moreiraa57b7ba2019-05-02 17:01:44 -0700105 << subscriber_fd->StrError();
Jorge E. Moreira81afca12018-07-26 16:48:49 -0700106 }
Jorge E. Moreiraa57b7ba2019-05-02 17:01:44 -0700107 subscriber_fd->Close();
Jorge E. Moreira81afca12018-07-26 16:48:49 -0700108 return monitor::SubscriptionAction::CancelSubscription;
109 }
110 return monitor::SubscriptionAction::ContinueSubscription;
111 });
112 } else {
Jorge E. Moreiraa57b7ba2019-05-02 17:01:44 -0700113 LOG(ERROR) << "Subscriber fd isn't valid: " << subscriber_fd->StrError();
Jorge E. Moreira81afca12018-07-26 16:48:49 -0700114 // 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}