db_streams.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 "api.h"
  20. #include "pal.h"
  21. #include "pal_debug.h"
  22. #include "pal_defs.h"
  23. #include "pal_error.h"
  24. #include "pal_internal.h"
  25. /* Stream handler table: this table corresponds to all the
  26. handle type supported by PAL. Threads, Semaphores and Events
  27. are not streams, so they need no handler */
  28. extern struct handle_ops file_ops;
  29. extern struct handle_ops pipe_ops;
  30. extern struct handle_ops pipeprv_ops;
  31. extern struct handle_ops dev_ops;
  32. extern struct handle_ops dir_ops;
  33. extern struct handle_ops tcp_ops;
  34. extern struct handle_ops udp_ops;
  35. extern struct handle_ops udpsrv_ops;
  36. extern struct handle_ops thread_ops;
  37. extern struct handle_ops proc_ops;
  38. extern struct handle_ops mutex_ops;
  39. extern struct handle_ops event_ops;
  40. extern struct handle_ops mcast_ops;
  41. extern struct handle_ops eventfd_ops;
  42. const struct handle_ops* pal_handle_ops[PAL_HANDLE_TYPE_BOUND] = {
  43. [pal_type_file] = &file_ops,
  44. [pal_type_pipe] = &pipe_ops,
  45. [pal_type_pipesrv] = &pipe_ops,
  46. [pal_type_pipecli] = &pipe_ops,
  47. [pal_type_pipeprv] = &pipeprv_ops,
  48. [pal_type_dev] = &dev_ops,
  49. [pal_type_dir] = &dir_ops,
  50. [pal_type_tcp] = &tcp_ops,
  51. [pal_type_tcpsrv] = &tcp_ops,
  52. [pal_type_udp] = &udp_ops,
  53. [pal_type_udpsrv] = &udpsrv_ops,
  54. [pal_type_process] = &proc_ops,
  55. [pal_type_mcast] = &mcast_ops,
  56. [pal_type_thread] = &thread_ops,
  57. [pal_type_mutex] = &mutex_ops,
  58. [pal_type_event] = &event_ops,
  59. [pal_type_eventfd] = &eventfd_ops,
  60. };
  61. /* parse_stream_uri scan the uri, seperate prefix and search for
  62. stream handler which will open or access the stream */
  63. static int parse_stream_uri(const char** uri, char** prefix, struct handle_ops** ops) {
  64. const char* p;
  65. const char* u = *uri;
  66. for (p = u; (*p) && (*p) != ':'; p++)
  67. ;
  68. if ((*p) != ':')
  69. return -PAL_ERROR_INVAL;
  70. ++p;
  71. struct handle_ops* hops = NULL;
  72. switch (p - u) {
  73. case 4: ;
  74. static_assert(static_strlen(URI_PREFIX_DIR) == 4, "URI_PREFIX_DIR has unexpected length");
  75. static_assert(static_strlen(URI_PREFIX_TCP) == 4, "URI_PREFIX_TCP has unexpected length");
  76. static_assert(static_strlen(URI_PREFIX_UDP) == 4, "URI_PREFIX_UDP has unexpected length");
  77. static_assert(static_strlen(URI_PREFIX_DEV) == 4, "URI_PREFIX_DEV has unexpected length");
  78. if (strstartswith_static(u, URI_PREFIX_DIR))
  79. hops = &dir_ops;
  80. else if (strstartswith_static(u, URI_PREFIX_TCP))
  81. hops = &tcp_ops;
  82. else if (strstartswith_static(u, URI_PREFIX_UDP))
  83. hops = &udp_ops;
  84. else if (strstartswith_static(u, URI_PREFIX_DEV))
  85. hops = &dev_ops;
  86. break;
  87. case 5: ;
  88. static_assert(static_strlen(URI_PREFIX_FILE) == 5, "URI_PREFIX_FILE has unexpected length");
  89. static_assert(static_strlen(URI_PREFIX_PIPE) == 5, "URI_PREFIX_PIPE has unexpected length");
  90. if (strstartswith_static(u, URI_PREFIX_FILE))
  91. hops = &file_ops;
  92. else if (strstartswith_static(u, URI_PREFIX_PIPE))
  93. hops = &pipe_ops;
  94. break;
  95. case 8: ;
  96. static_assert(static_strlen(URI_PREFIX_TCP_SRV) == 8, "URI_PREFIX_TCP_SRV has unexpected length");
  97. static_assert(static_strlen(URI_PREFIX_UDP_SRV) == 8, "URI_PREFIX_UDP_SRV has unexpected length");
  98. static_assert(static_strlen(URI_PREFIX_EVENTFD) == 8, "URI_PREFIX_EVENTFD has unexpected length");
  99. if (strstartswith_static(u, URI_PREFIX_TCP_SRV))
  100. hops = &tcp_ops;
  101. else if (strstartswith_static(u, URI_PREFIX_UDP_SRV))
  102. hops = &udp_ops;
  103. else if (strstartswith_static(u, URI_PREFIX_EVENTFD))
  104. hops = &eventfd_ops;
  105. break;
  106. case 9: ;
  107. static_assert(static_strlen(URI_PREFIX_PIPE_SRV) == 9, "URI_PREFIX_PIPE_SRV has unexpected length");
  108. if (strstartswith_static(u, URI_PREFIX_PIPE_SRV))
  109. hops = &pipe_ops;
  110. break;
  111. default:
  112. break;
  113. }
  114. if (!hops)
  115. return -PAL_ERROR_NOTSUPPORT;
  116. *uri = p;
  117. if (prefix) {
  118. *prefix = malloc_copy(u, p - u);
  119. if (!*prefix)
  120. return -PAL_ERROR_NOMEM;
  121. /* We don't want ':' in prefix, replacing that with nullbyte which also ends the string. */
  122. (*prefix)[p - 1 - u] = '\0';
  123. }
  124. if (ops)
  125. *ops = hops;
  126. return 0;
  127. }
  128. /* _DkStreamOpen for internal use. Open stream based on uri.
  129. access/share/create/options are the same flags defined for
  130. DkStreamOpen. */
  131. int _DkStreamOpen(PAL_HANDLE* handle, const char* uri, int access, int share, int create,
  132. int options) {
  133. struct handle_ops* ops = NULL;
  134. char* type = NULL;
  135. log_stream(uri);
  136. int ret = parse_stream_uri(&uri, &type, &ops);
  137. if (ret < 0)
  138. return ret;
  139. assert(ops && ops->open);
  140. ret = ops->open(handle, type, uri, access, share, create, options);
  141. free(type);
  142. return ret;
  143. }
  144. /* PAL call DkStreamOpen: Open stream based on uri, as given access/share/
  145. create/options flags. DkStreamOpen return a PAL_HANDLE to access the
  146. stream, or return NULL. Error code is notified. */
  147. PAL_HANDLE
  148. DkStreamOpen(PAL_STR uri, PAL_FLG access, PAL_FLG share, PAL_FLG create, PAL_FLG options) {
  149. ENTER_PAL_CALL(DkStreamOpen);
  150. PAL_HANDLE handle = NULL;
  151. int ret = _DkStreamOpen(&handle, uri, access, share, create, options);
  152. if (ret < 0) {
  153. _DkRaiseFailure(-ret);
  154. LEAVE_PAL_CALL_RETURN(NULL);
  155. }
  156. assert(handle);
  157. assert(!UNKNOWN_HANDLE(handle));
  158. LEAVE_PAL_CALL_RETURN(handle);
  159. }
  160. int _DkStreamWaitForClient(PAL_HANDLE handle, PAL_HANDLE* client) {
  161. if (UNKNOWN_HANDLE(handle))
  162. return -PAL_ERROR_BADHANDLE;
  163. const struct handle_ops* ops = HANDLE_OPS(handle);
  164. if (!ops)
  165. return -PAL_ERROR_BADHANDLE;
  166. if (!ops->waitforclient)
  167. return -PAL_ERROR_NOTSERVER;
  168. return ops->waitforclient(handle, client);
  169. }
  170. PAL_HANDLE
  171. DkStreamWaitForClient(PAL_HANDLE handle) {
  172. ENTER_PAL_CALL(DkStreamWaitForClient);
  173. PAL_HANDLE client;
  174. int ret = _DkStreamWaitForClient(handle, &client);
  175. if (ret < 0) {
  176. _DkRaiseFailure(-ret);
  177. client = NULL;
  178. }
  179. LEAVE_PAL_CALL_RETURN(client);
  180. }
  181. /* _DkStreamDelete for internal use. This function will explicit delete
  182. the stream. For example, file will be deleted, socket witll be
  183. disconnected, etc */
  184. int _DkStreamDelete(PAL_HANDLE handle, int access) {
  185. const struct handle_ops* ops = HANDLE_OPS(handle);
  186. if (!ops)
  187. return -PAL_ERROR_BADHANDLE;
  188. if (!ops->delete)
  189. return -PAL_ERROR_NOTSUPPORT;
  190. return ops->delete(handle, access);
  191. }
  192. /* PAL call DkStreamDelete: Explicitly delete stream as given handle. No
  193. return value, error code is notified. */
  194. void DkStreamDelete(PAL_HANDLE handle, PAL_FLG access) {
  195. ENTER_PAL_CALL(DkStreamDelete);
  196. if (!handle) {
  197. _DkRaiseFailure(PAL_ERROR_INVAL);
  198. LEAVE_PAL_CALL();
  199. }
  200. int ret = _DkStreamDelete(handle, access);
  201. if (ret < 0)
  202. _DkRaiseFailure(-ret);
  203. LEAVE_PAL_CALL();
  204. }
  205. /* _DkStreamRead for internal use. Read from stream as absolute offset.
  206. The actual behavior of stream read is defined by handler */
  207. int64_t _DkStreamRead(PAL_HANDLE handle, uint64_t offset, uint64_t count, void* buf, char* addr,
  208. int addrlen) {
  209. const struct handle_ops* ops = HANDLE_OPS(handle);
  210. if (!ops)
  211. return -PAL_ERROR_BADHANDLE;
  212. int64_t ret;
  213. if (addr) {
  214. if (!ops->readbyaddr)
  215. return -PAL_ERROR_NOTSUPPORT;
  216. ret = ops->readbyaddr(handle, offset, count, buf, addr, addrlen);
  217. } else {
  218. if (!ops->read)
  219. return -PAL_ERROR_NOTSUPPORT;
  220. ret = ops->read(handle, offset, count, buf);
  221. }
  222. return ret ? ret : -PAL_ERROR_ENDOFSTREAM;
  223. }
  224. /* PAL call DkStreamRead: Read from stream at absolute offset. Return number
  225. of bytes if succeeded,
  226. or PAL_STREAM_ERROR for failure. Error code is notified. */
  227. PAL_NUM
  228. DkStreamRead(PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count, PAL_PTR buffer, PAL_PTR source,
  229. PAL_NUM size) {
  230. ENTER_PAL_CALL(DkStreamRead);
  231. if (!handle || !buffer) {
  232. _DkRaiseFailure(-PAL_ERROR_INVAL);
  233. LEAVE_PAL_CALL_RETURN(0);
  234. }
  235. int64_t ret = _DkStreamRead(handle, offset, count, (void*)buffer, size ? (char*)source : NULL,
  236. source ? size : 0);
  237. if (ret < 0) {
  238. _DkRaiseFailure(-ret);
  239. ret = PAL_STREAM_ERROR;
  240. }
  241. LEAVE_PAL_CALL_RETURN(ret);
  242. }
  243. /* _DkStreamWrite for internal use, write to stream at absolute offset.
  244. The actual behavior of stream write is defined by handler */
  245. int64_t _DkStreamWrite(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buf,
  246. const char* addr, int addrlen) {
  247. const struct handle_ops* ops = HANDLE_OPS(handle);
  248. if (!ops)
  249. return -PAL_ERROR_BADHANDLE;
  250. int64_t ret;
  251. if (addr) {
  252. if (!ops->writebyaddr)
  253. return -PAL_ERROR_NOTSUPPORT;
  254. ret = ops->writebyaddr(handle, offset, count, buf, addr, addrlen);
  255. } else {
  256. if (!ops->write)
  257. return -PAL_ERROR_NOTSUPPORT;
  258. ret = ops->write(handle, offset, count, buf);
  259. }
  260. return ret ? ret : -PAL_ERROR_ENDOFSTREAM;
  261. }
  262. /* PAL call DkStreamWrite: Write to stream at absolute offset. Return number
  263. of bytes if succeeded,
  264. or PAL_STREAM_ERROR for failure. Error code is notified. */
  265. PAL_NUM
  266. DkStreamWrite(PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count, PAL_PTR buffer, PAL_STR dest) {
  267. ENTER_PAL_CALL(DkStreamWrite);
  268. if (!handle || !buffer) {
  269. _DkRaiseFailure(PAL_ERROR_INVAL);
  270. LEAVE_PAL_CALL_RETURN(0);
  271. }
  272. int64_t ret =
  273. _DkStreamWrite(handle, offset, count, (void*)buffer, dest, dest ? strlen(dest) : 0);
  274. if (ret < 0) {
  275. _DkRaiseFailure(-ret);
  276. ret = PAL_STREAM_ERROR;
  277. }
  278. LEAVE_PAL_CALL_RETURN(ret);
  279. }
  280. /* _DkStreamAttributesQuery of internal use. The function query attribute
  281. of streams by their URI */
  282. int _DkStreamAttributesQuery(const char* uri, PAL_STREAM_ATTR* attr) {
  283. struct handle_ops* ops = NULL;
  284. char* type = NULL;
  285. int ret = parse_stream_uri(&uri, &type, &ops);
  286. if (ret < 0)
  287. return ret;
  288. if (!ops->attrquery)
  289. return -PAL_ERROR_NOTSUPPORT;
  290. ret = ops->attrquery(type, uri, attr);
  291. free(type);
  292. return ret;
  293. }
  294. /* PAL call DkStreamAttributeQuery: query attribute of a stream by its
  295. URI, attr is memory given by user space. Return the pointer of attr
  296. if succeeded, or NULL if failed. Error code is notified */
  297. PAL_BOL
  298. DkStreamAttributesQuery(PAL_STR uri, PAL_STREAM_ATTR* attr) {
  299. ENTER_PAL_CALL(DkStreamAttributesQuery);
  300. if (!uri || !attr) {
  301. _DkRaiseFailure(PAL_ERROR_INVAL);
  302. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  303. }
  304. log_stream(uri);
  305. PAL_STREAM_ATTR attr_buf;
  306. int ret = _DkStreamAttributesQuery(uri, &attr_buf);
  307. if (ret < 0) {
  308. _DkRaiseFailure(-ret);
  309. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  310. }
  311. memcpy(attr, &attr_buf, sizeof(PAL_STREAM_ATTR));
  312. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  313. }
  314. /* _DkStreamAttributesQueryByHandle for internal use. Query attribute
  315. of streams by their handle */
  316. int _DkStreamAttributesQueryByHandle(PAL_HANDLE hdl, PAL_STREAM_ATTR* attr) {
  317. const struct handle_ops* ops = HANDLE_OPS(hdl);
  318. if (!ops)
  319. return -PAL_ERROR_BADHANDLE;
  320. if (!ops->attrquerybyhdl)
  321. return -PAL_ERROR_NOTSUPPORT;
  322. return ops->attrquerybyhdl(hdl, attr);
  323. }
  324. /* PAL call DkStreamAttributesQueryByHandle: Query attribute of a stream by
  325. its handle, attr is memory given by user space. Return the pointer of attr
  326. if succeeded, or NULL if failed. Error code is notified */
  327. PAL_BOL
  328. DkStreamAttributesQueryByHandle(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  329. ENTER_PAL_CALL(DkStreamAttributesQueryByHandle);
  330. if (!handle || !attr) {
  331. _DkRaiseFailure(PAL_ERROR_INVAL);
  332. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  333. }
  334. int ret = _DkStreamAttributesQueryByHandle(handle, attr);
  335. if (ret < 0) {
  336. _DkRaiseFailure(-ret);
  337. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  338. }
  339. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  340. }
  341. /* PAL call DkStreamAttributesSetByHandle: Set attribute of a stream by
  342. its handle, attr is memory given by user space. Return the pointer of attr
  343. if succeeded, or NULL if failed. Error code is notified */
  344. PAL_BOL
  345. DkStreamAttributesSetByHandle(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  346. ENTER_PAL_CALL(DkStreamAttributesSetByHandle);
  347. if (!handle || !attr) {
  348. _DkRaiseFailure(PAL_ERROR_INVAL);
  349. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  350. }
  351. const struct handle_ops* ops = HANDLE_OPS(handle);
  352. if (!ops) {
  353. _DkRaiseFailure(PAL_ERROR_BADHANDLE);
  354. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  355. }
  356. if (!ops->attrsetbyhdl) {
  357. _DkRaiseFailure(PAL_ERROR_NOTSUPPORT);
  358. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  359. }
  360. int ret = ops->attrsetbyhdl(handle, attr);
  361. if (ret < 0) {
  362. _DkRaiseFailure(-ret);
  363. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  364. }
  365. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  366. }
  367. int _DkStreamGetName(PAL_HANDLE handle, char* buffer, int size) {
  368. const struct handle_ops* ops = HANDLE_OPS(handle);
  369. if (!ops)
  370. return -PAL_ERROR_BADHANDLE;
  371. if (!ops->getname)
  372. return -PAL_ERROR_NOTSUPPORT;
  373. int ret = ops->getname(handle, buffer, size - 1);
  374. if (ret < 0)
  375. return ret;
  376. ((char*)buffer)[ret] = 0;
  377. return ret;
  378. }
  379. /* PAL call DkStreamAttributesSetByHandle: Set attribute of a stream by
  380. its handle, attr is memory given by user space. Return the pointer of attr
  381. if succeeded, or NULL if failed. Error code is notified */
  382. PAL_NUM DkStreamGetName(PAL_HANDLE handle, PAL_PTR buffer, PAL_NUM size) {
  383. ENTER_PAL_CALL(DkStreamGetName);
  384. if (!handle || !buffer || !size) {
  385. _DkRaiseFailure(PAL_ERROR_INVAL);
  386. LEAVE_PAL_CALL_RETURN(0);
  387. }
  388. int ret = _DkStreamGetName(handle, (void*)buffer, size);
  389. if (ret < 0) {
  390. _DkRaiseFailure(-ret);
  391. ret = 0;
  392. }
  393. LEAVE_PAL_CALL_RETURN(ret);
  394. }
  395. /* _DkStreamMap for internal use. Map specific handle to certain memory,
  396. with given protection, offset and size */
  397. int _DkStreamMap(PAL_HANDLE handle, void** paddr, int prot, uint64_t offset, uint64_t size) {
  398. void* addr = *paddr;
  399. int ret;
  400. const struct handle_ops* ops = HANDLE_OPS(handle);
  401. if (!ops)
  402. return -PAL_ERROR_BADHANDLE;
  403. if (!ops->map)
  404. return -PAL_ERROR_NOTSUPPORT;
  405. if ((ret = ops->map(handle, &addr, prot, offset, size)) < 0)
  406. return ret;
  407. *paddr = addr;
  408. return 0;
  409. }
  410. /* PAL call DkStreamMap: Map a stream of a given handle to certain memery
  411. space. prot/offset/size are the protection, offset and size of the memory
  412. mapping. Return the address if succeeded or NULL if failed. Error code
  413. is notified. */
  414. PAL_PTR
  415. DkStreamMap(PAL_HANDLE handle, PAL_PTR addr, PAL_FLG prot, PAL_NUM offset, PAL_NUM size) {
  416. ENTER_PAL_CALL(DkStreamMap);
  417. void* map_addr = (void*)addr;
  418. if (!handle) {
  419. _DkRaiseFailure(PAL_ERROR_INVAL);
  420. LEAVE_PAL_CALL_RETURN((PAL_PTR)NULL);
  421. }
  422. /* Check that all addresses and sizes are aligned */
  423. if ((addr && !IS_ALLOC_ALIGNED_PTR(addr)) || !size || !IS_ALLOC_ALIGNED(size) ||
  424. !IS_ALLOC_ALIGNED(offset)) {
  425. _DkRaiseFailure(PAL_ERROR_INVAL);
  426. LEAVE_PAL_CALL_RETURN((PAL_PTR)NULL);
  427. }
  428. if (map_addr && _DkCheckMemoryMappable(map_addr, size)) {
  429. _DkRaiseFailure(PAL_ERROR_DENIED);
  430. LEAVE_PAL_CALL_RETURN((PAL_PTR)NULL);
  431. }
  432. int ret = _DkStreamMap(handle, &map_addr, prot, offset, size);
  433. if (ret < 0) {
  434. _DkRaiseFailure(-ret);
  435. map_addr = NULL;
  436. }
  437. LEAVE_PAL_CALL_RETURN((PAL_PTR)map_addr);
  438. }
  439. /* PAL call DkStreamUnmap: Unmap memory mapped at an address. The memory has
  440. to be a stream map, and it got unmapped as a whole memory area. No
  441. return value. Error code is notified */
  442. void DkStreamUnmap(PAL_PTR addr, PAL_NUM size) {
  443. ENTER_PAL_CALL(DkStreamUnmap);
  444. if (!addr || !IS_ALLOC_ALIGNED_PTR(addr) || !size || !IS_ALLOC_ALIGNED(size)) {
  445. _DkRaiseFailure(PAL_ERROR_INVAL);
  446. LEAVE_PAL_CALL();
  447. }
  448. if (_DkCheckMemoryMappable((void*)addr, size)) {
  449. _DkRaiseFailure(PAL_ERROR_DENIED);
  450. LEAVE_PAL_CALL();
  451. }
  452. int ret = _DkStreamUnmap((void*)addr, size);
  453. if (ret < 0)
  454. _DkRaiseFailure(-ret);
  455. LEAVE_PAL_CALL();
  456. }
  457. /* _DkStreamSetLength for internal use. This function truncate the stream
  458. to certain length. This call might not be support for certain streams */
  459. int64_t _DkStreamSetLength(PAL_HANDLE handle, uint64_t length) {
  460. const struct handle_ops* ops = HANDLE_OPS(handle);
  461. if (!ops)
  462. return -PAL_ERROR_BADHANDLE;
  463. if (!ops->setlength)
  464. return -PAL_ERROR_NOTSUPPORT;
  465. return ops->setlength(handle, length);
  466. }
  467. /* PAL call DkStreamSetLength: Truncate the stream at certain length.
  468. Return the length if succeeded or 0 if failed. Error code is notified. */
  469. PAL_NUM
  470. DkStreamSetLength(PAL_HANDLE handle, PAL_NUM length) {
  471. PAL_NUM rv = 0;
  472. ENTER_PAL_CALL(DkStreamSetLength);
  473. if (!handle) {
  474. _DkRaiseFailure(PAL_ERROR_INVAL);
  475. LEAVE_PAL_CALL_RETURN(0);
  476. }
  477. int64_t ret = _DkStreamSetLength(handle, length);
  478. // Convert failure to a positive value
  479. if (ret < 0) {
  480. _DkRaiseFailure(-ret);
  481. rv = -ret;
  482. } else {
  483. // At this point, ret should equal length
  484. assert((uint64_t)ret == length);
  485. }
  486. LEAVE_PAL_CALL_RETURN(rv);
  487. }
  488. /* _DkStreamFlush for internal use. This function sync up the handle with
  489. devices. Some streams may not support this operations. */
  490. int _DkStreamFlush(PAL_HANDLE handle) {
  491. if (UNKNOWN_HANDLE(handle))
  492. return -PAL_ERROR_BADHANDLE;
  493. const struct handle_ops* ops = HANDLE_OPS(handle);
  494. if (!ops)
  495. return -PAL_ERROR_BADHANDLE;
  496. if (!ops->flush)
  497. return -PAL_ERROR_NOTSUPPORT;
  498. return ops->flush(handle);
  499. }
  500. /* PAL call DkStreamFlush: Sync up a stream of a given handle. No return
  501. value. Error code is notified. */
  502. PAL_BOL DkStreamFlush(PAL_HANDLE handle) {
  503. ENTER_PAL_CALL(DkStreamFlush);
  504. if (!handle) {
  505. _DkRaiseFailure(PAL_ERROR_INVAL);
  506. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  507. }
  508. int ret = _DkStreamFlush(handle);
  509. if (ret < 0) {
  510. _DkRaiseFailure(-ret);
  511. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  512. }
  513. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  514. }
  515. /* PAL call DkSendHandle: Write to a process handle.
  516. Return 1 on success and 0 on failure */
  517. PAL_BOL DkSendHandle(PAL_HANDLE handle, PAL_HANDLE cargo) {
  518. ENTER_PAL_CALL(DkSendHandle);
  519. // Return error if any of the handle is NULL
  520. if (!handle || !cargo) {
  521. _DkRaiseFailure(PAL_ERROR_INVAL);
  522. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  523. }
  524. // Call the internal function after validating input args
  525. int ret = _DkSendHandle(handle, cargo);
  526. if (ret < 0) {
  527. _DkRaiseFailure(-ret);
  528. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  529. }
  530. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  531. }
  532. /* PAL call DkRecvHandle: Read a handle to a pipe/process handle.
  533. Return the received PAL_HANDLE by reference and 0 on success and
  534. negative number on failure */
  535. /* 1. Should i take the received PAL_HANDLE as an input argument and
  536. pass by reference or return it rather?
  537. Ans - We are not aware of the size of the variable members to return
  538. 2. Would the recieved PAL_HANDLE start functioning automatically in
  539. the new process environment? Should we initialize/modify some
  540. attibutes of the handle?
  541. Ans - Yes, Initialize and make it compatibile in the target process
  542. 3. Should malloc_copy be done or the process shares the same references?
  543. Ans - Variables members have to allocated data again.
  544. */
  545. PAL_HANDLE DkReceiveHandle(PAL_HANDLE handle) {
  546. ENTER_PAL_CALL(DkReceiveHandle);
  547. // return error if any of the handle is NULL
  548. if (!handle) {
  549. _DkRaiseFailure(PAL_ERROR_INVAL);
  550. LEAVE_PAL_CALL_RETURN(NULL);
  551. }
  552. // create a reference for the received PAL_HANDLE
  553. PAL_HANDLE cargo = NULL;
  554. // call the internal function after validating input args
  555. int ret = _DkReceiveHandle(handle, &cargo);
  556. // notify failure would have been called from other functions
  557. if (ret < 0) {
  558. _DkRaiseFailure(-ret);
  559. LEAVE_PAL_CALL_RETURN(NULL);
  560. }
  561. assert(cargo);
  562. LEAVE_PAL_CALL_RETURN(cargo);
  563. }
  564. PAL_BOL DkStreamChangeName(PAL_HANDLE hdl, PAL_STR uri) {
  565. ENTER_PAL_CALL(DkStreamChangeName);
  566. struct handle_ops* ops = NULL;
  567. char* type = NULL;
  568. int ret;
  569. if (uri) {
  570. ret = parse_stream_uri(&uri, &type, &ops);
  571. if (ret < 0) {
  572. _DkRaiseFailure(-ret);
  573. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  574. }
  575. }
  576. const struct handle_ops* hops = HANDLE_OPS(hdl);
  577. if (!hops || !hops->rename || (ops && hops != ops)) {
  578. free(type);
  579. _DkRaiseFailure(PAL_ERROR_NOTSUPPORT);
  580. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  581. }
  582. ret = hops->rename(hdl, type, uri);
  583. free(type);
  584. if (ret < 0) {
  585. _DkRaiseFailure(-ret);
  586. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  587. }
  588. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  589. }
  590. /* _DkStreamRealpath is used to obtain the real path of a stream. Some
  591. streams may not have a real path. */
  592. const char* _DkStreamRealpath(PAL_HANDLE hdl) {
  593. const struct handle_ops* ops = HANDLE_OPS(hdl);
  594. if (!ops || !ops->getrealpath)
  595. return NULL;
  596. return ops->getrealpath(hdl);
  597. }