2 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "namespace.h"
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/event.h>
43 #include "un-namespace.h"
44 #include "nscachedcli.h"
46 #define NS_DEFAULT_CACHED_IO_TIMEOUT 4
48 static int safe_write(struct cached_connection_ *, const void *, size_t);
49 static int safe_read(struct cached_connection_ *, void *, size_t);
50 static int send_credentials(struct cached_connection_ *, int);
53 * safe_write writes data to the specified connection and tries to do it in
54 * the very safe manner. We ensure, that we can write to the socket with
55 * kevent. If the data_size can't be sent in one piece, then it would be
59 safe_write(struct cached_connection_ *connection, const void *data,
62 struct kevent eventlist;
66 struct timespec timeout;
71 timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
75 nevents = _kevent(connection->write_queue, NULL, 0, &eventlist,
77 if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
78 s_result = _sendto(connection->sockfd, data + result,
79 eventlist.data < data_size - result ?
80 eventlist.data : data_size - result, MSG_NOSIGNAL,
87 if (eventlist.flags & EV_EOF)
88 return (result < data_size ? -1 : 0);
91 } while (result < data_size);
97 * safe_read reads data from connection and tries to do it in the very safe
98 * and stable way. It uses kevent to ensure, that the data are availabe for
99 * reading. If the amount of data to be read is too large, then they would
103 safe_read(struct cached_connection_ *connection, void *data, size_t data_size)
105 struct kevent eventlist;
108 struct timespec timeout;
114 timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
118 nevents = _kevent(connection->read_queue, NULL, 0, &eventlist,
120 if (nevents == 1 && eventlist.filter == EVFILT_READ) {
121 s_result = _read(connection->sockfd, data + result,
122 eventlist.data <= data_size - result ?
123 eventlist.data : data_size - result);
129 if (eventlist.flags & EV_EOF)
130 return (result < data_size ? -1 : 0);
133 } while (result < data_size);
139 * Sends the credentials information to the connection along with the
140 * communication element type.
143 send_credentials(struct cached_connection_ *connection, int type)
145 struct kevent eventlist;
150 struct msghdr cred_hdr;
155 char cred[CMSG_SPACE(sizeof(struct cmsgcred))];
158 memset(&cmsg, 0, sizeof(cmsg));
159 cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
160 cmsg.hdr.cmsg_level = SOL_SOCKET;
161 cmsg.hdr.cmsg_type = SCM_CREDS;
163 memset(&cred_hdr, 0, sizeof(struct msghdr));
164 cred_hdr.msg_iov = &iov;
165 cred_hdr.msg_iovlen = 1;
166 cred_hdr.msg_control = (caddr_t)&cmsg;
167 cred_hdr.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
169 iov.iov_base = &type;
170 iov.iov_len = sizeof(int);
172 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
173 NOTE_LOWAT, sizeof(int), NULL);
174 res = _kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
176 nevents = _kevent(connection->write_queue, NULL, 0, &eventlist, 1,
178 if (nevents == 1 && eventlist.filter == EVFILT_WRITE) {
179 result = (_sendmsg(connection->sockfd, &cred_hdr,
180 MSG_NOSIGNAL) == -1) ? -1 : 0;
181 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
183 _kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
190 * Opens the connection with the specified params. Initializes all kqueues.
192 struct cached_connection_ *
193 __open_cached_connection(struct cached_connection_params const *params)
195 struct cached_connection_ *retval;
196 struct kevent eventlist;
197 struct sockaddr_un client_address;
198 int client_address_len, client_socket;
201 assert(params != NULL);
203 client_socket = _socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
204 client_address.sun_family = PF_LOCAL;
205 strncpy(client_address.sun_path, params->socket_path,
206 sizeof(client_address.sun_path));
207 client_address_len = sizeof(client_address.sun_family) +
208 strlen(client_address.sun_path) + 1;
210 res = _connect(client_socket, (struct sockaddr *)&client_address,
213 _close(client_socket);
216 _fcntl(client_socket, F_SETFL, O_NONBLOCK);
218 retval = malloc(sizeof(struct cached_connection_));
219 assert(retval != NULL);
220 memset(retval, 0, sizeof(struct cached_connection_));
222 retval->sockfd = client_socket;
224 retval->write_queue = kqueue();
225 assert(retval->write_queue != -1);
227 EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
228 res = _kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
230 retval->read_queue = kqueue();
231 assert(retval->read_queue != -1);
233 EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD, 0, 0, NULL);
234 res = _kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
240 __close_cached_connection(struct cached_connection_ *connection)
242 assert(connection != NULL);
244 _close(connection->sockfd);
245 _close(connection->read_queue);
246 _close(connection->write_queue);
251 * This function is very close to the cache_write function of the caching
252 * library, which is used in the caching daemon. It caches the data with the
253 * specified key in the cache entry with entry_name.
256 __cached_write(struct cached_connection_ *connection, const char *entry_name,
257 const char *key, size_t key_size, const char *data, size_t data_size)
265 result = send_credentials(connection, CET_WRITE_REQUEST);
269 name_size = strlen(entry_name);
270 result = safe_write(connection, &name_size, sizeof(size_t));
274 result = safe_write(connection, &key_size, sizeof(size_t));
278 result = safe_write(connection, &data_size, sizeof(size_t));
282 result = safe_write(connection, entry_name, name_size);
286 result = safe_write(connection, key, key_size);
290 result = safe_write(connection, data, data_size);
294 result = safe_read(connection, &error_code, sizeof(int));
303 * This function is very close to the cache_read function of the caching
304 * library, which is used in the caching daemon. It reads cached data with the
305 * specified key from the cache entry with entry_name.
308 __cached_read(struct cached_connection_ *connection, const char *entry_name,
309 const char *key, size_t key_size, char *data, size_t *data_size)
311 size_t name_size, result_size;
312 int error_code, rec_error_code;
315 assert(connection != NULL);
319 result = send_credentials(connection, CET_READ_REQUEST);
323 name_size = strlen(entry_name);
324 result = safe_write(connection, &name_size, sizeof(size_t));
328 result = safe_write(connection, &key_size, sizeof(size_t));
332 result = safe_write(connection, entry_name, name_size);
336 result = safe_write(connection, key, key_size);
340 result = safe_read(connection, &rec_error_code, sizeof(int));
344 if (rec_error_code != 0) {
345 error_code = rec_error_code;
349 result = safe_read(connection, &result_size, sizeof(size_t));
353 if (result_size > *data_size) {
354 *data_size = result_size;
359 result = safe_read(connection, data, result_size);
363 *data_size = result_size;
371 * Initializes the mp_write_session. For such a session the new connection
372 * would be opened. The data should be written to the session with
373 * __cached_mp_write function. The __close_cached_mp_write_session function
374 * should be used to submit session and __abandon_cached_mp_write_session - to
375 * abandon it. When the session is submitted, the whole se
377 struct cached_connection_ *
378 __open_cached_mp_write_session(struct cached_connection_params const *params,
379 const char *entry_name)
381 struct cached_connection_ *connection, *retval;
387 connection = __open_cached_connection(params);
388 if (connection == NULL)
390 connection->mp_flag = 1;
392 result = send_credentials(connection, CET_MP_WRITE_SESSION_REQUEST);
396 name_size = strlen(entry_name);
397 result = safe_write(connection, &name_size, sizeof(size_t));
401 result = safe_write(connection, entry_name, name_size);
405 result = safe_read(connection, &error_code, sizeof(int));
414 __close_cached_connection(connection);
421 * Adds new portion of data to the opened write session
424 __cached_mp_write(struct cached_connection_ *ws, const char *data,
432 request = CET_MP_WRITE_SESSION_WRITE_REQUEST;
433 result = safe_write(ws, &request, sizeof(int));
437 result = safe_write(ws, &data_size, sizeof(size_t));
441 result = safe_write(ws, data, data_size);
445 result = safe_read(ws, &error_code, sizeof(int));
454 * Abandons all operations with the write session. All data, that were written
455 * to the session before, are discarded.
458 __abandon_cached_mp_write_session(struct cached_connection_ *ws)
463 notification = CET_MP_WRITE_SESSION_ABANDON_NOTIFICATION;
464 result = safe_write(ws, ¬ification, sizeof(int));
465 __close_cached_connection(ws);
470 * Gracefully closes the write session. The data, that were previously written
471 * to the session, are committed.
474 __close_cached_mp_write_session(struct cached_connection_ *ws)
479 notification = CET_MP_WRITE_SESSION_CLOSE_NOTIFICATION;
480 result = safe_write(ws, ¬ification, sizeof(int));
481 __close_cached_connection(ws);
485 struct cached_connection_ *
486 __open_cached_mp_read_session(struct cached_connection_params const *params,
487 const char *entry_name)
489 struct cached_connection_ *connection, *retval;
495 connection = __open_cached_connection(params);
496 if (connection == NULL)
498 connection->mp_flag = 1;
500 result = send_credentials(connection, CET_MP_READ_SESSION_REQUEST);
504 name_size = strlen(entry_name);
505 result = safe_write(connection, &name_size, sizeof(size_t));
509 result = safe_write(connection, entry_name, name_size);
513 result = safe_read(connection, &error_code, sizeof(int));
522 __close_cached_connection(connection);
529 __cached_mp_read(struct cached_connection_ *rs, char *data, size_t *data_size)
532 int error_code, rec_error_code;
536 request = CET_MP_READ_SESSION_READ_REQUEST;
537 result = safe_write(rs, &request, sizeof(int));
541 result = safe_read(rs, &rec_error_code, sizeof(int));
545 if (rec_error_code != 0) {
546 error_code = rec_error_code;
550 result = safe_read(rs, &result_size, sizeof(size_t));
554 if (result_size > *data_size) {
555 *data_size = result_size;
560 result = safe_read(rs, data, result_size);
564 *data_size = result_size;
572 __close_cached_mp_read_session(struct cached_connection_ *rs)
575 __close_cached_connection(rs);