blob: 7ea8761e3546fb5cbbe0e360a953abdc6eb42a3b [file] [log] [blame]
William A. Kennington IIIdffd6522022-02-08 01:40:43 -08001// 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 Zhang1e760602024-03-07 12:35:24 -080015#include "file-io.hpp"
16
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080017#include <sdeventplus/event.hpp>
18#include <sdeventplus/source/io.hpp>
19#include <stdplus/fd/create.hpp>
20#include <stdplus/fd/ops.hpp>
Willy Tu253e6462023-12-28 09:57:27 -080021#include <stdplus/print.hpp>
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080022
Yuxiao Zhang5ca20ff2023-03-30 14:54:11 -070023using namespace std::string_view_literals;
24
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080025// A privileged port that is reserved for querying BMC DHCP completion.
26// This is well known by the clients querying the status.
27constexpr uint16_t kListenPort = 23;
28
29stdplus::ManagedFd createListener()
30{
31 using namespace stdplus::fd;
Patrick Williams2be45232023-05-10 07:51:22 -050032 auto sock = socket(SocketDomain::INet6, SocketType::Stream,
33 SocketProto::TCP);
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080034 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 Zhang1e760602024-03-07 12:35:24 -080043int main()
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080044{
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 Williams2be45232023-05-10 07:51:22 -050052 while (auto fd = stdplus::fd::accept(listener))
53 {
Yuxiao Zhang1e760602024-03-07 12:35:24 -080054 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 Williams2be45232023-05-10 07:51:22 -050068 stdplus::fd::sendExact(*fd, data, stdplus::fd::SendFlags(0));
69 }
Patrick Williams60849572023-10-20 11:19:52 -050070 });
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080071 return event.loop();
72 }
73 catch (const std::exception& e)
74 {
Willy Tu253e6462023-12-28 09:57:27 -080075 stdplus::println(stderr, "Failed: {}", e.what());
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080076 return 1;
77 }
78}