William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Yuxiao Zhang | 1e76060 | 2024-03-07 12:35:24 -0800 | [diff] [blame^] | 15 | #include "file-io.hpp" |
| 16 | |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 17 | #include <sdeventplus/event.hpp> |
| 18 | #include <sdeventplus/source/io.hpp> |
| 19 | #include <stdplus/fd/create.hpp> |
| 20 | #include <stdplus/fd/ops.hpp> |
Willy Tu | 253e646 | 2023-12-28 09:57:27 -0800 | [diff] [blame] | 21 | #include <stdplus/print.hpp> |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 22 | |
Yuxiao Zhang | 5ca20ff | 2023-03-30 14:54:11 -0700 | [diff] [blame] | 23 | using namespace std::string_view_literals; |
| 24 | |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 25 | // A privileged port that is reserved for querying BMC DHCP completion. |
| 26 | // This is well known by the clients querying the status. |
| 27 | constexpr uint16_t kListenPort = 23; |
| 28 | |
| 29 | stdplus::ManagedFd createListener() |
| 30 | { |
| 31 | using namespace stdplus::fd; |
Patrick Williams | 2be4523 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 32 | auto sock = socket(SocketDomain::INet6, SocketType::Stream, |
| 33 | SocketProto::TCP); |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 34 | setFileFlags(sock, getFileFlags(sock).set(stdplus::fd::FileFlag::NonBlock)); |
| 35 | sockaddr_in6 addr = {}; |
| 36 | addr.sin6_family = AF_INET6; |
| 37 | addr.sin6_port = htons(kListenPort); |
| 38 | bind(sock, addr); |
| 39 | listen(sock, 10); |
| 40 | return sock; |
| 41 | } |
| 42 | |
Yuxiao Zhang | 1e76060 | 2024-03-07 12:35:24 -0800 | [diff] [blame^] | 43 | int main() |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 44 | { |
| 45 | try |
| 46 | { |
| 47 | auto listener = createListener(); |
| 48 | auto event = sdeventplus::Event::get_default(); |
| 49 | sdeventplus::source::IO do_accept( |
| 50 | event, listener.get(), EPOLLIN | EPOLLET, |
| 51 | [&](sdeventplus::source::IO&, int, uint32_t) { |
Patrick Williams | 2be4523 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 52 | while (auto fd = stdplus::fd::accept(listener)) |
| 53 | { |
Yuxiao Zhang | 1e76060 | 2024-03-07 12:35:24 -0800 | [diff] [blame^] | 54 | std::string data; |
| 55 | try |
| 56 | { |
| 57 | data = fileRead(statusFile); |
| 58 | } |
| 59 | catch (const std::exception& e) |
| 60 | { |
| 61 | // we don't want to fail the upgrade process, set the status |
| 62 | // to ONGOING |
| 63 | data.push_back(2); |
| 64 | data.append("Failed to read status "); |
| 65 | data.append(e.what()); |
| 66 | } |
| 67 | |
Patrick Williams | 2be4523 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 68 | stdplus::fd::sendExact(*fd, data, stdplus::fd::SendFlags(0)); |
| 69 | } |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 70 | }); |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 71 | return event.loop(); |
| 72 | } |
| 73 | catch (const std::exception& e) |
| 74 | { |
Willy Tu | 253e646 | 2023-12-28 09:57:27 -0800 | [diff] [blame] | 75 | stdplus::println(stderr, "Failed: {}", e.what()); |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 76 | return 1; |
| 77 | } |
| 78 | } |