db_devices.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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. switch (p - u) {
  59. case 3:
  60. if (!memcmp(u, "tty", 3))
  61. dops = &term_ops;
  62. break;
  63. default:
  64. break;
  65. }
  66. if (!dops)
  67. return -PAL_ERROR_NOTSUPPORT;
  68. *uri = (*p) ? p + 1 : p;
  69. if (type)
  70. *type = u;
  71. if (ops)
  72. *ops = dops;
  73. return 0;
  74. }
  75. static inline void
  76. dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat);
  77. static int char_read (PAL_HANDLE handle, int offset, int count, void * buffer);
  78. static int char_write (PAL_HANDLE handle, int offset, int count,
  79. const void * buffer);
  80. static int term_attrquery (const char * type, const char * uri,
  81. PAL_STREAM_ATTR * attr);
  82. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  83. PAL_STREAM_ATTR * attr);
  84. /* Method to open standard terminal */
  85. static int open_standard_term (PAL_HANDLE * handle, const char * param,
  86. int access)
  87. {
  88. if (param)
  89. return -PAL_ERROR_NOTIMPLEMENTED;
  90. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
  91. SET_HANDLE_TYPE(hdl, dev);
  92. hdl->dev.dev_type = device_type_term;
  93. if (!(access & PAL_ACCESS_WRONLY)) {
  94. hdl->__in.flags |= RFD(0);
  95. hdl->dev.fd_in = 0;
  96. }
  97. if (access & (PAL_ACCESS_WRONLY|PAL_ACCESS_RDWR)) {
  98. hdl->__in.flags |= WFD(1);
  99. hdl->dev.fd_out = 1;
  100. }
  101. *handle = hdl;
  102. return 0;
  103. }
  104. /* 'open' operation for terminal stream */
  105. static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
  106. int access, int share, int create, int options)
  107. {
  108. const char * term = NULL;
  109. const char * param = NULL;
  110. const char * tmp = uri;
  111. while (*tmp) {
  112. if (!term && *tmp == '/')
  113. term = tmp + 1;
  114. if (*tmp == ',') {
  115. param = param + 1;
  116. break;
  117. }
  118. tmp++;
  119. }
  120. if (term)
  121. return -PAL_ERROR_NOTIMPLEMENTED;
  122. return open_standard_term(handle, param, access);
  123. }
  124. static int term_close (PAL_HANDLE handle)
  125. {
  126. return 0;
  127. }
  128. /* 'attrquery' operation for terminal stream */
  129. static int term_attrquery (const char * type, const char * uri,
  130. PAL_STREAM_ATTR * attr)
  131. {
  132. attr->type = pal_type_dev;
  133. attr->file_id = 0;
  134. attr->size = 0;
  135. attr->readable = PAL_TRUE;
  136. attr->writeable = PAL_TRUE;
  137. attr->runnable = PAL_FALSE;
  138. attr->access_time = 0;
  139. attr->change_time = 0;
  140. attr->create_time = 0;
  141. return 0;
  142. }
  143. /* 'attrquery' operation for terminal stream */
  144. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  145. PAL_STREAM_ATTR * attr)
  146. {
  147. attr->type = pal_type_dev;
  148. attr->file_id = 0;
  149. attr->size = 0;
  150. attr->readable = (hdl->dev.fd_in != PAL_IDX_POISON);
  151. attr->writeable = (hdl->dev.fd_out != PAL_IDX_POISON);
  152. attr->runnable = PAL_FALSE;
  153. attr->access_time = 0;
  154. attr->change_time = 0;
  155. attr->create_time = 0;
  156. return 0;
  157. }
  158. static struct handle_ops term_ops = {
  159. .open = &term_open,
  160. .close = &term_close,
  161. .read = &char_read,
  162. .write = &char_write,
  163. .attrquery = &term_attrquery,
  164. .attrquerybyhdl = &term_attrquerybyhdl,
  165. };
  166. /* 'read' operation for character streams. */
  167. static int char_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  168. {
  169. int fd = handle->dev.fd_in;
  170. if (fd == PAL_IDX_POISON)
  171. return -PAL_ERROR_DENIED;
  172. int bytes = INLINE_SYSCALL(read, 3, fd, buffer, size);
  173. if (IS_ERR(bytes))
  174. return unix_to_pal_error(ERRNO(bytes));
  175. return bytes;
  176. }
  177. /* 'write' operation for character streams. */
  178. static int char_write (PAL_HANDLE handle, int offset, int size,
  179. const void * buffer)
  180. {
  181. int fd = handle->dev.fd_out;
  182. if (fd == PAL_IDX_POISON)
  183. return -PAL_ERROR_DENIED;
  184. int bytes = INLINE_SYSCALL(write, 3, fd, buffer, size);
  185. if (IS_ERR(bytes))
  186. return unix_to_pal_error(ERRNO(bytes));
  187. return bytes;
  188. }
  189. /* 'open' operation for device streams */
  190. static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
  191. int access, int share, int create, int options)
  192. {
  193. struct handle_ops * ops = NULL;
  194. const char * dev_type = NULL;
  195. int ret = 0;
  196. ret = parse_device_uri(&uri, &dev_type, &ops);
  197. if (ret < 0)
  198. return ret;
  199. if (!ops->open)
  200. return -PAL_ERROR_NOTSUPPORT;
  201. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
  202. hdl->dev.fd_in = PAL_IDX_POISON;
  203. hdl->dev.fd_out = PAL_IDX_POISON;
  204. *handle = hdl;
  205. return ops->open(handle, dev_type, uri,
  206. access, share, create, options);
  207. }
  208. /* 'read' operation for device stream */
  209. static int dev_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  210. {
  211. const struct handle_ops * ops = DEVICE_OPS(handle);
  212. if (!ops || !ops->read)
  213. return -PAL_ERROR_NOTSUPPORT;
  214. return ops->read(handle, offset, size, buffer);
  215. }
  216. /* 'write' operation for device stream */
  217. static int dev_write (PAL_HANDLE handle, int offset, int size,
  218. const void * buffer)
  219. {
  220. const struct handle_ops * ops = DEVICE_OPS(handle);
  221. if (!ops || !ops->write)
  222. return -PAL_ERROR_NOTSUPPORT;
  223. return ops->write(handle, offset, size, buffer);
  224. }
  225. /* 'close' operation for device streams */
  226. static int dev_close (PAL_HANDLE handle)
  227. {
  228. const struct handle_ops * ops = DEVICE_OPS(handle);
  229. if (ops && ops->close)
  230. return ops->close(handle);
  231. if (handle->dev.fd_in != PAL_IDX_POISON) {
  232. int fd = handle->dev.fd_in;
  233. int ret = INLINE_SYSCALL(close, 1, fd);
  234. if (IS_ERR(ret)) {
  235. if (ERRNO(ret) != EBADF && ERRNO(ret) != EINVAL)
  236. return unix_to_pal_error(ERRNO(ret));
  237. }
  238. }
  239. if (handle->dev.fd_out != PAL_IDX_POISON) {
  240. int fd = handle->dev.fd_out;
  241. int ret = INLINE_SYSCALL(close, 1, fd);
  242. if (IS_ERR(ret)) {
  243. if (ERRNO(ret) != EBADF && ERRNO(ret) != EINVAL)
  244. return unix_to_pal_error(ERRNO(ret));
  245. }
  246. }
  247. if (handle->file.realpath)
  248. free((void *) handle->file.realpath);
  249. return 0;
  250. }
  251. /* 'delete' operation for device streams */
  252. static int dev_delete (PAL_HANDLE handle, int access)
  253. {
  254. const struct handle_ops * ops = DEVICE_OPS(handle);
  255. if (!ops || !ops->delete)
  256. return -PAL_ERROR_DENIED;
  257. int ret = dev_close(handle);
  258. if (ret < 0)
  259. return ret;
  260. return ops->delete(handle, access);
  261. }
  262. /* 'flush' operation for device streams */
  263. static int dev_flush (PAL_HANDLE handle)
  264. {
  265. const struct handle_ops * ops = DEVICE_OPS(handle);
  266. if (ops && ops->flush)
  267. return ops->flush(handle);
  268. /* try to flush input stream */
  269. if (handle->dev.fd_in != PAL_IDX_POISON) {
  270. int fd = handle->dev.fd_in;
  271. int ret = INLINE_SYSCALL(fsync, 1, fd);
  272. if (IS_ERR(ret)) {
  273. if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
  274. return -PAL_ERROR_BADHANDLE;
  275. else
  276. return unix_to_pal_error(ERRNO(ret));
  277. }
  278. }
  279. /* if output stream exists and does not equal to input stream,
  280. flush output stream as well */
  281. if (handle->dev.fd_out != PAL_IDX_POISON &&
  282. handle->dev.fd_out != handle->dev.fd_in) {
  283. int fd = handle->dev.fd_out;
  284. int ret = INLINE_SYSCALL(fsync, 1, fd);
  285. if (IS_ERR(ret)) {
  286. if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
  287. return -PAL_ERROR_BADHANDLE;
  288. else
  289. return unix_to_pal_error(ERRNO(ret));
  290. }
  291. }
  292. return 0;
  293. }
  294. static inline void
  295. dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
  296. {
  297. /* readable, writable and runnable are decied by euidstataccess */
  298. attr->readable = stataccess(stat, ACCESS_R);
  299. attr->writeable = stataccess(stat, ACCESS_W);
  300. attr->runnable = stataccess(stat, ACCESS_X);
  301. attr->type = pal_type_dev;
  302. attr->file_id = stat->st_ino;
  303. attr->size = stat->st_size;
  304. attr->access_time = stat->st_atime;
  305. attr->change_time = stat->st_mtime;
  306. attr->create_time = stat->st_ctime;
  307. }
  308. /* 'attrquery' operation for device streams */
  309. static int dev_attrquery (const char * type, const char * uri,
  310. PAL_STREAM_ATTR * attr)
  311. {
  312. struct handle_ops * ops = NULL;
  313. const char * dev_type = NULL;
  314. int ret = 0;
  315. ret = parse_device_uri(&uri, &dev_type, &ops);
  316. if (ret < 0)
  317. return ret;
  318. if (!ops || !ops->attrquery)
  319. return -PAL_ERROR_NOTSUPPORT;
  320. return ops->attrquery(dev_type, uri, attr);
  321. }
  322. /* 'attrquerybyhdl' operation for device stream */
  323. static int dev_attrquerybyhdl (PAL_HANDLE handle,
  324. PAL_STREAM_ATTR * attr)
  325. {
  326. const struct handle_ops * ops = DEVICE_OPS(handle);
  327. if (ops && ops->attrquerybyhdl)
  328. return ops->attrquerybyhdl(handle, attr);
  329. struct stat stat_buf, * stat_in = NULL, * stat_out = NULL;
  330. int ret;
  331. attr->type = pal_type_dev;
  332. attr->file_id = 0;
  333. if (handle->dev.fd_in != PAL_IDX_POISON) {
  334. ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
  335. if (!IS_ERR(ret))
  336. stat_in = &stat_buf;
  337. }
  338. attr->readable = (stat_in && stataccess(stat_in, ACCESS_R));
  339. attr->runnable = (stat_in && stataccess(stat_in, ACCESS_X));
  340. if (stat_in) {
  341. attr->size = stat_in->st_size;
  342. attr->access_time = stat_in->st_atime;
  343. attr->change_time = stat_in->st_mtime;
  344. attr->create_time = stat_in->st_ctime;
  345. }
  346. if (handle->dev.fd_in != PAL_IDX_POISON) {
  347. ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
  348. if (!IS_ERR(ret))
  349. stat_out = &stat_buf;
  350. }
  351. attr->writeable = (stat_out && stataccess(stat_out, ACCESS_W));
  352. if (!stat_in) {
  353. attr->size = stat_out ? stat_out->st_size : 0;
  354. attr->access_time = stat_out ? stat_out->st_atime : 0;
  355. attr->change_time = stat_out ? stat_out->st_mtime : 0;
  356. attr->create_time = stat_out ? stat_out->st_ctime : 0;
  357. }
  358. return 0;
  359. }
  360. static const char * dev_getrealpath (PAL_HANDLE handle)
  361. {
  362. return handle->dev.realpath;
  363. }
  364. struct handle_ops dev_ops = {
  365. .getrealpath = &dev_getrealpath,
  366. .open = &dev_open,
  367. .read = &dev_read,
  368. .write = &dev_write,
  369. .close = &dev_close,
  370. .delete = &dev_delete,
  371. .flush = &dev_flush,
  372. .attrquery = &dev_attrquery,
  373. .attrquerybyhdl = &dev_attrquerybyhdl,
  374. };