xref: /freebsd/lib/libc/net/nscachedcli.c (revision 42249ef2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 	int res;
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(&mhdr, 0, sizeof(mhdr));
164 	mhdr.msg_iov = &iov;
165 	mhdr.msg_iovlen = 1;
166 	mhdr.msg_control = &cmsg;
167 	mhdr.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, &mhdr,
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