blob: 5236ff5d529563bbf3c98885d7a8c49efb673c79 [file] [log] [blame]
Tom Joseph22c5ad32017-03-14 18:04:22 +05301#include <sys/socket.h>
2#include <sys/un.h>
3#include <cmath>
4#include <phosphor-logging/log.hpp>
5#include "main.hpp"
6#include "sol_context.hpp"
7#include "sol_manager.hpp"
8
9namespace sol
10{
11
12using namespace phosphor::logging;
13
14void Manager::initHostConsoleFd()
15{
16 struct sockaddr_un addr;
17 int rc = 0;
18 int fd = 0;
19
20 fd = socket(AF_UNIX, SOCK_STREAM, 0);
21 if (fd < 0)
22 {
23 log<level::ERR>("Failed to open the host console socket",
24 entry("ERRNO=%d", errno));
25 throw std::runtime_error("Failed to open the host console socket");
26 }
27
28 memset(&addr, 0, sizeof(addr));
29 addr.sun_family = AF_UNIX;
30 memcpy(&addr.sun_path, &CONSOLE_SOCKET_PATH, CONSOLE_SOCKET_PATH_LEN);
31 consoleFD = std::make_unique<CustomFD>(fd);
32 auto& conFD = *(consoleFD.get());
33
34 rc = connect(conFD(), (struct sockaddr *)&addr, sizeof(addr));
35 if (rc < 0)
36 {
37 log<level::ERR>("Failed to connect to host console socket address",
38 entry("ERRNO=%d", errno));
39 consoleFD.reset();
40 throw std::runtime_error("Failed to connect to console server");
41 }
42}
43
Tom Josephb51f6412017-03-14 18:08:14 +053044int Manager::writeConsoleSocket(const Buffer& input) const
45{
46 auto inBuffer = input.data();
47 auto inBufferSize = input.size();
48 size_t pos = 0;
49 ssize_t rc = 0;
50 int errVal = 0;
51 auto& conFD = *(consoleFD.get());
52
53 for (pos = 0; pos < inBufferSize; pos += rc)
54 {
55 rc = write(conFD(), inBuffer + pos, inBufferSize - pos);
56 if (rc <= 0)
57 {
58 if (errno == EINTR)
59 {
60 log<level::INFO>(" Retrying to handle EINTR",
61 entry("ERRNO=%d", errno));
62 rc = 0;
63 continue;
64 }
65 else
66 {
67 errVal = errno;
68 log<level::ERR>("Failed to write to host console socket",
69 entry("ERRNO=%d", errno));
70 return -errVal;
71 }
72 }
73 }
74
75 return 0;
76}
77
Tom Joseph22c5ad32017-03-14 18:04:22 +053078} // namespace sol