utils.cpp (c453e1641fc46a5a2e6c082b5a561410ecdaf779) utils.cpp (49cfb138af156020599361584b20c9ed591eeeb6)
1#include "utils.hpp"
2
3#include <libpldm/base.h>
4#include <sys/socket.h>
5#include <sys/types.h>
6#include <sys/un.h>
7#include <unistd.h>
8
1#include "utils.hpp"
2
3#include <libpldm/base.h>
4#include <sys/socket.h>
5#include <sys/types.h>
6#include <sys/un.h>
7#include <unistd.h>
8
9#include <phosphor-logging/lg2.hpp>
10
9#include <iostream>
10
11#include <iostream>
12
13PHOSPHOR_LOG2_USING;
14
11namespace pldm
12{
13namespace responder
14{
15namespace utils
16{
17int setupUnixSocket(const std::string& socketInterface)
18{
19 int sock;
20 struct sockaddr_un addr;
21 memset(&addr, 0, sizeof(addr));
22 addr.sun_family = AF_UNIX;
23 if (strnlen(socketInterface.c_str(), sizeof(addr.sun_path)) ==
24 sizeof(addr.sun_path))
25 {
15namespace pldm
16{
17namespace responder
18{
19namespace utils
20{
21int setupUnixSocket(const std::string& socketInterface)
22{
23 int sock;
24 struct sockaddr_un addr;
25 memset(&addr, 0, sizeof(addr));
26 addr.sun_family = AF_UNIX;
27 if (strnlen(socketInterface.c_str(), sizeof(addr.sun_path)) ==
28 sizeof(addr.sun_path))
29 {
26 std::cerr << "setupUnixSocket: UNIX socket path too long" << std::endl;
30 error("setupUnixSocket: UNIX socket path too long");
27 return -1;
28 }
29
30 strncpy(addr.sun_path, socketInterface.c_str(), sizeof(addr.sun_path) - 1);
31
32 if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1)
33 {
31 return -1;
32 }
33
34 strncpy(addr.sun_path, socketInterface.c_str(), sizeof(addr.sun_path) - 1);
35
36 if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1)
37 {
34 std::cerr << "setupUnixSocket: socket() call failed" << std::endl;
38 error("setupUnixSocket: socket() call failed");
35 return -1;
36 }
37
38 if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
39 {
39 return -1;
40 }
41
42 if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
43 {
40 std::cerr << "setupUnixSocket: bind() call failed with errno " << errno
41 << std::endl;
44 error("setupUnixSocket: bind() call failed with errno {ERR}", "ERR",
45 errno);
42 close(sock);
43 return -1;
44 }
45
46 if (listen(sock, 1) == -1)
47 {
46 close(sock);
47 return -1;
48 }
49
50 if (listen(sock, 1) == -1)
51 {
48 std::cerr << "setupUnixSocket: listen() call failed" << std::endl;
52 error("setupUnixSocket: listen() call failed");
49 close(sock);
50 return -1;
51 }
52
53 fd_set rfd;
54 struct timeval tv;
55 tv.tv_sec = 1;
56 tv.tv_usec = 0;
57
58 FD_ZERO(&rfd);
59 FD_SET(sock, &rfd);
60 int nfd = sock + 1;
61 int fd = -1;
62
63 int retval = select(nfd, &rfd, NULL, NULL, &tv);
64 if (retval < 0)
65 {
53 close(sock);
54 return -1;
55 }
56
57 fd_set rfd;
58 struct timeval tv;
59 tv.tv_sec = 1;
60 tv.tv_usec = 0;
61
62 FD_ZERO(&rfd);
63 FD_SET(sock, &rfd);
64 int nfd = sock + 1;
65 int fd = -1;
66
67 int retval = select(nfd, &rfd, NULL, NULL, &tv);
68 if (retval < 0)
69 {
66 std::cerr << "setupUnixSocket: select call failed " << errno
67 << std::endl;
70 error("setupUnixSocket: select call failed {ERR}", "ERR", errno);
68 close(sock);
69 return -1;
70 }
71
72 if ((retval > 0) && (FD_ISSET(sock, &rfd)))
73 {
74 fd = accept(sock, NULL, NULL);
75 if (fd < 0)
76 {
71 close(sock);
72 return -1;
73 }
74
75 if ((retval > 0) && (FD_ISSET(sock, &rfd)))
76 {
77 fd = accept(sock, NULL, NULL);
78 if (fd < 0)
79 {
77 std::cerr << "setupUnixSocket: accept() call failed " << errno
78 << std::endl;
80 error("setupUnixSocket: accept() call failed {ERR}", "ERR", errno);
79 close(sock);
80 return -1;
81 }
82 close(sock);
83 }
84 return fd;
85}
86

--- 11 unchanged lines hidden (view full) ---

98
99 FD_ZERO(&wfd);
100 FD_SET(sock, &wfd);
101 int nfd = sock + 1;
102
103 int retval = select(nfd, NULL, &wfd, NULL, &tv);
104 if (retval < 0)
105 {
81 close(sock);
82 return -1;
83 }
84 close(sock);
85 }
86 return fd;
87}
88

--- 11 unchanged lines hidden (view full) ---

100
101 FD_ZERO(&wfd);
102 FD_SET(sock, &wfd);
103 int nfd = sock + 1;
104
105 int retval = select(nfd, NULL, &wfd, NULL, &tv);
106 if (retval < 0)
107 {
106 std::cerr << "writeToUnixSocket: select call failed " << errno
107 << std::endl;
108 error("writeToUnixSocket: select call failed {ERR}", "ERR", errno);
108 close(sock);
109 return -1;
110 }
111 if (retval == 0)
112 {
113 nwrite = 0;
114 continue;
115 }
116 if ((retval > 0) && (FD_ISSET(sock, &wfd)))
117 {
118 nwrite = write(sock, buf + i, blockSize - i);
119 if (nwrite < 0)
120 {
121 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
122 {
109 close(sock);
110 return -1;
111 }
112 if (retval == 0)
113 {
114 nwrite = 0;
115 continue;
116 }
117 if ((retval > 0) && (FD_ISSET(sock, &wfd)))
118 {
119 nwrite = write(sock, buf + i, blockSize - i);
120 if (nwrite < 0)
121 {
122 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
123 {
123 std::cerr << "writeToUnixSocket: Write call failed with "
124 "EAGAIN or EWOULDBLOCK or EINTR"
125 << std::endl;
124 error(
125 "writeToUnixSocket: Write call failed with EAGAIN or EWOULDBLOCK or EINTR");
126 nwrite = 0;
127 continue;
128 }
126 nwrite = 0;
127 continue;
128 }
129 std::cerr << "writeToUnixSocket: Failed to write" << std::endl;
129 error("writeToUnixSocket: Failed to write {ERR}", "ERR", errno);
130 close(sock);
131 return -1;
132 }
133 }
134 else
135 {
136 nwrite = 0;
137 }
138 }
139 return 0;
140}
141
142} // namespace utils
143} // namespace responder
144} // namespace pldm
130 close(sock);
131 return -1;
132 }
133 }
134 else
135 {
136 nwrite = 0;
137 }
138 }
139 return 0;
140}
141
142} // namespace utils
143} // namespace responder
144} // namespace pldm