Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <sys/neutrino.h>
  6. #include <sys/dispatch.h>
  7. #include <folder_manager/QnxFolderManager.h>
  8. #include <sys/types.h>
  9. #include <sys/iofunc.h>
  10. #include <pthread.h>
  11. #include <process.h>
  12. #include <unistd.h>
  13.  
  14. #include <map>
  15.  
  16.  
  17.  
  18. std::map<char *, pid_t> pathProcess;
  19.  
  20. typedef struct Mount Mount;
  21. struct Mount {
  22. char *from;
  23. char *to;
  24. };
  25.  
  26. typedef struct Unmount unmount;
  27. struct Unmount {
  28. char *from;
  29. };
  30.  
  31.  
  32. Unmount parse_unmount_command(char *data);
  33.  
  34. Mount parse_mount_command(char *data);
  35.  
  36. void process_data(int offset, void *buffer, int nbytes) {
  37. char *data = (char *) buffer;
  38. if (data[0] == 'M') {
  39. //if mount command
  40. Mount mount;
  41. mount = parse_mount_command(data);
  42. pid_t pid;
  43.  
  44. if ((pid = fork()) == -1) {
  45. perror("fork");
  46. }
  47.  
  48.  
  49. if (pid == 0) {
  50. printf("mount command: %s\n", data);
  51. QnxFolderManager manager;
  52. if (manager.Init_res_manager(mount.to) == -1) {
  53. printf("Can not create folder manager: %s", mount.to);
  54. }
  55. printf("Folder manager created 1, process id: %d\n", getpid());
  56. } else {
  57. printf("back to main process");
  58. pathProcess.insert(std::pair<char *, pid_t>(mount.to, pid));
  59. return;
  60. }
  61. } else {
  62. //Unmount resourse manager
  63. Unmount unmount;
  64. unmount = parse_unmount_command(data);
  65. pid_t pid = pathProcess.at(unmount.from);
  66. if (pid) {
  67. kill(pid, 0);
  68. }
  69. }
  70. }
  71.  
  72. Mount parse_mount_command(char *data) {
  73. char *token = strtok(data, " ");
  74. int i = 0;
  75.  
  76. Mount res = Mount{};
  77.  
  78. while (token != NULL) {
  79. i++;
  80. if (i == 2) {
  81. res.from = token;
  82. }
  83. if (i == 3) {
  84. res.to = token;
  85. }
  86. token = strtok(NULL, " ");
  87. }
  88. return res;
  89. }
  90.  
  91. Unmount parse_unmount_command(char *data) {
  92. char *token = strtok(data, " ");
  93. int i = 0;
  94.  
  95. Unmount res = Unmount{};
  96.  
  97. while (token != NULL) {
  98. i++;
  99. if (i == 2) {
  100. res.from = token;
  101. }
  102. token = strtok(NULL, " ");
  103. }
  104. return res;
  105. }
  106.  
  107. int io_write(resmgr_context_t *ctp, io_write_t *msg,
  108. iofunc_ocb_t *ocb) {
  109. printf("Write\n");
  110. int sts;
  111. int nbytes;
  112. int off;
  113. int start_data_offset;
  114. int xtype;
  115. char *buffer;
  116. struct _xtype_offset *xoffset;
  117.  
  118.  
  119. if ((sts = iofunc_write_verify(ctp, msg, ocb, NULL)) != EOK)
  120. return (sts);
  121.  
  122.  
  123. xtype = msg->i.xtype & _IO_XTYPE_MASK;
  124. if (xtype == _IO_XTYPE_OFFSET) {
  125. xoffset = (struct _xtype_offset *) (&msg->i + 1);
  126. start_data_offset = sizeof(msg->i) + sizeof(*xoffset);
  127. off = xoffset->offset;
  128. } else if (xtype == _IO_XTYPE_NONE) {
  129. off = ocb->offset;
  130. start_data_offset = sizeof(msg->i);
  131. } else
  132. return (ENOSYS);
  133.  
  134.  
  135. nbytes = msg->i.nbytes;
  136. if ((buffer = (char *) malloc(nbytes)) == NULL)
  137. return (ENOMEM);
  138.  
  139.  
  140. if (resmgr_msgread(ctp, buffer, nbytes, start_data_offset) == -1) {
  141. free(buffer);
  142. return (errno);
  143. }
  144.  
  145.  
  146. process_data(off, buffer, nbytes);
  147.  
  148.  
  149. free(buffer);
  150.  
  151. _IO_SET_WRITE_NBYTES (ctp, nbytes);
  152.  
  153.  
  154. if (nbytes) {
  155. ocb->attr->flags |= IOFUNC_ATTR_MTIME | IOFUNC_ATTR_DIRTY_TIME;
  156. if (xtype == _IO_XTYPE_NONE)
  157. ocb->offset += nbytes;
  158. }
  159.  
  160.  
  161. return (EOK);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement