db_devices.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 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, 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 = u;
  64. if (ops)
  65. *ops = dops;
  66. return 0;
  67. }
  68. static inline void dev_attrcopy(PAL_STREAM_ATTR* attr, struct stat* stat);
  69. static int64_t char_read(PAL_HANDLE handle, uint64_t offset, uint64_t count, void* buffer);
  70. static int64_t char_write(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buffer);
  71. static int term_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr);
  72. static int term_attrquerybyhdl(PAL_HANDLE hdl, PAL_STREAM_ATTR* attr);
  73. /* Method to open standard terminal */
  74. static int open_standard_term(PAL_HANDLE* handle, const char* param, int access) {
  75. if (param)
  76. return -PAL_ERROR_NOTIMPLEMENTED;
  77. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
  78. SET_HANDLE_TYPE(hdl, dev);
  79. hdl->dev.dev_type = device_type_term;
  80. if (!(access & PAL_ACCESS_WRONLY)) {
  81. HANDLE_HDR(hdl)->flags |= RFD(0);
  82. hdl->dev.fd_in = 0;
  83. }
  84. if (access & (PAL_ACCESS_WRONLY | PAL_ACCESS_RDWR)) {
  85. HANDLE_HDR(hdl)->flags |= WFD(1);
  86. hdl->dev.fd_out = 1;
  87. }
  88. *handle = hdl;
  89. return 0;
  90. }
  91. /* 'open' operation for terminal stream */
  92. static int term_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  93. int create, int options) {
  94. const char* term = NULL;
  95. const char* param = NULL;
  96. const char* tmp = uri;
  97. while (*tmp) {
  98. if (!term && *tmp == '/')
  99. term = tmp + 1;
  100. if (*tmp == ',') {
  101. param = param + 1;
  102. break;
  103. }
  104. tmp++;
  105. }
  106. if (term)
  107. return -PAL_ERROR_NOTIMPLEMENTED;
  108. return open_standard_term(handle, param, access);
  109. }
  110. static int term_close(PAL_HANDLE handle) {
  111. return 0;
  112. }
  113. /* 'attrquery' operation for terminal stream */
  114. static int term_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr) {
  115. attr->handle_type = pal_type_dev;
  116. attr->readable = PAL_TRUE;
  117. attr->writeable = PAL_TRUE;
  118. attr->runnable = PAL_FALSE;
  119. attr->pending_size = 0;
  120. return 0;
  121. }
  122. /* 'attrquery' operation for terminal stream */
  123. static int term_attrquerybyhdl(PAL_HANDLE hdl, PAL_STREAM_ATTR* attr) {
  124. attr->handle_type = pal_type_dev;
  125. attr->readable = (hdl->dev.fd_in != PAL_IDX_POISON);
  126. attr->writeable = (hdl->dev.fd_out != PAL_IDX_POISON);
  127. attr->runnable = PAL_FALSE;
  128. attr->pending_size = 0;
  129. return 0;
  130. }
  131. static struct handle_ops term_ops = {
  132. .open = &term_open,
  133. .close = &term_close,
  134. .read = &char_read,
  135. .write = &char_write,
  136. .attrquery = &term_attrquery,
  137. .attrquerybyhdl = &term_attrquerybyhdl,
  138. };
  139. /* 'read' operation for character streams. */
  140. static int64_t char_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* buffer) {
  141. int fd = handle->dev.fd_in;
  142. if (fd == PAL_IDX_POISON)
  143. return -PAL_ERROR_DENIED;
  144. int64_t bytes = INLINE_SYSCALL(read, 3, fd, buffer, size);
  145. if (IS_ERR(bytes))
  146. return unix_to_pal_error(ERRNO(bytes));
  147. return bytes;
  148. }
  149. /* 'write' operation for character streams. */
  150. static int64_t char_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, const void* buffer) {
  151. int fd = handle->dev.fd_out;
  152. if (fd == PAL_IDX_POISON)
  153. return -PAL_ERROR_DENIED;
  154. int64_t bytes = INLINE_SYSCALL(write, 3, fd, buffer, size);
  155. if (IS_ERR(bytes))
  156. return unix_to_pal_error(ERRNO(bytes));
  157. return bytes;
  158. }
  159. /* 'open' operation for device streams */
  160. static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  161. int create, int options) {
  162. struct handle_ops* ops = NULL;
  163. const char* dev_type = NULL;
  164. int ret = 0;
  165. ret = parse_device_uri(&uri, &dev_type, &ops);
  166. if (ret < 0)
  167. return ret;
  168. if (!ops->open)
  169. return -PAL_ERROR_NOTSUPPORT;
  170. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
  171. SET_HANDLE_TYPE(hdl, dev);
  172. hdl->dev.fd_in = PAL_IDX_POISON;
  173. hdl->dev.fd_out = PAL_IDX_POISON;
  174. *handle = hdl;
  175. return ops->open(handle, dev_type, uri, access, share, create, options);
  176. }
  177. /* 'read' operation for device stream */
  178. static int64_t dev_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* buffer) {
  179. const struct handle_ops* ops = DEVICE_OPS(handle);
  180. if (!ops || !ops->read)
  181. return -PAL_ERROR_NOTSUPPORT;
  182. return ops->read(handle, offset, size, buffer);
  183. }
  184. /* 'write' operation for device stream */
  185. static int64_t dev_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, const void* buffer) {
  186. const struct handle_ops* ops = DEVICE_OPS(handle);
  187. if (!ops || !ops->write)
  188. return -PAL_ERROR_NOTSUPPORT;
  189. return ops->write(handle, offset, size, buffer);
  190. }
  191. /* 'close' operation for device streams */
  192. static int dev_close(PAL_HANDLE handle) {
  193. const struct handle_ops* ops = DEVICE_OPS(handle);
  194. if (ops && ops->close)
  195. return ops->close(handle);
  196. if (handle->dev.fd_in != PAL_IDX_POISON) {
  197. int fd = handle->dev.fd_in;
  198. int ret = INLINE_SYSCALL(close, 1, fd);
  199. if (IS_ERR(ret)) {
  200. if (ERRNO(ret) != EBADF && ERRNO(ret) != EINVAL)
  201. return unix_to_pal_error(ERRNO(ret));
  202. }
  203. }
  204. if (handle->dev.fd_out != PAL_IDX_POISON) {
  205. int fd = handle->dev.fd_out;
  206. int ret = INLINE_SYSCALL(close, 1, fd);
  207. if (IS_ERR(ret)) {
  208. if (ERRNO(ret) != EBADF && ERRNO(ret) != EINVAL)
  209. return unix_to_pal_error(ERRNO(ret));
  210. }
  211. }
  212. if (handle->file.realpath)
  213. free((void*)handle->file.realpath);
  214. return 0;
  215. }
  216. /* 'delete' operation for device streams */
  217. static int dev_delete(PAL_HANDLE handle, int access) {
  218. const struct handle_ops* ops = DEVICE_OPS(handle);
  219. if (!ops || !ops->delete)
  220. return -PAL_ERROR_DENIED;
  221. int ret = dev_close(handle);
  222. if (ret < 0)
  223. return ret;
  224. return ops->delete (handle, access);
  225. }
  226. /* 'flush' operation for device streams */
  227. static int dev_flush(PAL_HANDLE handle) {
  228. const struct handle_ops* ops = DEVICE_OPS(handle);
  229. if (ops && ops->flush)
  230. return ops->flush(handle);
  231. /* try to flush input stream */
  232. if (handle->dev.fd_in != PAL_IDX_POISON) {
  233. int fd = handle->dev.fd_in;
  234. int ret = INLINE_SYSCALL(fsync, 1, fd);
  235. if (IS_ERR(ret)) {
  236. if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
  237. return -PAL_ERROR_BADHANDLE;
  238. else
  239. return unix_to_pal_error(ERRNO(ret));
  240. }
  241. }
  242. /* if output stream exists and does not equal to input stream,
  243. flush output stream as well */
  244. if (handle->dev.fd_out != PAL_IDX_POISON && handle->dev.fd_out != handle->dev.fd_in) {
  245. int fd = handle->dev.fd_out;
  246. int ret = INLINE_SYSCALL(fsync, 1, fd);
  247. if (IS_ERR(ret)) {
  248. if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
  249. return -PAL_ERROR_BADHANDLE;
  250. else
  251. return unix_to_pal_error(ERRNO(ret));
  252. }
  253. }
  254. return 0;
  255. }
  256. static inline void dev_attrcopy(PAL_STREAM_ATTR* attr, struct stat* stat) {
  257. attr->handle_type = pal_type_dev;
  258. /* readable, writable and runnable are decied by euidstataccess */
  259. attr->readable = stataccess(stat, ACCESS_R);
  260. attr->writeable = stataccess(stat, ACCESS_W);
  261. attr->runnable = stataccess(stat, ACCESS_X);
  262. attr->pending_size = stat->st_size;
  263. }
  264. /* 'attrquery' operation for device streams */
  265. static int dev_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr) {
  266. struct handle_ops* ops = NULL;
  267. const char* dev_type = NULL;
  268. int ret = 0;
  269. ret = parse_device_uri(&uri, &dev_type, &ops);
  270. if (ret < 0)
  271. return ret;
  272. if (!ops || !ops->attrquery)
  273. return -PAL_ERROR_NOTSUPPORT;
  274. return ops->attrquery(dev_type, uri, attr);
  275. }
  276. /* 'attrquerybyhdl' operation for device stream */
  277. static int dev_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  278. const struct handle_ops* ops = DEVICE_OPS(handle);
  279. if (ops && ops->attrquerybyhdl)
  280. return ops->attrquerybyhdl(handle, attr);
  281. struct stat stat_buf, *stat_in = NULL, *stat_out = NULL;
  282. int ret;
  283. attr->handle_type = pal_type_dev;
  284. if (handle->dev.fd_in != PAL_IDX_POISON) {
  285. ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
  286. if (!IS_ERR(ret))
  287. stat_in = &stat_buf;
  288. }
  289. if (handle->dev.fd_in != PAL_IDX_POISON) {
  290. ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
  291. if (!IS_ERR(ret))
  292. stat_out = &stat_buf;
  293. }
  294. attr->readable = (stat_in && stataccess(stat_in, ACCESS_R));
  295. attr->runnable = (stat_in && stataccess(stat_in, ACCESS_X));
  296. attr->writeable = (stat_out && stataccess(stat_out, ACCESS_W));
  297. attr->pending_size = stat_in ? stat_in->st_size : (stat_out ? stat_out->st_size : 0);
  298. return 0;
  299. }
  300. static const char* dev_getrealpath(PAL_HANDLE handle) {
  301. return handle->dev.realpath;
  302. }
  303. struct handle_ops dev_ops = {
  304. .getrealpath = &dev_getrealpath,
  305. .open = &dev_open,
  306. .read = &dev_read,
  307. .write = &dev_write,
  308. .close = &dev_close,
  309. .delete = &dev_delete,
  310. .flush = &dev_flush,
  311. .attrquery = &dev_attrquery,
  312. .attrquerybyhdl = &dev_attrquerybyhdl,
  313. };