xref: /dragonfly/lib/libc/net/nscachedcli.c (revision 73610d44)
1 /*-
2  * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/lib/libc/net/nscachedcli.c,v 1.3 2006/12/04 17:08:43 ume Exp $
27  */
28 
29 #include "namespace.h"
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/event.h>
33 #include <sys/uio.h>
34 #include <sys/un.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include "un-namespace.h"
42 #include "nscachedcli.h"
43 
44 #define NS_DEFAULT_CACHED_IO_TIMEOUT	4
45 
46 static int safe_write(struct cached_connection_ *, const void *, size_t);
47 static int safe_read(struct cached_connection_ *, void *, size_t);
48 static int send_credentials(struct cached_connection_ *, int);
49 
50 /*
51  * safe_write writes data to the specified connection and tries to do it in
52  * the very safe manner. We ensure, that we can write to the socket with
53  * kevent. If the data_size can't be sent in one piece, then it would be
54  * splitted.
55  */
56 static int
57 safe_write(struct cached_connection_ *connection, const void *data,
58     size_t data_size)
59 {
60 	struct kevent eventlist;
61 	int nevents;
62 	size_t result;
63 	ssize_t s_result;
64 	struct timespec timeout;
65 
66 	if (data_size == 0)
67 		return (0);
68 
69 	timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
70 	timeout.tv_nsec = 0;
71 	result = 0;
72 	do {
73 		nevents = _kevent(connection->write_queue, NULL, 0, &eventlist,
74 		    1, &timeout);
75 		if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
76 			s_result = _write(connection->sockfd,
77 			    (char *)data + result,
78 			    eventlist.data < data_size - result ?
79 			    eventlist.data : data_size - result);
80 			if (s_result == -1)
81 				return (-1);
82 			else
83 				result += s_result;
84 
85 			if (eventlist.flags & EV_EOF)
86 				return (result < data_size ? -1 : 0);
87 		} else
88 			return (-1);
89 	} while (result < data_size);
90 
91 	return (0);
92 }
93 
94 /*
95  * safe_read reads data from connection and tries to do it in the very safe
96  * and stable way. It uses kevent to ensure, that the data are availabe for
97  * reading. If the amount of data to be read is too large, then they would
98  * be splitted.
99  */
100 static int
101 safe_read(struct cached_connection_ *connection, void *data, size_t data_size)
102 {
103 	struct kevent eventlist;
104 	size_t result;
105 	ssize_t s_result;
106 	struct timespec timeout;
107 	int nevents;
108 
109 	if (data_size == 0)
110 		return (0);
111 
112 	timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
113 	timeout.tv_nsec = 0;
114 	result = 0;
115 	do {
116 		nevents = _kevent(connection->read_queue, NULL, 0, &eventlist,
117 		    1, &timeout);
118 		if (nevents == 1 && eventlist.filter == EVFILT_READ) {
119 			s_result = _read(connection->sockfd,
120 			    (char *)data + result,
121 			    eventlist.data <= data_size - result ?
122 			    eventlist.data : data_size - result);
123 			if (s_result == -1)
124 				return (-1);
125 			else
126 				result += s_result;
127 
128 			if (eventlist.flags & EV_EOF)
129 				return (result < data_size ? -1 : 0);
130 		} else
131 			return (-1);
132 	} while (result < data_size);
133 
134 	return (0);
135 }
136 
137 /*
138  * Sends the credentials information to the connection along with the
139  * communication element type.
140  */
141 static int
142 send_credentials(struct cached_connection_ *connection, int type)
143 {
144 	struct kevent eventlist;
145 	int nevents;
146 	ssize_t result;
147 	int res __unused;
148 
149 	struct msghdr cred_hdr;
150 	struct iovec iov;
151 
152 	struct {
153 		struct cmsghdr hdr;
154 		char cred[CMSG_SPACE(sizeof(struct cmsgcred))];
155 	} cmsg;
156 
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;
161 
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));
167 
168 	iov.iov_base = &type;
169 	iov.iov_len = sizeof(int);
170 
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);
174 
175 	nevents = _kevent(connection->write_queue, NULL, 0, &eventlist, 1,
176 	    NULL);
177 	if (nevents == 1 && eventlist.filter == EVFILT_WRITE) {
178 		result = (_sendmsg(connection->sockfd, &cred_hdr, 0) == -1) ?
179 		    -1 : 0;
180 		EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
181 		    0, 0, NULL);
182 		_kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
183 		return (result);
184 	} else
185 		return (-1);
186 }
187 
188 /*
189  * Opens the connection with the specified params. Initializes all kqueues.
190  */
191 struct cached_connection_ *
192 __open_cached_connection(struct cached_connection_params const *params)
193 {
194 	struct cached_connection_ *retval;
195 	struct kevent eventlist;
196 	struct sockaddr_un client_address;
197 	int client_address_len, client_socket;
198 	int res;
199 
200 	assert(params != NULL);
201 
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;
208 
209 	res = _connect(client_socket, (struct sockaddr *)&client_address,
210 	    client_address_len);
211 	if (res == -1) {
212 		_close(client_socket);
213 		return (NULL);
214 	}
215 	_fcntl(client_socket, F_SETFL, O_NONBLOCK);
216 
217 	retval = malloc(sizeof(struct cached_connection_));
218 	assert(retval != NULL);
219 	memset(retval, 0, sizeof(struct cached_connection_));
220 
221 	retval->sockfd = client_socket;
222 
223 	retval->write_queue = kqueue();
224 	assert(retval->write_queue != -1);
225 
226 	EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
227 	res = _kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
228 
229 	retval->read_queue = kqueue();
230 	assert(retval->read_queue != -1);
231 
232 	EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD, 0, 0, NULL);
233 	res = _kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
234 
235 	return (retval);
236 }
237 
238 void
239 __close_cached_connection(struct cached_connection_ *connection)
240 {
241 	assert(connection != NULL);
242 
243 	_close(connection->sockfd);
244 	_close(connection->read_queue);
245 	_close(connection->write_queue);
246 	free(connection);
247 }
248 
249 /*
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.
253  */
254 int
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)
257 {
258 	size_t name_size;
259 	int error_code;
260 	int result;
261 
262 	error_code = -1;
263 	result = send_credentials(connection, CET_WRITE_REQUEST);
264 	if (result != 0)
265 		goto fin;
266 
267 	name_size = strlen(entry_name);
268 	result = safe_write(connection, &name_size, sizeof(size_t));
269 	if (result != 0)
270 		goto fin;
271 
272 	result = safe_write(connection, &key_size, sizeof(size_t));
273 	if (result != 0)
274 		goto fin;
275 
276 	result = safe_write(connection, &data_size, sizeof(size_t));
277 	if (result != 0)
278 		goto fin;
279 
280 	result = safe_write(connection, entry_name, name_size);
281 	if (result != 0)
282 		goto fin;
283 
284 	result = safe_write(connection, key, key_size);
285 	if (result != 0)
286 		goto fin;
287 
288 	result = safe_write(connection, data, data_size);
289 	if (result != 0)
290 		goto fin;
291 
292 	result = safe_read(connection, &error_code, sizeof(int));
293 	if (result != 0)
294 		error_code = -1;
295 
296 fin:
297 	return (error_code);
298 }
299 
300 /*
301  * This function is very close to the cache_read function of the caching
302  * library, which is used in the caching daemon. It reads cached data with the
303  * specified key from the cache entry with entry_name.
304  */
305 int
306 __cached_read(struct cached_connection_ *connection, const char *entry_name,
307     const char *key, size_t key_size, char *data, size_t *data_size)
308 {
309 	size_t name_size, result_size;
310 	int error_code, rec_error_code;
311 	int result;
312 
313 	assert(connection != NULL);
314 	result = 0;
315 	error_code = -1;
316 
317 	result = send_credentials(connection, CET_READ_REQUEST);
318 	if (result != 0)
319 		goto fin;
320 
321 	name_size = strlen(entry_name);
322 	result = safe_write(connection, &name_size, sizeof(size_t));
323 	if (result != 0)
324 		goto fin;
325 
326 	result = safe_write(connection, &key_size, sizeof(size_t));
327 	if (result != 0)
328 		goto fin;
329 
330 	result = safe_write(connection, entry_name, name_size);
331 	if (result != 0)
332 		goto fin;
333 
334 	result = safe_write(connection, key, key_size);
335 	if (result != 0)
336 		goto fin;
337 
338 	result = safe_read(connection, &rec_error_code, sizeof(int));
339 	if (result != 0)
340 		goto fin;
341 
342 	if (rec_error_code != 0) {
343 		error_code = rec_error_code;
344 		goto fin;
345 	}
346 
347 	result = safe_read(connection, &result_size, sizeof(size_t));
348 	if (result != 0)
349 		goto fin;
350 
351 	if (result_size > *data_size) {
352 		*data_size = result_size;
353 		error_code = -2;
354 		goto fin;
355 	}
356 
357 	result = safe_read(connection, data, result_size);
358 	if (result != 0)
359 		goto fin;
360 
361 	*data_size = result_size;
362 	error_code = 0;
363 
364 fin:
365 	return (error_code);
366 }
367 
368 /*
369  * Initializes the mp_write_session. For such a session the new connection
370  * would be opened. The data should be written to the session with
371  * __cached_mp_write function. The __close_cached_mp_write_session function
372  * should be used to submit session and __abandon_cached_mp_write_session - to
373  * abandon it. When the session is submitted, the whole se
374  */
375 struct cached_connection_ *
376 __open_cached_mp_write_session(struct cached_connection_params const *params,
377     const char *entry_name)
378 {
379 	struct cached_connection_ *connection, *retval;
380 	size_t name_size;
381 	int error_code;
382 	int result;
383 
384 	retval = NULL;
385 	connection = __open_cached_connection(params);
386 	if (connection == NULL)
387 		return (NULL);
388 	connection->mp_flag = 1;
389 
390 	result = send_credentials(connection, CET_MP_WRITE_SESSION_REQUEST);
391 	if (result != 0)
392 		goto fin;
393 
394 	name_size = strlen(entry_name);
395 	result = safe_write(connection, &name_size, sizeof(size_t));
396 	if (result != 0)
397 		goto fin;
398 
399 	result = safe_write(connection, entry_name, name_size);
400 	if (result != 0)
401 		goto fin;
402 
403 	result = safe_read(connection, &error_code, sizeof(int));
404 	if (result != 0)
405 		goto fin;
406 
407 	if (error_code != 0)
408 		result = error_code;
409 
410 fin:
411 	if (result != 0)
412 		__close_cached_connection(connection);
413 	else
414 		retval = connection;
415 	return (retval);
416 }
417 
418 /*
419  * Adds new portion of data to the opened write session
420  */
421 int
422 __cached_mp_write(struct cached_connection_ *ws, const char *data,
423     size_t data_size)
424 {
425 	int request, result;
426 	int error_code;
427 
428 	error_code = -1;
429 
430 	request = CET_MP_WRITE_SESSION_WRITE_REQUEST;
431 	result = safe_write(ws, &request, sizeof(int));
432 	if (result != 0)
433 		goto fin;
434 
435 	result = safe_write(ws, &data_size, sizeof(size_t));
436 	if (result != 0)
437 		goto fin;
438 
439 	result = safe_write(ws, data, data_size);
440 	if (result != 0)
441 		goto fin;
442 
443 	result = safe_read(ws, &error_code, sizeof(int));
444 	if (result != 0)
445 		error_code = -1;
446 
447 fin:
448 	return (error_code);
449 }
450 
451 /*
452  * Abandons all operations with the write session. All data, that were written
453  * to the session before, are discarded.
454  */
455 int
456 __abandon_cached_mp_write_session(struct cached_connection_ *ws)
457 {
458 	int notification;
459 	int result;
460 
461 	notification = CET_MP_WRITE_SESSION_ABANDON_NOTIFICATION;
462 	result = safe_write(ws, &notification, sizeof(int));
463 	__close_cached_connection(ws);
464 	return (result);
465 }
466 
467 /*
468  * Gracefully closes the write session. The data, that were previously written
469  * to the session, are committed.
470  */
471 int
472 __close_cached_mp_write_session(struct cached_connection_ *ws)
473 {
474 	int notification;
475 	int result __unused;
476 
477 	notification = CET_MP_WRITE_SESSION_CLOSE_NOTIFICATION;
478 	result = safe_write(ws, &notification, sizeof(int));
479 	__close_cached_connection(ws);
480 	return (0);
481 }
482 
483 struct cached_connection_ *
484 __open_cached_mp_read_session(struct cached_connection_params const *params,
485 	const char *entry_name)
486 {
487 	struct cached_connection_ *connection, *retval;
488 	size_t name_size;
489 	int error_code;
490 	int result;
491 
492 	retval = NULL;
493 	connection = __open_cached_connection(params);
494 	if (connection == NULL)
495 		return (NULL);
496 	connection->mp_flag = 1;
497 
498 	result = send_credentials(connection, CET_MP_READ_SESSION_REQUEST);
499 	if (result != 0)
500 		goto fin;
501 
502 	name_size = strlen(entry_name);
503 	result = safe_write(connection, &name_size, sizeof(size_t));
504 	if (result != 0)
505 		goto fin;
506 
507 	result = safe_write(connection, entry_name, name_size);
508 	if (result != 0)
509 		goto fin;
510 
511 	result = safe_read(connection, &error_code, sizeof(int));
512 	if (result != 0)
513 		goto fin;
514 
515 	if (error_code != 0)
516 		result = error_code;
517 
518 fin:
519 	if (result != 0)
520 		__close_cached_connection(connection);
521 	else
522 		retval = connection;
523 	return (retval);
524 }
525 
526 int
527 __cached_mp_read(struct cached_connection_ *rs, char *data, size_t *data_size)
528 {
529 	size_t result_size;
530 	int error_code, rec_error_code;
531 	int request, result;
532 
533 	error_code = -1;
534 	request = CET_MP_READ_SESSION_READ_REQUEST;
535 	result = safe_write(rs, &request, sizeof(int));
536 	if (result != 0)
537 		goto fin;
538 
539 	result = safe_read(rs, &rec_error_code, sizeof(int));
540 	if (result != 0)
541 		goto fin;
542 
543 	if (rec_error_code != 0) {
544 		error_code = rec_error_code;
545 		goto fin;
546 	}
547 
548 	result = safe_read(rs, &result_size, sizeof(size_t));
549 	if (result != 0)
550 		goto fin;
551 
552 	if (result_size > *data_size) {
553 		*data_size = result_size;
554 		error_code = -2;
555 		goto fin;
556 	}
557 
558 	result = safe_read(rs, data, result_size);
559 	if (result != 0)
560 		goto fin;
561 
562 	*data_size = result_size;
563 	error_code = 0;
564 
565 fin:
566 	return (error_code);
567 }
568 
569 int
570 __close_cached_mp_read_session(struct cached_connection_ *rs)
571 {
572 
573 	__close_cached_connection(rs);
574 	return (0);
575 }
576