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.
 
 
 
 

1045 lines
25 KiB

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