2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "namespace.h"
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/event.h>
45 #include "un-namespace.h"
46 #include "nscachedcli.h"
48 #define NS_DEFAULT_CACHED_IO_TIMEOUT 4
50 static int safe_write(struct cached_connection_ *, const void *, size_t);
51 static int safe_read(struct cached_connection_ *, void *, size_t);
52 static int send_credentials(struct cached_connection_ *, int);
55 * safe_write writes data to the specified connection and tries to do it in
56 * the very safe manner. We ensure, that we can write to the socket with
57 * kevent. If the data_size can't be sent in one piece, then it would be
61 safe_write(struct cached_connection_ *connection, const void *data,
64 struct kevent eventlist;
68 struct timespec timeout;
73 timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
77 nevents = _kevent(connection->write_queue, NULL, 0, &eventlist,
79 if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
80 s_result = _sendto(connection->sockfd, data + result,
81 eventlist.data < data_size - result ?
82 eventlist.data : data_size - result, MSG_NOSIGNAL,
89 if (eventlist.flags & EV_EOF)
90 return (result < data_size ? -1 : 0);
93 } while (result < data_size);
99 * safe_read reads data from connection and tries to do it in the very safe
100 * and stable way. It uses kevent to ensure, that the data are available for
101 * reading. If the amount of data to be read is too large, then they would
105 safe_read(struct cached_connection_ *connection, void *data, size_t data_size)
107 struct kevent eventlist;
110 struct timespec timeout;
116 timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
120 nevents = _kevent(connection->read_queue, NULL, 0, &eventlist,
122 if (nevents == 1 && eventlist.filter == EVFILT_READ) {
123 s_result = _read(connection->sockfd, data + result,
124 eventlist.data <= data_size - result ?
125 eventlist.data : data_size - result);
131 if (eventlist.flags & EV_EOF)
132 return (result < data_size ? -1 : 0);
135 } while (result < data_size);
141 * Sends the credentials information to the connection along with the
142 * communication element type.
145 send_credentials(struct cached_connection_ *connection, int type)
147 struct kevent eventlist;
152 struct msghdr cred_hdr;
157 char cred[CMSG_SPACE(sizeof(struct cmsgcred))];
160 memset(&cmsg, 0, sizeof(cmsg));
161 cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
162 cmsg.hdr.cmsg_level = SOL_SOCKET;
163 cmsg.hdr.cmsg_type = SCM_CREDS;
165 memset(&cred_hdr, 0, sizeof(struct msghdr));
166 cred_hdr.msg_iov = &iov;
167 cred_hdr.msg_iovlen = 1;
168 cred_hdr.msg_control = (caddr_t)&cmsg;
169 cred_hdr.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
171 iov.iov_base = &type;
172 iov.iov_len = sizeof(int);
174 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
175 NOTE_LOWAT, sizeof(int), NULL);
176 res = _kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
178 nevents = _kevent(connection->write_queue, NULL, 0, &eventlist, 1,
180 if (nevents == 1 && eventlist.filter == EVFILT_WRITE) {
181 result = (_sendmsg(connection->sockfd, &cred_hdr,
182 MSG_NOSIGNAL) == -1) ? -1 : 0;
183 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
185 _kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
192 * Opens the connection with the specified params. Initializes all kqueues.
194 struct cached_connection_ *
195 __open_cached_connection(struct cached_connection_params const *params)
197 struct cached_connection_ *retval;
198 struct kevent eventlist;
199 struct sockaddr_un client_address;
200 int client_address_len, client_socket;
203 assert(params != NULL);
205 client_socket = _socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
206 client_address.sun_family = PF_LOCAL;
207 strncpy(client_address.sun_path, params->socket_path,
208 sizeof(client_address.sun_path));
209 client_address_len = sizeof(client_address.sun_family) +
210 strlen(client_address.sun_path) + 1;
212 res = _connect(client_socket, (struct sockaddr *)&client_address,
215 _close(client_socket);
218 _fcntl(client_socket, F_SETFL, O_NONBLOCK);
220 retval = malloc(sizeof(struct cached_connection_));
221 assert(retval != NULL);
222 memset(retval, 0, sizeof(struct cached_connection_));
224 retval->sockfd = client_socket;
226 retval->write_queue = kqueue();
227 assert(retval->write_queue != -1);
229 EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
230 res = _kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
232 retval->read_queue = kqueue();
233 assert(retval->read_queue != -1);
235 EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD, 0, 0, NULL);
236 res = _kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
242 __close_cached_connection(struct cached_connection_ *connection)
244 assert(connection != NULL);
246 _close(connection->sockfd);
247 _close(connection->read_queue);
248 _close(connection->write_queue);
253 * This function is very close to the cache_write function of the caching
254 * library, which is used in the caching daemon. It caches the data with the
255 * specified key in the cache entry with entry_name.
258 __cached_write(struct cached_connection_ *connection, const char *entry_name,
259 const char *key, size_t key_size, const char *data, size_t data_size)
267 result = send_credentials(connection, CET_WRITE_REQUEST);
271 name_size = strlen(entry_name);
272 result = safe_write(connection, &name_size, sizeof(size_t));
276 result = safe_write(connection, &key_size, sizeof(size_t));
280 result = safe_write(connection, &data_size, sizeof(size_t));
284 result = safe_write(connection, entry_name, name_size);
288 result = safe_write(connection, key, key_size);
292 result = safe_write(connection, data, data_size);
296 result = safe_read(connection, &error_code, sizeof(int));
305 * This function is very close to the cache_read function of the caching
306 * library, which is used in the caching daemon. It reads cached data with the
307 * specified key from the cache entry with entry_name.
310 __cached_read(struct cached_connection_ *connection, const char *entry_name,
311 const char *key, size_t key_size, char *data, size_t *data_size)
313 size_t name_size, result_size;
314 int error_code, rec_error_code;
317 assert(connection != NULL);
321 result = send_credentials(connection, CET_READ_REQUEST);
325 name_size = strlen(entry_name);
326 result = safe_write(connection, &name_size, sizeof(size_t));
330 result = safe_write(connection, &key_size, sizeof(size_t));
334 result = safe_write(connection, entry_name, name_size);
338 result = safe_write(connection, key, key_size);
342 result = safe_read(connection, &rec_error_code, sizeof(int));
346 if (rec_error_code != 0) {
347 error_code = rec_error_code;
351 result = safe_read(connection, &result_size, sizeof(size_t));
355 if (result_size > *data_size) {
356 *data_size = result_size;
361 result = safe_read(connection, data, result_size);
365 *data_size = result_size;
373 * Initializes the mp_write_session. For such a session the new connection
374 * would be opened. The data should be written to the session with
375 * __cached_mp_write function. The __close_cached_mp_write_session function
376 * should be used to submit session and __abandon_cached_mp_write_session - to
377 * abandon it. When the session is submitted, the whole se
379 struct cached_connection_ *
380 __open_cached_mp_write_session(struct cached_connection_params const *params,
381 const char *entry_name)
383 struct cached_connection_ *connection, *retval;
389 connection = __open_cached_connection(params);
390 if (connection == NULL)
392 connection->mp_flag = 1;
394 result = send_credentials(connection, CET_MP_WRITE_SESSION_REQUEST);
398 name_size = strlen(entry_name);
399 result = safe_write(connection, &name_size, sizeof(size_t));
403 result = safe_write(connection, entry_name, name_size);
407 result = safe_read(connection, &error_code, sizeof(int));
416 __close_cached_connection(connection);
423 * Adds new portion of data to the opened write session
426 __cached_mp_write(struct cached_connection_ *ws, const char *data,
434 request = CET_MP_WRITE_SESSION_WRITE_REQUEST;
435 result = safe_write(ws, &request, sizeof(int));
439 result = safe_write(ws, &data_size, sizeof(size_t));
443 result = safe_write(ws, data, data_size);
447 result = safe_read(ws, &error_code, sizeof(int));
456 * Abandons all operations with the write session. All data, that were written
457 * to the session before, are discarded.
460 __abandon_cached_mp_write_session(struct cached_connection_ *ws)
465 notification = CET_MP_WRITE_SESSION_ABANDON_NOTIFICATION;
466 result = safe_write(ws, ¬ification, sizeof(int));
467 __close_cached_connection(ws);
472 * Gracefully closes the write session. The data, that were previously written
473 * to the session, are committed.
476 __close_cached_mp_write_session(struct cached_connection_ *ws)
481 notification = CET_MP_WRITE_SESSION_CLOSE_NOTIFICATION;
482 result = safe_write(ws, ¬ification, sizeof(int));
483 __close_cached_connection(ws);
487 struct cached_connection_ *
488 __open_cached_mp_read_session(struct cached_connection_params const *params,
489 const char *entry_name)
491 struct cached_connection_ *connection, *retval;
497 connection = __open_cached_connection(params);
498 if (connection == NULL)
500 connection->mp_flag = 1;
502 result = send_credentials(connection, CET_MP_READ_SESSION_REQUEST);
506 name_size = strlen(entry_name);
507 result = safe_write(connection, &name_size, sizeof(size_t));
511 result = safe_write(connection, entry_name, name_size);
515 result = safe_read(connection, &error_code, sizeof(int));
524 __close_cached_connection(connection);
531 __cached_mp_read(struct cached_connection_ *rs, char *data, size_t *data_size)
534 int error_code, rec_error_code;
538 request = CET_MP_READ_SESSION_READ_REQUEST;
539 result = safe_write(rs, &request, sizeof(int));
543 result = safe_read(rs, &rec_error_code, sizeof(int));
547 if (rec_error_code != 0) {
548 error_code = rec_error_code;
552 result = safe_read(rs, &result_size, sizeof(size_t));
556 if (result_size > *data_size) {
557 *data_size = result_size;
562 result = safe_read(rs, data, result_size);
566 *data_size = result_size;
574 __close_cached_mp_read_session(struct cached_connection_ *rs)
577 __close_cached_connection(rs);