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 15 #include "file-io.hpp" 16 17 #include <sdeventplus/event.hpp> 18 #include <sdeventplus/source/io.hpp> 19 #include <stdplus/fd/create.hpp> 20 #include <stdplus/fd/ops.hpp> 21 #include <stdplus/print.hpp> 22 23 using namespace std::string_view_literals; 24 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; 32 auto sock = 33 socket(SocketDomain::INet6, SocketType::Stream, SocketProto::TCP); 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 43 int main() 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) { 52 while (auto fd = stdplus::fd::accept(listener)) 53 { 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 62 // status to ONGOING 63 data.push_back(2); 64 data.append("Failed to read status "); 65 data.append(e.what()); 66 } 67 68 stdplus::fd::sendExact(*fd, data, 69 stdplus::fd::SendFlags(0)); 70 } 71 }); 72 return event.loop(); 73 } 74 catch (const std::exception& e) 75 { 76 stdplus::println(stderr, "Failed: {}", e.what()); 77 return 1; 78 } 79 } 80