2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "namespace.h"
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/event.h>
42 #include "un-namespace.h"
43 #include "nscachedcli.h"
45 #define NS_DEFAULT_CACHED_IO_TIMEOUT 4
47 static int safe_write(struct cached_connection_ *, const void *, size_t);
48 static int safe_read(struct cached_connection_ *, void *, size_t);
49 static int send_credentials(struct cached_connection_ *, int);
52 * safe_write writes data to the specified connection and tries to do it in
53 * the very safe manner. We ensure, that we can write to the socket with
54 * kevent. If the data_size can't be sent in one piece, then it would be
58 safe_write(struct cached_connection_ *connection, const void *data,
61 struct kevent eventlist;
65 struct timespec timeout;
70 timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
74 nevents = _kevent(connection->write_queue, NULL, 0, &eventlist,
76 if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
77 s_result = _sendto(connection->sockfd, data + result,
78 eventlist.data < data_size - result ?
79 eventlist.data : data_size - result, MSG_NOSIGNAL,
86 if (eventlist.flags & EV_EOF)
87 return (result < data_size ? -1 : 0);
90 } while (result < data_size);
96 * safe_read reads data from connection and tries to do it in the very safe
97 * and stable way. It uses kevent to ensure, that the data are available for
98 * reading. If the amount of data to be read is too large, then they would
102 safe_read(struct cached_connection_ *connection, void *data, size_t data_size)
104 struct kevent eventlist;
107 struct timespec timeout;
113 timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
117 nevents = _kevent(connection->read_queue, NULL, 0, &eventlist,
119 if (nevents == 1 && eventlist.filter == EVFILT_READ) {
120 s_result = _read(connection->sockfd, data + result,
121 eventlist.data <= data_size - result ?
122 eventlist.data : data_size - result);
128 if (eventlist.flags & EV_EOF)
129 return (result < data_size ? -1 : 0);
132 } while (result < data_size);
138 * Sends the credentials information to the connection along with the
139 * communication element type.
142 send_credentials(struct cached_connection_ *connection, int type)
146 char pad[CMSG_SPACE(sizeof(struct cmsgcred))];
150 struct kevent eventlist;
154 memset(&cmsg, 0, sizeof(cmsg));
155 cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
156 cmsg.hdr.cmsg_level = SOL_SOCKET;
157 cmsg.hdr.cmsg_type = SCM_CREDS;
159 memset(&mhdr, 0, sizeof(mhdr));
162 mhdr.msg_control = &cmsg;
163 mhdr.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
165 iov.iov_base = &type;
166 iov.iov_len = sizeof(int);
168 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
169 NOTE_LOWAT, sizeof(int), NULL);
170 (void)_kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
172 nevents = _kevent(connection->write_queue, NULL, 0, &eventlist, 1,
174 if (nevents == 1 && eventlist.filter == EVFILT_WRITE) {
175 result = _sendmsg(connection->sockfd, &mhdr,
176 MSG_NOSIGNAL) == -1 ? -1 : 0;
177 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
179 _kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
186 * Opens the connection with the specified params. Initializes all kqueues.
188 struct cached_connection_ *
189 __open_cached_connection(struct cached_connection_params const *params)
191 struct cached_connection_ *retval;
192 struct kevent eventlist;
193 struct sockaddr_un client_address;
194 int client_address_len, client_socket;
197 assert(params != NULL);
199 client_socket = _socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
200 client_address.sun_family = PF_LOCAL;
201 strncpy(client_address.sun_path, params->socket_path,
202 sizeof(client_address.sun_path));
203 client_address_len = sizeof(client_address.sun_family) +
204 strlen(client_address.sun_path) + 1;
206 res = _connect(client_socket, (struct sockaddr *)&client_address,
209 _close(client_socket);
212 _fcntl(client_socket, F_SETFL, O_NONBLOCK);
214 retval = malloc(sizeof(struct cached_connection_));
215 assert(retval != NULL);
216 memset(retval, 0, sizeof(struct cached_connection_));
218 retval->sockfd = client_socket;
220 retval->write_queue = kqueue();
221 assert(retval->write_queue != -1);
223 EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
224 res = _kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
226 retval->read_queue = kqueue();
227 assert(retval->read_queue != -1);
229 EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD, 0, 0, NULL);
230 res = _kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
236 __close_cached_connection(struct cached_connection_ *connection)
238 assert(connection != NULL);
240 _close(connection->sockfd);
241 _close(connection->read_queue);
242 _close(connection->write_queue);
247 * This function is very close to the cache_write function of the caching
248 * library, which is used in the caching daemon. It caches the data with the
249 * specified key in the cache entry with entry_name.
252 __cached_write(struct cached_connection_ *connection, const char *entry_name,
253 const char *key, size_t key_size, const char *data, size_t data_size)
261 result = send_credentials(connection, CET_WRITE_REQUEST);
265 name_size = strlen(entry_name);
266 result = safe_write(connection, &name_size, sizeof(size_t));
270 result = safe_write(connection, &key_size, sizeof(size_t));
274 result = safe_write(connection, &data_size, sizeof(size_t));
278 result = safe_write(connection, entry_name, name_size);
282 result = safe_write(connection, key, key_size);
286 result = safe_write(connection, data, data_size);
290 result = safe_read(connection, &error_code, sizeof(int));
299 * This function is very close to the cache_read function of the caching
300 * library, which is used in the caching daemon. It reads cached data with the
301 * specified key from the cache entry with entry_name.
304 __cached_read(struct cached_connection_ *connection, const char *entry_name,
305 const char *key, size_t key_size, char *data, size_t *data_size)
307 size_t name_size, result_size;
308 int error_code, rec_error_code;
311 assert(connection != NULL);
315 result = send_credentials(connection, CET_READ_REQUEST);
319 name_size = strlen(entry_name);
320 result = safe_write(connection, &name_size, sizeof(size_t));
324 result = safe_write(connection, &key_size, sizeof(size_t));
328 result = safe_write(connection, entry_name, name_size);
332 result = safe_write(connection, key, key_size);
336 result = safe_read(connection, &rec_error_code, sizeof(int));
340 if (rec_error_code != 0) {
341 error_code = rec_error_code;
345 result = safe_read(connection, &result_size, sizeof(size_t));
349 if (result_size > *data_size) {
350 *data_size = result_size;
355 result = safe_read(connection, data, result_size);
359 *data_size = result_size;
367 * Initializes the mp_write_session. For such a session the new connection
368 * would be opened. The data should be written to the session with
369 * __cached_mp_write function. The __close_cached_mp_write_session function
370 * should be used to submit session and __abandon_cached_mp_write_session - to
371 * abandon it. When the session is submitted, the whole se
373 struct cached_connection_ *
374 __open_cached_mp_write_session(struct cached_connection_params const *params,
375 const char *entry_name)
377 struct cached_connection_ *connection, *retval;
383 connection = __open_cached_connection(params);
384 if (connection == NULL)
386 connection->mp_flag = 1;
388 result = send_credentials(connection, CET_MP_WRITE_SESSION_REQUEST);
392 name_size = strlen(entry_name);
393 result = safe_write(connection, &name_size, sizeof(size_t));
397 result = safe_write(connection, entry_name, name_size);
401 result = safe_read(connection, &error_code, sizeof(int));
410 __close_cached_connection(connection);
417 * Adds new portion of data to the opened write session
420 __cached_mp_write(struct cached_connection_ *ws, const char *data,
428 request = CET_MP_WRITE_SESSION_WRITE_REQUEST;
429 result = safe_write(ws, &request, sizeof(int));
433 result = safe_write(ws, &data_size, sizeof(size_t));
437 result = safe_write(ws, data, data_size);
441 result = safe_read(ws, &error_code, sizeof(int));
450 * Abandons all operations with the write session. All data, that were written
451 * to the session before, are discarded.
454 __abandon_cached_mp_write_session(struct cached_connection_ *ws)
459 notification = CET_MP_WRITE_SESSION_ABANDON_NOTIFICATION;
460 result = safe_write(ws, ¬ification, sizeof(int));
461 __close_cached_connection(ws);
466 * Gracefully closes the write session. The data, that were previously written
467 * to the session, are committed.
470 __close_cached_mp_write_session(struct cached_connection_ *ws)
474 notification = CET_MP_WRITE_SESSION_CLOSE_NOTIFICATION;
475 (void)safe_write(ws, ¬ification, sizeof(int));
476 __close_cached_connection(ws);
480 struct cached_connection_ *
481 __open_cached_mp_read_session(struct cached_connection_params const *params,
482 const char *entry_name)
484 struct cached_connection_ *connection, *retval;
490 connection = __open_cached_connection(params);
491 if (connection == NULL)
493 connection->mp_flag = 1;
495 result = send_credentials(connection, CET_MP_READ_SESSION_REQUEST);
499 name_size = strlen(entry_name);
500 result = safe_write(connection, &name_size, sizeof(size_t));
504 result = safe_write(connection, entry_name, name_size);
508 result = safe_read(connection, &error_code, sizeof(int));
517 __close_cached_connection(connection);
524 __cached_mp_read(struct cached_connection_ *rs, char *data, size_t *data_size)
527 int error_code, rec_error_code;
531 request = CET_MP_READ_SESSION_READ_REQUEST;
532 result = safe_write(rs, &request, sizeof(int));
536 result = safe_read(rs, &rec_error_code, sizeof(int));
540 if (rec_error_code != 0) {
541 error_code = rec_error_code;
545 result = safe_read(rs, &result_size, sizeof(size_t));
549 if (result_size > *data_size) {
550 *data_size = result_size;
555 result = safe_read(rs, data, result_size);
559 *data_size = result_size;
567 __close_cached_mp_read_session(struct cached_connection_ *rs)
570 __close_cached_connection(rs);