db_streams.c 21 KB

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