Process_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program 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 General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "util/events/EventBase.h"
  17. #include "util/events/Pipe.h"
  18. #include "util/events/Timeout.h"
  19. #include "memory/Allocator.h"
  20. #include "memory/MallocAllocator.h"
  21. #include "util/events/Process.h"
  22. #include "util/log/Log.h"
  23. #include "util/log/FileWriterLog.h"
  24. #include "util/CString.h"
  25. #include "util/Assert.h"
  26. #include "wire/Message.h"
  27. #include "wire/Error.h"
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #define MESSAGE "IT WORKS!"
  32. #define MESSAGEB "INDEED"
  33. struct Context {
  34. struct Iface iface;
  35. struct Allocator* alloc;
  36. struct EventBase* base;
  37. struct Log* log;
  38. Identity
  39. };
  40. static void onConnectionParent(struct Pipe* p, int status)
  41. {
  42. Assert_true(!status);
  43. struct Context* c = Identity_check((struct Context*) p->userData);
  44. struct Allocator* alloc = Allocator_child(c->alloc);
  45. uint8_t* bytes = Allocator_calloc(alloc, CString_strlen(MESSAGE) + 1, 1);
  46. Bits_memcpy(bytes, MESSAGE, CString_strlen(MESSAGE));
  47. struct Message* m = Allocator_clone(alloc, (&(struct Message) {
  48. .length = CString_strlen(MESSAGE),
  49. .padding = 0,
  50. .capacity = CString_strlen(MESSAGE),
  51. .alloc = alloc,
  52. .bytes = bytes
  53. }));
  54. printf("Parent sending message [%s]\n", bytes);
  55. Iface_send(&c->iface, m);
  56. Allocator_free(alloc);
  57. }
  58. static Iface_DEFUN receiveMessageParent(struct Message* msg, struct Iface* iface)
  59. {
  60. struct Context* c = Identity_check((struct Context*) iface);
  61. Assert_true(msg->length == (int)CString_strlen(MESSAGEB));
  62. Assert_true(!Bits_memcmp(msg->bytes, MESSAGEB, CString_strlen(MESSAGEB)));
  63. Allocator_free(c->alloc);
  64. return NULL;
  65. }
  66. static void timeout(void* vNULL)
  67. {
  68. Assert_true(!"timed out.");
  69. }
  70. static void onConnectionChild(struct Pipe* p, int status)
  71. {
  72. Assert_true(!status);
  73. printf("Child connected\n");
  74. }
  75. static Iface_DEFUN receiveMessageChild(struct Message* msg, struct Iface* iface)
  76. {
  77. struct Context* c = Identity_check((struct Context*) iface);
  78. struct Message* m = Message_clone(msg, c->alloc);
  79. printf("Child received message\n");
  80. Assert_true(m->length == (int)CString_strlen(MESSAGE));
  81. Assert_true(!Bits_memcmp(m->bytes, MESSAGE, CString_strlen(MESSAGE)));
  82. Message_shift(m, -((int)CString_strlen(MESSAGE)), NULL);
  83. Message_push(m, MESSAGEB, CString_strlen(MESSAGEB), NULL);
  84. Iface_send(&c->iface, m);
  85. // shutdown
  86. Allocator_free(c->alloc);
  87. return NULL;
  88. }
  89. static void child(char* name, struct Context* ctx)
  90. {
  91. struct Pipe* pipe = Pipe_named(name, ctx->base, NULL, ctx->alloc);
  92. pipe->logger = ctx->log;
  93. pipe->onConnection = onConnectionChild;
  94. pipe->userData = ctx;
  95. ctx->iface.send = receiveMessageChild;
  96. Iface_plumb(&ctx->iface, &pipe->iface);
  97. Timeout_setTimeout(timeout, NULL, 2000, ctx->base, ctx->alloc);
  98. EventBase_beginLoop(ctx->base);
  99. }
  100. int main(int argc, char** argv)
  101. {
  102. struct Allocator* alloc = MallocAllocator_new(1<<20);
  103. struct EventBase* eb = EventBase_new(alloc);
  104. struct Log* log = FileWriterLog_new(stdout, alloc);
  105. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  106. Identity_set(ctx);
  107. ctx->alloc = alloc;
  108. ctx->base = eb;
  109. ctx->log = log;
  110. ctx->iface.send = receiveMessageParent;
  111. if (argc > 3 && !CString_strcmp("Process_test", argv[1]) && !CString_strcmp("child", argv[2])) {
  112. child(argv[3], ctx);
  113. return 0;
  114. }
  115. struct Random* rand = Random_new(alloc, log, NULL);
  116. char name[32] = {0};
  117. Random_base32(rand, (uint8_t*)name, 31);
  118. struct Pipe* pipe = Pipe_named(name, eb, NULL, alloc);
  119. pipe->logger = log;
  120. pipe->userData = ctx;
  121. pipe->onConnection = onConnectionParent;
  122. Iface_plumb(&ctx->iface, &pipe->iface);
  123. char* path = Process_getPath(alloc);
  124. Assert_true(path != NULL);
  125. #ifdef win32
  126. Assert_true(CString_strstr(path, ":\\") == path + 1); /* C:\ */
  127. Assert_true(CString_strstr(path, ".exe"));
  128. #elif openbsd
  129. Assert_true(path[0] == 'b'); // Process_getPath returns relative paths on openbsd
  130. #else
  131. Assert_true(path[0] == '/');
  132. #endif
  133. char* args[] = { "Process_test", "child", name, NULL };
  134. Assert_true(!Process_spawn(path, args, eb, alloc, NULL));
  135. Timeout_setTimeout(timeout, NULL, 2000, eb, alloc);
  136. EventBase_beginLoop(eb);
  137. return 0;
  138. }