db_streams.c 22 KB

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