forked from veos-sxarr-NEC/veosinfo_source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ve_sock.c
196 lines (181 loc) · 5.04 KB
/
ve_sock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Copyright (C) 2017-2018 NEC Corporation
* This file is part of the VEOS information library.
*
* The VEOS information library is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either version
* 2.1 of the License, or (at your option) any later version.
*
* The VEOS information library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the VEOS information library; if not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file ve_sock.c
* @brief Handles IPC communication between RPM library and VEOS
*
* @internal
* @author RPM command
*/
#include <stdio.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include "ve_sock.h"
#include "veosinfo_log.h"
/**
* @brief Create socket for communication with VEOS module
*
* @param sockpath[in] Socket file path name
*
* @return Socket file descriptor on success and negative values on failure
*/
int velib_sock(char *sockpath)
{
struct sockaddr_un sa = {0};
int sockfd = -1;
int retval = -1;
char *sock_path = sockpath;
VE_RPMLIB_TRACE("Entering");
VE_RPMLIB_INFO("SOCKPATH:%s", sockpath);
if (!sockpath) {
VE_RPMLIB_ERR("Wrong argument received: sockpath = %p",
sockpath);
errno = EINVAL;
goto hndl_return;
}
/* Create a socket to enable communication between VEOS and RPM library
*/
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0) {
VE_RPMLIB_ERR("Failed to create '%s' socket: %s",
sock_path, strerror(errno));
goto hndl_return;
}
VE_RPMLIB_DEBUG("Socket created successfully and socket descriptor = %d",
sockfd);
retval = sockfd;
if (strlen(sockpath) > (sizeof(sa.sun_path) - 1)) {
VE_RPMLIB_ERR("Socket path is too long.: %s\n", sockpath);
errno = ENAMETOOLONG;
retval = -2;
goto hndl_return;
}
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, sock_path, sizeof(sa.sun_path));
sa.sun_path[sizeof(sa.sun_path) - 1] = '\0';
/* Connect with VEOS socket to enable communication between VEOS and
* RPM library
*/
if (-1 == connect(sockfd, (struct sockaddr *)&sa, sizeof(sa))) {
VE_RPMLIB_ERR("Connection to socket failed: %s",
strerror(errno));
retval = -2;
}
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief Communicate with VEOS modules to send data
*
* @param socket_fd[in] File descriptor used to communicate with VEOS
* @param buf[in] Pointer to buffer to send
* @param max_len[in] Size of buffer
*
* @return positive value on success, -1 on failure
*/
int velib_send_cmd(int socket_fd, void *buf, int max_len)
{
ssize_t transferred = 0;
ssize_t write_byte = 0;
VE_RPMLIB_TRACE("Entering");
if (!buf) {
VE_RPMLIB_ERR("Wrong argument received: buf: %p", buf);
errno = EINVAL;
transferred = -1;
goto send_error;
}
while ((write_byte = send(socket_fd, buf + transferred,
max_len - transferred, MSG_NOSIGNAL)) != 0) {
if (-1 == write_byte) {
if (errno == EINTR || errno == EAGAIN) {
continue;
} else if (EPIPE == errno) {
VE_RPMLIB_ERR("IPC failed: %s", strerror(errno));
perror("IPC failed");
close(socket_fd);
abort();
} else {
VE_RPMLIB_ERR("Writing on socket failed: %s",
strerror(errno));
perror("Writing on socket failed");
close(socket_fd);
abort();
}
}
transferred += write_byte;
if (transferred == max_len) {
VE_RPMLIB_DEBUG("successfully transferred = %zu," \
" write_byte = %zu",
transferred, write_byte);
break;
}
VE_RPMLIB_DEBUG("transferred = %zu, remaining_bytes = %zu",
transferred, (max_len-transferred));
}
send_error:
VE_RPMLIB_TRACE("Exiting");
return transferred;
}
/**
* @brief Communicate with VEOS modules to receive data
*
* @param socket_fd[in] File descriptor to communicate with VEOS
* @param buf[out] Pointer to buffer to receive
* @param max_len[in] Size of buffer
*
* @return 0 on success and -1 on failure
*/
int velib_recv_cmd(int socket_fd, void *buf, int max_len)
{
ssize_t read_byte = -1;
VE_RPMLIB_TRACE("Entering");
if (!buf) {
VE_RPMLIB_ERR("Wrong argument received: buf: %p", buf);
errno = EINVAL;
goto recv_error;
}
while ((read_byte = recv(socket_fd, buf, max_len, 0)) == -1) {
if (errno == EINTR || errno == EAGAIN) {
VE_RPMLIB_ERR("Received command from VEOS"
"to RPM failed :%s",
strerror(errno));
continue;
} else {
VE_RPMLIB_ERR("Reading from socket failed: %s",
strerror(errno));
perror("Reading from socket failed");
close(socket_fd);
abort();
}
}
if (!read_byte) {
VE_RPMLIB_ERR("peer has performed an orderly shutdown");
close(socket_fd);
abort();
}
VE_RPMLIB_DEBUG("successfully read %zu bytes", read_byte);
recv_error:
VE_RPMLIB_TRACE("Exiting");
return read_byte;
}