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