db_streams.c 14 KB

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