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 = _write(connection->sockfd, data + result,
79 eventlist.data < data_size - result ?
80 eventlist.data : data_size - result);
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 availabe 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)
144 struct kevent eventlist;
149 struct msghdr cred_hdr;
154 char cred[CMSG_SPACE(sizeof(struct cmsgcred))];
157 memset(&cmsg, 0, sizeof(cmsg));
158 cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
159 cmsg.hdr.cmsg_level = SOL_SOCKET;
160 cmsg.hdr.cmsg_type = SCM_CREDS;
162 memset(&cred_hdr, 0, sizeof(struct msghdr));
163 cred_hdr.msg_iov = &iov;
164 cred_hdr.msg_iovlen = 1;
165 cred_hdr.msg_control = (caddr_t)&cmsg;
166 cred_hdr.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
168 iov.iov_base = &type;
169 iov.iov_len = sizeof(int);
171 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
172 NOTE_LOWAT, sizeof(int), NULL);
173 res = _kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
175 nevents = _kevent(connection->write_queue, NULL, 0, &eventlist, 1,
177 if (nevents == 1 && eventlist.filter == EVFILT_WRITE) {
178 result = (_sendmsg(connection->sockfd, &cred_hdr, 0) == -1) ?
180 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
182 _kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
189 * Opens the connection with the specified params. Initializes all kqueues.
191 struct cached_connection_ *
192 __open_cached_connection(struct cached_connection_params const *params)
194 struct cached_connection_ *retval;
195 struct kevent eventlist;
196 struct sockaddr_un client_address;
197 int client_address_len, client_socket;
200 assert(params != NULL);
202 client_socket = _socket(PF_LOCAL, SOCK_STREAM, 0);
203 client_address.sun_family = PF_LOCAL;
204 strncpy(client_address.sun_path, params->socket_path,
205 sizeof(client_address.sun_path));
206 client_address_len = sizeof(client_address.sun_family) +
207 strlen(client_address.sun_path) + 1;
209 res = _connect(client_socket, (struct sockaddr *)&client_address,
212 _close(client_socket);
215 _fcntl(client_socket, F_SETFL, O_NONBLOCK);
217 retval = malloc(sizeof(struct cached_connection_));
218 assert(retval != NULL);
219 memset(retval, 0, sizeof(struct cached_connection_));
221 retval->sockfd = client_socket;
223 retval->write_queue = kqueue();
224 assert(retval->write_queue != -1);
226 EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
227 res = _kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
229 retval->read_queue = kqueue();
230 assert(retval->read_queue != -1);
232 EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD, 0, 0, NULL);
233 res = _kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
239 __close_cached_connection(struct cached_connection_ *connection)
241 assert(connection != NULL);
243 _close(connection->sockfd);
244 _close(connection->read_queue);
245 _close(connection->write_queue);
250 * This function is very close to the cache_write function of the caching
251 * library, which is used in the caching daemon. It caches the data with the
252 * specified key in the cache entry with entry_name.
255 __cached_write(struct cached_connection_ *connection, const char *entry_name,
256 const char *key, size_t key_size, const char *data, size_t data_size)
264 result = send_credentials(connection, CET_WRITE_REQUEST);
268 name_size = strlen(entry_name);
269 result = safe_write(connection, &name_size, sizeof(size_t));
273 result = safe_write(connection, &key_size, sizeof(size_t));
277 result = safe_write(connection, &data_size, sizeof(size_t));
281 result = safe_write(connection, entry_name, name_size);
285 result = safe_write(connection, key, key_size);
289 result = safe_write(connection, data, data_size);
293 result = safe_read(connection, &error_code, sizeof(int));
302 * This function is very close to the cache_read function of the caching
303 * library, which is used in the caching daemon. It reads cached data with the
304 * specified key from the cache entry with entry_name.
307 __cached_read(struct cached_connection_ *connection, const char *entry_name,
308 const char *key, size_t key_size, char *data, size_t *data_size)
310 size_t name_size, result_size;
311 int error_code, rec_error_code;
314 assert(connection != NULL);
318 result = send_credentials(connection, CET_READ_REQUEST);
322 name_size = strlen(entry_name);
323 result = safe_write(connection, &name_size, sizeof(size_t));
327 result = safe_write(connection, &key_size, sizeof(size_t));
331 result = safe_write(connection, entry_name, name_size);
335 result = safe_write(connection, key, key_size);
339 result = safe_read(connection, &rec_error_code, sizeof(int));
343 if (rec_error_code != 0) {
344 error_code = rec_error_code;
348 result = safe_read(connection, &result_size, sizeof(size_t));
352 if (result_size > *data_size) {
353 *data_size = result_size;
358 result = safe_read(connection, data, result_size);
362 *data_size = result_size;
370 * Initializes the mp_write_session. For such a session the new connection
371 * would be opened. The data should be written to the session with
372 * __cached_mp_write function. The __close_cached_mp_write_session function
373 * should be used to submit session and __abandon_cached_mp_write_session - to
374 * abandon it. When the session is submitted, the whole se
376 struct cached_connection_ *
377 __open_cached_mp_write_session(struct cached_connection_params const *params,
378 const char *entry_name)
380 struct cached_connection_ *connection, *retval;
386 connection = __open_cached_connection(params);
387 if (connection == NULL)
389 connection->mp_flag = 1;
391 result = send_credentials(connection, CET_MP_WRITE_SESSION_REQUEST);
395 name_size = strlen(entry_name);
396 result = safe_write(connection, &name_size, sizeof(size_t));
400 result = safe_write(connection, entry_name, name_size);
404 result = safe_read(connection, &error_code, sizeof(int));
413 __close_cached_connection(connection);
420 * Adds new portion of data to the opened write session
423 __cached_mp_write(struct cached_connection_ *ws, const char *data,
431 request = CET_MP_WRITE_SESSION_WRITE_REQUEST;
432 result = safe_write(ws, &request, sizeof(int));
436 result = safe_write(ws, &data_size, sizeof(size_t));
440 result = safe_write(ws, data, data_size);
444 result = safe_read(ws, &error_code, sizeof(int));
453 * Abandons all operations with the write session. All data, that were written
454 * to the session before, are discarded.
457 __abandon_cached_mp_write_session(struct cached_connection_ *ws)
462 notification = CET_MP_WRITE_SESSION_ABANDON_NOTIFICATION;
463 result = safe_write(ws, ¬ification, sizeof(int));
464 __close_cached_connection(ws);
469 * Gracefully closes the write session. The data, that were previously written
470 * to the session, are committed.
473 __close_cached_mp_write_session(struct cached_connection_ *ws)
478 notification = CET_MP_WRITE_SESSION_CLOSE_NOTIFICATION;
479 result = safe_write(ws, ¬ification, sizeof(int));
480 __close_cached_connection(ws);
484 struct cached_connection_ *
485 __open_cached_mp_read_session(struct cached_connection_params const *params,
486 const char *entry_name)
488 struct cached_connection_ *connection, *retval;
494 connection = __open_cached_connection(params);
495 if (connection == NULL)
497 connection->mp_flag = 1;
499 result = send_credentials(connection, CET_MP_READ_SESSION_REQUEST);
503 name_size = strlen(entry_name);
504 result = safe_write(connection, &name_size, sizeof(size_t));
508 result = safe_write(connection, entry_name, name_size);
512 result = safe_read(connection, &error_code, sizeof(int));
521 __close_cached_connection(connection);
528 __cached_mp_read(struct cached_connection_ *rs, char *data, size_t *data_size)
531 int error_code, rec_error_code;
535 request = CET_MP_READ_SESSION_READ_REQUEST;
536 result = safe_write(rs, &request, sizeof(int));
540 result = safe_read(rs, &rec_error_code, sizeof(int));
544 if (rec_error_code != 0) {
545 error_code = rec_error_code;
549 result = safe_read(rs, &result_size, sizeof(size_t));
553 if (result_size > *data_size) {
554 *data_size = result_size;
559 result = safe_read(rs, data, result_size);
563 *data_size = result_size;
571 __close_cached_mp_read_session(struct cached_connection_ *rs)
574 __close_cached_connection(rs);