db_streams.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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->__in.flags & (RFD(i)|WFD(i))) {
  63. long flags = enable ? FD_CLOEXEC : 0;
  64. int ret = INLINE_SYSCALL(fcntl, 3,
  65. handle->__in.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, int 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. #define addr_size(addr) \
  83. ({ int _size = 0; \
  84. switch (((struct sockaddr *) addr)->sa_family) { \
  85. case AF_INET: \
  86. _size = sizeof(struct sockaddr_in); break; \
  87. case AF_INET6: \
  88. _size = sizeof(struct sockaddr_in6); break; \
  89. default: break; \
  90. } _size; \
  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 = remalloc(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 = remalloc(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.data_size = ret;
  254. int fds[MAX_FDS];
  255. int nfds = 0;
  256. for (int i = 0 ; i < MAX_FDS ; i++)
  257. if (cargo->__in.flags & (RFD(i)|WFD(1))) {
  258. hdl_hdr.fds |= 1U << i;
  259. fds[nfds++] = cargo->__in.fds[i];
  260. }
  261. // ~ Initialize common parameter formessage passing
  262. // Channel between parent and child
  263. int ch = hdl->process.cargo;
  264. // Declare variables required for sending the message
  265. struct msghdr hdr; // message header
  266. struct cmsghdr * chdr; //control message header
  267. struct iovec iov[1]; // IO Vector
  268. iov[0].iov_base = &hdl_hdr;
  269. iov[0].iov_len = sizeof(struct hdl_header);
  270. hdr.msg_name = NULL;
  271. hdr.msg_namelen = 0;
  272. hdr.msg_iov = iov;
  273. hdr.msg_iovlen = 1;
  274. hdr.msg_control = NULL;
  275. hdr.msg_controllen = 0;
  276. hdr.msg_flags = 0;
  277. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, MSG_NOSIGNAL);
  278. // Unlock is error
  279. if (IS_ERR(ret)) {
  280. free(hdl_data);
  281. return -PAL_ERROR_DENIED;
  282. }
  283. /* Message Body Composition:
  284. IOVEC[0]: PAL_HANDLE
  285. IOVEC[1..n]: Additional handle member follow
  286. Control Message: file descriptors */
  287. // Control message buffer with added space for 2 fds (ie. max size
  288. // that it will have)
  289. char cbuf[CMSG_LEN(nfds * sizeof(int))];
  290. memset(cbuf, 0, sizeof(cbuf));
  291. // Initialize iovec[0] with struct PAL_HANDLE
  292. iov[0].iov_base = hdl_data;
  293. iov[0].iov_len = hdl_hdr.data_size;
  294. hdr.msg_iov = iov;
  295. hdr.msg_iovlen = 1;
  296. hdr.msg_control = cbuf; // Control Message Buffer
  297. hdr.msg_controllen = CMSG_LEN(sizeof(int) * nfds);
  298. // Fill control message infomation for the file descriptors
  299. // Check hdr.msg_controllen >= sizeof(struct cmsghdr) to point to
  300. // cbuf, which is redundant based on the above code as we have
  301. // statically allocated memory.
  302. // or (struct cmsghdr*) cbuf
  303. chdr = CMSG_FIRSTHDR(&hdr); // Pointer to msg_control
  304. chdr->cmsg_level = SOL_SOCKET; // Originating Protocol
  305. chdr->cmsg_type = SCM_RIGHTS; // Protocol Specific Type
  306. // Length of control message = sizeof(struct cmsghdr) + nfds
  307. chdr->cmsg_len = CMSG_LEN(sizeof(int) * nfds);
  308. // Copy the fds below control header
  309. memcpy(CMSG_DATA(chdr), fds, sizeof(int) * nfds);
  310. // Also, Update main header with control message length (duplicate)
  311. hdr.msg_controllen = chdr->cmsg_len;
  312. // Send message
  313. ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, 0);
  314. free(hdl_data);
  315. return IS_ERR(ret) ? -PAL_ERROR_DENIED : 0;
  316. }
  317. /* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
  318. given PAL_HANDLE else return negative value. */
  319. int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo)
  320. {
  321. struct hdl_header hdl_hdr;
  322. // ~ Check connection PAL_HANDLE - is of process type for sending handle
  323. // else fail
  324. if (!IS_HANDLE_TYPE(hdl, process))
  325. return -PAL_ERROR_BADHANDLE;
  326. // ~ Initialize common parameter for message passing
  327. // Channel between parent and child
  328. int ch = hdl->process.cargo;
  329. struct msghdr hdr;
  330. struct iovec iov[1];
  331. iov[0].iov_base = &hdl_hdr;
  332. iov[0].iov_len = sizeof(struct hdl_header);
  333. hdr.msg_name = NULL;
  334. hdr.msg_namelen = 0;
  335. hdr.msg_iov = iov;
  336. hdr.msg_iovlen = 1;
  337. hdr.msg_control = NULL;
  338. hdr.msg_controllen = 0;
  339. hdr.msg_flags = 0;
  340. int ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  341. if (IS_ERR(ret) || ret < sizeof(struct hdl_header)) {
  342. if (!IS_ERR(ret))
  343. return -PAL_ERROR_TRYAGAIN;
  344. if (ERRNO(ret) != EINTR && ERRNO(ret) != ERESTART)
  345. return -ERRNO(ret);
  346. }
  347. // initialize variables to get body
  348. void * buffer = __alloca(hdl_hdr.data_size);
  349. int nfds = 0;
  350. for (int i = 0 ; i < MAX_FDS ; i++)
  351. if (hdl_hdr.fds & (1U << i))
  352. nfds++;
  353. // receive PAL_HANDLE contents in the body
  354. char cbuf[CMSG_LEN(nfds * sizeof(int))];
  355. memset(cbuf, 0, sizeof(cbuf));
  356. // initialize iovec[0] with struct PAL_HANDLE
  357. iov[0].iov_base = buffer;
  358. iov[0].iov_len = hdl_hdr.data_size;
  359. // set message header values
  360. hdr.msg_iov = iov;
  361. hdr.msg_iovlen = 1;
  362. hdr.msg_control = cbuf;
  363. hdr.msg_controllen = sizeof(cbuf);
  364. ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
  365. // if error was returned
  366. if (IS_ERR(ret) && ERRNO(ret) != EINTR && ERRNO(ret) != ERESTART)
  367. return -ERRNO(ret);
  368. struct cmsghdr * chdr = CMSG_FIRSTHDR(&hdr);
  369. if (!chdr || chdr->cmsg_type != SCM_RIGHTS)
  370. return -PAL_ERROR_INVAL;
  371. PAL_HANDLE handle = NULL;
  372. ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
  373. if (ret < 0)
  374. return ret;
  375. int total_fds = (hdr.msg_controllen - sizeof(struct cmsghdr)) /
  376. sizeof(int);
  377. int n = 0;
  378. for (int i = 0 ; i < MAX_FDS ; i++)
  379. if (hdl_hdr.fds & (1U << i)) {
  380. if (n < total_fds) {
  381. handle->__in.fds[i] = ((int *) CMSG_DATA(chdr))[n++];
  382. } else {
  383. handle->__in.flags &= ~(RFD(i)|WFD(i));
  384. }
  385. }
  386. if (IS_HANDLE_TYPE(handle, file)) {
  387. ret = INLINE_SYSCALL(lseek, 3, handle->file.fd, 0, SEEK_SET);
  388. if (!IS_ERR(ret))
  389. handle->file.offset = ret;
  390. }
  391. *cargo = handle;
  392. return 0;
  393. }