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 struct kevent eventlist; 148 int nevents; 149 ssize_t result; 150 int res; 151 152 struct msghdr cred_hdr; 153 struct iovec iov; 154 155 struct { 156 struct cmsghdr hdr; 157 char cred[CMSG_SPACE(sizeof(struct cmsgcred))]; 158 } cmsg; 159 160 memset(&cmsg, 0, sizeof(cmsg)); 161 cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred)); 162 cmsg.hdr.cmsg_level = SOL_SOCKET; 163 cmsg.hdr.cmsg_type = SCM_CREDS; 164 165 memset(&cred_hdr, 0, sizeof(struct msghdr)); 166 cred_hdr.msg_iov = &iov; 167 cred_hdr.msg_iovlen = 1; 168 cred_hdr.msg_control = (caddr_t)&cmsg; 169 cred_hdr.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred)); 170 171 iov.iov_base = &type; 172 iov.iov_len = sizeof(int); 173 174 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD, 175 NOTE_LOWAT, sizeof(int), NULL); 176 res = _kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL); 177 178 nevents = _kevent(connection->write_queue, NULL, 0, &eventlist, 1, 179 NULL); 180 if (nevents == 1 && eventlist.filter == EVFILT_WRITE) { 181 result = (_sendmsg(connection->sockfd, &cred_hdr, 182 MSG_NOSIGNAL) == -1) ? -1 : 0; 183 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD, 184 0, 0, NULL); 185 _kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL); 186 return (result); 187 } else 188 return (-1); 189 } 190 191 /* 192 * Opens the connection with the specified params. Initializes all kqueues. 193 */ 194 struct cached_connection_ * 195 __open_cached_connection(struct cached_connection_params const *params) 196 { 197 struct cached_connection_ *retval; 198 struct kevent eventlist; 199 struct sockaddr_un client_address; 200 int client_address_len, client_socket; 201 int res; 202 203 assert(params != NULL); 204 205 client_socket = _socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); 206 client_address.sun_family = PF_LOCAL; 207 strncpy(client_address.sun_path, params->socket_path, 208 sizeof(client_address.sun_path)); 209 client_address_len = sizeof(client_address.sun_family) + 210 strlen(client_address.sun_path) + 1; 211 212 res = _connect(client_socket, (struct sockaddr *)&client_address, 213 client_address_len); 214 if (res == -1) { 215 _close(client_socket); 216 return (NULL); 217 } 218 _fcntl(client_socket, F_SETFL, O_NONBLOCK); 219 220 retval = malloc(sizeof(struct cached_connection_)); 221 assert(retval != NULL); 222 memset(retval, 0, sizeof(struct cached_connection_)); 223 224 retval->sockfd = client_socket; 225 226 retval->write_queue = kqueue(); 227 assert(retval->write_queue != -1); 228 229 EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); 230 res = _kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL); 231 232 retval->read_queue = kqueue(); 233 assert(retval->read_queue != -1); 234 235 EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD, 0, 0, NULL); 236 res = _kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL); 237 238 return (retval); 239 } 240 241 void 242 __close_cached_connection(struct cached_connection_ *connection) 243 { 244 assert(connection != NULL); 245 246 _close(connection->sockfd); 247 _close(connection->read_queue); 248 _close(connection->write_queue); 249 free(connection); 250 } 251 252 /* 253 * This function is very close to the cache_write function of the caching 254 * library, which is used in the caching daemon. It caches the data with the 255 * specified key in the cache entry with entry_name. 256 */ 257 int 258 __cached_write(struct cached_connection_ *connection, const char *entry_name, 259 const char *key, size_t key_size, const char *data, size_t data_size) 260 { 261 size_t name_size; 262 int error_code; 263 int result; 264 265 error_code = -1; 266 result = 0; 267 result = send_credentials(connection, CET_WRITE_REQUEST); 268 if (result != 0) 269 goto fin; 270 271 name_size = strlen(entry_name); 272 result = safe_write(connection, &name_size, sizeof(size_t)); 273 if (result != 0) 274 goto fin; 275 276 result = safe_write(connection, &key_size, sizeof(size_t)); 277 if (result != 0) 278 goto fin; 279 280 result = safe_write(connection, &data_size, sizeof(size_t)); 281 if (result != 0) 282 goto fin; 283 284 result = safe_write(connection, entry_name, name_size); 285 if (result != 0) 286 goto fin; 287 288 result = safe_write(connection, key, key_size); 289 if (result != 0) 290 goto fin; 291 292 result = safe_write(connection, data, data_size); 293 if (result != 0) 294 goto fin; 295 296 result = safe_read(connection, &error_code, sizeof(int)); 297 if (result != 0) 298 error_code = -1; 299 300 fin: 301 return (error_code); 302 } 303 304 /* 305 * This function is very close to the cache_read function of the caching 306 * library, which is used in the caching daemon. It reads cached data with the 307 * specified key from the cache entry with entry_name. 308 */ 309 int 310 __cached_read(struct cached_connection_ *connection, const char *entry_name, 311 const char *key, size_t key_size, char *data, size_t *data_size) 312 { 313 size_t name_size, result_size; 314 int error_code, rec_error_code; 315 int result; 316 317 assert(connection != NULL); 318 result = 0; 319 error_code = -1; 320 321 result = send_credentials(connection, CET_READ_REQUEST); 322 if (result != 0) 323 goto fin; 324 325 name_size = strlen(entry_name); 326 result = safe_write(connection, &name_size, sizeof(size_t)); 327 if (result != 0) 328 goto fin; 329 330 result = safe_write(connection, &key_size, sizeof(size_t)); 331 if (result != 0) 332 goto fin; 333 334 result = safe_write(connection, entry_name, name_size); 335 if (result != 0) 336 goto fin; 337 338 result = safe_write(connection, key, key_size); 339 if (result != 0) 340 goto fin; 341 342 result = safe_read(connection, &rec_error_code, sizeof(int)); 343 if (result != 0) 344 goto fin; 345 346 if (rec_error_code != 0) { 347 error_code = rec_error_code; 348 goto fin; 349 } 350 351 result = safe_read(connection, &result_size, sizeof(size_t)); 352 if (result != 0) 353 goto fin; 354 355 if (result_size > *data_size) { 356 *data_size = result_size; 357 error_code = -2; 358 goto fin; 359 } 360 361 result = safe_read(connection, data, result_size); 362 if (result != 0) 363 goto fin; 364 365 *data_size = result_size; 366 error_code = 0; 367 368 fin: 369 return (error_code); 370 } 371 372 /* 373 * Initializes the mp_write_session. For such a session the new connection 374 * would be opened. The data should be written to the session with 375 * __cached_mp_write function. The __close_cached_mp_write_session function 376 * should be used to submit session and __abandon_cached_mp_write_session - to 377 * abandon it. When the session is submitted, the whole se 378 */ 379 struct cached_connection_ * 380 __open_cached_mp_write_session(struct cached_connection_params const *params, 381 const char *entry_name) 382 { 383 struct cached_connection_ *connection, *retval; 384 size_t name_size; 385 int error_code; 386 int result; 387 388 retval = NULL; 389 connection = __open_cached_connection(params); 390 if (connection == NULL) 391 return (NULL); 392 connection->mp_flag = 1; 393 394 result = send_credentials(connection, CET_MP_WRITE_SESSION_REQUEST); 395 if (result != 0) 396 goto fin; 397 398 name_size = strlen(entry_name); 399 result = safe_write(connection, &name_size, sizeof(size_t)); 400 if (result != 0) 401 goto fin; 402 403 result = safe_write(connection, entry_name, name_size); 404 if (result != 0) 405 goto fin; 406 407 result = safe_read(connection, &error_code, sizeof(int)); 408 if (result != 0) 409 goto fin; 410 411 if (error_code != 0) 412 result = error_code; 413 414 fin: 415 if (result != 0) 416 __close_cached_connection(connection); 417 else 418 retval = connection; 419 return (retval); 420 } 421 422 /* 423 * Adds new portion of data to the opened write session 424 */ 425 int 426 __cached_mp_write(struct cached_connection_ *ws, const char *data, 427 size_t data_size) 428 { 429 int request, result; 430 int error_code; 431 432 error_code = -1; 433 434 request = CET_MP_WRITE_SESSION_WRITE_REQUEST; 435 result = safe_write(ws, &request, sizeof(int)); 436 if (result != 0) 437 goto fin; 438 439 result = safe_write(ws, &data_size, sizeof(size_t)); 440 if (result != 0) 441 goto fin; 442 443 result = safe_write(ws, data, data_size); 444 if (result != 0) 445 goto fin; 446 447 result = safe_read(ws, &error_code, sizeof(int)); 448 if (result != 0) 449 error_code = -1; 450 451 fin: 452 return (error_code); 453 } 454 455 /* 456 * Abandons all operations with the write session. All data, that were written 457 * to the session before, are discarded. 458 */ 459 int 460 __abandon_cached_mp_write_session(struct cached_connection_ *ws) 461 { 462 int notification; 463 int result; 464 465 notification = CET_MP_WRITE_SESSION_ABANDON_NOTIFICATION; 466 result = safe_write(ws, ¬ification, sizeof(int)); 467 __close_cached_connection(ws); 468 return (result); 469 } 470 471 /* 472 * Gracefully closes the write session. The data, that were previously written 473 * to the session, are committed. 474 */ 475 int 476 __close_cached_mp_write_session(struct cached_connection_ *ws) 477 { 478 int notification; 479 int result; 480 481 notification = CET_MP_WRITE_SESSION_CLOSE_NOTIFICATION; 482 result = safe_write(ws, ¬ification, sizeof(int)); 483 __close_cached_connection(ws); 484 return (0); 485 } 486 487 struct cached_connection_ * 488 __open_cached_mp_read_session(struct cached_connection_params const *params, 489 const char *entry_name) 490 { 491 struct cached_connection_ *connection, *retval; 492 size_t name_size; 493 int error_code; 494 int result; 495 496 retval = NULL; 497 connection = __open_cached_connection(params); 498 if (connection == NULL) 499 return (NULL); 500 connection->mp_flag = 1; 501 502 result = send_credentials(connection, CET_MP_READ_SESSION_REQUEST); 503 if (result != 0) 504 goto fin; 505 506 name_size = strlen(entry_name); 507 result = safe_write(connection, &name_size, sizeof(size_t)); 508 if (result != 0) 509 goto fin; 510 511 result = safe_write(connection, entry_name, name_size); 512 if (result != 0) 513 goto fin; 514 515 result = safe_read(connection, &error_code, sizeof(int)); 516 if (result != 0) 517 goto fin; 518 519 if (error_code != 0) 520 result = error_code; 521 522 fin: 523 if (result != 0) 524 __close_cached_connection(connection); 525 else 526 retval = connection; 527 return (retval); 528 } 529 530 int 531 __cached_mp_read(struct cached_connection_ *rs, char *data, size_t *data_size) 532 { 533 size_t result_size; 534 int error_code, rec_error_code; 535 int request, result; 536 537 error_code = -1; 538 request = CET_MP_READ_SESSION_READ_REQUEST; 539 result = safe_write(rs, &request, sizeof(int)); 540 if (result != 0) 541 goto fin; 542 543 result = safe_read(rs, &rec_error_code, sizeof(int)); 544 if (result != 0) 545 goto fin; 546 547 if (rec_error_code != 0) { 548 error_code = rec_error_code; 549 goto fin; 550 } 551 552 result = safe_read(rs, &result_size, sizeof(size_t)); 553 if (result != 0) 554 goto fin; 555 556 if (result_size > *data_size) { 557 *data_size = result_size; 558 error_code = -2; 559 goto fin; 560 } 561 562 result = safe_read(rs, data, result_size); 563 if (result != 0) 564 goto fin; 565 566 *data_size = result_size; 567 error_code = 0; 568 569 fin: 570 return (error_code); 571 } 572 573 int 574 __close_cached_mp_read_session(struct cached_connection_ *rs) 575 { 576 577 __close_cached_connection(rs); 578 return (0); 579 } 580