db_streams.c 22 KB

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