db_streams.c 14 KB

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