myco-ipc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (C) 2016 Max Riechelmann <max.riechelmann@student.kit.edu>
  2. (Karlsruhe Institute of Technology)
  3. This library is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Lesser General Public License as published by the
  5. Free Software Foundation; either version 2.1 of the License, or (at your
  6. option) any later version.
  7. This library is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  10. details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this library; if not, write to the Free Software Foundation, Inc., 51
  13. Franklin St, Fifth Floor, Boston, MA 02110, USA
  14. */
  15. #include "myco-ipc.h"
  16. int myco_error(char *error_message, int message_queue_id) {
  17. fprintf(stderr, "ERROR: ");
  18. fprintf(stderr, error_message);
  19. fprintf(stderr, "=> %s\n", strerror(errno));
  20. myco_remove_message_queue(message_queue_id);
  21. exit(1);
  22. }
  23. int myco_create_private_message_queue() {
  24. int message_queue_id;
  25. message_queue_id = msgget(IPC_PRIVATE, PERM | IPC_CREAT);
  26. if (message_queue_id == -1) {
  27. fprintf(stderr, "ERROR: myco_create_private_message_queue() => %s\n", strerror(errno));
  28. error(1);
  29. }
  30. return message_queue_id;
  31. }
  32. int myco_create_global_message_queue() {
  33. int message_queue_id;
  34. message_queue_id = msgget(KEY, PERM | IPC_CREAT);
  35. if (message_queue_id == -1) {
  36. fprintf(stderr, "ERROR: myco_create_global_message_queue() => %s\n", strerror(errno));
  37. error(1);
  38. }
  39. return message_queue_id;
  40. }
  41. int myco_get_global_message_queue() {
  42. int message_queue_id;
  43. message_queue_id = msgget(KEY, 0);
  44. if (message_queue_id == -1) {
  45. fprintf(stderr, "ERROR: myco_get_global_message_queue() => %s\n", strerror(errno));
  46. error(1);
  47. }
  48. return message_queue_id;
  49. }
  50. int myco_remove_message_queue(int message_queue_id) {
  51. if (msgctl(message_queue_id, IPC_RMID, NULL) == -1) {
  52. fprintf(stderr, "ERROR: myco_remove_message_queue() %d => %s\n", message_queue_id, strerror(errno));
  53. exit(1);
  54. }
  55. return 0;
  56. }
  57. int myco_send(int message_queue_id, message msg) {
  58. if (msgsnd(message_queue_id, &msg, TOTAL_LENGTH, 0) < 0) {
  59. myco_error("myco_send()", message_queue_id);
  60. }
  61. return 0;
  62. }
  63. message myco_receive(int message_queue_id) {
  64. message msg;
  65. if (msgrcv(message_queue_id, &msg, TOTAL_LENGTH, 0, 0) == -1) {
  66. myco_error("myco_receive()", message_queue_id);
  67. }
  68. if (msg.message == NULL) {
  69. myco_error("myco_receive()", message_queue_id);
  70. }
  71. return msg;
  72. }
  73. message myco_send_and_receive(message msg, int send_message_queue_id, int receive_message_queue_id) {
  74. myco_send(send_message_queue_id, msg);
  75. msg = myco_receive(receive_message_queue_id);
  76. return msg;
  77. }
  78. int myco_read_transactional(pid_t source_pid, void *source_pointer, int source_length, void *target_pointer, int target_length) {
  79. struct iovec local[1];
  80. struct iovec remote[1];
  81. ssize_t nread;
  82. pid_t pid = source_pid;
  83. local[0].iov_base = target_pointer;
  84. local[0].iov_len = target_length;
  85. remote[0].iov_base = source_pointer;
  86. remote[0].iov_len = source_length;
  87. nread = process_vm_readv(pid, local, 1, remote, 1, 0);
  88. if (nread != target_length) {
  89. return -1;
  90. }
  91. return 0;
  92. }