Jorge E. Moreira | f1f7cb3 | 2019-04-15 18:44:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 <common/libs/device_config/device_config.h> |
| 18 | #include <gflags/gflags.h> |
| 19 | #include <glog/logging.h> |
| 20 | |
| 21 | #include "common/libs/fs/shared_fd.h" |
| 22 | #include "host/libs/config/cuttlefish_config.h" |
| 23 | |
| 24 | DEFINE_int32( |
| 25 | server_fd, -1, |
| 26 | "File descriptor to an already created vsock server. If negative a new " |
| 27 | "server will be created at the port specified on the config file"); |
| 28 | |
| 29 | int main(int argc, char** argv) { |
| 30 | ::android::base::InitLogging(argv, android::base::StderrLogger); |
| 31 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 32 | |
| 33 | auto config = vsoc::CuttlefishConfig::Get(); |
| 34 | |
| 35 | cvd::SharedFD server_fd; |
| 36 | if (FLAGS_server_fd < 0) { |
| 37 | unsigned int port = config->config_server_port(); |
| 38 | server_fd = cvd::SharedFD::VsockServer(port, SOCK_STREAM); |
| 39 | } else { |
| 40 | server_fd = cvd::SharedFD::Dup(FLAGS_server_fd); |
| 41 | close(FLAGS_server_fd); |
| 42 | } |
| 43 | |
| 44 | CHECK(server_fd->IsOpen()) << "Error creating or inheriting logcat server: " |
| 45 | << server_fd->StrError(); |
| 46 | |
| 47 | auto device_config = cvd::DeviceConfig::Get(); |
| 48 | if (!device_config) { |
| 49 | LOG(ERROR) << "Failed to obtain device configuration"; |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | // Server loop |
| 54 | while (true) { |
| 55 | auto conn = cvd::SharedFD::Accept(*server_fd); |
| 56 | LOG(INFO) << "Connection received on configuration server"; |
| 57 | |
| 58 | bool succeeded = device_config->SendRawData(conn); |
| 59 | if (succeeded) { |
| 60 | LOG(INFO) << "Successfully sent device configuration"; |
| 61 | } else { |
| 62 | LOG(ERROR) << "Failed to send the device configuration: " |
| 63 | << conn->StrError(); |
| 64 | } |
| 65 | } |
| 66 | } |