db_streams.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_stream.c
  17. *
  18. * This file contains APIs to open, read, write and get attribute of
  19. * streams.
  20. */
  21. #include "pal_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_debug.h"
  25. #include "pal_error.h"
  26. #include "api.h"
  27. /* Stream handler table: this table corresponds to all the
  28. handle type supported by PAL. Threads, Semaphores and Events
  29. are not streams, so they need no handler */
  30. extern struct handle_ops file_ops;
  31. extern struct handle_ops pipe_ops;
  32. extern struct handle_ops pipeprv_ops;
  33. extern struct handle_ops dev_ops;
  34. extern struct handle_ops dir_ops;
  35. extern struct handle_ops tcp_ops;
  36. extern struct handle_ops udp_ops;
  37. extern struct handle_ops udpsrv_ops;
  38. extern struct hadnle_ops udppacket_ops;
  39. extern struct handle_ops thread_ops;
  40. extern struct handle_ops proc_ops;
  41. extern struct handle_ops mutex_ops;
  42. extern struct handle_ops event_ops;
  43. extern struct handle_ops gipc_ops;
  44. extern struct handle_ops mcast_ops;
  45. const struct handle_ops * pal_handle_ops [PAL_HANDLE_TYPE_BOUND] = {
  46. [pal_type_file] = &file_ops,
  47. [pal_type_pipe] = &pipe_ops,
  48. [pal_type_pipesrv] = &pipe_ops,
  49. [pal_type_pipecli] = &pipe_ops,
  50. [pal_type_pipeprv] = &pipeprv_ops,
  51. [pal_type_dev] = &dev_ops,
  52. [pal_type_dir] = &dir_ops,
  53. [pal_type_tcp] = &tcp_ops,
  54. [pal_type_tcpsrv] = &tcp_ops,
  55. [pal_type_udp] = &udp_ops,
  56. [pal_type_udpsrv] = &udpsrv_ops,
  57. [pal_type_process] = &proc_ops,
  58. [pal_type_mcast] = &mcast_ops,
  59. [pal_type_thread] = &thread_ops,
  60. [pal_type_mutex] = &mutex_ops,
  61. [pal_type_event] = &event_ops,
  62. [pal_type_gipc] = &gipc_ops,
  63. };
  64. /* parse_stream_uri scan the uri, seperate prefix and search for
  65. stream handler which will open or access the stream */
  66. int parse_stream_uri (const char ** uri, const char ** prefix,
  67. struct handle_ops ** ops)
  68. {
  69. const char * p, * u = (*uri);
  70. for (p = u ; (*p) && (*p) != ':' ; p++);
  71. if ((*p) != ':')
  72. return -PAL_ERROR_INVAL;
  73. struct handle_ops * hops = NULL;
  74. switch (p - u) {
  75. case 3:
  76. if (strpartcmp_static(u, "dir"))
  77. hops = &dir_ops;
  78. else if (strpartcmp_static(u, "tcp"))
  79. hops = &tcp_ops;
  80. else if (strpartcmp_static(u, "udp"))
  81. hops = &udp_ops;
  82. else if (strpartcmp_static(u, "dev"))
  83. hops = &dev_ops;
  84. break;
  85. case 4:
  86. if (strpartcmp_static(u, "file"))
  87. hops = &file_ops;
  88. else if (strpartcmp_static(u, "pipe"))
  89. hops = &pipe_ops;
  90. else if (strpartcmp_static(u, "gipc"))
  91. hops = &gipc_ops;
  92. break;
  93. case 7:
  94. if (strpartcmp_static(u, "tcp.srv"))
  95. hops = &tcp_ops;
  96. else if (strpartcmp_static(u, "udp.srv"))
  97. hops = &udp_ops;
  98. break;
  99. case 8:
  100. if (strpartcmp_static(u, "pipe.srv"))
  101. hops = &pipe_ops;
  102. break;
  103. default:
  104. break;
  105. }
  106. if (!hops)
  107. return -PAL_ERROR_NOTSUPPORT;
  108. *uri = p + 1;
  109. if (prefix)
  110. *prefix = u;
  111. if (ops)
  112. *ops = hops;
  113. return 0;
  114. }
  115. /* _DkStreamOpen for internal use. Open stream based on uri.
  116. access/share/create/options are the same flags defined for
  117. DkStreamOpen. */
  118. int _DkStreamOpen (PAL_HANDLE * handle, const char * uri,
  119. int access, int share, int create, int options)
  120. {
  121. struct handle_ops * ops = NULL;
  122. const 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. return ops->open(handle, type, uri, access, share, create, options);
  129. }
  130. /* PAL call DkStreamOpen: Open stream based on uri, as given access/share/
  131. create/options flags. DkStreamOpen return a PAL_HANDLE to access the
  132. stream, or return NULL. Error code is notified. */
  133. PAL_HANDLE
  134. DkStreamOpen (PAL_STR uri, PAL_FLG access, PAL_FLG share, PAL_FLG create,
  135. PAL_FLG options)
  136. {
  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(PAL_GET_TYPE(handle));
  146. TRACE_HEAP(handle);
  147. LEAVE_PAL_CALL_RETURN(handle);
  148. }
  149. int _DkStreamWaitForClient(PAL_HANDLE handle, PAL_HANDLE * client)
  150. {
  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. {
  163. ENTER_PAL_CALL(DkStreamWaitForClient);
  164. PAL_HANDLE client;
  165. int ret = _DkStreamWaitForClient(handle, &client);
  166. if (ret < 0) {
  167. _DkRaiseFailure(-ret);
  168. client = NULL;
  169. }
  170. TRACE_HEAP(client);
  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. {
  178. if (UNKNOWN_HANDLE(handle))
  179. return -PAL_ERROR_BADHANDLE;
  180. const struct handle_ops * ops = HANDLE_OPS(handle);
  181. /* No ops or ops->delete being defined, inferring the handle can never
  182. be deleted */
  183. if (!ops || !ops->delete)
  184. return -PAL_ERROR_NOTSUPPORT;
  185. return ops->delete(handle, access);
  186. }
  187. /* PAL call DkStreamDelete: Explicitly delete stream as given handle. No
  188. return value, error code is notified. */
  189. void DkStreamDelete (PAL_HANDLE handle, PAL_FLG access)
  190. {
  191. ENTER_PAL_CALL(DkStreamDelete);
  192. if (!handle) {
  193. _DkRaiseFailure(PAL_ERROR_INVAL);
  194. LEAVE_PAL_CALL();
  195. }
  196. int ret = _DkStreamDelete(handle, access);
  197. if (ret < 0)
  198. _DkRaiseFailure(-ret);
  199. LEAVE_PAL_CALL();
  200. }
  201. /* _DkStreamRead for internal use. Read from stream as absolute offset.
  202. The actual behavior of stream read is defined by handler */
  203. int64_t _DkStreamRead (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  204. void * buf, char * addr, int addrlen)
  205. {
  206. if (UNKNOWN_HANDLE(handle))
  207. return -PAL_ERROR_BADHANDLE;
  208. const struct handle_ops * ops = HANDLE_OPS(handle);
  209. /* if ops or ops->read is not defined, it infers that the stream can
  210. never be read */
  211. if (!ops)
  212. return -PAL_ERROR_NOTSUPPORT;
  213. if (!count)
  214. return -PAL_ERROR_ZEROSIZE;
  215. int64_t ret;
  216. if (addr) {
  217. if (!ops->readbyaddr)
  218. return -PAL_ERROR_NOTSUPPORT;
  219. ret = ops->readbyaddr(handle, offset, count, buf, addr, addrlen);
  220. } else {
  221. if (!ops->read)
  222. return -PAL_ERROR_NOTSUPPORT;
  223. ret = ops->read(handle, offset, count, buf);
  224. }
  225. return ret ? ret : -PAL_ERROR_ENDOFSTREAM;
  226. }
  227. /* PAL call DkStreamRead: Read from stream at absolute offset. Return number
  228. of bytes if succeeded, or 0 for failure. Error code is notified. */
  229. PAL_NUM
  230. DkStreamRead (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  231. PAL_PTR buffer, PAL_PTR source, PAL_NUM size)
  232. {
  233. ENTER_PAL_CALL(DkStreamRead);
  234. if (!handle || !buffer) {
  235. _DkRaiseFailure(-PAL_ERROR_INVAL);
  236. LEAVE_PAL_CALL_RETURN(0);
  237. }
  238. int64_t ret = _DkStreamRead(handle, offset, count, (void *) buffer,
  239. size ? (char *) source : NULL,
  240. source ? size : 0);
  241. if (ret < 0) {
  242. _DkRaiseFailure(-ret);
  243. ret = 0;
  244. }
  245. LEAVE_PAL_CALL_RETURN(ret);
  246. }
  247. /* _DkStreamWrite for internal use, write to stream at absolute offset.
  248. The actual behavior of stream write is defined by handler */
  249. int64_t _DkStreamWrite (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  250. const void * buf, const char * addr, int addrlen)
  251. {
  252. if (UNKNOWN_HANDLE(handle))
  253. return -PAL_ERROR_BADHANDLE;
  254. const struct handle_ops * ops = HANDLE_OPS(handle);
  255. if (!ops)
  256. return -PAL_ERROR_NOTSUPPORT;
  257. if (!count)
  258. return -PAL_ERROR_ZEROSIZE;
  259. int64_t ret;
  260. if (addr) {
  261. if (!ops->writebyaddr)
  262. return -PAL_ERROR_NOTSUPPORT;
  263. ret = ops->writebyaddr(handle, offset, count, buf, addr, addrlen);
  264. } else {
  265. if (!ops->write)
  266. return -PAL_ERROR_NOTSUPPORT;
  267. ret = ops->write(handle, offset, count, buf);
  268. }
  269. return ret ? ret : -PAL_ERROR_ENDOFSTREAM;
  270. }
  271. /* PAL call DkStreamWrite: Write to stream at absolute offset. Return number
  272. of bytes if succeeded, or 0 for failure. Error code is notified. */
  273. PAL_NUM
  274. DkStreamWrite (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  275. PAL_PTR buffer, PAL_STR dest)
  276. {
  277. ENTER_PAL_CALL(DkStreamWrite);
  278. if (!handle || !buffer) {
  279. _DkRaiseFailure(PAL_ERROR_INVAL);
  280. LEAVE_PAL_CALL_RETURN(0);
  281. }
  282. int64_t ret = _DkStreamWrite(handle, offset, count, (void *) buffer, dest,
  283. dest ? strlen(dest) : 0);
  284. if (ret < 0) {
  285. _DkRaiseFailure(-ret);
  286. ret = 0;
  287. }
  288. LEAVE_PAL_CALL_RETURN(ret);
  289. }
  290. /* _DkStreamAttributesQuery of internal use. The function query attribute
  291. of streams by their URI */
  292. int _DkStreamAttributesQuery (const char * uri, PAL_STREAM_ATTR * attr)
  293. {
  294. struct handle_ops * ops = NULL;
  295. const char * type = NULL;
  296. int ret = parse_stream_uri (&uri, &type, &ops);
  297. if (ret < 0)
  298. return ret;
  299. if (!ops->attrquery)
  300. return -PAL_ERROR_NOTSUPPORT;
  301. return ops->attrquery(type, uri, attr);
  302. }
  303. /* PAL call DkStreamAttributeQuery: query attribute of a stream by its
  304. URI, attr is memory given by user space. Return the pointer of attr
  305. if succeeded, or NULL if failed. Error code is notified */
  306. PAL_BOL
  307. DkStreamAttributesQuery (PAL_STR uri, PAL_STREAM_ATTR * attr)
  308. {
  309. ENTER_PAL_CALL(DkStreamAttributesQuery);
  310. if (!uri || !attr) {
  311. _DkRaiseFailure(PAL_ERROR_INVAL);
  312. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  313. }
  314. log_stream(uri);
  315. PAL_STREAM_ATTR attr_buf;
  316. int ret = _DkStreamAttributesQuery(uri, &attr_buf);
  317. if (ret < 0) {
  318. _DkRaiseFailure(-ret);
  319. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  320. }
  321. memcpy(attr, &attr_buf, sizeof(PAL_STREAM_ATTR));
  322. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  323. }
  324. /* _DkStreamAttributesQuerybyHandle for internal use. Query attribute
  325. of streams by their handle */
  326. int _DkStreamAttributesQuerybyHandle (PAL_HANDLE hdl, PAL_STREAM_ATTR * attr)
  327. {
  328. if (UNKNOWN_HANDLE(hdl))
  329. return -PAL_ERROR_BADHANDLE;
  330. const struct handle_ops * ops = HANDLE_OPS(hdl);
  331. assert(ops);
  332. /* if ops->attrquerybyhdl is not defined, the stream cannot be queried */
  333. if (!ops->attrquerybyhdl)
  334. return -PAL_ERROR_NOTSUPPORT;
  335. return ops->attrquerybyhdl(hdl, attr);
  336. }
  337. /* PAL call DkStreamAttributesQuerybyHandle: Query attribute of a stream by
  338. its handle, attr is memory given by user space. Return the pointer of attr
  339. if succeeded, or NULL if failed. Error code is notified */
  340. PAL_BOL
  341. DkStreamAttributesQuerybyHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  342. {
  343. ENTER_PAL_CALL(DkStreamAttributesQuerybyHandle);
  344. if (!handle || !attr) {
  345. _DkRaiseFailure(PAL_ERROR_INVAL);
  346. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  347. }
  348. int ret = _DkStreamAttributesQuerybyHandle(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. /* PAL call DkStreamAttributesSetbyHandle: Set attribute of a stream by
  356. its handle, attr is memory given by user space. Return the pointer of attr
  357. if succeeded, or NULL if failed. Error code is notified */
  358. PAL_BOL
  359. DkStreamAttributesSetbyHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  360. {
  361. ENTER_PAL_CALL(DkStreamAttributesSetbyHandle);
  362. if (!handle || !attr) {
  363. _DkRaiseFailure(PAL_ERROR_INVAL);
  364. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  365. }
  366. if (UNKNOWN_HANDLE(handle)) {
  367. _DkRaiseFailure(PAL_ERROR_BADHANDLE);
  368. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  369. }
  370. const struct handle_ops * ops = HANDLE_OPS(handle);
  371. assert(ops);
  372. if (!ops->attrsetbyhdl) {
  373. _DkRaiseFailure(PAL_ERROR_NOTSUPPORT);
  374. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  375. }
  376. int ret = ops->attrsetbyhdl(handle, attr);
  377. if (ret < 0) {
  378. _DkRaiseFailure(-ret);
  379. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  380. }
  381. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  382. }
  383. int _DkStreamGetName (PAL_HANDLE handle, char * buffer, int size)
  384. {
  385. const struct handle_ops * ops = HANDLE_OPS(handle);
  386. assert(ops);
  387. /* if ops->getname is not defined, the stream cannot be queried */
  388. if (!ops->getname)
  389. return -PAL_ERROR_NOTSUPPORT;
  390. int ret = ops->getname(handle, buffer, size - 1);
  391. if (ret < 0)
  392. return ret;
  393. ((char *)buffer)[ret] = 0;
  394. return ret;
  395. }
  396. /* PAL call DkStreamAttributesSetbyHandle: Set attribute of a stream by
  397. its handle, attr is memory given by user space. Return the pointer of attr
  398. if succeeded, or NULL if failed. Error code is notified */
  399. PAL_NUM DkStreamGetName (PAL_HANDLE handle, PAL_PTR buffer, PAL_NUM size)
  400. {
  401. ENTER_PAL_CALL(DkStreamGetName);
  402. if (!handle || !buffer || !size) {
  403. _DkRaiseFailure(PAL_ERROR_INVAL);
  404. LEAVE_PAL_CALL_RETURN(0);
  405. }
  406. if (UNKNOWN_HANDLE(handle)) {
  407. _DkRaiseFailure(PAL_ERROR_BADHANDLE);
  408. LEAVE_PAL_CALL_RETURN(0);
  409. }
  410. int ret = _DkStreamGetName(handle, (void *) buffer, size);
  411. if (ret < 0) {
  412. _DkRaiseFailure(-ret);
  413. ret = 0;
  414. }
  415. LEAVE_PAL_CALL_RETURN(ret);
  416. }
  417. /* _DkStreamMap for internal use. Map specific handle to certain memory,
  418. with given protection, offset and size */
  419. int _DkStreamMap (PAL_HANDLE handle, void ** paddr, int prot, uint64_t offset,
  420. uint64_t size)
  421. {
  422. void * addr = *paddr;
  423. int ret;
  424. const struct handle_ops * ops = HANDLE_OPS(handle);
  425. /* if ops or ops->map is not defined, the stream cannot be mapped */
  426. if (!ops || !ops->map)
  427. return -PAL_ERROR_NOTSUPPORT;
  428. if ((ret = ops->map(handle, &addr, prot, offset, size)) < 0)
  429. return ret;
  430. *paddr = addr;
  431. return 0;
  432. }
  433. /* PAL call DkStreamMap: Map a stream of a given handle to certain memery
  434. space. prot/offset/size are the protection, offset and size of the memory
  435. mapping. Return the address if succeeded or NULL if failed. Error code
  436. is notified. */
  437. PAL_PTR
  438. DkStreamMap (PAL_HANDLE handle, PAL_PTR addr, PAL_FLG prot, PAL_NUM offset,
  439. PAL_NUM size)
  440. {
  441. ENTER_PAL_CALL(DkStreamMap);
  442. void * map_addr = (void *) addr;
  443. if (!handle) {
  444. _DkRaiseFailure(PAL_ERROR_INVAL);
  445. LEAVE_PAL_CALL_RETURN((PAL_PTR) NULL);
  446. }
  447. /* Check that all addresses and sizes are aligned */
  448. if ((addr && !ALLOC_ALIGNED(addr)) || !size || !ALLOC_ALIGNED(size) ||
  449. !ALLOC_ALIGNED(offset)) {
  450. _DkRaiseFailure(PAL_ERROR_INVAL);
  451. LEAVE_PAL_CALL_RETURN((PAL_PTR) NULL);
  452. }
  453. if (map_addr && _DkCheckMemoryMappable((void *) map_addr, size)) {
  454. _DkRaiseFailure(PAL_ERROR_DENIED);
  455. LEAVE_PAL_CALL_RETURN((PAL_PTR) NULL);
  456. }
  457. int ret = _DkStreamMap(handle, &map_addr, prot, offset, size);
  458. if (ret < 0) {
  459. _DkRaiseFailure(-ret);
  460. map_addr = NULL;
  461. }
  462. LEAVE_PAL_CALL_RETURN((PAL_PTR) map_addr);
  463. }
  464. /* PAL call DkStreamUnmap: Unmap memory mapped at an address. The memory has
  465. to be a stream map, and it got unmapped as a whole memory area. No
  466. return value. Error code is notified */
  467. void DkStreamUnmap (PAL_PTR addr, PAL_NUM size)
  468. {
  469. ENTER_PAL_CALL(DkStreamUnmap);
  470. if (!addr || !ALLOC_ALIGNED((void *) addr) || !size || !ALLOC_ALIGNED(size)) {
  471. _DkRaiseFailure(PAL_ERROR_INVAL);
  472. LEAVE_PAL_CALL();
  473. }
  474. if (_DkCheckMemoryMappable((void *) addr, size)) {
  475. _DkRaiseFailure(PAL_ERROR_DENIED);
  476. LEAVE_PAL_CALL();
  477. }
  478. int ret = _DkStreamUnmap((void *) addr, size);
  479. if (ret < 0)
  480. _DkRaiseFailure(-ret);
  481. LEAVE_PAL_CALL();
  482. }
  483. /* _DkStreamSetLength for internal use. This function truncate the stream
  484. to certain length. This call might not be support for certain streams */
  485. int64_t _DkStreamSetLength (PAL_HANDLE handle, uint64_t length)
  486. {
  487. if (UNKNOWN_HANDLE(handle))
  488. return -PAL_ERROR_BADHANDLE;
  489. const struct handle_ops * ops = HANDLE_OPS(handle);
  490. if (!ops || !ops->setlength)
  491. return -PAL_ERROR_NOTSUPPORT;
  492. return ops->setlength(handle, length);
  493. }
  494. /* PAL call DkStreamSetLength: Truncate the stream at certain length.
  495. Return the length if succeeded or 0 if failed. Error code is notified. */
  496. PAL_NUM
  497. DkStreamSetLength (PAL_HANDLE handle, PAL_NUM length)
  498. {
  499. ENTER_PAL_CALL(DkStreamSetLength);
  500. if (!handle) {
  501. _DkRaiseFailure(PAL_ERROR_INVAL);
  502. LEAVE_PAL_CALL_RETURN(0);
  503. }
  504. int64_t ret = _DkStreamSetLength(handle, length);
  505. if (ret < 0) {
  506. _DkRaiseFailure(-ret);
  507. ret = 0;
  508. }
  509. LEAVE_PAL_CALL_RETURN(ret);
  510. }
  511. /* _DkStreamFlush for internal use. This function sync up the handle with
  512. devices. Some streams may not support this operations. */
  513. int _DkStreamFlush (PAL_HANDLE handle)
  514. {
  515. if (UNKNOWN_HANDLE(handle))
  516. return -PAL_ERROR_BADHANDLE;
  517. const struct handle_ops * ops = HANDLE_OPS(handle);
  518. if (!ops || !ops->flush)
  519. return -PAL_ERROR_NOTSUPPORT;
  520. return ops->flush(handle);
  521. }
  522. /* PAL call DkStreamFlush: Sync up a stream of a given handle. No return
  523. value. Error code is notified. */
  524. PAL_BOL DkStreamFlush (PAL_HANDLE handle)
  525. {
  526. ENTER_PAL_CALL(DkStreamFlush);
  527. if (!handle) {
  528. _DkRaiseFailure(PAL_ERROR_INVAL);
  529. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  530. }
  531. int ret = _DkStreamFlush(handle);
  532. if (ret < 0) {
  533. _DkRaiseFailure(-ret);
  534. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  535. }
  536. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  537. }
  538. /* PAL call DkSendHandle: Write to a process handle.
  539. Return 1 on success and 0 on failure */
  540. PAL_BOL DkSendHandle(PAL_HANDLE handle, PAL_HANDLE cargo)
  541. {
  542. ENTER_PAL_CALL(DkSendHandle);
  543. // Return error if any of the handle is NULL
  544. if (!handle || !cargo) {
  545. _DkRaiseFailure(PAL_ERROR_INVAL);
  546. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  547. }
  548. // Call the internal function after validating input args
  549. int ret = _DkSendHandle(handle, cargo);
  550. if (ret < 0) {
  551. _DkRaiseFailure(-ret);
  552. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  553. }
  554. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  555. }
  556. /* PAL call DkRecvHandle: Read a handle to a pipe/process handle.
  557. Return the received PAL_HANDLE by reference and 0 on success and
  558. negative number on failure */
  559. /* 1. Should i take the received PAL_HANDLE as an input argument and
  560. pass by reference or return it rather?
  561. Ans - We are not aware of the size of the variable members to return
  562. 2. Would the recieved PAL_HANDLE start functioning automatically in
  563. the new process environment? Should we initialize/modify some
  564. attibutes of the handle?
  565. Ans - Yes, Initialize and make it compatibile in the target process
  566. 3. Should malloc_copy be done or the process shares the same references?
  567. Ans - Variables members have to allocated data again.
  568. */
  569. PAL_HANDLE DkReceiveHandle (PAL_HANDLE handle)
  570. {
  571. ENTER_PAL_CALL(DkReceiveHandle);
  572. // return error if any of the handle is NULL
  573. if(!handle) {
  574. _DkRaiseFailure(PAL_ERROR_INVAL);
  575. LEAVE_PAL_CALL_RETURN(NULL);
  576. }
  577. // create a reference for the received PAL_HANDLE
  578. PAL_HANDLE cargo = NULL;
  579. // call the internal function after validating input args
  580. int ret = _DkReceiveHandle(handle, &cargo);
  581. // notify failure would have been called from other functions
  582. if (ret < 0) {
  583. _DkRaiseFailure(-ret);
  584. LEAVE_PAL_CALL_RETURN(NULL);
  585. }
  586. assert(cargo);
  587. assert(PAL_GET_TYPE(cargo));
  588. LEAVE_PAL_CALL_RETURN(cargo);
  589. }
  590. PAL_BOL DkStreamChangeName (PAL_HANDLE hdl, PAL_STR uri)
  591. {
  592. ENTER_PAL_CALL(DkStreamChangeName);
  593. struct handle_ops * ops = NULL;
  594. const char * type = NULL;
  595. int ret;
  596. if (uri) {
  597. ret = parse_stream_uri(&uri, &type, &ops);
  598. if (ret < 0) {
  599. _DkRaiseFailure(-ret);
  600. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  601. }
  602. }
  603. const struct handle_ops * hops = HANDLE_OPS(hdl);
  604. if (!hops || !hops->rename || (ops && hops != ops)) {
  605. _DkRaiseFailure(PAL_ERROR_NOTSUPPORT);
  606. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  607. }
  608. ret = hops->rename(hdl, type, uri);
  609. if (ret < 0) {
  610. _DkRaiseFailure(-ret);
  611. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  612. }
  613. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  614. }
  615. /* _DkStreamRealpath is used to obtain the real path of a stream. Some
  616. streams may not have a real path. */
  617. const char * _DkStreamRealpath (PAL_HANDLE hdl)
  618. {
  619. const struct handle_ops * ops = HANDLE_OPS(hdl);
  620. if (!ops || !ops->getrealpath)
  621. return NULL;
  622. return ops->getrealpath(hdl);
  623. }