Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stddef.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/iofunc.h>
  8. #include <sys/dispatch.h>
  9. #include <time.h>
  10.  
  11. int io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb);
  12.  
  13. //static char *buffer = "Hello world\n";
  14.  
  15. static resmgr_connect_funcs_t connect_funcs;
  16. static resmgr_io_funcs_t io_funcs;
  17. static iofunc_attr_t attr;
  18. static time_t time_of_day;
  19.  
  20.  
  21. int main(int argc, char **argv)
  22. {
  23. /* declare variables we'll be using */
  24. resmgr_attr_t resmgr_attr;
  25. dispatch_t *dpp;
  26. dispatch_context_t *ctp;
  27. int id;
  28. time_of_day = time( NULL );
  29.  
  30. /* initialize dispatch interface */
  31. if((dpp = dispatch_create()) == NULL) {
  32. fprintf(stderr, "%s: Unable to allocate dispatch handle.\n",
  33. argv[0]);
  34. return EXIT_FAILURE;
  35. }
  36.  
  37. /* initialize resource manager attributes */
  38. memset(&resmgr_attr, 0, sizeof resmgr_attr);
  39. resmgr_attr.nparts_max = 1;
  40. resmgr_attr.msg_max_size = 2048;
  41.  
  42. /* initialize functions for handling messages, including
  43. our read handlers */
  44. iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs,
  45. _RESMGR_IO_NFUNCS, &io_funcs);
  46. io_funcs.read = io_read;
  47. io_funcs.read64 = io_read;
  48.  
  49. /* initialize attribute structure used by the device */
  50. iofunc_attr_init(&attr, S_IFNAM | 0666, 0, 0);
  51. attr.nbytes = 100;
  52.  
  53. /* attach our device name */
  54. if((id = resmgr_attach(dpp, &resmgr_attr, "/dev/sample_time", _FTYPE_ANY, 0,
  55. &connect_funcs, &io_funcs, &attr)) == -1) {
  56. fprintf(stderr, "%s: Unable to attach name.\n", argv[0]);
  57. return EXIT_FAILURE;
  58. }
  59.  
  60. /* allocate a context structure */
  61. ctp = dispatch_context_alloc(dpp);
  62.  
  63. /* start the resource manager message loop */
  64. while(1) {
  65. if((ctp = dispatch_block(ctp)) == NULL) {
  66. fprintf(stderr, "block error\n");
  67. return EXIT_FAILURE;
  68. }
  69. dispatch_handler(ctp);
  70. }
  71. return EXIT_SUCCESS;
  72. }
  73.  
  74. int
  75. io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb)
  76. {
  77. size_t nleft;
  78. size_t nbytes;
  79. int nparts;
  80. int status;
  81.  
  82.  
  83. if ((status = iofunc_read_verify (ctp, msg, ocb, NULL)) != EOK)
  84. return (status);
  85.  
  86. if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE)
  87. return (ENOSYS);
  88.  
  89. /*
  90. * On all reads (first and subsequent), calculate
  91. * how many bytes we can return to the client,
  92. * based upon the number of bytes available (nleft)
  93. * and the client's buffer size
  94. */
  95.  
  96. nleft = ocb->attr->nbytes - ocb->offset;
  97. nbytes = min (_IO_READ_GET_NBYTES(msg), nleft);
  98.  
  99. if (nbytes > 0) {
  100. /* set up the return data IOV */
  101. SETIOV (ctp->iov, time_of_day + ocb->offset, nbytes);
  102.  
  103. /* set up the number of bytes (returned by client's read()) */
  104. _IO_SET_READ_NBYTES (ctp, nbytes);
  105.  
  106. /*
  107. * advance the offset by the number of bytes
  108. * returned to the client.
  109. */
  110.  
  111. ocb->offset += nbytes;
  112.  
  113. nparts = 1;
  114. } else {
  115. /*
  116. * they've asked for zero bytes or they've already previously
  117. * read everything
  118. */
  119.  
  120. _IO_SET_READ_NBYTES (ctp, 0);
  121.  
  122. nparts = 0;
  123. }
  124.  
  125. /* mark the access time as invalid (we just accessed it) */
  126.  
  127. if (msg->i.nbytes > 0)
  128. ocb->attr->flags |= IOFUNC_ATTR_ATIME;
  129.  
  130. return (_RESMGR_NPARTS (nparts));
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement