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