xref: /freebsd/lib/libc/net/nscachedcli.c (revision bdd1243d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "namespace.h"
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/event.h>
37 #include <sys/uio.h>
38 #include <sys/un.h>
39 #include <assert.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include "un-namespace.h"
46 #include "nscachedcli.h"
47 
48 #define NS_DEFAULT_CACHED_IO_TIMEOUT	4
49 
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);
53 
54 /*
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
58  * splitted.
59  */
60 static int
61 safe_write(struct cached_connection_ *connection, const void *data,
62     size_t data_size)
63 {
64 	struct kevent eventlist;
65 	int nevents;
66 	size_t result;
67 	ssize_t s_result;
68 	struct timespec timeout;
69 
70 	if (data_size == 0)
71 		return (0);
72 
73 	timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
74 	timeout.tv_nsec = 0;
75 	result = 0;
76 	do {
77 		nevents = _kevent(connection->write_queue, NULL, 0, &eventlist,
78 		    1, &timeout);
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,
83 			    NULL, 0);
84 			if (s_result == -1)
85 				return (-1);
86 			else
87 				result += s_result;
88 
89 			if (eventlist.flags & EV_EOF)
90 				return (result < data_size ? -1 : 0);
91 		} else
92 			return (-1);
93 	} while (result < data_size);
94 
95 	return (0);
96 }
97 
98 /*
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
102  * be splitted.
103  */
104 static int
105 safe_read(struct cached_connection_ *connection, void *data, size_t data_size)
106 {
107 	struct kevent eventlist;
108 	size_t result;
109 	ssize_t s_result;
110 	struct timespec timeout;
111 	int nevents;
112 
113 	if (data_size == 0)
114 		return (0);
115 
116 	timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
117 	timeout.tv_nsec = 0;
118 	result = 0;
119 	do {
120 		nevents = _kevent(connection->read_queue, NULL, 0, &eventlist,
121 		    1, &timeout);
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);
126 			if (s_result == -1)
127 				return (-1);
128 			else
129 				result += s_result;
130 
131 			if (eventlist.flags & EV_EOF)
132 				return (result < data_size ? -1 : 0);
133 		} else
134 			return (-1);
135 	} while (result < data_size);
136 
137 	return (0);
138 }
139 
140 /*
141  * Sends the credentials information to the connection along with the
142  * communication element type.
143  */
144 static int
145 send_credentials(struct cached_connection_ *connection, int type)
146 {
147 	union {
148 		struct cmsghdr hdr;
149 		char pad[CMSG_SPACE(sizeof(struct cmsgcred))];
150 	} cmsg;
151 	struct msghdr mhdr;
152 	struct iovec iov;
153 	struct kevent eventlist;
154 	int nevents;
155 	ssize_t result;
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(&mhdr, 0, sizeof(mhdr));
163 	mhdr.msg_iov = &iov;
164 	mhdr.msg_iovlen = 1;
165 	mhdr.msg_control = &cmsg;
166 	mhdr.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 	(void)_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, &mhdr,
179 		    MSG_NOSIGNAL) == -1 ? -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 | SOCK_CLOEXEC, 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 = 0;
264 	result = send_credentials(connection, CET_WRITE_REQUEST);
265 	if (result != 0)
266 		goto fin;
267 
268 	name_size = strlen(entry_name);
269 	result = safe_write(connection, &name_size, sizeof(size_t));
270 	if (result != 0)
271 		goto fin;
272 
273 	result = safe_write(connection, &key_size, sizeof(size_t));
274 	if (result != 0)
275 		goto fin;
276 
277 	result = safe_write(connection, &data_size, sizeof(size_t));
278 	if (result != 0)
279 		goto fin;
280 
281 	result = safe_write(connection, entry_name, name_size);
282 	if (result != 0)
283 		goto fin;
284 
285 	result = safe_write(connection, key, key_size);
286 	if (result != 0)
287 		goto fin;
288 
289 	result = safe_write(connection, data, data_size);
290 	if (result != 0)
291 		goto fin;
292 
293 	result = safe_read(connection, &error_code, sizeof(int));
294 	if (result != 0)
295 		error_code = -1;
296 
297 fin:
298 	return (error_code);
299 }
300 
301 /*
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.
305  */
306 int
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)
309 {
310 	size_t name_size, result_size;
311 	int error_code, rec_error_code;
312 	int result;
313 
314 	assert(connection != NULL);
315 	result = 0;
316 	error_code = -1;
317 
318 	result = send_credentials(connection, CET_READ_REQUEST);
319 	if (result != 0)
320 		goto fin;
321 
322 	name_size = strlen(entry_name);
323 	result = safe_write(connection, &name_size, sizeof(size_t));
324 	if (result != 0)
325 		goto fin;
326 
327 	result = safe_write(connection, &key_size, sizeof(size_t));
328 	if (result != 0)
329 		goto fin;
330 
331 	result = safe_write(connection, entry_name, name_size);
332 	if (result != 0)
333 		goto fin;
334 
335 	result = safe_write(connection, key, key_size);
336 	if (result != 0)
337 		goto fin;
338 
339 	result = safe_read(connection, &rec_error_code, sizeof(int));
340 	if (result != 0)
341 		goto fin;
342 
343 	if (rec_error_code != 0) {
344 		error_code = rec_error_code;
345 		goto fin;
346 	}
347 
348 	result = safe_read(connection, &result_size, sizeof(size_t));
349 	if (result != 0)
350 		goto fin;
351 
352 	 if (result_size > *data_size) {
353 		 *data_size = result_size;
354 		 error_code = -2;
355 		 goto fin;
356 	 }
357 
358 	result = safe_read(connection, data, result_size);
359 	if (result != 0)
360 		goto fin;
361 
362 	*data_size = result_size;
363 	error_code = 0;
364 
365 fin:
366 	return (error_code);
367 }
368 
369 /*
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
375  */
376 struct cached_connection_ *
377 __open_cached_mp_write_session(struct cached_connection_params const *params,
378     const char *entry_name)
379 {
380 	struct cached_connection_ *connection, *retval;
381 	size_t name_size;
382 	int error_code;
383 	int result;
384 
385 	retval = NULL;
386 	connection = __open_cached_connection(params);
387 	if (connection == NULL)
388 		return (NULL);
389 	connection->mp_flag = 1;
390 
391 	result = send_credentials(connection, CET_MP_WRITE_SESSION_REQUEST);
392 	if (result != 0)
393 		goto fin;
394 
395 	name_size = strlen(entry_name);
396 	result = safe_write(connection, &name_size, sizeof(size_t));
397 	if (result != 0)
398 		goto fin;
399 
400 	result = safe_write(connection, entry_name, name_size);
401 	if (result != 0)
402 		goto fin;
403 
404 	result = safe_read(connection, &error_code, sizeof(int));
405 	if (result != 0)
406 		goto fin;
407 
408 	if (error_code != 0)
409 		result = error_code;
410 
411 fin:
412 	if (result != 0)
413 		__close_cached_connection(connection);
414 	else
415 		retval = connection;
416 	return (retval);
417 }
418 
419 /*
420  * Adds new portion of data to the opened write session
421  */
422 int
423 __cached_mp_write(struct cached_connection_ *ws, const char *data,
424     size_t data_size)
425 {
426 	int request, result;
427 	int error_code;
428 
429 	error_code = -1;
430 
431 	request = CET_MP_WRITE_SESSION_WRITE_REQUEST;
432 	result = safe_write(ws, &request, sizeof(int));
433 	if (result != 0)
434 		goto fin;
435 
436 	result = safe_write(ws, &data_size, sizeof(size_t));
437 	if (result != 0)
438 		goto fin;
439 
440 	result = safe_write(ws, data, data_size);
441 	if (result != 0)
442 		goto fin;
443 
444 	result = safe_read(ws, &error_code, sizeof(int));
445 	if (result != 0)
446 		error_code = -1;
447 
448 fin:
449 	return (error_code);
450 }
451 
452 /*
453  * Abandons all operations with the write session. All data, that were written
454  * to the session before, are discarded.
455  */
456 int
457 __abandon_cached_mp_write_session(struct cached_connection_ *ws)
458 {
459 	int notification;
460 	int result;
461 
462 	notification = CET_MP_WRITE_SESSION_ABANDON_NOTIFICATION;
463 	result = safe_write(ws, &notification, sizeof(int));
464 	__close_cached_connection(ws);
465 	return (result);
466 }
467 
468 /*
469  * Gracefully closes the write session. The data, that were previously written
470  * to the session, are committed.
471  */
472 int
473 __close_cached_mp_write_session(struct cached_connection_ *ws)
474 {
475 	int notification;
476 
477 	notification = CET_MP_WRITE_SESSION_CLOSE_NOTIFICATION;
478 	(void)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