db_devices.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_device.c
  15. *
  16. * This file contains operands to handle streams with URIs that start with
  17. * "dev:".
  18. */
  19. #include "pal_defs.h"
  20. #include "pal.h"
  21. #include "pal_internal.h"
  22. #include "pal_debug.h"
  23. #include "pal_error.h"
  24. #include "api.h"
  25. #define DEVICE_OPS(handle) \
  26. ({ int _type = (handle)->dev.dev_type; \
  27. (_type <= 0 || _type >= PAL_DEVICE_TYPE_BOUND) ? \
  28. NULL : pal_device_ops[_type]; \
  29. })
  30. enum {
  31. device_type_none = 0,
  32. device_type_term,
  33. PAL_DEVICE_TYPE_BOUND,
  34. };
  35. static struct handle_ops term_ops;
  36. static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
  37. NULL,
  38. &term_ops,
  39. };
  40. /* parse_device_uri scans the uri, parses the prefix of the uri and searches
  41. for stream handler wich will open or access the device. */
  42. static int parse_device_uri(const char ** uri, char ** type, struct handle_ops ** ops)
  43. {
  44. struct handle_ops * dops = NULL;
  45. const char * p, * u = (*uri);
  46. for (p = u ; (*p) && (*p) != ',' && (*p) != '/' ; p++);
  47. if (strpartcmp_static(u, "tty"))
  48. dops = &term_ops;
  49. if (!dops)
  50. return -PAL_ERROR_NOTSUPPORT;
  51. *uri = (*p) ? p + 1 : p;
  52. if (type) {
  53. *type = malloc_copy(u, p - u + 1);
  54. if (!*type)
  55. return -PAL_ERROR_NOMEM;
  56. (*type)[p - u] = '\0';
  57. }
  58. if (ops)
  59. *ops = dops;
  60. return 0;
  61. }
  62. static int64_t char_read (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  63. void * buffer);
  64. static int64_t char_write (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  65. const void * buffer);
  66. static int term_attrquery (const char * type, const char * uri,
  67. PAL_STREAM_ATTR * attr);
  68. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  69. PAL_STREAM_ATTR * attr);
  70. /* 'open' operation for terminal stream */
  71. static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
  72. int access, int share, int create, int options)
  73. {
  74. return -PAL_ERROR_NOTIMPLEMENTED;
  75. }
  76. static int term_close (PAL_HANDLE handle)
  77. {
  78. return -PAL_ERROR_NOTIMPLEMENTED;
  79. }
  80. /* 'attrquery' operation for terminal stream */
  81. static int term_attrquery (const char * type, const char * uri,
  82. PAL_STREAM_ATTR * attr)
  83. {
  84. return -PAL_ERROR_NOTIMPLEMENTED;
  85. }
  86. /* 'attrquery' operation for terminal stream */
  87. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  88. PAL_STREAM_ATTR * attr)
  89. {
  90. return -PAL_ERROR_NOTIMPLEMENTED;
  91. }
  92. static struct handle_ops term_ops = {
  93. .open = &term_open,
  94. .close = &term_close,
  95. .read = &char_read,
  96. .write = &char_write,
  97. .attrquery = &term_attrquery,
  98. .attrquerybyhdl = &term_attrquerybyhdl,
  99. };
  100. /* 'read' operation for character streams. */
  101. static int64_t char_read (PAL_HANDLE handle, uint64_t offset, uint64_t size, void * buffer)
  102. {
  103. return -PAL_ERROR_NOTIMPLEMENTED;
  104. }
  105. /* 'write' operation for character streams. */
  106. static int64_t char_write (PAL_HANDLE handle, uint64_t offset, uint64_t size,
  107. const void * buffer)
  108. {
  109. return -PAL_ERROR_NOTIMPLEMENTED;
  110. }
  111. /* 'open' operation for device streams */
  112. static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
  113. int access, int share, int create, int options)
  114. {
  115. return -PAL_ERROR_NOTIMPLEMENTED;
  116. }
  117. /* 'read' operation for device stream */
  118. static int64_t dev_read (PAL_HANDLE handle, uint64_t offset, uint64_t size, void * buffer)
  119. {
  120. const struct handle_ops * ops = DEVICE_OPS(handle);
  121. if (!ops || !ops->read)
  122. return -PAL_ERROR_NOTSUPPORT;
  123. return ops->read(handle, offset, size, buffer);
  124. }
  125. /* 'write' operation for device stream */
  126. static int64_t dev_write (PAL_HANDLE handle, uint64_t offset, uint64_t size,
  127. const void * buffer)
  128. {
  129. const struct handle_ops * ops = DEVICE_OPS(handle);
  130. if (!ops || !ops->write)
  131. return -PAL_ERROR_NOTSUPPORT;
  132. return ops->write(handle, offset, size, buffer);
  133. }
  134. /* 'close' operation for device streams */
  135. static int dev_close (PAL_HANDLE handle)
  136. {
  137. const struct handle_ops * ops = DEVICE_OPS(handle);
  138. if (ops && ops->close)
  139. return ops->close(handle);
  140. return -PAL_ERROR_NOTIMPLEMENTED;
  141. }
  142. /* 'delete' operation for device streams */
  143. static int dev_delete (PAL_HANDLE handle, int access)
  144. {
  145. const struct handle_ops * ops = DEVICE_OPS(handle);
  146. if (!ops || !ops->delete)
  147. return -PAL_ERROR_DENIED;
  148. int ret = dev_close(handle);
  149. if (ret < 0)
  150. return ret;
  151. return ops->delete(handle, access);
  152. }
  153. /* 'flush' operation for device streams */
  154. static int dev_flush (PAL_HANDLE handle)
  155. {
  156. const struct handle_ops * ops = DEVICE_OPS(handle);
  157. if (ops && ops->flush)
  158. return ops->flush(handle);
  159. return -PAL_ERROR_NOTIMPLEMENTED;
  160. }
  161. /* 'attrquery' operation for device streams */
  162. static int dev_attrquery (const char * type, const char * uri,
  163. PAL_STREAM_ATTR * attr)
  164. {
  165. struct handle_ops * ops = NULL;
  166. char * dev_type = NULL;
  167. int ret = 0;
  168. ret = parse_device_uri(&uri, &dev_type, &ops);
  169. if (ret < 0)
  170. return ret;
  171. if (!ops || !ops->attrquery)
  172. return -PAL_ERROR_NOTSUPPORT;
  173. ret = ops->attrquery(dev_type, uri, attr);
  174. free(dev_type);
  175. return ret;
  176. }
  177. /* 'attrquerybyhdl' operation for device stream */
  178. static int dev_attrquerybyhdl (PAL_HANDLE handle,
  179. PAL_STREAM_ATTR * attr)
  180. {
  181. const struct handle_ops * ops = DEVICE_OPS(handle);
  182. if (ops && ops->attrquerybyhdl)
  183. return ops->attrquerybyhdl(handle, attr);
  184. return -PAL_ERROR_NOTIMPLEMENTED;
  185. }
  186. static const char * dev_getrealpath (PAL_HANDLE handle)
  187. {
  188. return NULL;
  189. }
  190. struct handle_ops dev_ops = {
  191. .getrealpath = &dev_getrealpath,
  192. .open = &dev_open,
  193. .read = &dev_read,
  194. .write = &dev_write,
  195. .close = &dev_close,
  196. .delete = &dev_delete,
  197. .flush = &dev_flush,
  198. .attrquery = &dev_attrquery,
  199. .attrquerybyhdl = &dev_attrquerybyhdl,
  200. };