db_streams.c 22 KB

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