db_streams.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. NULL,
  47. &file_ops,
  48. &pipe_ops,
  49. &pipe_ops,
  50. &pipe_ops,
  51. &pipeprv_ops,
  52. &dev_ops,
  53. &dir_ops,
  54. &tcp_ops,
  55. &tcp_ops,
  56. &udp_ops,
  57. &udpsrv_ops,
  58. &proc_ops,
  59. &mcast_ops,
  60. &thread_ops,
  61. &sem_ops,
  62. &event_ops,
  63. &gipc_ops,
  64. };
  65. /* parse_stream_uri scan the uri, seperate prefix and search for
  66. stream handler which will open or access the stream */
  67. int parse_stream_uri (const char ** uri, const char ** prefix,
  68. struct handle_ops ** ops)
  69. {
  70. const char * p, * u = (*uri);
  71. for (p = u ; (*p) && (*p) != ':' ; p++);
  72. if ((*p) != ':')
  73. return -PAL_ERROR_INVAL;
  74. struct handle_ops * hops = NULL;
  75. switch (p - u) {
  76. case 3:
  77. if (memcmp(u, "dir", 3) == 0)
  78. hops = &dir_ops;
  79. else if (memcmp(u, "tcp", 3) == 0)
  80. hops = &tcp_ops;
  81. else if (memcmp(u, "udp", 3) == 0)
  82. hops = &udp_ops;
  83. else if (memcmp(u, "dev", 3) == 0)
  84. hops = &dev_ops;
  85. break;
  86. case 4:
  87. if (memcmp(u, "file", 4) == 0)
  88. hops = &file_ops;
  89. else if (memcmp(u, "pipe", 4) == 0)
  90. hops = &pipe_ops;
  91. else if (memcmp(u, "gipc", 4) == 0)
  92. hops = &gipc_ops;
  93. break;
  94. case 7:
  95. if (memcmp(u, "tcp.srv", 7) == 0)
  96. hops = &tcp_ops;
  97. else if (memcmp(u, "udp.srv", 7) == 0)
  98. hops = &udp_ops;
  99. break;
  100. case 8:
  101. if (memcmp(u, "pipe.srv", 8) == 0)
  102. hops = &pipe_ops;
  103. break;
  104. default:
  105. break;
  106. }
  107. if (!hops)
  108. return -PAL_ERROR_NOTSUPPORT;
  109. *uri = p + 1;
  110. if (prefix)
  111. *prefix = u;
  112. if (ops)
  113. *ops = hops;
  114. return 0;
  115. }
  116. /* _DkStreamOpen for internal use. Open stream based on uri.
  117. access/share/create/options are the same flags defined for
  118. DkStreamOpen. */
  119. int _DkStreamOpen (PAL_HANDLE * handle, const char * uri,
  120. int access, int share, int create, int options)
  121. {
  122. struct handle_ops * ops = NULL;
  123. const char * type = NULL;
  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;
  139. int ret = _DkStreamOpen(&handle, uri, access, share, create, options);
  140. if (ret < 0) {
  141. notify_failure (-ret);
  142. return NULL;
  143. }
  144. return handle;
  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. union pal_handle *client;
  162. int ret = _DkStreamWaitForClient(handle, &client);
  163. if (ret < 0) {
  164. notify_failure (-ret);
  165. return NULL;
  166. }
  167. return client;
  168. }
  169. /* _DkStreamDelete for internal use. This function will explicit delete
  170. the stream. For example, file will be deleted, socket witll be
  171. disconnected, etc */
  172. int _DkStreamDelete (PAL_HANDLE handle, int access)
  173. {
  174. if (UNKNOWN_HANDLE(handle))
  175. return -PAL_ERROR_BADHANDLE;
  176. const struct handle_ops * ops = HANDLE_OPS(handle);
  177. /* No ops or ops->delete being defined, inferring the handle can never
  178. be deleted */
  179. if (!ops || !ops->delete)
  180. return -PAL_ERROR_NOTSUPPORT;
  181. return ops->delete(handle, access);
  182. }
  183. /* PAL call DkStreamDelete: Explicitly delete stream as given handle. No
  184. return value, error code is notified. */
  185. void DkStreamDelete (PAL_HANDLE handle, PAL_FLG access)
  186. {
  187. store_frame(StreamDelete);
  188. if (handle == NULL) {
  189. notify_failure (PAL_ERROR_INVAL);
  190. return;
  191. }
  192. int ret = _DkStreamDelete(handle, access);
  193. if (ret < 0)
  194. notify_failure(-ret);
  195. }
  196. /* _DkStreamRead for internal use. Read from stream as absolute offset.
  197. The actual behavior of stream read is defined by handler */
  198. int _DkStreamRead (PAL_HANDLE handle, int offset, int count, void * buf,
  199. char * addr, int addrlen)
  200. {
  201. if (UNKNOWN_HANDLE(handle))
  202. return -PAL_ERROR_BADHANDLE;
  203. const struct handle_ops * ops = HANDLE_OPS(handle);
  204. /* if ops or ops->read is not defined, it infers that the stream can
  205. never be read */
  206. if (!ops)
  207. return -PAL_ERROR_NOTSUPPORT;
  208. if (!count)
  209. return -PAL_ERROR_ZEROSIZE;
  210. int ret;
  211. if (addr) {
  212. if (!ops->readbyaddr)
  213. return -PAL_ERROR_NOTSUPPORT;
  214. ret = ops->readbyaddr(handle, offset, count, buf, addr, addrlen);
  215. } else {
  216. if (!ops->read)
  217. return -PAL_ERROR_NOTSUPPORT;
  218. ret = ops->read(handle, offset, count, buf);
  219. }
  220. return ret ? : -PAL_ERROR_ENDOFSTREAM;
  221. }
  222. /* PAL call DkStreamRead: Read from stream at absolute offset. Return number
  223. of bytes if succeeded, or 0 for failure. Error code is notified. */
  224. PAL_NUM
  225. DkStreamRead (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  226. PAL_BUF buffer, PAL_BUF source, PAL_NUM size)
  227. {
  228. store_frame(StreamRead);
  229. if (!handle || !buffer) {
  230. notify_failure (PAL_ERROR_INVAL);
  231. return 0;
  232. }
  233. int ret = _DkStreamRead(handle, offset, count, buffer,
  234. size ? (char *) source : NULL,
  235. source ? size : 0);
  236. if (ret < 0) {
  237. notify_failure(-ret);
  238. ret = 0;
  239. }
  240. return ret;
  241. }
  242. /* _DkStreamWrite for internal use, write to stream at absolute offset.
  243. The actual behavior of stream write is defined by handler */
  244. int _DkStreamWrite (PAL_HANDLE handle, int offset, int count, const void * buf,
  245. const char * addr, int addrlen)
  246. {
  247. if (UNKNOWN_HANDLE(handle))
  248. return -PAL_ERROR_BADHANDLE;
  249. const struct handle_ops * ops = HANDLE_OPS(handle);
  250. if (!ops)
  251. return -PAL_ERROR_NOTSUPPORT;
  252. if (!count)
  253. return -PAL_ERROR_ZEROSIZE;
  254. int ret;
  255. if (addr) {
  256. if (!ops->writebyaddr)
  257. return -PAL_ERROR_NOTSUPPORT;
  258. ret = ops->writebyaddr(handle, offset, count, buf, addr, addrlen);
  259. } else {
  260. if (!ops->write)
  261. return -PAL_ERROR_NOTSUPPORT;
  262. ret = ops->write(handle, offset, count, buf);
  263. }
  264. return ret ? : -PAL_ERROR_ENDOFSTREAM;
  265. }
  266. /* PAL call DkStreamWrite: Write to stream at absolute offset. Return number
  267. of bytes if succeeded, or 0 for failure. Error code is notified. */
  268. PAL_NUM
  269. DkStreamWrite (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
  270. const PAL_PTR buffer, PAL_STR dest)
  271. {
  272. store_frame(StreamWrite);
  273. if (handle == NULL || buffer == NULL) {
  274. notify_failure (PAL_ERROR_INVAL);
  275. return 0;
  276. }
  277. int ret = _DkStreamWrite(handle, offset, count, buffer, dest,
  278. dest ? strlen(dest) : 0);
  279. if (ret < 0) {
  280. notify_failure(-ret);
  281. ret = 0;
  282. }
  283. return ret;
  284. }
  285. /* _DkStreamAttributesQuery of internal use. The function query attribute
  286. of streams by their URI */
  287. int _DkStreamAttributesQuery (PAL_STR uri, PAL_STREAM_ATTR * attr)
  288. {
  289. struct handle_ops * ops = NULL;
  290. const char * type = NULL;
  291. int ret = parse_stream_uri (&uri, &type, &ops);
  292. if (ret < 0)
  293. return ret;
  294. if (!ops->attrquery)
  295. return -PAL_ERROR_NOTSUPPORT;
  296. return ops->attrquery(type, uri, attr);
  297. }
  298. /* PAL call DkStreamAttributeQuery: query attribute of a stream by its
  299. URI, attr is memory given by user space. Return the pointer of attr
  300. if succeeded, or NULL if failed. Error code is notified */
  301. PAL_BOL
  302. DkStreamAttributesQuery (PAL_STR uri, PAL_STREAM_ATTR * attr)
  303. {
  304. store_frame(StreamAttributesQuery);
  305. if (!uri || !attr) {
  306. notify_failure(PAL_ERROR_INVAL);
  307. return PAL_FALSE;
  308. }
  309. PAL_STREAM_ATTR attr_buf;
  310. int ret = _DkStreamAttributesQuery(uri, &attr_buf);
  311. if (ret < 0) {
  312. notify_failure(-ret);
  313. return PAL_FALSE;
  314. }
  315. memcpy(attr, &attr_buf, sizeof(PAL_STREAM_ATTR));
  316. return PAL_TRUE;
  317. }
  318. /* _DkStreamAttributesQuerybyHandle for internal use. Query attribute
  319. of streams by their handle */
  320. int _DkStreamAttributesQuerybyHandle (PAL_HANDLE hdl, PAL_STREAM_ATTR * attr)
  321. {
  322. if (UNKNOWN_HANDLE(hdl))
  323. return -PAL_ERROR_BADHANDLE;
  324. const struct handle_ops * ops = HANDLE_OPS(hdl);
  325. assert(ops);
  326. /* if ops->attrquerybyhdl is not defined, the stream cannot be queried */
  327. if (!ops->attrquerybyhdl)
  328. return -PAL_ERROR_NOTSUPPORT;
  329. return ops->attrquerybyhdl(hdl, attr);
  330. }
  331. /* PAL call DkStreamAttributesQuerybyHandle: Query 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. DkStreamAttributesQuerybyHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  336. {
  337. store_frame(StreamAttributesQuerybyHandle);
  338. if (!handle || !attr) {
  339. notify_failure(PAL_ERROR_INVAL);
  340. return PAL_FALSE;
  341. }
  342. int ret = _DkStreamAttributesQuerybyHandle(handle, attr);
  343. if (ret < 0) {
  344. notify_failure (-ret);
  345. return PAL_FALSE;
  346. }
  347. return PAL_TRUE;
  348. }
  349. /* PAL call DkStreamAttributesSetbyHandle: Set attribute of a stream by
  350. its handle, attr is memory given by user space. Return the pointer of attr
  351. if succeeded, or NULL if failed. Error code is notified */
  352. PAL_BOL
  353. DkStreamAttributesSetbyHandle (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  354. {
  355. store_frame(StreamAttributesSetbyHandle);
  356. if (!handle || !attr) {
  357. notify_failure(PAL_ERROR_INVAL);
  358. return PAL_FALSE;
  359. }
  360. if (UNKNOWN_HANDLE(handle)) {
  361. notify_failure(PAL_ERROR_BADHANDLE);
  362. return PAL_FALSE;
  363. }
  364. const struct handle_ops * ops = HANDLE_OPS(handle);
  365. assert(ops);
  366. if (!ops->attrsetbyhdl) {
  367. notify_failure(PAL_ERROR_NOTSUPPORT);
  368. return PAL_FALSE;
  369. }
  370. int ret = ops->attrsetbyhdl(handle, attr);
  371. if (ret < 0) {
  372. notify_failure (-ret);
  373. return PAL_FALSE;
  374. }
  375. return PAL_TRUE;
  376. }
  377. /* PAL call DkStreamAttributesSetbyHandle: Set attribute of a stream by
  378. its handle, attr is memory given by user space. Return the pointer of attr
  379. if succeeded, or NULL if failed. Error code is notified */
  380. PAL_NUM DkStreamGetName (PAL_HANDLE handle, PAL_BUF buffer, PAL_NUM size)
  381. {
  382. store_frame(StreamGetName);
  383. if (!handle || !buffer || !size) {
  384. notify_failure(PAL_ERROR_INVAL);
  385. return 0;
  386. }
  387. if (UNKNOWN_HANDLE(handle)) {
  388. notify_failure(PAL_ERROR_BADHANDLE);
  389. return 0;
  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. notify_failure(PAL_ERROR_NOTSUPPORT);
  396. return 0;
  397. }
  398. int ret = ops->getname(handle, buffer, size - 1);
  399. if (ret < 0) {
  400. notify_failure(-ret);
  401. return 0;
  402. }
  403. ((char *)buffer)[ret] = 0;
  404. return ret;
  405. }
  406. /* _DkStreamMap for internal use. Map specific handle to certain memory,
  407. with given protection, offset and size */
  408. int _DkStreamMap (PAL_HANDLE handle, void ** paddr, int prot, int offset,
  409. size_t size)
  410. {
  411. void * addr = *paddr;
  412. int ret;
  413. const struct handle_ops * ops = HANDLE_OPS(handle);
  414. /* if ops or ops->map is not defined, the stream cannot be mapped */
  415. if (!ops || !ops->map)
  416. return -PAL_ERROR_NOTSUPPORT;
  417. if ((ret = ops->map(handle, &addr, prot, offset, size)) < 0)
  418. return ret;
  419. *paddr = addr;
  420. return 0;
  421. }
  422. bool check_memory_overlap (void * addr, size_t size);
  423. /* PAL call DkStreamMap: Map a stream of a given handle to certain memery
  424. space. prot/offset/size are the protection, offset and size of the memory
  425. mapping. Return the address if succeeded or NULL if failed. Error code
  426. is notified. */
  427. PAL_BUF
  428. DkStreamMap (PAL_HANDLE handle, PAL_BUF addr, PAL_FLG prot, PAL_NUM offset,
  429. PAL_NUM size)
  430. {
  431. store_frame(StreamMap);
  432. if (!handle) {
  433. notify_failure(PAL_ERROR_INVAL);
  434. return NULL;
  435. }
  436. if ((addr && !ALLOC_ALIGNED(addr)) || !size || !ALLOC_ALIGNED(size) ||
  437. !ALLOC_ALIGNED(offset)) {
  438. notify_failure(PAL_ERROR_INVAL);
  439. return NULL;
  440. }
  441. if (check_memory_overlap(addr, size)) {
  442. notify_failure(PAL_ERROR_DENIED);
  443. return NULL;
  444. }
  445. int ret = _DkStreamMap(handle, &addr, prot, offset, size);
  446. if (ret < 0) {
  447. notify_failure(-ret);
  448. return NULL;
  449. }
  450. return addr;
  451. }
  452. /* PAL call DkStreamUnmap: Unmap memory mapped at an address. The memory has
  453. to be a stream map, and it got unmapped as a whole memory area. No
  454. return value. Error code is notified */
  455. void DkStreamUnmap (PAL_BUF addr, PAL_NUM size)
  456. {
  457. store_frame(StreamUnmap);
  458. if (!addr) {
  459. notify_failure (PAL_ERROR_INVAL);
  460. return;
  461. }
  462. if (check_memory_overlap(addr, size)) {
  463. notify_failure(PAL_ERROR_DENIED);
  464. return;
  465. }
  466. int ret = _DkStreamUnmap(addr, size);
  467. if (ret < 0)
  468. notify_failure(ret);
  469. }
  470. /* _DkStreamSetLength for internal use. This function truncate the stream
  471. to certain length. This call might not be support for certain streams */
  472. int _DkStreamSetLength (PAL_HANDLE handle, size_t length)
  473. {
  474. if (UNKNOWN_HANDLE(handle))
  475. return -PAL_ERROR_BADHANDLE;
  476. const struct handle_ops * ops = HANDLE_OPS(handle);
  477. if (!ops || !ops->setlength)
  478. return -PAL_ERROR_NOTSUPPORT;
  479. return ops->setlength(handle, length);
  480. }
  481. /* PAL call DkStreamSetLength: Truncate the stream at certain length.
  482. Return the length if succeeded or 0 if failed. Error code is notified. */
  483. PAL_NUM
  484. DkStreamSetLength (PAL_HANDLE handle, PAL_NUM length)
  485. {
  486. store_frame(StreamSetLength);
  487. if (!handle) {
  488. notify_failure(PAL_ERROR_INVAL);
  489. return 0;
  490. }
  491. int ret = _DkStreamSetLength(handle, length);
  492. if (ret < 0) {
  493. notify_failure(-ret);
  494. return 0;
  495. }
  496. return ret;
  497. }
  498. /* _DkStreamFlush for internal use. This function sync up the handle with
  499. devices. Some streams may not support this operations. */
  500. int _DkStreamFlush (PAL_HANDLE handle)
  501. {
  502. if (UNKNOWN_HANDLE(handle))
  503. return -PAL_ERROR_BADHANDLE;
  504. const struct handle_ops * ops = HANDLE_OPS(handle);
  505. if (!ops || !ops->flush)
  506. return -PAL_ERROR_NOTSUPPORT;
  507. return ops->flush(handle);
  508. }
  509. /* PAL call DkStreamFlush: Sync up a stream of a given handle. No return
  510. value. Error code is notified. */
  511. PAL_BOL DkStreamFlush (PAL_HANDLE handle)
  512. {
  513. store_frame(StreamFlush);
  514. if (!handle) {
  515. notify_failure(PAL_ERROR_INVAL);
  516. return PAL_FALSE;
  517. }
  518. int ret = _DkStreamFlush(handle);
  519. if (ret < 0) {
  520. notify_failure(-ret);
  521. return PAL_FALSE;
  522. }
  523. return PAL_TRUE;
  524. }
  525. /* PAL call DkSendHandle: Write to a process handle.
  526. Return 1 on success and 0 on failure */
  527. PAL_BOL DkSendHandle(PAL_HANDLE handle, PAL_HANDLE cargo)
  528. {
  529. store_frame(SendHandle);
  530. // Return error if any of the handle is NULL
  531. if (!handle || !cargo) {
  532. notify_failure(PAL_ERROR_INVAL);
  533. return PAL_FALSE;
  534. }
  535. // Call the internal function after validating input args
  536. int ret = _DkSendHandle(handle, cargo);
  537. if (ret < 0) {
  538. notify_failure(-ret);
  539. return PAL_FALSE;
  540. }
  541. return PAL_TRUE;
  542. }
  543. /* PAL call DkRecvHandle: Read a handle to a pipe/process handle.
  544. Return the received PAL_HANDLE by reference and 0 on success and
  545. negative number on failure */
  546. /* 1. Should i take the received PAL_HANDLE as an input argument and
  547. pass by reference or return it rather?
  548. Ans - We are not aware of the size of the variable members to return
  549. 2. Would the recieved PAL_HANDLE start functioning automatically in
  550. the new process environment? Should we initialize/modify some
  551. attibutes of the handle?
  552. Ans - Yes, Initialize and make it compatibile in the target process
  553. 3. Should remalloc be done or the process shares the same references?
  554. Ans - Variables members have to allocated data again.
  555. */
  556. PAL_HANDLE DkReceiveHandle (PAL_HANDLE handle)
  557. {
  558. store_frame(ReceiveHandle);
  559. // return error if any of the handle is NULL
  560. if(handle == NULL) {
  561. notify_failure(PAL_ERROR_INVAL);
  562. return 0;
  563. }
  564. // create a reference for the received PAL_HANDLE
  565. union pal_handle cargo;
  566. // call the internal function after validating input args
  567. int ret = _DkReceiveHandle(handle, &cargo);
  568. // notify failure would have been called from other functions
  569. if(ret < 0) {
  570. return NULL;
  571. }
  572. return remalloc(&cargo, handle_size(&cargo));
  573. }
  574. PAL_BOL DkStreamChangeName (PAL_HANDLE hdl, PAL_STR uri)
  575. {
  576. struct handle_ops * ops = NULL;
  577. const char * type = NULL;
  578. int ret;
  579. if (uri) {
  580. ret = parse_stream_uri(&uri, &type, &ops);
  581. if (ret < 0) {
  582. notify_failure(-ret);
  583. return PAL_FALSE;
  584. }
  585. }
  586. const struct handle_ops * hops = HANDLE_OPS(hdl);
  587. if (!hops || !hops->rename || (ops && hops != ops)) {
  588. notify_failure(PAL_ERROR_NOTSUPPORT);
  589. return PAL_FALSE;
  590. }
  591. ret = hops->rename(hdl, type, uri);
  592. if (ret < 0) {
  593. notify_failure(-ret);
  594. return PAL_FALSE;
  595. }
  596. return PAL_TRUE;
  597. }
  598. /* _DkStreamRealpath is used to obtain the real path of a stream. Some
  599. streams may not have a real path. */
  600. const char * _DkStreamRealpath (PAL_HANDLE hdl)
  601. {
  602. const struct handle_ops * ops = HANDLE_OPS(hdl);
  603. if (!ops || !ops->getrealpath)
  604. return NULL;
  605. return ops->getrealpath(hdl);
  606. }