db_devices.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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_freebsd_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_freebsd.h"
  26. #include "pal_debug.h"
  27. #include "pal_error.h"
  28. #include "api.h"
  29. #include <sys/types.h>
  30. typedef __kernel_pid_t pid_t;
  31. #include <sys/stat.h>
  32. #include <errno.h>
  33. #define DEVICE_OPS(handle) \
  34. ({ int _type = (handle)->dev.dev_type; \
  35. (_type <= 0 || _type >= PAL_DEVICE_TYPE_BOUND) ? \
  36. NULL : pal_device_ops[_type]; \
  37. })
  38. enum {
  39. device_type_none = 0,
  40. device_type_term,
  41. PAL_DEVICE_TYPE_BOUND,
  42. };
  43. static struct handle_ops term_ops;
  44. static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
  45. NULL,
  46. &term_ops,
  47. };
  48. /* parse_device_uri scans the uri, parses the prefix of the uri and searches
  49. for stream handler wich will open or access the device. */
  50. static int parse_device_uri(const char ** uri, char ** type, struct handle_ops ** ops)
  51. {
  52. struct handle_ops * dops = NULL;
  53. const char * p, * u = (*uri);
  54. for (p = u ; (*p) && (*p) != ',' && (*p) != '/' ; p++);
  55. if (strpartcmp_static(u, "tty"))
  56. dops = &term_ops;
  57. if (!dops)
  58. return -PAL_ERROR_NOTSUPPORT;
  59. *uri = (*p) ? p + 1 : p;
  60. if (type) {
  61. *type = malloc_copy(u, p - u + 1);
  62. if (!*type)
  63. return -PAL_ERROR_NOMEM;
  64. (*type)[p - u] = '\0';
  65. }
  66. if (ops)
  67. *ops = dops;
  68. return 0;
  69. }
  70. static inline void
  71. dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat);
  72. static int char_read (PAL_HANDLE handle, int offset, int count, void * buffer);
  73. static int char_write (PAL_HANDLE handle, int offset, int count,
  74. const void * buffer);
  75. static int term_attrquery (const char * type, const char * uri,
  76. PAL_STREAM_ATTR * attr);
  77. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  78. PAL_STREAM_ATTR * attr);
  79. /* Method to open standard terminal */
  80. static int open_standard_term (PAL_HANDLE * handle, const char * param,
  81. int access)
  82. {
  83. if (param)
  84. return -PAL_ERROR_NOTIMPLEMENTED;
  85. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
  86. SET_HANDLE_TYPE(hdl, dev);
  87. hdl->dev.dev_type = device_type_term;
  88. if (!(access & PAL_ACCESS_WRONLY)) {
  89. hdl->hdr.flags |= RFD(0);
  90. hdl->dev.fd_in = 0;
  91. }
  92. if (access & (PAL_ACCESS_WRONLY|PAL_ACCESS_RDWR)) {
  93. hdl->hdr.flags |= WFD(1);
  94. hdl->dev.fd_out = 1;
  95. }
  96. *handle = hdl;
  97. return 0;
  98. }
  99. /* 'open' operation for terminal stream */
  100. static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
  101. int access, int share, int create, int options)
  102. {
  103. const char * term = NULL;
  104. const char * param = NULL;
  105. const char * tmp = uri;
  106. while (*tmp) {
  107. if (!term && *tmp == '/')
  108. term = tmp + 1;
  109. if (*tmp == ',') {
  110. param = param + 1;
  111. break;
  112. }
  113. tmp++;
  114. }
  115. if (term)
  116. return -PAL_ERROR_NOTIMPLEMENTED;
  117. return open_standard_term(handle, param, access);
  118. }
  119. static int term_close (PAL_HANDLE handle)
  120. {
  121. return 0;
  122. }
  123. /* 'attrquery' operation for terminal stream */
  124. static int term_attrquery (const char * type, const char * uri,
  125. PAL_STREAM_ATTR * attr)
  126. {
  127. attr->handle_type = pal_type_dev;
  128. attr->readable = PAL_TRUE;
  129. attr->writable = PAL_TRUE;
  130. attr->runnable = PAL_FALSE;
  131. attr->pending_size = 0;
  132. return 0;
  133. }
  134. /* 'attrquery' operation for terminal stream */
  135. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  136. PAL_STREAM_ATTR * attr)
  137. {
  138. attr->handle_type = pal_type_dev;
  139. attr->readable = (hdl->dev.fd_in != PAL_IDX_POISON);
  140. attr->writable = (hdl->dev.fd_out != PAL_IDX_POISON);
  141. attr->runnable = PAL_FALSE;
  142. attr->pending_size = 0;
  143. return 0;
  144. }
  145. static struct handle_ops term_ops = {
  146. .open = &term_open,
  147. .close = &term_close,
  148. .read = &char_read,
  149. .write = &char_write,
  150. .attrquery = &term_attrquery,
  151. .attrquerybyhdl = &term_attrquerybyhdl,
  152. };
  153. /* 'read' operation for character streams. */
  154. static int char_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  155. {
  156. int fd = handle->dev.fd_in;
  157. if (fd == PAL_IDX_POISON)
  158. return -PAL_ERROR_DENIED;
  159. int bytes = INLINE_SYSCALL(read, 3, fd, buffer, size);
  160. if (IS_ERR(bytes))
  161. return unix_to_pal_error(ERRNO(bytes));
  162. return bytes;
  163. }
  164. /* 'write' operation for character streams. */
  165. static int char_write (PAL_HANDLE handle, int offset, int size,
  166. const void * buffer)
  167. {
  168. int fd = handle->dev.fd_out;
  169. if (fd == PAL_IDX_POISON)
  170. return -PAL_ERROR_DENIED;
  171. int bytes = INLINE_SYSCALL(write, 3, fd, buffer, size);
  172. if (IS_ERR(bytes))
  173. return unix_to_pal_error(ERRNO(bytes));
  174. return bytes;
  175. }
  176. /* 'open' operation for device streams */
  177. static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
  178. int access, int share, int create, int options)
  179. {
  180. struct handle_ops * ops = NULL;
  181. char * dev_type = NULL;
  182. int ret = 0;
  183. ret = parse_device_uri(&uri, &dev_type, &ops);
  184. if (ret < 0)
  185. return ret;
  186. if (!ops->open)
  187. return -PAL_ERROR_NOTSUPPORT;
  188. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
  189. hdl->dev.fd_in = PAL_IDX_POISON;
  190. hdl->dev.fd_out = PAL_IDX_POISON;
  191. *handle = hdl;
  192. ret = ops->open(handle, dev_type, uri,
  193. access, share, create, options);
  194. free(dev_type);
  195. return ret;
  196. }
  197. /* 'read' operation for device stream */
  198. static int dev_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  199. {
  200. const struct handle_ops * ops = DEVICE_OPS(handle);
  201. if (!ops || !ops->read)
  202. return -PAL_ERROR_NOTSUPPORT;
  203. return ops->read(handle, offset, size, buffer);
  204. }
  205. /* 'write' operation for device stream */
  206. static int dev_write (PAL_HANDLE handle, int offset, int size,
  207. const void * buffer)
  208. {
  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. {
  217. const struct handle_ops * ops = DEVICE_OPS(handle);
  218. if (ops && ops->close)
  219. return ops->close(handle);
  220. if (handle->dev.fd_in != PAL_IDX_POISON) {
  221. int fd = handle->dev.fd_in;
  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->dev.fd_out != PAL_IDX_POISON) {
  229. int fd = handle->dev.fd_out;
  230. int ret = INLINE_SYSCALL(close, 1, fd);
  231. if (IS_ERR(ret)) {
  232. if (ERRNO(ret) != EBADF && ERRNO(ret) != EINVAL)
  233. return unix_to_pal_error(ERRNO(ret));
  234. }
  235. }
  236. if (handle->file.realpath)
  237. free((void *) handle->file.realpath);
  238. return 0;
  239. }
  240. /* 'delete' operation for device streams */
  241. static int dev_delete (PAL_HANDLE handle, int access)
  242. {
  243. const struct handle_ops * ops = DEVICE_OPS(handle);
  244. if (!ops || !ops->delete)
  245. return -PAL_ERROR_DENIED;
  246. int ret = dev_close(handle);
  247. if (ret < 0)
  248. return ret;
  249. return ops->delete(handle, access);
  250. }
  251. /* 'flush' operation for device streams */
  252. static int dev_flush (PAL_HANDLE handle)
  253. {
  254. const struct handle_ops * ops = DEVICE_OPS(handle);
  255. if (ops && ops->flush)
  256. return ops->flush(handle);
  257. /* try to flush input stream */
  258. if (handle->dev.fd_in != PAL_IDX_POISON) {
  259. int fd = handle->dev.fd_in;
  260. int ret = INLINE_SYSCALL(fsync, 1, fd);
  261. if (IS_ERR(ret)) {
  262. if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
  263. return -PAL_ERROR_BADHANDLE;
  264. else
  265. return unix_to_pal_error(ERRNO(ret));
  266. }
  267. }
  268. /* if output stream exists and does not equal to input stream,
  269. flush output stream as well */
  270. if (handle->dev.fd_out != PAL_IDX_POISON &&
  271. handle->dev.fd_out != handle->dev.fd_in) {
  272. int fd = handle->dev.fd_out;
  273. int ret = INLINE_SYSCALL(fsync, 1, fd);
  274. if (IS_ERR(ret)) {
  275. if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
  276. return -PAL_ERROR_BADHANDLE;
  277. else
  278. return unix_to_pal_error(ERRNO(ret));
  279. }
  280. }
  281. return 0;
  282. }
  283. static inline void
  284. dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
  285. {
  286. attr->handle_type = pal_type_dev;
  287. /* readable, writable and runnable are decied by euidstataccess */
  288. attr->readable = stataccess(stat, ACCESS_R);
  289. attr->writable = stataccess(stat, ACCESS_W);
  290. attr->runnable = stataccess(stat, ACCESS_X);
  291. attr->pending_size = stat->st_size;
  292. }
  293. /* 'attrquery' operation for device streams */
  294. static int dev_attrquery (const char * type, const char * uri,
  295. PAL_STREAM_ATTR * attr)
  296. {
  297. struct handle_ops * ops = NULL;
  298. const char * dev_type = NULL;
  299. int ret = 0;
  300. ret = parse_device_uri(&uri, &dev_type, &ops);
  301. if (ret < 0)
  302. return ret;
  303. if (!ops || !ops->attrquery)
  304. return -PAL_ERROR_NOTSUPPORT;
  305. return ops->attrquery(dev_type, uri, attr);
  306. }
  307. /* 'attrquerybyhdl' operation for device stream */
  308. static int dev_attrquerybyhdl (PAL_HANDLE handle,
  309. PAL_STREAM_ATTR * attr)
  310. {
  311. const struct handle_ops * ops = DEVICE_OPS(handle);
  312. if (ops && ops->attrquerybyhdl)
  313. return ops->attrquerybyhdl(handle, attr);
  314. struct stat stat_buf, * stat_in = NULL, * stat_out = NULL;
  315. int ret;
  316. attr->handle_type = pal_type_dev;
  317. if (handle->dev.fd_in != PAL_IDX_POISON) {
  318. ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
  319. if (!IS_ERR(ret))
  320. stat_in = &stat_buf;
  321. }
  322. if (handle->dev.fd_in != PAL_IDX_POISON) {
  323. ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
  324. if (!IS_ERR(ret))
  325. stat_out = &stat_buf;
  326. }
  327. attr->readable = (stat_in && stataccess(stat_in, ACCESS_R));
  328. attr->runnable = (stat_in && stataccess(stat_in, ACCESS_X));
  329. attr->writable = (stat_out && stataccess(stat_out, ACCESS_W));
  330. attr->pending_size = stat_in ? stat_in->st_size :
  331. (stat_out ? stat_out->st_size : 0);
  332. return 0;
  333. }
  334. static const char * dev_getrealpath (PAL_HANDLE handle)
  335. {
  336. return handle->dev.realpath;
  337. }
  338. struct handle_ops dev_ops = {
  339. .getrealpath = &dev_getrealpath,
  340. .open = &dev_open,
  341. .read = &dev_read,
  342. .write = &dev_write,
  343. .close = &dev_close,
  344. .delete = &dev_delete,
  345. .flush = &dev_flush,
  346. .attrquery = &dev_attrquery,
  347. .attrquerybyhdl = &dev_attrquerybyhdl,
  348. };