xref: /dragonfly/lib/libc/net/nscachedcli.c (revision 3e7e41796563d188c56ba2080021feda4a2aace9)
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/time.h>
34 #include <sys/uio.h>
35 #include <sys/un.h>
36 #include <assert.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include "un-namespace.h"
43 #include "nscachedcli.h"
44 
45 #define NS_DEFAULT_CACHED_IO_TIMEOUT    4
46 
47 static int safe_write(struct cached_connection_ *, const void *, size_t);
48 static int safe_read(struct cached_connection_ *, void *, size_t);
49 static int send_credentials(struct cached_connection_ *, int);
50 
51 /*
52  * safe_write writes data to the specified connection and tries to do it in
53  * the very safe manner. We ensure, that we can write to the socket with
54  * kevent. If the data_size can't be sent in one piece, then it would be
55  * splitted.
56  */
57 static int
safe_write(struct cached_connection_ * connection,const void * data,size_t data_size)58 safe_write(struct cached_connection_ *connection, const void *data,
59     size_t data_size)
60 {
61           struct kevent eventlist;
62           int nevents;
63           size_t result;
64           ssize_t s_result;
65           struct timespec timeout;
66 
67           if (data_size == 0)
68                     return (0);
69 
70           timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
71           timeout.tv_nsec = 0;
72           result = 0;
73           do {
74                     nevents = _kevent(connection->write_queue, NULL, 0, &eventlist,
75                         1, &timeout);
76                     if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
77                               s_result = _write(connection->sockfd,
78                                   (char *)data + result,
79                                   eventlist.data < data_size - result ?
80                                   eventlist.data : data_size - result);
81                               if (s_result == -1)
82                                         return (-1);
83                               else
84                                         result += s_result;
85 
86                               if (eventlist.flags & EV_EOF)
87                                         return (result < data_size ? -1 : 0);
88                     } else
89                               return (-1);
90           } while (result < data_size);
91 
92           return (0);
93 }
94 
95 /*
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
99  * be splitted.
100  */
101 static int
safe_read(struct cached_connection_ * connection,void * data,size_t data_size)102 safe_read(struct cached_connection_ *connection, void *data, size_t data_size)
103 {
104           struct kevent eventlist;
105           size_t result;
106           ssize_t s_result;
107           struct timespec timeout;
108           int nevents;
109 
110           if (data_size == 0)
111                     return (0);
112 
113           timeout.tv_sec = NS_DEFAULT_CACHED_IO_TIMEOUT;
114           timeout.tv_nsec = 0;
115           result = 0;
116           do {
117                     nevents = _kevent(connection->read_queue, NULL, 0, &eventlist,
118                         1, &timeout);
119                     if (nevents == 1 && eventlist.filter == EVFILT_READ) {
120                               s_result = _read(connection->sockfd,
121                                   (char *)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
send_credentials(struct cached_connection_ * connection,int type)143 send_credentials(struct cached_connection_ *connection, int type)
144 {
145           struct kevent eventlist;
146           int nevents;
147           ssize_t result;
148           int res __unused;
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, 0) == -1) ?
180                         -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_ *
__open_cached_connection(struct cached_connection_params const * params)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, 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
__close_cached_connection(struct cached_connection_ * connection)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
__cached_write(struct cached_connection_ * connection,const char * entry_name,const char * key,size_t key_size,const char * data,size_t data_size)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 = 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
__cached_read(struct cached_connection_ * connection,const char * entry_name,const char * key,size_t key_size,char * data,size_t * data_size)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_ *
__open_cached_mp_write_session(struct cached_connection_params const * params,const char * entry_name)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
__cached_mp_write(struct cached_connection_ * ws,const char * data,size_t data_size)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
__abandon_cached_mp_write_session(struct cached_connection_ * ws)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
__close_cached_mp_write_session(struct cached_connection_ * ws)473 __close_cached_mp_write_session(struct cached_connection_ *ws)
474 {
475           int notification;
476           int result __unused;
477 
478           notification = CET_MP_WRITE_SESSION_CLOSE_NOTIFICATION;
479           result = safe_write(ws, &notification, sizeof(int));
480           __close_cached_connection(ws);
481           return (0);
482 }
483 
484 struct cached_connection_ *
__open_cached_mp_read_session(struct cached_connection_params const * params,const char * entry_name)485 __open_cached_mp_read_session(struct cached_connection_params const *params,
486           const char *entry_name)
487 {
488           struct cached_connection_ *connection, *retval;
489           size_t name_size;
490           int error_code;
491           int result;
492 
493           retval = NULL;
494           connection = __open_cached_connection(params);
495           if (connection == NULL)
496                     return (NULL);
497           connection->mp_flag = 1;
498 
499           result = send_credentials(connection, CET_MP_READ_SESSION_REQUEST);
500           if (result != 0)
501                     goto fin;
502 
503           name_size = strlen(entry_name);
504           result = safe_write(connection, &name_size, sizeof(size_t));
505           if (result != 0)
506                     goto fin;
507 
508           result = safe_write(connection, entry_name, name_size);
509           if (result != 0)
510                     goto fin;
511 
512           result = safe_read(connection, &error_code, sizeof(int));
513           if (result != 0)
514                     goto fin;
515 
516           if (error_code != 0)
517                     result = error_code;
518 
519 fin:
520           if (result != 0)
521                     __close_cached_connection(connection);
522           else
523                     retval = connection;
524           return (retval);
525 }
526 
527 int
__cached_mp_read(struct cached_connection_ * rs,char * data,size_t * data_size)528 __cached_mp_read(struct cached_connection_ *rs, char *data, size_t *data_size)
529 {
530           size_t result_size;
531           int error_code, rec_error_code;
532           int request, result;
533 
534           error_code = -1;
535           request = CET_MP_READ_SESSION_READ_REQUEST;
536           result = safe_write(rs, &request, sizeof(int));
537           if (result != 0)
538                     goto fin;
539 
540           result = safe_read(rs, &rec_error_code, sizeof(int));
541           if (result != 0)
542                     goto fin;
543 
544           if (rec_error_code != 0) {
545                     error_code = rec_error_code;
546                     goto fin;
547           }
548 
549           result = safe_read(rs, &result_size, sizeof(size_t));
550           if (result != 0)
551                     goto fin;
552 
553           if (result_size > *data_size) {
554                     *data_size = result_size;
555                     error_code = -2;
556                     goto fin;
557           }
558 
559           result = safe_read(rs, data, result_size);
560           if (result != 0)
561                     goto fin;
562 
563           *data_size = result_size;
564           error_code = 0;
565 
566 fin:
567           return (error_code);
568 }
569 
570 int
__close_cached_mp_read_session(struct cached_connection_ * rs)571 __close_cached_mp_read_session(struct cached_connection_ *rs)
572 {
573 
574           __close_cached_connection(rs);
575           return (0);
576 }
577