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