db_streams.c 22 KB

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