geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1130 lines
27 KiB

  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
  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 AUTHORS 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 AUTHORS 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. * $FreeBSD$
  29. */
  30. #include <sys/param.h>
  31. #include <sys/bio.h>
  32. #include <sys/disk.h>
  33. #include <sys/endian.h>
  34. #include <sys/ioctl.h>
  35. #include <sys/queue.h>
  36. #include <sys/socket.h>
  37. #include <sys/stat.h>
  38. #include <sys/time.h>
  39. #include <sys/un.h>
  40. #include <arpa/inet.h>
  41. #include <netinet/in.h>
  42. #include <netinet/tcp.h>
  43. #include <assert.h>
  44. #include <err.h>
  45. #include <errno.h>
  46. #include <fcntl.h>
  47. #include <libgen.h>
  48. #include <libutil.h>
  49. #include <paths.h>
  50. #include <pthread.h>
  51. #include <signal.h>
  52. #include <stdarg.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <stdint.h>
  56. #include <string.h>
  57. #include <syslog.h>
  58. #include <unistd.h>
  59. #include "ggate.h"
  60. #define GGATED_EXPORT_FILE "/etc/gg.exports"
  61. struct ggd_connection {
  62. off_t c_mediasize;
  63. unsigned c_sectorsize;
  64. unsigned c_flags; /* flags (RO/RW) */
  65. int c_diskfd;
  66. int c_sendfd;
  67. int c_recvfd;
  68. time_t c_birthtime;
  69. char *c_path;
  70. uint64_t c_token;
  71. in_addr_t c_srcip;
  72. LIST_ENTRY(ggd_connection) c_next;
  73. };
  74. struct ggd_request {
  75. struct g_gate_hdr r_hdr;
  76. char *r_data;
  77. TAILQ_ENTRY(ggd_request) r_next;
  78. };
  79. #define r_cmd r_hdr.gh_cmd
  80. #define r_offset r_hdr.gh_offset
  81. #define r_length r_hdr.gh_length
  82. #define r_error r_hdr.gh_error
  83. struct ggd_export {
  84. char *e_path; /* path to device/file */
  85. in_addr_t e_ip; /* remote IP address */
  86. in_addr_t e_mask; /* IP mask */
  87. unsigned e_flags; /* flags (RO/RW) */
  88. SLIST_ENTRY(ggd_export) e_next;
  89. };
  90. static int niothreads = 20;
  91. static const char *exports_file = GGATED_EXPORT_FILE;
  92. static int got_sighup = 0;
  93. static TAILQ_HEAD(, ggd_request) inqueue = TAILQ_HEAD_INITIALIZER(inqueue);
  94. static TAILQ_HEAD(, ggd_request) outqueue = TAILQ_HEAD_INITIALIZER(outqueue);
  95. static pthread_mutex_t inqueue_mtx, outqueue_mtx;
  96. static pthread_cond_t inqueue_cond, outqueue_cond;
  97. static SLIST_HEAD(, ggd_export) exports = SLIST_HEAD_INITIALIZER(exports);
  98. static LIST_HEAD(, ggd_connection) connections = LIST_HEAD_INITIALIZER(connections);
  99. static void *recv_thread(void *arg);
  100. static void *disk_thread(void *arg);
  101. static void *send_thread(void *arg);
  102. static void
  103. usage(void)
  104. {
  105. fprintf(stderr, "usage: %s [-nv] [-a address] [-F pidfile] [-p port] "
  106. "[-R rcvbuf] [-S sndbuf] [-s socket] [exports file]\n", getprogname());
  107. exit(EXIT_FAILURE);
  108. }
  109. static char *
  110. ip2str(in_addr_t ip)
  111. {
  112. static char sip[16];
  113. snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
  114. ((ip >> 24) & 0xff),
  115. ((ip >> 16) & 0xff),
  116. ((ip >> 8) & 0xff),
  117. (ip & 0xff));
  118. return (sip);
  119. }
  120. static in_addr_t
  121. countmask(unsigned m)
  122. {
  123. in_addr_t mask;
  124. if (m == 0) {
  125. mask = 0x0;
  126. } else {
  127. mask = 1 << (32 - m);
  128. mask--;
  129. mask = ~mask;
  130. }
  131. return (mask);
  132. }
  133. static void
  134. line_parse(char *line, unsigned lineno)
  135. {
  136. struct ggd_export *ex;
  137. char *word, *path, *sflags;
  138. unsigned flags, i, vmask;
  139. in_addr_t ip, mask;
  140. ip = mask = flags = vmask = 0;
  141. path = NULL;
  142. sflags = NULL;
  143. for (i = 0, word = strtok(line, " \t"); word != NULL;
  144. i++, word = strtok(NULL, " \t")) {
  145. switch (i) {
  146. case 0: /* IP address or host name */
  147. ip = g_gate_str2ip(strsep(&word, "/"));
  148. if (ip == INADDR_NONE) {
  149. g_gate_xlog("Invalid IP/host name at line %u.",
  150. lineno);
  151. }
  152. ip = ntohl(ip);
  153. if (word == NULL)
  154. vmask = 32;
  155. else {
  156. errno = 0;
  157. vmask = strtoul(word, NULL, 10);
  158. if (vmask == 0 && errno != 0) {
  159. g_gate_xlog("Invalid IP mask value at "
  160. "line %u.", lineno);
  161. }
  162. if ((unsigned)vmask > 32) {
  163. g_gate_xlog("Invalid IP mask value at line %u.",
  164. lineno);
  165. }
  166. }
  167. mask = countmask(vmask);
  168. break;
  169. case 1: /* flags */
  170. if (strcasecmp("rd", word) == 0 ||
  171. strcasecmp("ro", word) == 0) {
  172. flags = O_RDONLY;
  173. } else if (strcasecmp("wo", word) == 0) {
  174. flags = O_WRONLY;
  175. } else if (strcasecmp("rw", word) == 0) {
  176. flags = O_RDWR;
  177. } else {
  178. g_gate_xlog("Invalid value in flags field at "
  179. "line %u.", lineno);
  180. }
  181. sflags = word;
  182. break;
  183. case 2: /* path */
  184. if (strlen(word) >= MAXPATHLEN) {
  185. g_gate_xlog("Path too long at line %u. ",
  186. lineno);
  187. }
  188. path = word;
  189. break;
  190. default:
  191. g_gate_xlog("Too many arguments at line %u. ", lineno);
  192. }
  193. }
  194. if (i != 3)
  195. g_gate_xlog("Too few arguments at line %u.", lineno);
  196. ex = malloc(sizeof(*ex));
  197. if (ex == NULL)
  198. g_gate_xlog("Not enough memory.");
  199. ex->e_path = strdup(path);
  200. if (ex->e_path == NULL)
  201. g_gate_xlog("Not enough memory.");
  202. /* Made 'and' here. */
  203. ex->e_ip = (ip & mask);
  204. ex->e_mask = mask;
  205. ex->e_flags = flags;
  206. SLIST_INSERT_HEAD(&exports, ex, e_next);
  207. g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
  208. ip2str(ex->e_ip), vmask, path, sflags);
  209. }
  210. static void
  211. exports_clear(void)
  212. {
  213. struct ggd_export *ex;
  214. while (!SLIST_EMPTY(&exports)) {
  215. ex = SLIST_FIRST(&exports);
  216. SLIST_REMOVE_HEAD(&exports, e_next);
  217. free(ex);
  218. }
  219. }
  220. #define EXPORTS_LINE_SIZE 2048
  221. static void
  222. exports_get(void)
  223. {
  224. char buf[EXPORTS_LINE_SIZE], *line;
  225. unsigned lineno = 0, objs = 0, len;
  226. FILE *fd;
  227. exports_clear();
  228. fd = fopen(exports_file, "r");
  229. if (fd == NULL) {
  230. g_gate_xlog("Cannot open exports file (%s): %s.", exports_file,
  231. strerror(errno));
  232. }
  233. g_gate_log(LOG_INFO, "Reading exports file (%s).", exports_file);
  234. for (;;) {
  235. if (fgets(buf, sizeof(buf), fd) == NULL) {
  236. if (feof(fd))
  237. break;
  238. g_gate_xlog("Error while reading exports file: %s.",
  239. strerror(errno));
  240. }
  241. /* Increase line count. */
  242. lineno++;
  243. /* Skip spaces and tabs. */
  244. for (line = buf; *line == ' ' || *line == '\t'; ++line)
  245. ;
  246. /* Empty line, comment or empty line at the end of file. */
  247. if (*line == '\n' || *line == '#' || *line == '\0')
  248. continue;
  249. len = strlen(line);
  250. if (line[len - 1] == '\n') {
  251. /* Remove new line char. */
  252. line[len - 1] = '\0';
  253. } else {
  254. if (!feof(fd))
  255. g_gate_xlog("Line %u too long.", lineno);
  256. }
  257. line_parse(line, lineno);
  258. objs++;
  259. }
  260. fclose(fd);
  261. if (objs == 0)
  262. g_gate_xlog("There are no objects to export.");
  263. g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
  264. }
  265. static int
  266. exports_check(struct ggd_export *ex, struct g_gate_cinit *cinit,
  267. struct ggd_connection *conn)
  268. {
  269. char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
  270. int error = 0, flags;
  271. strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
  272. strlcat(ipmask, "/", sizeof(ipmask));
  273. strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
  274. if ((cinit->gc_flags & GGATE_FLAG_RDONLY) != 0) {
  275. if (ex->e_flags == O_WRONLY) {
  276. g_gate_log(LOG_WARNING, "Read-only access requested, "
  277. "but %s (%s) is exported write-only.", ex->e_path,
  278. ipmask);
  279. return (EPERM);
  280. } else {
  281. conn->c_flags |= GGATE_FLAG_RDONLY;
  282. }
  283. } else if ((cinit->gc_flags & GGATE_FLAG_WRONLY) != 0) {
  284. if (ex->e_flags == O_RDONLY) {
  285. g_gate_log(LOG_WARNING, "Write-only access requested, "
  286. "but %s (%s) is exported read-only.", ex->e_path,
  287. ipmask);
  288. return (EPERM);
  289. } else {
  290. conn->c_flags |= GGATE_FLAG_WRONLY;
  291. }
  292. } else {
  293. if (ex->e_flags == O_RDONLY) {
  294. g_gate_log(LOG_WARNING, "Read-write access requested, "
  295. "but %s (%s) is exported read-only.", ex->e_path,
  296. ipmask);
  297. return (EPERM);
  298. } else if (ex->e_flags == O_WRONLY) {
  299. g_gate_log(LOG_WARNING, "Read-write access requested, "
  300. "but %s (%s) is exported write-only.", ex->e_path,
  301. ipmask);
  302. return (EPERM);
  303. }
  304. }
  305. if ((conn->c_flags & GGATE_FLAG_RDONLY) != 0)
  306. flags = O_RDONLY;
  307. else if ((conn->c_flags & GGATE_FLAG_WRONLY) != 0)
  308. flags = O_WRONLY;
  309. else
  310. flags = O_RDWR;
  311. conn->c_diskfd = open(ex->e_path, flags);
  312. if (conn->c_diskfd == -1) {
  313. error = errno;
  314. g_gate_log(LOG_ERR, "Cannot open %s: %s.", ex->e_path,
  315. strerror(error));
  316. return (error);
  317. }
  318. return (0);
  319. }
  320. static struct ggd_export *
  321. exports_find(struct sockaddr *s, struct g_gate_cinit *cinit,
  322. struct ggd_connection *conn)
  323. {
  324. struct ggd_export *ex;
  325. in_addr_t ip;
  326. int error;
  327. ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
  328. SLIST_FOREACH(ex, &exports, e_next) {
  329. if ((ip & ex->e_mask) != ex->e_ip) {
  330. g_gate_log(LOG_DEBUG, "exports[%s]: IP mismatch.",
  331. ex->e_path);
  332. continue;
  333. }
  334. if (strcmp(cinit->gc_path, ex->e_path) != 0) {
  335. g_gate_log(LOG_DEBUG, "exports[%s]: Path mismatch.",
  336. ex->e_path);
  337. continue;
  338. }
  339. error = exports_check(ex, cinit, conn);
  340. if (error == 0)
  341. return (ex);
  342. else {
  343. errno = error;
  344. return (NULL);
  345. }
  346. }
  347. g_gate_log(LOG_WARNING, "Unauthorized connection from: %s.",
  348. ip2str(ip));
  349. errno = EPERM;
  350. return (NULL);
  351. }
  352. /*
  353. * Remove timed out connections.
  354. */
  355. static void
  356. connection_cleanups(void)
  357. {
  358. struct ggd_connection *conn, *tconn;
  359. time_t now;
  360. time(&now);
  361. LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
  362. if (now - conn->c_birthtime > 10) {
  363. LIST_REMOVE(conn, c_next);
  364. g_gate_log(LOG_NOTICE,
  365. "Connection from %s [%s] removed.",
  366. ip2str(conn->c_srcip), conn->c_path);
  367. close(conn->c_diskfd);
  368. close(conn->c_sendfd);
  369. close(conn->c_recvfd);
  370. free(conn->c_path);
  371. free(conn);
  372. }
  373. }
  374. }
  375. static struct ggd_connection *
  376. connection_find(struct g_gate_cinit *cinit)
  377. {
  378. struct ggd_connection *conn;
  379. LIST_FOREACH(conn, &connections, c_next) {
  380. if (conn->c_token == cinit->gc_token)
  381. break;
  382. }
  383. return (conn);
  384. }
  385. static struct ggd_connection *
  386. connection_new(struct g_gate_cinit *cinit, struct sockaddr *s, int sfd)
  387. {
  388. struct ggd_connection *conn;
  389. in_addr_t ip;
  390. /*
  391. * First, look for old connections.
  392. * We probably should do it every X seconds, but what for?
  393. * It is only dangerous if an attacker wants to overload connections
  394. * queue, so here is a good place to do the cleanups.
  395. */
  396. connection_cleanups();
  397. conn = malloc(sizeof(*conn));
  398. if (conn == NULL)
  399. return (NULL);
  400. conn->c_path = strdup(cinit->gc_path);
  401. if (conn->c_path == NULL) {
  402. free(conn);
  403. return (NULL);
  404. }
  405. conn->c_token = cinit->gc_token;
  406. ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
  407. conn->c_srcip = ip;
  408. conn->c_sendfd = conn->c_recvfd = -1;
  409. if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0)
  410. conn->c_sendfd = sfd;
  411. else
  412. conn->c_recvfd = sfd;
  413. conn->c_mediasize = 0;
  414. conn->c_sectorsize = 0;
  415. time(&conn->c_birthtime);
  416. conn->c_flags = cinit->gc_flags;
  417. LIST_INSERT_HEAD(&connections, conn, c_next);
  418. g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(ip),
  419. conn->c_path);
  420. return (conn);
  421. }
  422. static int
  423. connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit,
  424. struct sockaddr *s, int sfd)
  425. {
  426. in_addr_t ip;
  427. ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
  428. if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) {
  429. if (conn->c_sendfd != -1) {
  430. g_gate_log(LOG_WARNING,
  431. "Send socket already exists [%s, %s].", ip2str(ip),
  432. conn->c_path);
  433. return (EEXIST);
  434. }
  435. conn->c_sendfd = sfd;
  436. } else {
  437. if (conn->c_recvfd != -1) {
  438. g_gate_log(LOG_WARNING,
  439. "Receive socket already exists [%s, %s].",
  440. ip2str(ip), conn->c_path);
  441. return (EEXIST);
  442. }
  443. conn->c_recvfd = sfd;
  444. }
  445. g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(ip),
  446. conn->c_path);
  447. return (0);
  448. }
  449. /*
  450. * Remove one socket from the given connection or the whole
  451. * connection if sfd == -1.
  452. */
  453. static void
  454. connection_remove(struct ggd_connection *conn)
  455. {
  456. LIST_REMOVE(conn, c_next);
  457. g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
  458. ip2str(conn->c_srcip), conn->c_path);
  459. if (conn->c_sendfd != -1)
  460. close(conn->c_sendfd);
  461. if (conn->c_recvfd != -1)
  462. close(conn->c_recvfd);
  463. free(conn->c_path);
  464. free(conn);
  465. }
  466. static int
  467. connection_ready(struct ggd_connection *conn)
  468. {
  469. return (conn->c_sendfd != -1 && conn->c_recvfd != -1);
  470. }
  471. static void
  472. connection_launch(struct ggd_connection *conn)
  473. {
  474. pthread_t td;
  475. int error, pid;
  476. int i;
  477. pid = fork();
  478. if (pid > 0)
  479. return;
  480. else if (pid == -1) {
  481. g_gate_log(LOG_ERR, "Cannot fork: %s.", strerror(errno));
  482. return;
  483. }
  484. g_gate_log(LOG_DEBUG, "Process created [%s].", conn->c_path);
  485. /*
  486. * Create condition variables and mutexes for in-queue and out-queue
  487. * synchronization.
  488. */
  489. error = pthread_mutex_init(&inqueue_mtx, NULL);
  490. if (error != 0) {
  491. g_gate_xlog("pthread_mutex_init(inqueue_mtx): %s.",
  492. strerror(error));
  493. }
  494. error = pthread_cond_init(&inqueue_cond, NULL);
  495. if (error != 0) {
  496. g_gate_xlog("pthread_cond_init(inqueue_cond): %s.",
  497. strerror(error));
  498. }
  499. error = pthread_mutex_init(&outqueue_mtx, NULL);
  500. if (error != 0) {
  501. g_gate_xlog("pthread_mutex_init(outqueue_mtx): %s.",
  502. strerror(error));
  503. }
  504. error = pthread_cond_init(&outqueue_cond, NULL);
  505. if (error != 0) {
  506. g_gate_xlog("pthread_cond_init(outqueue_cond): %s.",
  507. strerror(error));
  508. }
  509. /*
  510. * Create threads:
  511. * recvtd - thread for receiving I/O request
  512. * diskio - thread for doing I/O request
  513. * sendtd - thread for sending I/O requests back
  514. */
  515. error = pthread_create(&td, NULL, send_thread, conn);
  516. if (error != 0) {
  517. g_gate_xlog("pthread_create(send_thread): %s.",
  518. strerror(error));
  519. }
  520. error = pthread_create(&td, NULL, recv_thread, conn);
  521. if (error != 0) {
  522. g_gate_xlog("pthread_create(recv_thread): %s.",
  523. strerror(error));
  524. }
  525. for (i = 0; niothreads && i < niothreads - 1; i++) {
  526. error = pthread_create(&td, NULL, disk_thread, conn);
  527. if (error != 0) {
  528. g_gate_xlog("pthread_create(disk_thread): %s.",
  529. strerror(error));
  530. }
  531. }
  532. disk_thread(conn);
  533. }
  534. static void
  535. sendfail(int sfd, int error, const char *fmt, ...)
  536. {
  537. struct g_gate_sinit sinit;
  538. va_list ap;
  539. ssize_t data;
  540. memset(&sinit, 0, sizeof(sinit));
  541. sinit.gs_error = error;
  542. g_gate_swap2n_sinit(&sinit);
  543. data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
  544. g_gate_swap2h_sinit(&sinit);
  545. if (data != sizeof(sinit)) {
  546. g_gate_log(LOG_WARNING, "Cannot send initial packet: %s.",
  547. strerror(errno));
  548. return;
  549. }
  550. if (fmt != NULL) {
  551. va_start(ap, fmt);
  552. g_gate_vlog(LOG_WARNING, fmt, ap);
  553. va_end(ap);
  554. }
  555. }
  556. static void *
  557. malloc_waitok(size_t size)
  558. {
  559. void *p;
  560. while ((p = malloc(size)) == NULL) {
  561. g_gate_log(LOG_DEBUG, "Cannot allocate %zu bytes.", size);
  562. sleep(1);
  563. }
  564. return (p);
  565. }
  566. static void *
  567. recv_thread(void *arg)
  568. {
  569. struct ggd_connection *conn;
  570. struct ggd_request *req;
  571. ssize_t data;
  572. int error, fd;
  573. conn = arg;
  574. g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
  575. fd = conn->c_recvfd;
  576. for (;;) {
  577. /*
  578. * Get header packet.
  579. */
  580. req = malloc_waitok(sizeof(*req));
  581. data = g_gate_recv(fd, &req->r_hdr, sizeof(req->r_hdr),
  582. MSG_WAITALL);
  583. if (data == 0) {
  584. g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
  585. exit(EXIT_SUCCESS);
  586. } else if (data == -1) {
  587. g_gate_xlog("Error while receiving hdr packet: %s.",
  588. strerror(errno));
  589. } else if (data != sizeof(req->r_hdr)) {
  590. g_gate_xlog("Malformed hdr packet received.");
  591. }
  592. g_gate_log(LOG_DEBUG, "Received hdr packet.");
  593. g_gate_swap2h_hdr(&req->r_hdr);
  594. g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
  595. (intmax_t)req->r_offset, (unsigned)req->r_length);
  596. /*
  597. * Allocate memory for data.
  598. */
  599. req->r_data = malloc_waitok(req->r_length);
  600. /*
  601. * Receive data to write for WRITE request.
  602. */
  603. if (req->r_cmd == GGATE_CMD_WRITE) {
  604. g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
  605. req->r_length);
  606. data = g_gate_recv(fd, req->r_data, req->r_length,
  607. MSG_WAITALL);
  608. if (data == -1) {
  609. g_gate_xlog("Error while receiving data: %s.",
  610. strerror(errno));
  611. }
  612. }
  613. /*
  614. * Put the request onto the incoming queue.
  615. */
  616. error = pthread_mutex_lock(&inqueue_mtx);
  617. assert(error == 0);
  618. TAILQ_INSERT_TAIL(&inqueue, req, r_next);
  619. error = pthread_cond_signal(&inqueue_cond);
  620. assert(error == 0);
  621. error = pthread_mutex_unlock(&inqueue_mtx);
  622. assert(error == 0);
  623. }
  624. }
  625. static void *
  626. disk_thread(void *arg)
  627. {
  628. struct ggd_connection *conn;
  629. struct ggd_request *req;
  630. ssize_t data;
  631. int error, fd;
  632. conn = arg;
  633. g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
  634. fd = conn->c_diskfd;
  635. for (;;) {
  636. /*
  637. * Get a request from the incoming queue.
  638. */
  639. error = pthread_mutex_lock(&inqueue_mtx);
  640. assert(error == 0);
  641. while ((req = TAILQ_FIRST(&inqueue)) == NULL) {
  642. error = pthread_cond_wait(&inqueue_cond, &inqueue_mtx);
  643. assert(error == 0);
  644. }
  645. TAILQ_REMOVE(&inqueue, req, r_next);
  646. error = pthread_mutex_unlock(&inqueue_mtx);
  647. assert(error == 0);
  648. /*
  649. * Check the request.
  650. */
  651. assert(req->r_cmd == GGATE_CMD_READ || req->r_cmd == GGATE_CMD_WRITE);
  652. assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize);
  653. assert((req->r_offset % conn->c_sectorsize) == 0);
  654. assert((req->r_length % conn->c_sectorsize) == 0);
  655. g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
  656. (intmax_t)req->r_offset, (unsigned)req->r_length);
  657. /*
  658. * Do the request.
  659. */
  660. data = 0;
  661. switch (req->r_cmd) {
  662. case GGATE_CMD_READ:
  663. data = pread(fd, req->r_data, req->r_length,
  664. req->r_offset);
  665. break;
  666. case GGATE_CMD_WRITE:
  667. data = pwrite(fd, req->r_data, req->r_length,
  668. req->r_offset);
  669. /* Free data memory here - better sooner. */
  670. free(req->r_data);
  671. req->r_data = NULL;
  672. break;
  673. }
  674. if (data != (ssize_t)req->r_length) {
  675. /* Report short reads/writes as I/O errors. */
  676. if (errno == 0)
  677. errno = EIO;
  678. g_gate_log(LOG_ERR, "Disk error: %s", strerror(errno));
  679. req->r_error = errno;
  680. if (req->r_data != NULL) {
  681. free(req->r_data);
  682. req->r_data = NULL;
  683. }
  684. }
  685. /*
  686. * Put the request onto the outgoing queue.
  687. */
  688. error = pthread_mutex_lock(&outqueue_mtx);
  689. assert(error == 0);
  690. TAILQ_INSERT_TAIL(&outqueue, req, r_next);
  691. error = pthread_cond_signal(&outqueue_cond);
  692. assert(error == 0);
  693. error = pthread_mutex_unlock(&outqueue_mtx);
  694. assert(error == 0);
  695. }
  696. /* NOTREACHED */
  697. return (NULL);
  698. }
  699. static void *
  700. send_thread(void *arg)
  701. {
  702. struct ggd_connection *conn;
  703. struct ggd_request *req;
  704. ssize_t data;
  705. int error, fd;
  706. conn = arg;
  707. g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
  708. fd = conn->c_sendfd;
  709. for (;;) {
  710. /*
  711. * Get a request from the outgoing queue.
  712. */
  713. error = pthread_mutex_lock(&outqueue_mtx);
  714. assert(error == 0);
  715. while ((req = TAILQ_FIRST(&outqueue)) == NULL) {
  716. error = pthread_cond_wait(&outqueue_cond,
  717. &outqueue_mtx);
  718. assert(error == 0);
  719. }
  720. TAILQ_REMOVE(&outqueue, req, r_next);
  721. error = pthread_mutex_unlock(&outqueue_mtx);
  722. assert(error == 0);
  723. g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
  724. (intmax_t)req->r_offset, (unsigned)req->r_length);
  725. /*
  726. * Send the request.
  727. */
  728. g_gate_swap2n_hdr(&req->r_hdr);
  729. if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) {
  730. g_gate_xlog("Error while sending hdr packet: %s.",
  731. strerror(errno));
  732. }
  733. g_gate_log(LOG_DEBUG, "Sent hdr packet.");
  734. g_gate_swap2h_hdr(&req->r_hdr);
  735. if (req->r_data != NULL) {
  736. data = g_gate_send(fd, req->r_data, req->r_length, 0);
  737. if (data != (ssize_t)req->r_length) {
  738. g_gate_xlog("Error while sending data: %s.",
  739. strerror(errno));
  740. }
  741. g_gate_log(LOG_DEBUG,
  742. "Sent %zd bytes (offset=%ju, size=%zu).", data,
  743. (uintmax_t)req->r_offset, (size_t)req->r_length);
  744. free(req->r_data);
  745. }
  746. free(req);
  747. }
  748. /* NOTREACHED */
  749. return (NULL);
  750. }
  751. static void
  752. log_connection(struct sockaddr *from)
  753. {
  754. in_addr_t ip;
  755. ip = htonl(((struct sockaddr_in *)(void *)from)->sin_addr.s_addr);
  756. g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
  757. }
  758. static int
  759. handshake(struct sockaddr *from, int sfd)
  760. {
  761. struct g_gate_version ver;
  762. struct g_gate_cinit cinit;
  763. struct g_gate_sinit sinit;
  764. struct ggd_connection *conn;
  765. struct ggd_export *ex;
  766. ssize_t data;
  767. log_connection(from);
  768. /*
  769. * Phase 1: Version verification.
  770. */
  771. g_gate_log(LOG_DEBUG, "Receiving version packet.");
  772. data = g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL);
  773. g_gate_swap2h_version(&ver);
  774. if (data != sizeof(ver)) {
  775. g_gate_log(LOG_WARNING, "Malformed version packet.");
  776. return (0);
  777. }
  778. g_gate_log(LOG_DEBUG, "Version packet received.");
  779. if (memcmp(ver.gv_magic, GGATE_MAGIC, strlen(GGATE_MAGIC)) != 0) {
  780. g_gate_log(LOG_WARNING, "Invalid magic field.");
  781. return (0);
  782. }
  783. if (ver.gv_version != GGATE_VERSION) {
  784. g_gate_log(LOG_WARNING, "Version %u is not supported.",
  785. ver.gv_version);
  786. return (0);
  787. }
  788. ver.gv_error = 0;
  789. g_gate_swap2n_version(&ver);
  790. data = g_gate_send(sfd, &ver, sizeof(ver), 0);
  791. g_gate_swap2h_version(&ver);
  792. if (data == -1) {
  793. sendfail(sfd, errno, "Error while sending version packet: %s.",
  794. strerror(errno));
  795. return (0);
  796. }
  797. /*
  798. * Phase 2: Request verification.
  799. */
  800. g_gate_log(LOG_DEBUG, "Receiving initial packet.");
  801. data = g_gate_recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
  802. g_gate_swap2h_cinit(&cinit);
  803. if (data != sizeof(cinit)) {
  804. g_gate_log(LOG_WARNING, "Malformed initial packet.");
  805. return (0);
  806. }
  807. g_gate_log(LOG_DEBUG, "Initial packet received.");
  808. conn = connection_find(&cinit);
  809. if (conn != NULL) {
  810. /*
  811. * Connection should already exists.
  812. */
  813. g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).",
  814. (unsigned long)conn->c_token);
  815. if (connection_add(conn, &cinit, from, sfd) == -1) {
  816. connection_remove(conn);
  817. return (0);
  818. }
  819. } else {
  820. /*
  821. * New connection, allocate space.
  822. */
  823. conn = connection_new(&cinit, from, sfd);
  824. if (conn == NULL) {
  825. sendfail(sfd, ENOMEM,
  826. "Cannot allocate new connection.");
  827. return (0);
  828. }
  829. g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
  830. (unsigned long)conn->c_token);
  831. }
  832. ex = exports_find(from, &cinit, conn);
  833. if (ex == NULL) {
  834. sendfail(sfd, errno, NULL);
  835. connection_remove(conn);
  836. return (0);
  837. }
  838. if (conn->c_mediasize == 0) {
  839. conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
  840. conn->c_sectorsize = g_gate_sectorsize(conn->c_diskfd);
  841. }
  842. sinit.gs_mediasize = conn->c_mediasize;
  843. sinit.gs_sectorsize = conn->c_sectorsize;
  844. sinit.gs_error = 0;
  845. g_gate_log(LOG_DEBUG, "Sending initial packet.");
  846. g_gate_swap2n_sinit(&sinit);
  847. data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
  848. g_gate_swap2h_sinit(&sinit);
  849. if (data == -1) {
  850. sendfail(sfd, errno, "Error while sending initial packet: %s.",
  851. strerror(errno));
  852. return (0);
  853. }
  854. if (connection_ready(conn)) {
  855. connection_launch(conn);
  856. connection_remove(conn);
  857. }
  858. return (1);
  859. }
  860. static void
  861. huphandler(int sig __unused)
  862. {
  863. got_sighup = 1;
  864. }
  865. /*
  866. * If sockaddr is not NULL, bind to the Unix domain socket at sockname,
  867. * otherwise bind to the INET addr:port
  868. */
  869. static int
  870. bindsocket(in_addr_t bindaddr, int port, char *sockname)
  871. {
  872. struct sockaddr_un unixaddr;
  873. struct sockaddr_in serv;
  874. int sfd;
  875. g_gate_log(LOG_INFO, "binding socket");
  876. if (sockname != NULL) {
  877. if (strlen(sockname) + 1 > sizeof(unixaddr.sun_path))
  878. g_gate_xlog("Socket path is too long.");
  879. sfd = socket(AF_UNIX, SOCK_STREAM, 0);
  880. if (sfd == -1)
  881. g_gate_xlog("Cannot open stream socket: %s.", strerror(errno));
  882. unixaddr = (struct sockaddr_un) {
  883. .sun_len = sizeof(unixaddr),
  884. .sun_family = AF_UNIX,
  885. };
  886. strncpy(unixaddr.sun_path, sockname, sizeof(unixaddr.sun_path));
  887. if (bind(sfd, (struct sockaddr *)&unixaddr, sizeof(unixaddr)) == -1)
  888. g_gate_xlog("bind(): %s.", strerror(errno));
  889. if (listen(sfd, 5) == -1)
  890. g_gate_xlog("listen(): %s.", strerror(errno));
  891. g_gate_log(LOG_INFO, "Listen on path: %s.", sockname);
  892. } else {
  893. sfd = socket(AF_INET, SOCK_STREAM, 0);
  894. if (sfd == -1)
  895. g_gate_xlog("Cannot open stream socket: %s.", strerror(errno));
  896. bzero(&serv, sizeof(serv));
  897. serv.sin_family = AF_INET;
  898. serv.sin_addr.s_addr = bindaddr;
  899. serv.sin_port = htons(port);
  900. g_gate_socket_settings(sfd);
  901. if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1)
  902. g_gate_xlog("bind(): %s.", strerror(errno));
  903. if (listen(sfd, 5) == -1)
  904. g_gate_xlog("listen(): %s.", strerror(errno));
  905. g_gate_log(LOG_INFO, "Listen on port: %d.", port);
  906. }
  907. return sfd;
  908. }
  909. int
  910. main(int argc, char *argv[])
  911. {
  912. const char *ggated_pidfile = _PATH_VARRUN "/ggated.pid";
  913. char *sockname = NULL;
  914. struct pidfh *pfh;
  915. struct sockaddr from;
  916. socklen_t fromlen;
  917. in_addr_t bindaddr;
  918. pid_t otherpid;
  919. int ch, sfd, tmpsfd;
  920. unsigned port;
  921. bindaddr = htonl(INADDR_ANY);
  922. port = G_GATE_PORT;
  923. while ((ch = getopt(argc, argv, "a:hnp:q:F:R:S:s:v")) != -1) {
  924. switch (ch) {
  925. case 'a':
  926. bindaddr = g_gate_str2ip(optarg);
  927. if (bindaddr == INADDR_NONE) {
  928. errx(EXIT_FAILURE,
  929. "Invalid IP/host name to bind to.");
  930. }
  931. break;
  932. case 'F':
  933. ggated_pidfile = optarg;
  934. break;
  935. case 'n':
  936. nagle = 0;
  937. break;
  938. case 'p':
  939. errno = 0;
  940. port = strtoul(optarg, NULL, 10);
  941. if (port == 0 && errno != 0)
  942. errx(EXIT_FAILURE, "Invalid port.");
  943. break;
  944. case 'q':
  945. niothreads = strtol(optarg, NULL, 10);
  946. if (niothreads < 0)
  947. errx(EXIT_FAILURE, "Invalid queue size.");
  948. break;
  949. case 'R':
  950. errno = 0;
  951. rcvbuf = strtoul(optarg, NULL, 10);
  952. if (rcvbuf == 0 && errno != 0)
  953. errx(EXIT_FAILURE, "Invalid rcvbuf.");
  954. break;
  955. case 'S':
  956. errno = 0;
  957. sndbuf = strtoul(optarg, NULL, 10);
  958. if (sndbuf == 0 && errno != 0)
  959. errx(EXIT_FAILURE, "Invalid sndbuf.");
  960. break;
  961. case 's':
  962. sockname = strdup(optarg);
  963. break;
  964. case 'v':
  965. g_gate_verbose++;
  966. break;
  967. case 'h':
  968. default:
  969. usage();
  970. }
  971. }
  972. argc -= optind;
  973. argv += optind;
  974. if (argv[0] != NULL)
  975. exports_file = argv[0];
  976. exports_get();
  977. pfh = pidfile_open(ggated_pidfile, 0600, &otherpid);
  978. if (pfh == NULL) {
  979. if (errno == EEXIST) {
  980. errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
  981. (intmax_t)otherpid);
  982. }
  983. err(EXIT_FAILURE, "Cannot open/create pidfile");
  984. }
  985. signal(SIGCHLD, SIG_IGN);
  986. sfd = bindsocket(bindaddr, port, sockname);
  987. if (!g_gate_verbose) {
  988. FILE *t = fopen("local.log", "w");
  989. /* Run in daemon mode. */
  990. if (daemon(0, 0) == -1)
  991. g_gate_xlog("Cannot daemonize: %s", strerror(errno));
  992. stderr = t;
  993. }
  994. pidfile_write(pfh);
  995. signal(SIGHUP, huphandler);
  996. for (;;) {
  997. fromlen = sizeof(from);
  998. tmpsfd = accept(sfd, &from, &fromlen);
  999. if (tmpsfd == -1)
  1000. g_gate_xlog("accept(): %s.", strerror(errno));
  1001. if (got_sighup) {
  1002. got_sighup = 0;
  1003. exports_get();
  1004. }
  1005. if (!handshake(&from, tmpsfd))
  1006. close(tmpsfd);
  1007. }
  1008. close(sfd);
  1009. pidfile_remove(pfh);
  1010. exit(EXIT_SUCCESS);
  1011. }