blob: 4aaba41c755da3afec89f405650bf127a466e131 [file] [log] [blame]
Nan Zhou042b5ba2021-06-18 09:32:45 -07001// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2021 Google
3
4#pragma once
5
6#include "config.hpp"
7#include "dbus_loop.hpp"
8#include "file_storage.hpp"
9#include "host_console.hpp"
10#include "log_buffer.hpp"
11#include "service.hpp"
12
13#include <sys/un.h>
14
15/**
16 * @class Service
17 * @brief Log service: watches for events and handles them.
18 */
19class StreamService : public Service
20{
21 public:
22 /**
23 * @brief Constructor for stream-only mode. All arguments should outlive
24 * this class.
25 *
26 * @param streamDestination the destination socket to stream logs.
27 * @param dbusLoop the DbusLoop instance.
28 * @param hostConsole the HostConsole instance.
29 */
30 StreamService(const char* streamDestination, DbusLoop& dbusLoop,
31 HostConsole& hostConsole);
32
33 /**
34 * @brief Destructor; close the file descriptor.
35 */
36 ~StreamService() override;
37
38 /**
39 * @brief Run the service.
40 *
41 * @throw std::exception in case of errors
42 */
43 void run() override;
44
45 protected:
46 /**
47 * @brief Read data from host console and perform actions according to
48 * modes.
49 */
50 virtual void readConsole();
51
52 /**
53 * @brief Stream console data to a datagram unix socket.
54 *
55 * @param data the bytes to stream
56 * @param len the length of the bytes array
57 *
58 * @throw std::exception in case of errors
59 */
60 virtual void streamConsole(const char* data, size_t len);
61
62 /**
63 * @brief Set up stream socket
64 *
65 * @throw std::exception in case of errors
66 */
67 virtual void setStreamSocket();
68
69 private:
70 /** @brief Path to the destination (the rsyslog unix socket) */
71 const char* destinationPath;
72 /** @brief D-Bus event loop. */
73 DbusLoop* dbusLoop;
74 /** @brief Host console connection. */
75 HostConsole* hostConsole;
76 /** @brief File descriptor of the ouput socket */
77 int outputSocketFd;
78 /** @brief Address of the destination (the rsyslog unix socket) */
79 sockaddr_un destination;
80};