db_devices.c 12 KB

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