client.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/mman.h>
  7. #include <fcntl.h>
  8. #include "../graphene-ipc.h"
  9. #define PAGE_SIZE 4096
  10. #define MAX_PIDBUF 20
  11. #define MAX_PATH 256
  12. int main () {
  13. volatile int *x = NULL;
  14. int fd, fd1, rv;
  15. struct gipc_recv gr;
  16. struct gipc_send gs;
  17. pid_t other_client;
  18. char pidbuf[MAX_PIDBUF];
  19. unsigned long len = PAGE_SIZE;
  20. unsigned long addr = 0;
  21. int64_t token;
  22. int order;
  23. unsigned long gr_addr, gr_len;
  24. int gr_prot;
  25. gr.entries = 1;
  26. gr.addr = &gr_addr;
  27. gr.len = &gr_len;
  28. gr.prot = &gr_prot;
  29. read(0, &token, sizeof(token));
  30. read(0, &order, sizeof(order));
  31. /* Open an IPC link */
  32. fd = open (GIPC_FILE, O_RDWR);
  33. if (fd < 0) {
  34. printf ("[client] Fd is %d %d\n", fd, errno);
  35. return -1;
  36. }
  37. /* Set the channel */
  38. rv = ioctl(fd, GIPC_JOIN, token);
  39. if (rv < 0) {
  40. printf("[client] Failed to join ipc channel - %d, %d\n", rv, errno);
  41. return -1;
  42. }
  43. gr_len = PAGE_SIZE * 3;
  44. gr_addr = 0;
  45. gr_prot = PROT_READ | PROT_WRITE;
  46. /* Recv the page */
  47. rv = ioctl(fd, GIPC_RECV, &gr);
  48. if (rv) {
  49. printf ("Bad map %p rv %d %d\n", (void *)gr_addr, rv, errno);
  50. return 1;
  51. }
  52. /* Try a memory barrier */
  53. asm volatile ("nop" :::"memory");
  54. x = (int *) gr_addr;
  55. /* Print the value */
  56. printf("[client] X contains %d, from %d\n", *x, fd);
  57. x += PAGE_SIZE / sizeof(int);
  58. printf("[client] X contains %d, from %d\n", *x, fd);
  59. x += PAGE_SIZE / sizeof(int);
  60. printf("[client] X contains %d, from %d\n", *x, fd);
  61. *x += 3820;
  62. other_client = *(int *) gr_addr;
  63. printf("[client] other pid is %d\n", other_client);
  64. /* Map an anonymous page */
  65. addr = (unsigned long) x;
  66. gs.entries = 1;
  67. gs.addr = &addr;
  68. gs.len = &len;
  69. /* Send the pages to other client */
  70. fd1 = open (GIPC_FILE, O_RDWR);
  71. if (!order) {
  72. // Create the queue
  73. token = ioctl(fd1, GIPC_CREATE, 0);
  74. if (token < 0) {
  75. printf ("[server] Failed to create a new token %ld\n", token);
  76. return -1;
  77. }
  78. // Write to token to handle 3 (write pipe)
  79. write(3, &token, sizeof(token));
  80. } else {
  81. // Join the queue
  82. rv = read(3, &token, sizeof(token));
  83. if (rv != sizeof(token)) {
  84. printf("Failed to get the token - %d\n", errno);
  85. }
  86. rv = ioctl(fd1, GIPC_JOIN, token);
  87. if (rv < 0) {
  88. printf("[client] Failed to join ipc channel - %d, %d\n", rv, errno);
  89. return -1;
  90. }
  91. }
  92. rv = ioctl(fd1, GIPC_SEND, &gs);
  93. if (rv != gs.entries)
  94. printf ("[client] Bad rv %d (%d)\n", errno, rv);
  95. gr_len = PAGE_SIZE;
  96. gr_addr = 0;
  97. //gr.wait_on_src = other_client;
  98. /* Recv the page */
  99. rv = ioctl(fd1, GIPC_RECV, &gr);
  100. if (rv) {
  101. printf ("Bad map %p rv %d %d\n", (void *)gr_addr, rv, errno);
  102. return 1;
  103. }
  104. /* Try a memory barrier */
  105. asm volatile ("nop" :::"memory");
  106. x = (int *) gr_addr;
  107. /* Print the value */
  108. printf("[client] X contains %d, from %d\n", *x, fd1);
  109. close(fd1);
  110. close(fd);
  111. return 0;
  112. }