db_devices.c 12 KB

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