db_devices.c 11 KB

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