db_streams.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_stream.c
  17. *
  18. * This file contains APIs to open, read, write and get attribute of
  19. * streams.
  20. */
  21. #include "pal_defs.h"
  22. #include "pal_freebsd_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_freebsd.h"
  26. #include "pal_debug.h"
  27. #include "pal_error.h"
  28. #include "api.h"
  29. #include <sys/types.h>
  30. typedef __kernel_pid_t pid_t;
  31. #include <sys/msg.h>
  32. #include <sys/socket.h>
  33. #include <sys/wait.h>
  34. #include <sys/stat.h>
  35. #include <poll.h>
  36. #include <sys/signal.h>
  37. #include <netinet/in.h>
  38. #include <errno.h>
  39. void _DkPrintConsole (const void * buf, int size)
  40. {
  41. INLINE_SYSCALL(write, 3, 2, buf, size);
  42. }
  43. bool stataccess (struct stat * stat, int acc)
  44. {
  45. mode_t mode = stat->st_mode;
  46. if (bsd_state.uid && bsd_state.uid == stat->st_uid) {
  47. mode >>= 6;
  48. goto out;
  49. }
  50. if (bsd_state.gid && bsd_state.gid == stat->st_gid) {
  51. mode >>= 3;
  52. goto out;
  53. }
  54. if (!bsd_state.uid)
  55. mode >>= 6;
  56. out:
  57. return (mode & acc);
  58. }
  59. int handle_set_cloexec (PAL_HANDLE handle, bool enable)
  60. {
  61. for (int i = 0 ; i < MAX_FDS ; i++)
  62. if (handle->hdr.flags & (RFD(i)|WFD(i))) {
  63. long flags = enable ? FD_CLOEXEC : 0;
  64. int ret = INLINE_SYSCALL(fcntl, 3,
  65. handle->hdr.fds[i], F_SETFD,
  66. flags);
  67. if (IS_ERR(ret) && ERRNO(ret) != EBADF)
  68. return -PAL_ERROR_DENIED;
  69. }
  70. return 0;
  71. }
  72. /* _DkStreamUnmap for internal use. Unmap stream at certain memory address.
  73. The memory is unmapped as a whole.*/
  74. int _DkStreamUnmap (void * addr, uint64_t size)
  75. {
  76. /* Just let the kernel tell us if the mapping isn't good. */
  77. int ret = INLINE_SYSCALL(munmap, 2, addr, size);
  78. if (IS_ERR(ret))
  79. return -PAL_ERROR_DENIED;
  80. return 0;
  81. }
  82. static size_t addr_size(const struct sockaddr* addr) {
  83. switch (addr->sa_family) {
  84. case AF_INET:
  85. return sizeof(struct sockaddr_in);
  86. case AF_INET6:
  87. return sizeof(struct sockaddr_in6);
  88. default:
  89. return 0;
  90. }
  91. }
  92. int handle_serialize (PAL_HANDLE handle, void ** data)
  93. {
  94. int hdlsz = handle_size(handle);
  95. const void * d1, * d2;
  96. int dsz1 = 0, dsz2 = 0;
  97. // ~ Check cargo PAL_HANDLE - is allowed to be sent (White List checking
  98. // of cargo type)
  99. // ~ Also, Initialize common parameter formessage passing
  100. // Channel between parent and child
  101. switch(PAL_GET_TYPE(handle)) {
  102. case pal_type_file:
  103. d1 = handle->file.realpath;
  104. dsz1 = strlen(handle->file.realpath) + 1;
  105. break;
  106. case pal_type_pipe:
  107. case pal_type_pipesrv:
  108. case pal_type_pipecli:
  109. case pal_type_pipeprv:
  110. break;
  111. case pal_type_dev:
  112. if (handle->dev.realpath) {
  113. d1 = handle->dev.realpath;
  114. dsz1 = strlen(handle->dev.realpath) + 1;
  115. }
  116. break;
  117. case pal_type_dir:
  118. if (handle->dir.realpath) {
  119. d1 = handle->dir.realpath;
  120. dsz1 = strlen(handle->dir.realpath) + 1;
  121. }
  122. break;
  123. case pal_type_tcp:
  124. case pal_type_tcpsrv:
  125. case pal_type_udp:
  126. case pal_type_udpsrv:
  127. if (handle->sock.bind) {
  128. d1 = handle->sock.bind;
  129. dsz1 = addr_size(handle->sock.bind);
  130. }
  131. if (handle->sock.conn) {
  132. d2 = handle->sock.conn;
  133. dsz2 = addr_size(handle->sock.conn);
  134. }
  135. break;
  136. case pal_type_gipc:
  137. case pal_type_process:
  138. break;
  139. default:
  140. return -PAL_ERROR_INVAL;
  141. }
  142. void * buffer = malloc(hdlsz + dsz1 + dsz2);
  143. if (!buffer)
  144. return -PAL_ERROR_NOMEM;
  145. memcpy(buffer, handle, hdlsz);
  146. if (dsz1)
  147. memcpy(buffer + hdlsz, d1, dsz1);
  148. if (dsz2)
  149. memcpy(buffer + hdlsz + dsz1, d2, dsz2);
  150. *data = buffer;
  151. return hdlsz + dsz1 + dsz2;
  152. }
  153. #ifndef SEEK_SET
  154. # define SEEK_SET 0
  155. #endif
  156. int handle_deserialize (PAL_HANDLE * handle, const void * data, int size)
  157. {
  158. PAL_HANDLE hdl_data = (void *) data, hdl = NULL;
  159. int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
  160. data += hdlsz;
  161. size -= hdlsz;
  162. // recreate PAL_HANDLE based on type
  163. switch(PAL_GET_TYPE(hdl_data)) {
  164. case pal_type_file: {
  165. int l = strlen((const char *) data) + 1;
  166. hdl = malloc(hdlsz + l);
  167. if (!hdl)
  168. break;
  169. memcpy(hdl, hdl_data, hdlsz);
  170. memcpy((void *) hdl + hdlsz, data, l);
  171. hdl->file.realpath = (void *) hdl + hdlsz;
  172. break;
  173. }
  174. case pal_type_pipe:
  175. case pal_type_pipesrv:
  176. case pal_type_pipecli:
  177. case pal_type_pipeprv:
  178. hdl = malloc_copy(hdl_data, hdlsz);
  179. break;
  180. case pal_type_dev: {
  181. int l = hdl_data->dev.realpath ? strlen((const char *) data) + 1 : 0;
  182. hdl = malloc(hdlsz + l);
  183. if (!hdl)
  184. break;
  185. memcpy(hdl, hdl_data, hdlsz);
  186. if (l) {
  187. memcpy((void *) hdl + hdlsz, data, l);
  188. hdl->dev.realpath = (void *) hdl + hdlsz;
  189. }
  190. break;
  191. }
  192. case pal_type_dir: {
  193. int l = hdl_data->dir.realpath ? strlen((const char *) data) + 1 : 0;
  194. hdl = malloc(hdlsz + l);
  195. if (!hdl)
  196. break;
  197. memcpy(hdl, hdl_data, hdlsz);
  198. if (l) {
  199. memcpy((void *) hdl + hdlsz, data, l);
  200. hdl->dir.realpath = (void *) hdl + hdlsz;
  201. }
  202. break;
  203. }
  204. case pal_type_tcp:
  205. case pal_type_tcpsrv:
  206. case pal_type_udp:
  207. case pal_type_udpsrv: {
  208. int s1 = 0, s2 = 0;
  209. if (hdl_data->sock.bind)
  210. s1 = addr_size(data);
  211. if (hdl_data->sock.conn)
  212. s2 = addr_size(data + s1);
  213. hdl = malloc(hdlsz + s1 + s2);
  214. if (!hdl)
  215. break;
  216. memcpy(hdl, hdl_data, hdlsz);
  217. if (s1) {
  218. memcpy((void *) hdl + hdlsz, data, s1);
  219. hdl->sock.bind = (void *) hdl + hdlsz;
  220. }
  221. if (s2) {
  222. memcpy((void *) hdl + hdlsz + s1, data + s1, s2);
  223. hdl->sock.conn = (void *) hdl + hdlsz + s2;
  224. }
  225. break;
  226. }
  227. case pal_type_gipc:
  228. case pal_type_process:
  229. hdl = malloc_copy(hdl_data, hdlsz);
  230. break;
  231. default :
  232. return -PAL_ERROR_BADHANDLE;
  233. }
  234. if (!hdl)
  235. return ret;
  236. *handle = hdl;
  237. return 0;
  238. }
  239. // Header for DkSendHandle and DkRecvHandle
  240. struct hdl_header {
  241. unsigned short fds:(MAX_FDS);
  242. unsigned short data_size:(16-(MAX_FDS));
  243. };
  244. /* _DkSendHandle for internal use. Send a Pal Handle over the given
  245. process handle. Return 1 if success else return negative error code */
  246. int _DkSendHandle (PAL_HANDLE hdl, PAL_HANDLE cargo)
  247. {
  248. struct hdl_header hdl_hdr;
  249. void * hdl_data;
  250. int ret = handle_serialize(cargo, &hdl_data);
  251. if (ret < 0)
  252. return ret;
  253. hdl_hdr.fds = 0;
  254. hdl_hdr.data_size = ret;
  255. int fds[MAX_FDS];
  256. int nfds = 0;
  257. for (int i = 0 ; i < MAX_FDS ; i++)
  258. if (cargo->hdr.flags & (RFD(i)|WFD(1))) {
  259. hdl_hdr.fds |= 1U << i;
  260. fds[nfds++] = cargo->hdr.fds[i];
  261. }
  262. // ~ Initialize common parameter formessage passing
  263. // Channel between parent and child
  264. int ch = hdl->process.cargo;
  265. // Declare variables required for sending the message
  266. struct msghdr hdr; // message header
  267. struct cmsghdr * chdr; //control message header
  268. struct iovec iov[1]; // IO Vector
  269. iov[0].iov_base = &hdl_hdr;
  270. iov[0].iov_len = sizeof(struct hdl_header);
  271. hdr.msg_name = NULL;
  272. hdr.msg_namelen = 0;
  273. hdr.msg_iov = iov;
  274. hdr.msg_iovlen = 1;
  275. hdr.msg_control = NULL;
  276. hdr.msg_controllen = 0;
  277. hdr.msg_flags = 0;
  278. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, MSG_NOSIGNAL);
  279. // Unlock is error
  280. if (IS_ERR(ret)) {
  281. free(hdl_data);
  282. return -PAL_ERROR_DENIED;
  283. }
  284. /* Message Body Composition:
  285. IOVEC[0]: PAL_HANDLE
  286. IOVEC[1..n]: Additional handle member follow
  287. Control Message: file descriptors */
  288. // Control message buffer with added space for 2 fds (ie. max size
  289. // that it will have)
  290. char cbuf[CMSG_LEN(nfds * sizeof(int))];
  291. memset(cbuf, 0, sizeof(cbuf));
  292. // Initialize iovec[0] with struct PAL_HANDLE
  293. iov[0].iov_base = hdl_data;
  294. iov[0].iov_len = hdl_hdr.data_size;
  295. hdr.msg_iov = iov;
  296. hdr.msg_iovlen = 1;
  297. hdr.msg_control = cbuf; // Control Message Buffer
  298. hdr.msg_controllen = CMSG_LEN(sizeof(int) * nfds);
  299. // Fill control message infomation for the file descriptors
  300. // Check hdr.msg_controllen >= sizeof(struct cmsghdr) to point to
  301. // cbuf, which is redundant based on the above code as we have
  302. // statically allocated memory.
  303. // or (struct cmsghdr*) cbuf
  304. chdr = CMSG_FIRSTHDR(&hdr); // Pointer to msg_control
  305. chdr->cmsg_level = SOL_SOCKET; // Originating Protocol
  306. chdr->cmsg_type = SCM_RIGHTS; // Protocol Specific Type
  307. // Length of control message = sizeof(struct cmsghdr) + nfds
  308. chdr->cmsg_len = CMSG_LEN(sizeof(int) * nfds);
  309. // Copy the fds below control header
  310. memcpy(CMSG_DATA(chdr), fds, sizeof(int) * nfds);
  311. // Also, Update main header with control message length (duplicate)
  312. hdr.msg_controllen = chdr->cmsg_len;
  313. // Send message
  314. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, 0);
  315. free(hdl_data);
  316. return IS_ERR(ret) ? -PAL_ERROR_DENIED : 0;
  317. }
  318. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  319. given PAL_HANDLE else return negative value. */
  320. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo)
  321. {
  322. struct hdl_header hdl_hdr;
  323. // ~ Check connection PAL_HANDLE - is of process type for sending handle
  324. // else fail
  325. if (!IS_HANDLE_TYPE(hdl, process))
  326. return -PAL_ERROR_BADHANDLE;
  327. // ~ Initialize common parameter for message passing
  328. // Channel between parent and child
  329. int ch = hdl->process.cargo;
  330. struct msghdr hdr;
  331. struct iovec iov[1];
  332. iov[0].iov_base = &hdl_hdr;
  333. iov[0].iov_len = sizeof(struct hdl_header);
  334. hdr.msg_name = NULL;
  335. hdr.msg_namelen = 0;
  336. hdr.msg_iov = iov;
  337. hdr.msg_iovlen = 1;
  338. hdr.msg_control = NULL;
  339. hdr.msg_controllen = 0;
  340. hdr.msg_flags = 0;
  341. int ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  342. if (IS_ERR(ret) || ret < sizeof(struct hdl_header)) {
  343. if (!IS_ERR(ret))
  344. return -PAL_ERROR_TRYAGAIN;
  345. if (ERRNO(ret) != EINTR && ERRNO(ret) != ERESTART)
  346. return -ERRNO(ret);
  347. }
  348. // initialize variables to get body
  349. void * buffer = __alloca(hdl_hdr.data_size);
  350. int nfds = 0;
  351. for (int i = 0 ; i < MAX_FDS ; i++)
  352. if (hdl_hdr.fds & (1U << i))
  353. nfds++;
  354. // receive PAL_HANDLE contents in the body
  355. char cbuf[CMSG_LEN(nfds * sizeof(int))];
  356. memset(cbuf, 0, sizeof(cbuf));
  357. // initialize iovec[0] with struct PAL_HANDLE
  358. iov[0].iov_base = buffer;
  359. iov[0].iov_len = hdl_hdr.data_size;
  360. // set message header values
  361. hdr.msg_iov = iov;
  362. hdr.msg_iovlen = 1;
  363. hdr.msg_control = cbuf;
  364. hdr.msg_controllen = sizeof(cbuf);
  365. ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  366. // if error was returned
  367. if (IS_ERR(ret) && ERRNO(ret) != EINTR && ERRNO(ret) != ERESTART)
  368. return -ERRNO(ret);
  369. struct cmsghdr * chdr = CMSG_FIRSTHDR(&hdr);
  370. if (!chdr || chdr->cmsg_type != SCM_RIGHTS)
  371. return -PAL_ERROR_INVAL;
  372. PAL_HANDLE handle = NULL;
  373. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  374. if (ret < 0)
  375. return ret;
  376. int total_fds = (hdr.msg_controllen - sizeof(struct cmsghdr)) /
  377. sizeof(int);
  378. int n = 0;
  379. for (int i = 0 ; i < MAX_FDS ; i++)
  380. if (hdl_hdr.fds & (1U << i)) {
  381. if (n < total_fds) {
  382. handle->hdr.fds[i] = ((int *) CMSG_DATA(chdr))[n++];
  383. } else {
  384. handle->hdr.flags &= ~(RFD(i)|WFD(i));
  385. }
  386. }
  387. if (IS_HANDLE_TYPE(handle, file)) {
  388. ret = INLINE_SYSCALL(lseek, 3, handle->file.fd, 0, SEEK_SET);
  389. if (!IS_ERR(ret))
  390. handle->file.offset = ret;
  391. }
  392. *cargo = handle;
  393. return 0;
  394. }