db_streams.c 21 KB

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