db_streams.c 20 KB

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