db_devices.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "dev:".
  17. */
  18. #include "api.h"
  19. #include "pal.h"
  20. #include "pal_debug.h"
  21. #include "pal_defs.h"
  22. #include "pal_error.h"
  23. #include "pal_internal.h"
  24. #define DEVICE_OPS(handle) \
  25. ({ \
  26. int _type = (handle)->dev.dev_type; \
  27. (_type <= 0 || _type >= PAL_DEVICE_TYPE_BOUND) ? NULL : pal_device_ops[_type]; \
  28. })
  29. enum {
  30. device_type_none = 0,
  31. device_type_term,
  32. PAL_DEVICE_TYPE_BOUND,
  33. };
  34. static struct handle_ops term_ops;
  35. static const struct handle_ops* pal_device_ops[PAL_DEVICE_TYPE_BOUND] = {
  36. NULL,
  37. &term_ops,
  38. };
  39. /* parse_device_uri scans the uri, parses the prefix of the uri and searches for stream handler
  40. * which will open or access the device. */
  41. static int parse_device_uri(const char** uri, char** type, struct handle_ops** ops) {
  42. struct handle_ops* dops = NULL;
  43. const char* p;
  44. const char* u = *uri;
  45. for (p = u; *p && *p != ',' && *p != '/'; p++)
  46. ;
  47. if (strstartswith_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, void* buffer);
  63. static int64_t char_write(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buffer);
  64. static int term_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr);
  65. static int term_attrquerybyhdl(PAL_HANDLE hdl, PAL_STREAM_ATTR* attr);
  66. /* 'open' operation for terminal stream */
  67. static int term_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  68. int create, int options) {
  69. return -PAL_ERROR_NOTIMPLEMENTED;
  70. }
  71. static int term_close(PAL_HANDLE handle) {
  72. return -PAL_ERROR_NOTIMPLEMENTED;
  73. }
  74. /* 'attrquery' operation for terminal stream */
  75. static int term_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr) {
  76. return -PAL_ERROR_NOTIMPLEMENTED;
  77. }
  78. /* 'attrquery' operation for terminal stream */
  79. static int term_attrquerybyhdl(PAL_HANDLE hdl, PAL_STREAM_ATTR* attr) {
  80. return -PAL_ERROR_NOTIMPLEMENTED;
  81. }
  82. static struct handle_ops term_ops = {
  83. .open = &term_open,
  84. .close = &term_close,
  85. .read = &char_read,
  86. .write = &char_write,
  87. .attrquery = &term_attrquery,
  88. .attrquerybyhdl = &term_attrquerybyhdl,
  89. };
  90. /* 'read' operation for character streams. */
  91. static int64_t char_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* buffer) {
  92. return -PAL_ERROR_NOTIMPLEMENTED;
  93. }
  94. /* 'write' operation for character streams. */
  95. static int64_t char_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, const void* buffer) {
  96. return -PAL_ERROR_NOTIMPLEMENTED;
  97. }
  98. /* 'open' operation for device streams */
  99. static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  100. int create, int options) {
  101. return -PAL_ERROR_NOTIMPLEMENTED;
  102. }
  103. /* 'read' operation for device stream */
  104. static int64_t dev_read(PAL_HANDLE handle, uint64_t offset, uint64_t size, void* buffer) {
  105. const struct handle_ops* ops = DEVICE_OPS(handle);
  106. if (!ops || !ops->read)
  107. return -PAL_ERROR_NOTSUPPORT;
  108. return ops->read(handle, offset, size, buffer);
  109. }
  110. /* 'write' operation for device stream */
  111. static int64_t dev_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, const void* buffer) {
  112. const struct handle_ops* ops = DEVICE_OPS(handle);
  113. if (!ops || !ops->write)
  114. return -PAL_ERROR_NOTSUPPORT;
  115. return ops->write(handle, offset, size, buffer);
  116. }
  117. /* 'close' operation for device streams */
  118. static int dev_close(PAL_HANDLE handle) {
  119. const struct handle_ops* ops = DEVICE_OPS(handle);
  120. if (ops && ops->close)
  121. return ops->close(handle);
  122. return -PAL_ERROR_NOTIMPLEMENTED;
  123. }
  124. /* 'delete' operation for device streams */
  125. static int dev_delete(PAL_HANDLE handle, int access) {
  126. const struct handle_ops* ops = DEVICE_OPS(handle);
  127. if (!ops || !ops->delete)
  128. return -PAL_ERROR_DENIED;
  129. int ret = dev_close(handle);
  130. if (ret < 0)
  131. return ret;
  132. return ops->delete(handle, access);
  133. }
  134. /* 'flush' operation for device streams */
  135. static int dev_flush(PAL_HANDLE handle) {
  136. const struct handle_ops* ops = DEVICE_OPS(handle);
  137. if (ops && ops->flush)
  138. return ops->flush(handle);
  139. return -PAL_ERROR_NOTIMPLEMENTED;
  140. }
  141. /* 'attrquery' operation for device streams */
  142. static int dev_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr) {
  143. struct handle_ops* ops = NULL;
  144. char* dev_type = NULL;
  145. int ret = 0;
  146. ret = parse_device_uri(&uri, &dev_type, &ops);
  147. if (ret < 0)
  148. return ret;
  149. if (!ops || !ops->attrquery)
  150. return -PAL_ERROR_NOTSUPPORT;
  151. ret = ops->attrquery(dev_type, uri, attr);
  152. free(dev_type);
  153. return ret;
  154. }
  155. /* 'attrquerybyhdl' operation for device stream */
  156. static int dev_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  157. const struct handle_ops* ops = DEVICE_OPS(handle);
  158. if (ops && ops->attrquerybyhdl)
  159. return ops->attrquerybyhdl(handle, attr);
  160. return -PAL_ERROR_NOTIMPLEMENTED;
  161. }
  162. static const char* dev_getrealpath(PAL_HANDLE handle) {
  163. return NULL;
  164. }
  165. struct handle_ops dev_ops = {
  166. .getrealpath = &dev_getrealpath,
  167. .open = &dev_open,
  168. .read = &dev_read,
  169. .write = &dev_write,
  170. .close = &dev_close,
  171. .delete = &dev_delete,
  172. .flush = &dev_flush,
  173. .attrquery = &dev_attrquery,
  174. .attrquerybyhdl = &dev_attrquerybyhdl,
  175. };