blob: bf429cb39102c27a87267685a7578629d6a58b74 [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 Williamsc66ebc32024-08-16 15:21:56 -040032 auto sock =
33 socket(SocketDomain::INet6, SocketType::Stream, 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 Williamsc66ebc32024-08-16 15:21:56 -040052 while (auto fd = stdplus::fd::accept(listener))
Yuxiao Zhang1e760602024-03-07 12:35:24 -080053 {
Patrick Williamsc66ebc32024-08-16 15:21:56 -040054 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 }
Yuxiao Zhang1e760602024-03-07 12:35:24 -080067
Patrick Williamsc66ebc32024-08-16 15:21:56 -040068 stdplus::fd::sendExact(*fd, data,
69 stdplus::fd::SendFlags(0));
70 }
71 });
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080072 return event.loop();
73 }
74 catch (const std::exception& e)
75 {
Willy Tu253e6462023-12-28 09:57:27 -080076 stdplus::println(stderr, "Failed: {}", e.what());
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080077 return 1;
78 }
79}