db_streams.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 sem_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_semaphore] = &sem_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 (memcmp(u, "dir", 3) == 0)
  77. hops = &dir_ops;
  78. else if (memcmp(u, "tcp", 3) == 0)
  79. hops = &tcp_ops;
  80. else if (memcmp(u, "udp", 3) == 0)
  81. hops = &udp_ops;
  82. else if (memcmp(u, "dev", 3) == 0)
  83. hops = &dev_ops;
  84. break;
  85. case 4:
  86. if (memcmp(u, "file", 4) == 0)
  87. hops = &file_ops;
  88. else if (memcmp(u, "pipe", 4) == 0)
  89. hops = &pipe_ops;
  90. else if (memcmp(u, "gipc", 4) == 0)
  91. hops = &gipc_ops;
  92. break;
  93. case 7:
  94. if (memcmp(u, "tcp.srv", 7) == 0)
  95. hops = &tcp_ops;
  96. else if (memcmp(u, "udp.srv", 7) == 0)
  97. hops = &udp_ops;
  98. break;
  99. case 8:
  100. if (memcmp(u, "pipe.srv", 8) == 0)
  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. store_frame(StreamOpen);
  138. PAL_HANDLE handle = NULL;
  139. int ret = _DkStreamOpen(&handle, uri, access, share, create, options);
  140. if (ret < 0)
  141. leave_frame(NULL, -ret);
  142. assert(handle);
  143. assert(PAL_GET_TYPE(handle));
  144. leave_frame(handle, 0);
  145. }
  146. int _DkStreamWaitForClient(PAL_HANDLE handle, PAL_HANDLE * client)
  147. {
  148. if (UNKNOWN_HANDLE(handle))
  149. return -PAL_ERROR_BADHANDLE;
  150. const struct handle_ops * ops = HANDLE_OPS(handle);
  151. /* No ops or ops->delete being defined, inferring the handle can never
  152. be deleted */
  153. if (!ops || !ops->waitforclient)
  154. return -PAL_ERROR_NOTSERVER;
  155. return ops->waitforclient(handle, client);
  156. }
  157. PAL_HANDLE
  158. DkStreamWaitForClient (PAL_HANDLE handle)
  159. {
  160. store_frame(StreamWaitForClient);
  161. PAL_HANDLE client;
  162. int ret = _DkStreamWaitForClient(handle, &client);
  163. if (ret < 0)
  164. leave_frame(NULL, -ret);
  165. leave_frame(client, 0);
  166. }
  167. /* _DkStreamDelete for internal use. This function will explicit delete
  168. the stream. For example, file will be deleted, socket witll be
  169. disconnected, etc */
  170. int _DkStreamDelete (PAL_HANDLE handle, int access)
  171. {
  172. if (UNKNOWN_HANDLE(handle))
  173. return -PAL_ERROR_BADHANDLE;
  174. const struct handle_ops * ops = HANDLE_OPS(handle);
  175. /* No ops or ops->delete being defined, inferring the handle can never
  176. be deleted */
  177. if (!ops || !ops->delete)
  178. return -PAL_ERROR_NOTSUPPORT;
  179. return ops->delete(handle, access);
  180. }
  181. /* PAL call DkStreamDelete: Explicitly delete stream as given handle. No
  182. return value, error code is notified. */
  183. void DkStreamDelete (PAL_HANDLE handle, PAL_FLG access)
  184. {
  185. store_frame(StreamDelete);
  186. if (!handle)
  187. leave_frame(, PAL_ERROR_INVAL);
  188. int ret = _DkStreamDelete(handle, access);
  189. if (ret < 0)
  190. leave_frame(, -ret);
  191. leave_frame(, 0);
  192. }
  193. /* _DkStreamRead for internal use. Read from stream as absolute offset.
  194. The actual behavior of stream read is defined by handler */
  195. int _DkStreamRead (PAL_HANDLE handle, int offset, int count, void * buf,
  196. char * addr, int addrlen)
  197. {
  198. if (UNKNOWN_HANDLE(handle))
  199. return -PAL_ERROR_BADHANDLE;
  200. const struct handle_ops * ops = HANDLE_OPS(handle);
  201. /* if ops or ops->read is not defined, it infers that the stream can
  202. never be read */
  203. if (!ops)
  204. return -PAL_ERROR_NOTSUPPORT;
  205. if (!count)
  206. return -PAL_ERROR_ZEROSIZE;
  207. int ret;
  208. if (addr) {
  209. if (!ops->readbyaddr)
  210. return -PAL_ERROR_NOTSUPPORT;
  211. ret = ops->readbyaddr(handle, offset, count, buf, addr, addrlen);
  212. } else {
  213. if (!ops->read)
  214. return -PAL_ERROR_NOTSUPPORT;
  215. ret = ops->read(handle, offset, count, buf);
  216. }
  217. return ret ? : -PAL_ERROR_ENDOFSTREAM;
  218. }
  219. /* PAL call DkStreamRead: Read from stream at absolute offset. Return number
  220. of bytes if succeeded, or 0 for failure. Error code is notified. */
  221. PAL_NUM
  222. DkStreamRead (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  223. PAL_BUF buffer, PAL_BUF source, PAL_NUM size)
  224. {
  225. store_frame(StreamRead);
  226. if (!handle || !buffer)
  227. leave_frame(0, PAL_ERROR_INVAL);
  228. int ret = _DkStreamRead(handle, offset, count, buffer,
  229. size ? (char *) source : NULL,
  230. source ? size : 0);
  231. if (ret < 0)
  232. leave_frame(0, -ret);
  233. leave_frame(ret, 0);
  234. }
  235. /* _DkStreamWrite for internal use, write to stream at absolute offset.
  236. The actual behavior of stream write is defined by handler */
  237. int _DkStreamWrite (PAL_HANDLE handle, int offset, int count, const void * buf,
  238. const char * addr, int addrlen)
  239. {
  240. if (UNKNOWN_HANDLE(handle))
  241. return -PAL_ERROR_BADHANDLE;
  242. const struct handle_ops * ops = HANDLE_OPS(handle);
  243. if (!ops)
  244. return -PAL_ERROR_NOTSUPPORT;
  245. if (!count)
  246. return -PAL_ERROR_ZEROSIZE;
  247. int ret;
  248. if (addr) {
  249. if (!ops->writebyaddr)
  250. return -PAL_ERROR_NOTSUPPORT;
  251. ret = ops->writebyaddr(handle, offset, count, buf, addr, addrlen);
  252. } else {
  253. if (!ops->write)
  254. return -PAL_ERROR_NOTSUPPORT;
  255. ret = ops->write(handle, offset, count, buf);
  256. }
  257. return ret ? : -PAL_ERROR_ENDOFSTREAM;
  258. }
  259. /* PAL call DkStreamWrite: Write to stream at absolute offset. Return number
  260. of bytes if succeeded, or 0 for failure. Error code is notified. */
  261. PAL_NUM
  262. DkStreamWrite (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  263. const PAL_PTR buffer, PAL_STR dest)
  264. {
  265. store_frame(StreamWrite);
  266. if (!handle || !buffer)
  267. leave_frame(0, PAL_ERROR_INVAL);
  268. int ret = _DkStreamWrite(handle, offset, count, buffer, dest,
  269. dest ? strlen(dest) : 0);
  270. if (ret < 0)
  271. leave_frame(0, -ret);
  272. leave_frame(ret, 0);
  273. }
  274. /* _DkStreamAttributesQuery of internal use. The function query attribute
  275. of streams by their URI */
  276. int _DkStreamAttributesQuery (const char * uri, PAL_STREAM_ATTR * attr)
  277. {
  278. struct handle_ops * ops = NULL;
  279. const char * type = NULL;
  280. int ret = parse_stream_uri (&uri, &type, &ops);
  281. if (ret < 0)
  282. return ret;
  283. if (!ops->attrquery)
  284. return -PAL_ERROR_NOTSUPPORT;
  285. return ops->attrquery(type, uri, attr);
  286. }
  287. /* PAL call DkStreamAttributeQuery: query attribute of a stream by its
  288. URI, attr is memory given by user space. Return the pointer of attr
  289. if succeeded, or NULL if failed. Error code is notified */
  290. PAL_BOL
  291. DkStreamAttributesQuery (PAL_STR uri, PAL_STREAM_ATTR * attr)
  292. {
  293. store_frame(StreamAttributesQuery);
  294. if (!uri || !attr)
  295. leave_frame(PAL_FALSE, PAL_ERROR_INVAL);
  296. log_stream(uri);
  297. PAL_STREAM_ATTR attr_buf;
  298. int ret = _DkStreamAttributesQuery(uri, &attr_buf);
  299. if (ret < 0)
  300. leave_frame(PAL_FALSE, -ret);
  301. memcpy(attr, &attr_buf, sizeof(PAL_STREAM_ATTR));
  302. leave_frame(PAL_TRUE, 0);
  303. }
  304. /* _DkStreamAttributesQuerybyHandle for internal use. Query attribute
  305. of streams by their handle */
  306. int _DkStreamAttributesQuerybyHandle (PAL_HANDLE hdl, PAL_STREAM_ATTR * attr)
  307. {
  308. if (UNKNOWN_HANDLE(hdl))
  309. return -PAL_ERROR_BADHANDLE;
  310. const struct handle_ops * ops = HANDLE_OPS(hdl);
  311. assert(ops);
  312. /* if ops->attrquerybyhdl is not defined, the stream cannot be queried */
  313. if (!ops->attrquerybyhdl)
  314. return -PAL_ERROR_NOTSUPPORT;
  315. return ops->attrquerybyhdl(hdl, attr);
  316. }
  317. /* PAL call DkStreamAttributesQuerybyHandle: Query attribute of a stream by
  318. its handle, attr is memory given by user space. Return the pointer of attr
  319. if succeeded, or NULL if failed. Error code is notified */
  320. PAL_BOL
  321. DkStreamAttributesQuerybyHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  322. {
  323. store_frame(StreamAttributesQuerybyHandle);
  324. if (!handle || !attr)
  325. leave_frame(PAL_FALSE, PAL_ERROR_INVAL);
  326. int ret = _DkStreamAttributesQuerybyHandle(handle, attr);
  327. if (ret < 0)
  328. leave_frame(PAL_FALSE, -ret);
  329. leave_frame(PAL_TRUE, 0);
  330. }
  331. /* PAL call DkStreamAttributesSetbyHandle: Set attribute of a stream by
  332. its handle, attr is memory given by user space. Return the pointer of attr
  333. if succeeded, or NULL if failed. Error code is notified */
  334. PAL_BOL
  335. DkStreamAttributesSetbyHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  336. {
  337. store_frame(StreamAttributesSetbyHandle);
  338. if (!handle || !attr)
  339. leave_frame(PAL_FALSE, PAL_ERROR_INVAL);
  340. if (UNKNOWN_HANDLE(handle))
  341. leave_frame(PAL_FALSE, PAL_ERROR_BADHANDLE);
  342. const struct handle_ops * ops = HANDLE_OPS(handle);
  343. assert(ops);
  344. if (!ops->attrsetbyhdl)
  345. leave_frame(PAL_FALSE, PAL_ERROR_NOTSUPPORT);
  346. int ret = ops->attrsetbyhdl(handle, attr);
  347. if (ret < 0)
  348. leave_frame(PAL_FALSE, -ret);
  349. leave_frame(PAL_TRUE, 0);
  350. }
  351. int _DkStreamGetName (PAL_HANDLE handle, char * buffer, int size)
  352. {
  353. const struct handle_ops * ops = HANDLE_OPS(handle);
  354. assert(ops);
  355. /* if ops->getname is not defined, the stream cannot be queried */
  356. if (!ops->getname)
  357. return -PAL_ERROR_NOTSUPPORT;
  358. int ret = ops->getname(handle, buffer, size - 1);
  359. if (ret < 0)
  360. return ret;
  361. ((char *)buffer)[ret] = 0;
  362. return ret;
  363. }
  364. /* PAL call DkStreamAttributesSetbyHandle: Set attribute of a stream by
  365. its handle, attr is memory given by user space. Return the pointer of attr
  366. if succeeded, or NULL if failed. Error code is notified */
  367. PAL_NUM DkStreamGetName (PAL_HANDLE handle, PAL_BUF buffer, PAL_NUM size)
  368. {
  369. store_frame(StreamGetName);
  370. if (!handle || !buffer || !size)
  371. leave_frame(0, PAL_ERROR_INVAL);
  372. if (UNKNOWN_HANDLE(handle))
  373. leave_frame(0, PAL_ERROR_BADHANDLE);
  374. int ret = _DkStreamGetName(handle, buffer, size);
  375. if (ret < 0)
  376. leave_frame(0, -ret);
  377. leave_frame(ret, 0);
  378. }
  379. /* _DkStreamMap for internal use. Map specific handle to certain memory,
  380. with given protection, offset and size */
  381. int _DkStreamMap (PAL_HANDLE handle, void ** paddr, int prot, int offset,
  382. int size)
  383. {
  384. void * addr = *paddr;
  385. int ret;
  386. const struct handle_ops * ops = HANDLE_OPS(handle);
  387. /* if ops or ops->map is not defined, the stream cannot be mapped */
  388. if (!ops || !ops->map)
  389. return -PAL_ERROR_NOTSUPPORT;
  390. if ((ret = ops->map(handle, &addr, prot, offset, size)) < 0)
  391. return ret;
  392. *paddr = addr;
  393. return 0;
  394. }
  395. /* PAL call DkStreamMap: Map a stream of a given handle to certain memery
  396. space. prot/offset/size are the protection, offset and size of the memory
  397. mapping. Return the address if succeeded or NULL if failed. Error code
  398. is notified. */
  399. PAL_PTR
  400. DkStreamMap (PAL_HANDLE handle, PAL_PTR addr, PAL_FLG prot, PAL_NUM offset,
  401. PAL_NUM size)
  402. {
  403. store_frame(StreamMap);
  404. void * map_addr = (void *) addr;
  405. if (!handle)
  406. leave_frame(NULL, PAL_ERROR_INVAL);
  407. if ((addr && !ALLOC_ALIGNED(addr)) || !size || !ALLOC_ALIGNED(size) ||
  408. !ALLOC_ALIGNED(offset))
  409. leave_frame(NULL, PAL_ERROR_INVAL);
  410. if (map_addr && _DkCheckMemoryMappable((void *) map_addr, size))
  411. leave_frame(NULL, PAL_ERROR_DENIED);
  412. int ret = _DkStreamMap(handle, &map_addr, prot, offset, size);
  413. if (ret < 0)
  414. leave_frame(NULL, -ret);
  415. leave_frame(map_addr, 0);
  416. }
  417. /* PAL call DkStreamUnmap: Unmap memory mapped at an address. The memory has
  418. to be a stream map, and it got unmapped as a whole memory area. No
  419. return value. Error code is notified */
  420. void DkStreamUnmap (PAL_PTR addr, PAL_NUM size)
  421. {
  422. store_frame(StreamUnmap);
  423. if (!addr || !ALLOC_ALIGNED(addr) || !size || !ALLOC_ALIGNED(size))
  424. leave_frame(, PAL_ERROR_INVAL);
  425. if (_DkCheckMemoryMappable((void *) addr, size))
  426. leave_frame(, PAL_ERROR_DENIED);
  427. int ret = _DkStreamUnmap((void *) addr, size);
  428. if (ret < 0)
  429. leave_frame(, ret);
  430. leave_frame(, 0);
  431. }
  432. /* _DkStreamSetLength for internal use. This function truncate the stream
  433. to certain length. This call might not be support for certain streams */
  434. int _DkStreamSetLength (PAL_HANDLE handle, int length)
  435. {
  436. if (UNKNOWN_HANDLE(handle))
  437. return -PAL_ERROR_BADHANDLE;
  438. const struct handle_ops * ops = HANDLE_OPS(handle);
  439. if (!ops || !ops->setlength)
  440. return -PAL_ERROR_NOTSUPPORT;
  441. return ops->setlength(handle, length);
  442. }
  443. /* PAL call DkStreamSetLength: Truncate the stream at certain length.
  444. Return the length if succeeded or 0 if failed. Error code is notified. */
  445. PAL_NUM
  446. DkStreamSetLength (PAL_HANDLE handle, PAL_NUM length)
  447. {
  448. store_frame(StreamSetLength);
  449. if (!handle)
  450. leave_frame(0, PAL_ERROR_INVAL);
  451. int ret = _DkStreamSetLength(handle, length);
  452. if (ret < 0)
  453. leave_frame(0, -ret);
  454. leave_frame(ret, 0);
  455. }
  456. /* _DkStreamFlush for internal use. This function sync up the handle with
  457. devices. Some streams may not support this operations. */
  458. int _DkStreamFlush (PAL_HANDLE handle)
  459. {
  460. if (UNKNOWN_HANDLE(handle))
  461. return -PAL_ERROR_BADHANDLE;
  462. const struct handle_ops * ops = HANDLE_OPS(handle);
  463. if (!ops || !ops->flush)
  464. return -PAL_ERROR_NOTSUPPORT;
  465. return ops->flush(handle);
  466. }
  467. /* PAL call DkStreamFlush: Sync up a stream of a given handle. No return
  468. value. Error code is notified. */
  469. PAL_BOL DkStreamFlush (PAL_HANDLE handle)
  470. {
  471. store_frame(StreamFlush);
  472. if (!handle)
  473. leave_frame(PAL_FALSE, PAL_ERROR_INVAL);
  474. int ret = _DkStreamFlush(handle);
  475. if (ret < 0)
  476. leave_frame(PAL_FALSE, -ret);
  477. leave_frame(PAL_TRUE, 0);
  478. }
  479. /* PAL call DkSendHandle: Write to a process handle.
  480. Return 1 on success and 0 on failure */
  481. PAL_BOL DkSendHandle(PAL_HANDLE handle, PAL_HANDLE cargo)
  482. {
  483. store_frame(SendHandle);
  484. // Return error if any of the handle is NULL
  485. if (!handle || !cargo)
  486. leave_frame(PAL_FALSE, PAL_ERROR_INVAL);
  487. // Call the internal function after validating input args
  488. int ret = _DkSendHandle(handle, cargo);
  489. if (ret < 0)
  490. leave_frame(PAL_FALSE, -ret);
  491. leave_frame(PAL_TRUE, 0);
  492. }
  493. /* PAL call DkRecvHandle: Read a handle to a pipe/process handle.
  494. Return the received PAL_HANDLE by reference and 0 on success and
  495. negative number on failure */
  496. /* 1. Should i take the received PAL_HANDLE as an input argument and
  497. pass by reference or return it rather?
  498. Ans - We are not aware of the size of the variable members to return
  499. 2. Would the recieved PAL_HANDLE start functioning automatically in
  500. the new process environment? Should we initialize/modify some
  501. attibutes of the handle?
  502. Ans - Yes, Initialize and make it compatibile in the target process
  503. 3. Should remalloc be done or the process shares the same references?
  504. Ans - Variables members have to allocated data again.
  505. */
  506. PAL_HANDLE DkReceiveHandle (PAL_HANDLE handle)
  507. {
  508. store_frame(ReceiveHandle);
  509. // return error if any of the handle is NULL
  510. if(!handle)
  511. leave_frame(0, PAL_ERROR_INVAL);
  512. // create a reference for the received PAL_HANDLE
  513. PAL_HANDLE cargo = NULL;
  514. // call the internal function after validating input args
  515. int ret = _DkReceiveHandle(handle, &cargo);
  516. // notify failure would have been called from other functions
  517. if (ret < 0)
  518. leave_frame(NULL, -ret);
  519. assert(cargo);
  520. assert(PAL_GET_TYPE(cargo));
  521. leave_frame(cargo, 0);
  522. }
  523. PAL_BOL DkStreamChangeName (PAL_HANDLE hdl, PAL_STR uri)
  524. {
  525. store_frame(StreamChangeName);
  526. struct handle_ops * ops = NULL;
  527. const char * type = NULL;
  528. int ret;
  529. if (uri) {
  530. ret = parse_stream_uri(&uri, &type, &ops);
  531. if (ret < 0)
  532. leave_frame(PAL_FALSE, -ret);
  533. }
  534. const struct handle_ops * hops = HANDLE_OPS(hdl);
  535. if (!hops || !hops->rename || (ops && hops != ops))
  536. leave_frame(PAL_FALSE, PAL_ERROR_NOTSUPPORT);
  537. ret = hops->rename(hdl, type, uri);
  538. if (ret < 0)
  539. leave_frame(PAL_FALSE, -ret);
  540. leave_frame(PAL_TRUE, 0);
  541. }
  542. /* _DkStreamRealpath is used to obtain the real path of a stream. Some
  543. streams may not have a real path. */
  544. const char * _DkStreamRealpath (PAL_HANDLE hdl)
  545. {
  546. const struct handle_ops * ops = HANDLE_OPS(hdl);
  547. if (!ops || !ops->getrealpath)
  548. return NULL;
  549. return ops->getrealpath(hdl);
  550. }