db_streams.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 _DkStreamFile (PAL_HANDLE hdl, PAL_HANDLE * file)
  60. {
  61. if (IS_HANDLE_TYPE(hdl, file)) {
  62. _DkObjectReference(hdl);
  63. *file = hdl;
  64. return 0;
  65. }
  66. /* for other types of handles, try to save it to a local file */
  67. return -PAL_ERROR_NOTIMPLEMENTED;
  68. }
  69. int handle_set_cloexec (PAL_HANDLE handle, bool enable)
  70. {
  71. for (int i = 0 ; i < MAX_FDS ; i++)
  72. if (handle->__in.flags & (RFD(i)|WFD(i))) {
  73. long flags = enable ? FD_CLOEXEC : 0;
  74. int ret = INLINE_SYSCALL(fcntl, 3,
  75. handle->__in.fds[i], F_SETFD,
  76. flags);
  77. if (IS_ERR(ret) && ERRNO(ret) != EBADF)
  78. return -PAL_ERROR_DENIED;
  79. }
  80. return 0;
  81. }
  82. /* _DkStreamUnmap for internal use. Unmap stream at certain memory address.
  83. The memory is unmapped as a whole.*/
  84. int _DkStreamUnmap (void * addr, int size)
  85. {
  86. /* Just let the kernel tell us if the mapping isn't good. */
  87. int ret = INLINE_SYSCALL(munmap, 2, addr, size);
  88. if (IS_ERR(ret))
  89. return -PAL_ERROR_DENIED;
  90. return 0;
  91. }
  92. #define addr_size(addr) \
  93. ({ int _size = 0; \
  94. switch (((struct sockaddr *) addr)->sa_family) { \
  95. case AF_INET: \
  96. _size = sizeof(struct sockaddr_in); break; \
  97. case AF_INET6: \
  98. _size = sizeof(struct sockaddr_in6); break; \
  99. default: break; \
  100. } _size; \
  101. })
  102. int handle_serialize (PAL_HANDLE handle, void ** data)
  103. {
  104. int hdlsz = handle_size(handle);
  105. const void * d1, * d2;
  106. int dsz1 = 0, dsz2 = 0;
  107. // ~ Check cargo PAL_HANDLE - is allowed to be sent (White List checking
  108. // of cargo type)
  109. // ~ Also, Initialize common parameter formessage passing
  110. // Channel between parent and child
  111. switch(PAL_GET_TYPE(handle)) {
  112. case pal_type_file:
  113. d1 = handle->file.realpath;
  114. dsz1 = strlen(handle->file.realpath) + 1;
  115. break;
  116. case pal_type_pipe:
  117. case pal_type_pipesrv:
  118. case pal_type_pipecli:
  119. case pal_type_pipeprv:
  120. break;
  121. case pal_type_dev:
  122. if (handle->dev.realpath) {
  123. d1 = handle->dev.realpath;
  124. dsz1 = strlen(handle->dev.realpath) + 1;
  125. }
  126. break;
  127. case pal_type_dir:
  128. if (handle->dir.realpath) {
  129. d1 = handle->dir.realpath;
  130. dsz1 = strlen(handle->dir.realpath) + 1;
  131. }
  132. break;
  133. case pal_type_tcp:
  134. case pal_type_tcpsrv:
  135. case pal_type_udp:
  136. case pal_type_udpsrv:
  137. if (handle->sock.bind) {
  138. d1 = handle->sock.bind;
  139. dsz1 = addr_size(handle->sock.bind);
  140. }
  141. if (handle->sock.conn) {
  142. d2 = handle->sock.conn;
  143. dsz2 = addr_size(handle->sock.conn);
  144. }
  145. break;
  146. case pal_type_gipc:
  147. case pal_type_process:
  148. break;
  149. default:
  150. return -PAL_ERROR_INVAL;
  151. }
  152. void * buffer = malloc(hdlsz + dsz1 + dsz2);
  153. if (!buffer)
  154. return -PAL_ERROR_NOMEM;
  155. memcpy(buffer, handle, hdlsz);
  156. if (dsz1)
  157. memcpy(buffer + hdlsz, d1, dsz1);
  158. if (dsz2)
  159. memcpy(buffer + hdlsz + dsz1, d2, dsz2);
  160. *data = buffer;
  161. return hdlsz + dsz1 + dsz2;
  162. }
  163. #ifndef SEEK_SET
  164. # define SEEK_SET 0
  165. #endif
  166. int handle_deserialize (PAL_HANDLE * handle, const void * data, int size)
  167. {
  168. PAL_HANDLE hdl_data = (void *) data, hdl = NULL;
  169. int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
  170. data += hdlsz;
  171. size -= hdlsz;
  172. // recreate PAL_HANDLE based on type
  173. switch(PAL_GET_TYPE(hdl_data)) {
  174. case pal_type_file: {
  175. int l = strlen((const char *) data) + 1;
  176. hdl = malloc(hdlsz + l);
  177. if (!hdl)
  178. break;
  179. memcpy(hdl, hdl_data, hdlsz);
  180. memcpy((void *) hdl + hdlsz, data, l);
  181. hdl->file.realpath = (void *) hdl + hdlsz;
  182. break;
  183. }
  184. case pal_type_pipe:
  185. case pal_type_pipesrv:
  186. case pal_type_pipecli:
  187. case pal_type_pipeprv:
  188. hdl = remalloc(hdl_data, hdlsz);
  189. break;
  190. case pal_type_dev: {
  191. int l = hdl_data->dev.realpath ? strlen((const char *) data) + 1 : 0;
  192. hdl = malloc(hdlsz + l);
  193. if (!hdl)
  194. break;
  195. memcpy(hdl, hdl_data, hdlsz);
  196. if (l) {
  197. memcpy((void *) hdl + hdlsz, data, l);
  198. hdl->dev.realpath = (void *) hdl + hdlsz;
  199. }
  200. break;
  201. }
  202. case pal_type_dir: {
  203. int l = hdl_data->dir.realpath ? strlen((const char *) data) + 1 : 0;
  204. hdl = malloc(hdlsz + l);
  205. if (!hdl)
  206. break;
  207. memcpy(hdl, hdl_data, hdlsz);
  208. if (l) {
  209. memcpy((void *) hdl + hdlsz, data, l);
  210. hdl->dir.realpath = (void *) hdl + hdlsz;
  211. }
  212. break;
  213. }
  214. case pal_type_tcp:
  215. case pal_type_tcpsrv:
  216. case pal_type_udp:
  217. case pal_type_udpsrv: {
  218. int s1 = 0, s2 = 0;
  219. if (hdl_data->sock.bind)
  220. s1 = addr_size(data);
  221. if (hdl_data->sock.conn)
  222. s2 = addr_size(data + s1);
  223. hdl = malloc(hdlsz + s1 + s2);
  224. if (!hdl)
  225. break;
  226. memcpy(hdl, hdl_data, hdlsz);
  227. if (s1) {
  228. memcpy((void *) hdl + hdlsz, data, s1);
  229. hdl->sock.bind = (void *) hdl + hdlsz;
  230. }
  231. if (s2) {
  232. memcpy((void *) hdl + hdlsz + s1, data + s1, s2);
  233. hdl->sock.conn = (void *) hdl + hdlsz + s2;
  234. }
  235. break;
  236. }
  237. case pal_type_gipc:
  238. case pal_type_process:
  239. hdl = remalloc(hdl_data, hdlsz);
  240. break;
  241. default :
  242. return -PAL_ERROR_BADHANDLE;
  243. }
  244. if (!hdl)
  245. return ret;
  246. *handle = hdl;
  247. return 0;
  248. }
  249. // Header for DkSendHandle and DkRecvHandle
  250. struct hdl_header {
  251. unsigned short fds:(MAX_FDS);
  252. unsigned short data_size:(16-(MAX_FDS));
  253. };
  254. /* _DkSendHandle for internal use. Send a Pal Handle over the given
  255. process handle. Return 1 if success else return negative error code */
  256. int _DkSendHandle (PAL_HANDLE hdl, PAL_HANDLE cargo)
  257. {
  258. struct hdl_header hdl_hdr;
  259. void * hdl_data;
  260. int ret = handle_serialize(cargo, &hdl_data);
  261. if (ret < 0)
  262. return ret;
  263. hdl_hdr.data_size = ret;
  264. int fds[MAX_FDS];
  265. int nfds = 0;
  266. for (int i = 0 ; i < MAX_FDS ; i++)
  267. if (cargo->__in.flags & (RFD(i)|WFD(1))) {
  268. hdl_hdr.fds |= 1U << i;
  269. fds[nfds++] = cargo->__in.fds[i];
  270. }
  271. // ~ Initialize common parameter formessage passing
  272. // Channel between parent and child
  273. int ch = hdl->process.cargo;
  274. // Declare variables required for sending the message
  275. struct msghdr hdr; // message header
  276. struct cmsghdr * chdr; //control message header
  277. struct iovec iov[1]; // IO Vector
  278. iov[0].iov_base = &hdl_hdr;
  279. iov[0].iov_len = sizeof(struct hdl_header);
  280. hdr.msg_name = NULL;
  281. hdr.msg_namelen = 0;
  282. hdr.msg_iov = iov;
  283. hdr.msg_iovlen = 1;
  284. hdr.msg_control = NULL;
  285. hdr.msg_controllen = 0;
  286. hdr.msg_flags = 0;
  287. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, MSG_NOSIGNAL);
  288. // Unlock is error
  289. if (IS_ERR(ret)) {
  290. free(hdl_data);
  291. return -PAL_ERROR_DENIED;
  292. }
  293. /* Message Body Composition:
  294. IOVEC[0]: PAL_HANDLE
  295. IOVEC[1..n]: Additional handle member follow
  296. Control Message: file descriptors */
  297. // Control message buffer with added space for 2 fds (ie. max size
  298. // that it will have)
  299. char cbuf[CMSG_LEN(nfds * sizeof(int))];
  300. memset(cbuf, 0, sizeof(cbuf));
  301. // Initialize iovec[0] with struct PAL_HANDLE
  302. iov[0].iov_base = hdl_data;
  303. iov[0].iov_len = hdl_hdr.data_size;
  304. hdr.msg_iov = iov;
  305. hdr.msg_iovlen = 1;
  306. hdr.msg_control = cbuf; // Control Message Buffer
  307. hdr.msg_controllen = CMSG_LEN(sizeof(int) * nfds);
  308. // Fill control message infomation for the file descriptors
  309. // Check hdr.msg_controllen >= sizeof(struct cmsghdr) to point to
  310. // cbuf, which is redundant based on the above code as we have
  311. // statically allocated memory.
  312. // or (struct cmsghdr*) cbuf
  313. chdr = CMSG_FIRSTHDR(&hdr); // Pointer to msg_control
  314. chdr->cmsg_level = SOL_SOCKET; // Originating Protocol
  315. chdr->cmsg_type = SCM_RIGHTS; // Protocol Specific Type
  316. // Length of control message = sizeof(struct cmsghdr) + nfds
  317. chdr->cmsg_len = CMSG_LEN(sizeof(int) * nfds);
  318. // Copy the fds below control header
  319. memcpy(CMSG_DATA(chdr), fds, sizeof(int) * nfds);
  320. // Also, Update main header with control message length (duplicate)
  321. hdr.msg_controllen = chdr->cmsg_len;
  322. // Send message
  323. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, 0);
  324. free(hdl_data);
  325. return IS_ERR(ret) ? -PAL_ERROR_DENIED : 0;
  326. }
  327. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  328. given PAL_HANDLE else return negative value. */
  329. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo)
  330. {
  331. struct hdl_header hdl_hdr;
  332. // ~ Check connection PAL_HANDLE - is of process type for sending handle
  333. // else fail
  334. if (!IS_HANDLE_TYPE(hdl, process))
  335. return -PAL_ERROR_BADHANDLE;
  336. // ~ Initialize common parameter for message passing
  337. // Channel between parent and child
  338. int ch = hdl->process.cargo;
  339. struct msghdr hdr;
  340. struct iovec iov[1];
  341. iov[0].iov_base = &hdl_hdr;
  342. iov[0].iov_len = sizeof(struct hdl_header);
  343. hdr.msg_name = NULL;
  344. hdr.msg_namelen = 0;
  345. hdr.msg_iov = iov;
  346. hdr.msg_iovlen = 1;
  347. hdr.msg_control = NULL;
  348. hdr.msg_controllen = 0;
  349. hdr.msg_flags = 0;
  350. int ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  351. if (IS_ERR(ret) || ret < sizeof(struct hdl_header)) {
  352. if (!IS_ERR(ret))
  353. return -PAL_ERROR_TRYAGAIN;
  354. if (ERRNO(ret) != EINTR && ERRNO(ret) != ERESTART)
  355. return -ERRNO(ret);
  356. }
  357. // initialize variables to get body
  358. void * buffer = __alloca(hdl_hdr.data_size);
  359. int nfds = 0;
  360. for (int i = 0 ; i < MAX_FDS ; i++)
  361. if (hdl_hdr.fds & (1U << i))
  362. nfds++;
  363. // receive PAL_HANDLE contents in the body
  364. char cbuf[CMSG_LEN(nfds * sizeof(int))];
  365. memset(cbuf, 0, sizeof(cbuf));
  366. // initialize iovec[0] with struct PAL_HANDLE
  367. iov[0].iov_base = buffer;
  368. iov[0].iov_len = hdl_hdr.data_size;
  369. // set message header values
  370. hdr.msg_iov = iov;
  371. hdr.msg_iovlen = 1;
  372. hdr.msg_control = cbuf;
  373. hdr.msg_controllen = sizeof(cbuf);
  374. ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  375. // if error was returned
  376. if (IS_ERR(ret) && ERRNO(ret) != EINTR && ERRNO(ret) != ERESTART)
  377. return -ERRNO(ret);
  378. struct cmsghdr * chdr = CMSG_FIRSTHDR(&hdr);
  379. if (!chdr || chdr->cmsg_type != SCM_RIGHTS)
  380. return -PAL_ERROR_INVAL;
  381. PAL_HANDLE handle = NULL;
  382. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  383. if (ret < 0)
  384. return ret;
  385. int total_fds = (hdr.msg_controllen - sizeof(struct cmsghdr)) /
  386. sizeof(int);
  387. int n = 0;
  388. for (int i = 0 ; i < MAX_FDS ; i++)
  389. if (hdl_hdr.fds & (1U << i)) {
  390. if (n < total_fds) {
  391. handle->__in.fds[i] = ((int *) CMSG_DATA(chdr))[n++];
  392. } else {
  393. handle->__in.flags &= ~(RFD(i)|WFD(i));
  394. }
  395. }
  396. if (IS_HANDLE_TYPE(handle, file)) {
  397. ret = INLINE_SYSCALL(lseek, 3, handle->file.fd, 0, SEEK_SET);
  398. if (!IS_ERR(ret))
  399. handle->file.offset = ret;
  400. }
  401. *cargo = handle;
  402. return 0;
  403. }