db_devices.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_device.c
  17. *
  18. * This file contains operands to handle streams with URIs that start with
  19. * "dev:".
  20. */
  21. #include "pal_defs.h"
  22. #include "pal_linux_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_linux.h"
  26. #include "pal_debug.h"
  27. #include "pal_error.h"
  28. #include "api.h"
  29. #include <linux/types.h>
  30. typedef __kernel_pid_t pid_t;
  31. #include <linux/stat.h>
  32. #include <asm/stat.h>
  33. #include <asm/fcntl.h>
  34. #include <asm/errno.h>
  35. #define DEVICE_OPS(handle) \
  36. ({ int _type = (handle)->dev.dev_type; \
  37. (_type <= 0 || _type >= PAL_DEVICE_TYPE_BOUND) ? \
  38. NULL : pal_device_ops[_type]; \
  39. })
  40. enum {
  41. device_type_none = 0,
  42. device_type_term,
  43. PAL_DEVICE_TYPE_BOUND,
  44. };
  45. static struct handle_ops term_ops;
  46. static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
  47. NULL,
  48. &term_ops,
  49. };
  50. /* parse-device_uri scan the uri, parse the prefix of the uri and search
  51. for stream handler wich will open or access the device */
  52. static int parse_device_uri (const char ** uri, const char ** type,
  53. struct handle_ops ** ops)
  54. {
  55. struct handle_ops * dops = NULL;
  56. const char * p, * u = (*uri);
  57. for (p = u ; (*p) && (*p) != ',' && (*p) != '/' ; p++);
  58. if (strpartcmp_static(u, "tty"))
  59. dops = &term_ops;
  60. if (!dops)
  61. return -PAL_ERROR_NOTSUPPORT;
  62. *uri = (*p) ? p + 1 : p;
  63. if (type)
  64. *type = u;
  65. if (ops)
  66. *ops = dops;
  67. return 0;
  68. }
  69. static inline void
  70. dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat);
  71. static int64_t char_read (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  72. void * buffer);
  73. static int64_t char_write (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  74. const void * buffer);
  75. static int term_attrquery (const char * type, const char * uri,
  76. PAL_STREAM_ATTR * attr);
  77. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  78. PAL_STREAM_ATTR * attr);
  79. /* Method to open standard terminal */
  80. static int open_standard_term (PAL_HANDLE * handle, const char * param,
  81. int access)
  82. {
  83. if (param)
  84. return -PAL_ERROR_NOTIMPLEMENTED;
  85. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
  86. SET_HANDLE_TYPE(hdl, dev);
  87. hdl->dev.dev_type = device_type_term;
  88. if (!(access & PAL_ACCESS_WRONLY)) {
  89. HANDLE_HDR(hdl)->flags |= RFD(0);
  90. hdl->dev.fd_in = 0;
  91. }
  92. if (access & (PAL_ACCESS_WRONLY|PAL_ACCESS_RDWR)) {
  93. HANDLE_HDR(hdl)->flags |= WFD(1);
  94. hdl->dev.fd_out = 1;
  95. }
  96. *handle = hdl;
  97. return 0;
  98. }
  99. /* 'open' operation for terminal stream */
  100. static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
  101. int access, int share, int create, int options)
  102. {
  103. const char * term = NULL;
  104. const char * param = NULL;
  105. const char * tmp = uri;
  106. while (*tmp) {
  107. if (!term && *tmp == '/')
  108. term = tmp + 1;
  109. if (*tmp == ',') {
  110. param = param + 1;
  111. break;
  112. }
  113. tmp++;
  114. }
  115. if (term)
  116. return -PAL_ERROR_NOTIMPLEMENTED;
  117. return open_standard_term(handle, param, access);
  118. }
  119. static int term_close (PAL_HANDLE handle)
  120. {
  121. return 0;
  122. }
  123. /* 'attrquery' operation for terminal stream */
  124. static int term_attrquery (const char * type, const char * uri,
  125. PAL_STREAM_ATTR * attr)
  126. {
  127. attr->handle_type = pal_type_dev;
  128. attr->readable = PAL_TRUE;
  129. attr->writeable = PAL_TRUE;
  130. attr->runnable = PAL_FALSE;
  131. attr->pending_size = 0;
  132. return 0;
  133. }
  134. /* 'attrquery' operation for terminal stream */
  135. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  136. PAL_STREAM_ATTR * attr)
  137. {
  138. attr->handle_type = pal_type_dev;
  139. attr->readable = (hdl->dev.fd_in != PAL_IDX_POISON);
  140. attr->writeable = (hdl->dev.fd_out != PAL_IDX_POISON);
  141. attr->runnable = PAL_FALSE;
  142. attr->pending_size = 0;
  143. return 0;
  144. }
  145. static struct handle_ops term_ops = {
  146. .open = &term_open,
  147. .close = &term_close,
  148. .read = &char_read,
  149. .write = &char_write,
  150. .attrquery = &term_attrquery,
  151. .attrquerybyhdl = &term_attrquerybyhdl,
  152. };
  153. /* 'read' operation for character streams. */
  154. static int64_t char_read (PAL_HANDLE handle, uint64_t offset, uint64_t size,
  155. void * buffer)
  156. {
  157. int fd = handle->dev.fd_in;
  158. if (fd == PAL_IDX_POISON)
  159. return -PAL_ERROR_DENIED;
  160. int64_t bytes = INLINE_SYSCALL(read, 3, fd, buffer, size);
  161. if (IS_ERR(bytes))
  162. return unix_to_pal_error(ERRNO(bytes));
  163. return bytes;
  164. }
  165. /* 'write' operation for character streams. */
  166. static int64_t char_write (PAL_HANDLE handle, uint64_t offset, uint64_t size,
  167. const void * buffer)
  168. {
  169. int fd = handle->dev.fd_out;
  170. if (fd == PAL_IDX_POISON)
  171. return -PAL_ERROR_DENIED;
  172. int64_t bytes = INLINE_SYSCALL(write, 3, fd, buffer, size);
  173. if (IS_ERR(bytes))
  174. return unix_to_pal_error(ERRNO(bytes));
  175. return bytes;
  176. }
  177. /* 'open' operation for device streams */
  178. static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
  179. int access, int share, int create, int options)
  180. {
  181. struct handle_ops * ops = NULL;
  182. const char * dev_type = NULL;
  183. int ret = 0;
  184. ret = parse_device_uri(&uri, &dev_type, &ops);
  185. if (ret < 0)
  186. return ret;
  187. if (!ops->open)
  188. return -PAL_ERROR_NOTSUPPORT;
  189. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
  190. SET_HANDLE_TYPE(hdl, dev);
  191. hdl->dev.fd_in = PAL_IDX_POISON;
  192. hdl->dev.fd_out = PAL_IDX_POISON;
  193. *handle = hdl;
  194. return ops->open(handle, dev_type, uri,
  195. access, share, create, options);
  196. }
  197. /* 'read' operation for device stream */
  198. static int64_t dev_read (PAL_HANDLE handle, uint64_t offset, uint64_t size,
  199. void * buffer)
  200. {
  201. const struct handle_ops * ops = DEVICE_OPS(handle);
  202. if (!ops || !ops->read)
  203. return -PAL_ERROR_NOTSUPPORT;
  204. return ops->read(handle, offset, size, buffer);
  205. }
  206. /* 'write' operation for device stream */
  207. static int64_t dev_write (PAL_HANDLE handle, uint64_t offset, uint64_t size,
  208. const void * buffer)
  209. {
  210. const struct handle_ops * ops = DEVICE_OPS(handle);
  211. if (!ops || !ops->write)
  212. return -PAL_ERROR_NOTSUPPORT;
  213. return ops->write(handle, offset, size, buffer);
  214. }
  215. /* 'close' operation for device streams */
  216. static int dev_close (PAL_HANDLE handle)
  217. {
  218. const struct handle_ops * ops = DEVICE_OPS(handle);
  219. if (ops && ops->close)
  220. return ops->close(handle);
  221. if (handle->dev.fd_in != PAL_IDX_POISON) {
  222. int fd = handle->dev.fd_in;
  223. int ret = INLINE_SYSCALL(close, 1, fd);
  224. if (IS_ERR(ret)) {
  225. if (ERRNO(ret) != EBADF && ERRNO(ret) != EINVAL)
  226. return unix_to_pal_error(ERRNO(ret));
  227. }
  228. }
  229. if (handle->dev.fd_out != PAL_IDX_POISON) {
  230. int fd = handle->dev.fd_out;
  231. int ret = INLINE_SYSCALL(close, 1, fd);
  232. if (IS_ERR(ret)) {
  233. if (ERRNO(ret) != EBADF && ERRNO(ret) != EINVAL)
  234. return unix_to_pal_error(ERRNO(ret));
  235. }
  236. }
  237. if (handle->file.realpath)
  238. free((void *) handle->file.realpath);
  239. return 0;
  240. }
  241. /* 'delete' operation for device streams */
  242. static int dev_delete (PAL_HANDLE handle, int access)
  243. {
  244. const struct handle_ops * ops = DEVICE_OPS(handle);
  245. if (!ops || !ops->delete)
  246. return -PAL_ERROR_DENIED;
  247. int ret = dev_close(handle);
  248. if (ret < 0)
  249. return ret;
  250. return ops->delete(handle, access);
  251. }
  252. /* 'flush' operation for device streams */
  253. static int dev_flush (PAL_HANDLE handle)
  254. {
  255. const struct handle_ops * ops = DEVICE_OPS(handle);
  256. if (ops && ops->flush)
  257. return ops->flush(handle);
  258. /* try to flush input stream */
  259. if (handle->dev.fd_in != PAL_IDX_POISON) {
  260. int fd = handle->dev.fd_in;
  261. int ret = INLINE_SYSCALL(fsync, 1, fd);
  262. if (IS_ERR(ret)) {
  263. if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
  264. return -PAL_ERROR_BADHANDLE;
  265. else
  266. return unix_to_pal_error(ERRNO(ret));
  267. }
  268. }
  269. /* if output stream exists and does not equal to input stream,
  270. flush output stream as well */
  271. if (handle->dev.fd_out != PAL_IDX_POISON &&
  272. handle->dev.fd_out != handle->dev.fd_in) {
  273. int fd = handle->dev.fd_out;
  274. int ret = INLINE_SYSCALL(fsync, 1, fd);
  275. if (IS_ERR(ret)) {
  276. if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
  277. return -PAL_ERROR_BADHANDLE;
  278. else
  279. return unix_to_pal_error(ERRNO(ret));
  280. }
  281. }
  282. return 0;
  283. }
  284. static inline void
  285. dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
  286. {
  287. attr->handle_type = pal_type_dev;
  288. /* readable, writable and runnable are decied by euidstataccess */
  289. attr->readable = stataccess(stat, ACCESS_R);
  290. attr->writeable = stataccess(stat, ACCESS_W);
  291. attr->runnable = stataccess(stat, ACCESS_X);
  292. attr->pending_size = stat->st_size;
  293. }
  294. /* 'attrquery' operation for device streams */
  295. static int dev_attrquery (const char * type, const char * uri,
  296. PAL_STREAM_ATTR * attr)
  297. {
  298. struct handle_ops * ops = NULL;
  299. const char * dev_type = NULL;
  300. int ret = 0;
  301. ret = parse_device_uri(&uri, &dev_type, &ops);
  302. if (ret < 0)
  303. return ret;
  304. if (!ops || !ops->attrquery)
  305. return -PAL_ERROR_NOTSUPPORT;
  306. return ops->attrquery(dev_type, uri, attr);
  307. }
  308. /* 'attrquerybyhdl' operation for device stream */
  309. static int dev_attrquerybyhdl (PAL_HANDLE handle,
  310. PAL_STREAM_ATTR * attr)
  311. {
  312. const struct handle_ops * ops = DEVICE_OPS(handle);
  313. if (ops && ops->attrquerybyhdl)
  314. return ops->attrquerybyhdl(handle, attr);
  315. struct stat stat_buf, * stat_in = NULL, * stat_out = NULL;
  316. int ret;
  317. attr->handle_type = pal_type_dev;
  318. if (handle->dev.fd_in != PAL_IDX_POISON) {
  319. ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
  320. if (!IS_ERR(ret))
  321. stat_in = &stat_buf;
  322. }
  323. if (handle->dev.fd_in != PAL_IDX_POISON) {
  324. ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
  325. if (!IS_ERR(ret))
  326. stat_out = &stat_buf;
  327. }
  328. attr->readable = (stat_in && stataccess(stat_in, ACCESS_R));
  329. attr->runnable = (stat_in && stataccess(stat_in, ACCESS_X));
  330. attr->writeable = (stat_out && stataccess(stat_out, ACCESS_W));
  331. attr->pending_size = stat_in ? stat_in->st_size :
  332. (stat_out ? stat_out->st_size : 0);
  333. return 0;
  334. }
  335. static const char * dev_getrealpath (PAL_HANDLE handle)
  336. {
  337. return handle->dev.realpath;
  338. }
  339. struct handle_ops dev_ops = {
  340. .getrealpath = &dev_getrealpath,
  341. .open = &dev_open,
  342. .read = &dev_read,
  343. .write = &dev_write,
  344. .close = &dev_close,
  345. .delete = &dev_delete,
  346. .flush = &dev_flush,
  347. .attrquery = &dev_attrquery,
  348. .attrquerybyhdl = &dev_attrquerybyhdl,
  349. };