db_streams.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. int _DkStreamRead (PAL_HANDLE handle, int offset, int count, void * buf,
  204. 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. int 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. int 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. int _DkStreamWrite (PAL_HANDLE handle, int offset, int count, const void * buf,
  250. 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. int 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. int 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. if ((addr && !ALLOC_ALIGNED(addr)) || !size || !ALLOC_ALIGNED(size) ||
  448. !ALLOC_ALIGNED(offset)) {
  449. _DkRaiseFailure(PAL_ERROR_INVAL);
  450. LEAVE_PAL_CALL_RETURN((PAL_PTR) NULL);
  451. }
  452. if (map_addr && _DkCheckMemoryMappable((void *) map_addr, size)) {
  453. _DkRaiseFailure(PAL_ERROR_DENIED);
  454. LEAVE_PAL_CALL_RETURN((PAL_PTR) NULL);
  455. }
  456. int ret = _DkStreamMap(handle, &map_addr, prot, offset, size);
  457. if (ret < 0) {
  458. _DkRaiseFailure(-ret);
  459. map_addr = NULL;
  460. }
  461. LEAVE_PAL_CALL_RETURN((PAL_PTR) map_addr);
  462. }
  463. /* PAL call DkStreamUnmap: Unmap memory mapped at an address. The memory has
  464. to be a stream map, and it got unmapped as a whole memory area. No
  465. return value. Error code is notified */
  466. void DkStreamUnmap (PAL_PTR addr, PAL_NUM size)
  467. {
  468. ENTER_PAL_CALL(DkStreamUnmap);
  469. if (!addr || !ALLOC_ALIGNED((void *) addr) || !size || !ALLOC_ALIGNED(size)) {
  470. _DkRaiseFailure(PAL_ERROR_INVAL);
  471. LEAVE_PAL_CALL();
  472. }
  473. if (_DkCheckMemoryMappable((void *) addr, size)) {
  474. _DkRaiseFailure(PAL_ERROR_DENIED);
  475. LEAVE_PAL_CALL();
  476. }
  477. int ret = _DkStreamUnmap((void *) addr, size);
  478. if (ret < 0)
  479. _DkRaiseFailure(-ret);
  480. LEAVE_PAL_CALL();
  481. }
  482. /* _DkStreamSetLength for internal use. This function truncate the stream
  483. to certain length. This call might not be support for certain streams */
  484. int64_t _DkStreamSetLength (PAL_HANDLE handle, uint64_t length)
  485. {
  486. if (UNKNOWN_HANDLE(handle))
  487. return -PAL_ERROR_BADHANDLE;
  488. const struct handle_ops * ops = HANDLE_OPS(handle);
  489. if (!ops || !ops->setlength)
  490. return -PAL_ERROR_NOTSUPPORT;
  491. return ops->setlength(handle, length);
  492. }
  493. /* PAL call DkStreamSetLength: Truncate the stream at certain length.
  494. Return the length if succeeded or 0 if failed. Error code is notified. */
  495. PAL_NUM
  496. DkStreamSetLength (PAL_HANDLE handle, PAL_NUM length)
  497. {
  498. ENTER_PAL_CALL(DkStreamSetLength);
  499. if (!handle) {
  500. _DkRaiseFailure(PAL_ERROR_INVAL);
  501. LEAVE_PAL_CALL_RETURN(0);
  502. }
  503. int64_t ret = _DkStreamSetLength(handle, length);
  504. if (ret < 0) {
  505. _DkRaiseFailure(-ret);
  506. ret = 0;
  507. }
  508. LEAVE_PAL_CALL_RETURN(ret);
  509. }
  510. /* _DkStreamFlush for internal use. This function sync up the handle with
  511. devices. Some streams may not support this operations. */
  512. int _DkStreamFlush (PAL_HANDLE handle)
  513. {
  514. if (UNKNOWN_HANDLE(handle))
  515. return -PAL_ERROR_BADHANDLE;
  516. const struct handle_ops * ops = HANDLE_OPS(handle);
  517. if (!ops || !ops->flush)
  518. return -PAL_ERROR_NOTSUPPORT;
  519. return ops->flush(handle);
  520. }
  521. /* PAL call DkStreamFlush: Sync up a stream of a given handle. No return
  522. value. Error code is notified. */
  523. PAL_BOL DkStreamFlush (PAL_HANDLE handle)
  524. {
  525. ENTER_PAL_CALL(DkStreamFlush);
  526. if (!handle) {
  527. _DkRaiseFailure(PAL_ERROR_INVAL);
  528. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  529. }
  530. int ret = _DkStreamFlush(handle);
  531. if (ret < 0) {
  532. _DkRaiseFailure(-ret);
  533. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  534. }
  535. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  536. }
  537. /* PAL call DkSendHandle: Write to a process handle.
  538. Return 1 on success and 0 on failure */
  539. PAL_BOL DkSendHandle(PAL_HANDLE handle, PAL_HANDLE cargo)
  540. {
  541. ENTER_PAL_CALL(DkSendHandle);
  542. // Return error if any of the handle is NULL
  543. if (!handle || !cargo) {
  544. _DkRaiseFailure(PAL_ERROR_INVAL);
  545. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  546. }
  547. // Call the internal function after validating input args
  548. int ret = _DkSendHandle(handle, cargo);
  549. if (ret < 0) {
  550. _DkRaiseFailure(-ret);
  551. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  552. }
  553. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  554. }
  555. /* PAL call DkRecvHandle: Read a handle to a pipe/process handle.
  556. Return the received PAL_HANDLE by reference and 0 on success and
  557. negative number on failure */
  558. /* 1. Should i take the received PAL_HANDLE as an input argument and
  559. pass by reference or return it rather?
  560. Ans - We are not aware of the size of the variable members to return
  561. 2. Would the recieved PAL_HANDLE start functioning automatically in
  562. the new process environment? Should we initialize/modify some
  563. attibutes of the handle?
  564. Ans - Yes, Initialize and make it compatibile in the target process
  565. 3. Should remalloc be done or the process shares the same references?
  566. Ans - Variables members have to allocated data again.
  567. */
  568. PAL_HANDLE DkReceiveHandle (PAL_HANDLE handle)
  569. {
  570. ENTER_PAL_CALL(DkReceiveHandle);
  571. // return error if any of the handle is NULL
  572. if(!handle) {
  573. _DkRaiseFailure(PAL_ERROR_INVAL);
  574. LEAVE_PAL_CALL_RETURN(NULL);
  575. }
  576. // create a reference for the received PAL_HANDLE
  577. PAL_HANDLE cargo = NULL;
  578. // call the internal function after validating input args
  579. int ret = _DkReceiveHandle(handle, &cargo);
  580. // notify failure would have been called from other functions
  581. if (ret < 0) {
  582. _DkRaiseFailure(-ret);
  583. LEAVE_PAL_CALL_RETURN(NULL);
  584. }
  585. assert(cargo);
  586. assert(PAL_GET_TYPE(cargo));
  587. LEAVE_PAL_CALL_RETURN(cargo);
  588. }
  589. PAL_BOL DkStreamChangeName (PAL_HANDLE hdl, PAL_STR uri)
  590. {
  591. ENTER_PAL_CALL(DkStreamChangeName);
  592. struct handle_ops * ops = NULL;
  593. const char * type = NULL;
  594. int ret;
  595. if (uri) {
  596. ret = parse_stream_uri(&uri, &type, &ops);
  597. if (ret < 0) {
  598. _DkRaiseFailure(-ret);
  599. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  600. }
  601. }
  602. const struct handle_ops * hops = HANDLE_OPS(hdl);
  603. if (!hops || !hops->rename || (ops && hops != ops)) {
  604. _DkRaiseFailure(PAL_ERROR_NOTSUPPORT);
  605. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  606. }
  607. ret = hops->rename(hdl, type, uri);
  608. if (ret < 0) {
  609. _DkRaiseFailure(-ret);
  610. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  611. }
  612. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  613. }
  614. /* _DkStreamRealpath is used to obtain the real path of a stream. Some
  615. streams may not have a real path. */
  616. const char * _DkStreamRealpath (PAL_HANDLE hdl)
  617. {
  618. const struct handle_ops * ops = HANDLE_OPS(hdl);
  619. if (!ops || !ops->getrealpath)
  620. return NULL;
  621. return ops->getrealpath(hdl);
  622. }