db_devices.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.h"
  23. #include "pal_internal.h"
  24. #include "pal_debug.h"
  25. #include "pal_error.h"
  26. #include "api.h"
  27. #define DEVICE_OPS(handle) \
  28. ({ int _type = (handle)->dev.dev_type; \
  29. (_type <= 0 || _type >= PAL_DEVICE_TYPE_BOUND) ? \
  30. NULL : pal_device_ops[_type]; \
  31. })
  32. enum {
  33. device_type_none = 0,
  34. device_type_term,
  35. PAL_DEVICE_TYPE_BOUND,
  36. };
  37. static struct handle_ops term_ops;
  38. static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
  39. NULL,
  40. &term_ops,
  41. };
  42. /* parse-device_uri scan the uri, parse the prefix of the uri and search
  43. for stream handler wich will open or access the device */
  44. static int parse_device_uri (const char ** uri, const char ** type,
  45. struct handle_ops ** ops)
  46. {
  47. struct handle_ops * dops = NULL;
  48. const char * p, * u = (*uri);
  49. for (p = u ; (*p) && (*p) != ',' && (*p) != '/' ; p++);
  50. switch (p - u) {
  51. case 3:
  52. if (!memcmp(u, "tty", 3))
  53. dops = &term_ops;
  54. break;
  55. default:
  56. break;
  57. }
  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. /* 'open' operation for terminal stream */
  77. static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
  78. int access, int share, int create, int options)
  79. {
  80. return -PAL_ERROR_NOTIMPLEMENTED;
  81. }
  82. static int term_close (PAL_HANDLE handle)
  83. {
  84. return -PAL_ERROR_NOTIMPLEMENTED;
  85. }
  86. /* 'attrquery' operation for terminal stream */
  87. static int term_attrquery (const char * type, const char * uri,
  88. PAL_STREAM_ATTR * attr)
  89. {
  90. return -PAL_ERROR_NOTIMPLEMENTED;
  91. }
  92. /* 'attrquery' operation for terminal stream */
  93. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  94. PAL_STREAM_ATTR * attr)
  95. {
  96. return -PAL_ERROR_NOTIMPLEMENTED;
  97. }
  98. static struct handle_ops term_ops = {
  99. .open = &term_open,
  100. .close = &term_close,
  101. .read = &char_read,
  102. .write = &char_write,
  103. .attrquery = &term_attrquery,
  104. .attrquerybyhdl = &term_attrquerybyhdl,
  105. };
  106. /* 'read' operation for character streams. */
  107. static int char_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  108. {
  109. return -PAL_ERROR_NOTIMPLEMENTED;
  110. }
  111. /* 'write' operation for character streams. */
  112. static int char_write (PAL_HANDLE handle, int offset, int size,
  113. const void * buffer)
  114. {
  115. return -PAL_ERROR_NOTIMPLEMENTED;
  116. }
  117. /* 'open' operation for device streams */
  118. static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
  119. int access, int share, int create, int options)
  120. {
  121. return -PAL_ERROR_NOTIMPLEMENTED;
  122. }
  123. /* 'read' operation for device stream */
  124. static int dev_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  125. {
  126. const struct handle_ops * ops = DEVICE_OPS(handle);
  127. if (!ops || !ops->read)
  128. return -PAL_ERROR_NOTSUPPORT;
  129. return ops->read(handle, offset, size, buffer);
  130. }
  131. /* 'write' operation for device stream */
  132. static int dev_write (PAL_HANDLE handle, int offset, int size,
  133. const void * buffer)
  134. {
  135. const struct handle_ops * ops = DEVICE_OPS(handle);
  136. if (!ops || !ops->write)
  137. return -PAL_ERROR_NOTSUPPORT;
  138. return ops->write(handle, offset, size, buffer);
  139. }
  140. /* 'close' operation for device streams */
  141. static int dev_close (PAL_HANDLE handle)
  142. {
  143. const struct handle_ops * ops = DEVICE_OPS(handle);
  144. if (ops && ops->close)
  145. return ops->close(handle);
  146. return -PAL_ERROR_NOTIMPLEMENTED;
  147. }
  148. /* 'delete' operation for device streams */
  149. static int dev_delete (PAL_HANDLE handle, int access)
  150. {
  151. const struct handle_ops * ops = DEVICE_OPS(handle);
  152. if (!ops || !ops->delete)
  153. return -PAL_ERROR_DENIED;
  154. int ret = dev_close(handle);
  155. if (ret < 0)
  156. return ret;
  157. return ops->delete(handle, access);
  158. }
  159. /* 'flush' operation for device streams */
  160. static int dev_flush (PAL_HANDLE handle)
  161. {
  162. const struct handle_ops * ops = DEVICE_OPS(handle);
  163. if (ops && ops->flush)
  164. return ops->flush(handle);
  165. return -PAL_ERROR_NOTIMPLEMENTED;
  166. }
  167. /* 'attrquery' operation for device streams */
  168. static int dev_attrquery (const char * type, const char * uri,
  169. PAL_STREAM_ATTR * attr)
  170. {
  171. struct handle_ops * ops = NULL;
  172. const char * dev_type = NULL;
  173. int ret = 0;
  174. ret = parse_device_uri(&uri, &dev_type, &ops);
  175. if (ret < 0)
  176. return ret;
  177. if (!ops || !ops->attrquery)
  178. return -PAL_ERROR_NOTSUPPORT;
  179. return ops->attrquery(dev_type, uri, attr);
  180. }
  181. /* 'attrquerybyhdl' operation for device stream */
  182. static int dev_attrquerybyhdl (PAL_HANDLE handle,
  183. PAL_STREAM_ATTR * attr)
  184. {
  185. const struct handle_ops * ops = DEVICE_OPS(handle);
  186. if (ops && ops->attrquerybyhdl)
  187. return ops->attrquerybyhdl(handle, attr);
  188. return -PAL_ERROR_NOTIMPLEMENTED;
  189. }
  190. static const char * dev_getrealpath (PAL_HANDLE handle)
  191. {
  192. return -PAL_ERROR_NOTIMPLEMENTED;
  193. }
  194. struct handle_ops dev_ops = {
  195. .getrealpath = &dev_getrealpath,
  196. .open = &dev_open,
  197. .read = &dev_read,
  198. .write = &dev_write,
  199. .close = &dev_close,
  200. .delete = &dev_delete,
  201. .flush = &dev_flush,
  202. .attrquery = &dev_attrquery,
  203. .attrquerybyhdl = &dev_attrquerybyhdl,
  204. };