db_streams.c 22 KB

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