Process_test.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/String.h"
  16. #include "crypto/random/Random.h"
  17. #include "rust/cjdns_sys/Rffi.h"
  18. #include "util/Defined.h"
  19. #include "util/events/EventBase.h"
  20. #include "util/events/Pipe.h"
  21. #include "util/events/Socket.h"
  22. #include "util/events/Timeout.h"
  23. #include "memory/Allocator.h"
  24. #include "util/events/Process.h"
  25. #include "util/log/Log.h"
  26. #include "util/log/FileWriterLog.h"
  27. #include "util/CString.h"
  28. #include "util/Assert.h"
  29. #include "wire/Message.h"
  30. #include "interface/addressable/AddrIface.h"
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #define MESSAGE "IT WORKS!"
  38. #define MESSAGEB "INDEED"
  39. struct Context {
  40. struct Iface iface;
  41. struct Allocator* alloc;
  42. struct Allocator* rootAlloc;
  43. EventBase_t* base;
  44. struct Log* log;
  45. // Parent only
  46. int fd;
  47. // child only
  48. char* name;
  49. Identity
  50. };
  51. static void onConnectionParent(void* context, const struct Sockaddr* addr)
  52. {
  53. printf("onConnectionParent\n");
  54. struct Context* c = Identity_check((struct Context*) context);
  55. struct Allocator* alloc = Allocator_child(c->alloc);
  56. Message_t* msg = Message_new(0, 256, alloc);
  57. Err_assert(Message_epush(msg, MESSAGE, CString_strlen(MESSAGE) + 1));
  58. Err_assert(AddrIface_pushAddr(msg, addr));
  59. if (!Defined(win32)) {
  60. Message_setAssociatedFd(msg, c->fd);
  61. }
  62. printf("Parent sending message [%s] len [%d]\n", MESSAGE, Message_getLength(msg));
  63. Iface_send(&c->iface, msg);
  64. Allocator_free(alloc);
  65. }
  66. static int g_childStopped = 0;
  67. static struct Context* g_context = NULL;
  68. static Iface_DEFUN receiveMessageParent(Message_t* msg, struct Iface* iface)
  69. {
  70. struct Context* c = Identity_check((struct Context*) iface);
  71. Err_assert(AddrIface_popAddr(NULL, msg));
  72. printf("msg length is %d\n", Message_getLength(msg));
  73. Assert_true(Message_getLength(msg) == (int)CString_strlen(MESSAGEB)+1);
  74. Assert_true(!Bits_memcmp(Message_bytes(msg), MESSAGEB, CString_strlen(MESSAGEB)+1));
  75. g_context = c;
  76. if (g_childStopped) {
  77. printf("Parent stopped in receiveMessageParent\n");
  78. Allocator_free(c->rootAlloc);
  79. }
  80. return NULL;
  81. }
  82. static void timeout(void* vNULL)
  83. {
  84. Assert_true(!"timed out.");
  85. }
  86. static void childDone(struct Allocator_OnFreeJob* ofj) {
  87. printf("Shutdown child\n");
  88. struct Context* c = Identity_check((struct Context*) ofj->userData);
  89. EventBase_endLoop(c->base);
  90. // This causes a segfault because the logger calles into freed memory
  91. // Allocator_free(c->rootAlloc);
  92. }
  93. static Iface_DEFUN receiveMessageChild(Message_t* msg, struct Iface* iface)
  94. {
  95. struct Context* c = Identity_check((struct Context*) iface);
  96. Allocator_t* alloc = Allocator_child(c->alloc);
  97. Allocator_t* alloc1 = Allocator_child(alloc);
  98. Message_t* m = Message_clone(msg, alloc1);
  99. printf("Child received message\n");
  100. Assert_true(Message_getLength(m) == (int)CString_strlen(MESSAGE)+1);
  101. Assert_true(!Bits_memcmp(Message_bytes(m), MESSAGE, CString_strlen(MESSAGE)+1));
  102. if (!Defined(win32)) {
  103. int fd = Message_getAssociatedFd(msg);
  104. Assert_true(fd > -1);
  105. if (lseek(fd, 0, SEEK_SET) < 0) {
  106. printf("lseek(%d) failed: errno %s\n", fd, strerror(errno));
  107. Assert_failure("lseek()");
  108. }
  109. uint8_t* buf = Allocator_calloc(Message_getAlloc(msg), 2048, 1);
  110. if (read(fd, buf, 1024) < 0) {
  111. printf("read(%d) failed: errno %s\n", fd, strerror(errno));
  112. Assert_failure("read()");
  113. }
  114. if (CString_strncmp(buf, c->name, 1024)) {
  115. printf("want: %s\n"
  116. "got: %s", c->name, buf);
  117. Assert_failure("file content is wrong");
  118. }
  119. }
  120. Err_assert(Message_eshift(m, -((int)CString_strlen(MESSAGE))));
  121. Err_assert(Message_epush(m, MESSAGEB, CString_strlen(MESSAGEB)));
  122. Allocator_onFree(alloc1, childDone, c);
  123. Iface_send(&c->iface, m);
  124. Allocator_free(alloc);
  125. // exit(0);
  126. // shutdown
  127. // Allocator_free(c->rootAlloc);
  128. return NULL;
  129. }
  130. static void child(char* name, struct Context* ctx)
  131. {
  132. Iface_t* iface = NULL;
  133. Err_assert(Socket_connect(&iface, name, ctx->alloc));
  134. ctx->iface.send = receiveMessageChild;
  135. ctx->name = name;
  136. Iface_plumb(&ctx->iface, iface);
  137. Timeout_setTimeout(timeout, NULL, 10000, ctx->base, ctx->alloc);
  138. EventBase_beginLoop(ctx->base);
  139. }
  140. static void onChildExit(int64_t exit_status, int term_signal)
  141. {
  142. printf("\n\n\nChild process exit status = %d term_signal = %d\n\n\n",
  143. (int)exit_status, term_signal);
  144. Assert_true(exit_status == 0);
  145. g_childStopped = true;
  146. if (g_context) {
  147. printf("Parent stopped in onChildExit\n");
  148. EventBase_endLoop(g_context->base);
  149. // Allocator_free(g_context->rootAlloc);
  150. }
  151. }
  152. int main(int argc, char** argv)
  153. {
  154. struct Allocator* allocator = Allocator_new(1<<20);
  155. EventBase_t* eb = EventBase_new(allocator);
  156. struct Allocator* alloc = Allocator_child(allocator);
  157. struct Log* log = FileWriterLog_new(stdout, alloc);
  158. Rffi_setLogger(log);
  159. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  160. Identity_set(ctx);
  161. ctx->alloc = alloc;
  162. ctx->rootAlloc = allocator;
  163. ctx->base = eb;
  164. ctx->log = log;
  165. ctx->iface.send = receiveMessageParent;
  166. if (argc > 3 && !CString_strcmp("Process_test", argv[1]) && !CString_strcmp("child", argv[2])) {
  167. child(argv[3], ctx);
  168. return 0;
  169. }
  170. struct Random* rand = NULL;
  171. Err_assert(Random_new(&rand, alloc, log));
  172. char randName[32] = {0};
  173. Random_base32(rand, (uint8_t*)randName, 31);
  174. String* name = String_printf(alloc, "%s%scjdns-test-%s", Pipe_PATH, Pipe_PATH_SEP, randName);
  175. if (!Defined(win32)) {
  176. String* textName =
  177. String_printf(alloc, "%s%scjdns-test-%s.txt", Pipe_PATH, Pipe_PATH_SEP, randName);
  178. int fd = open(textName->bytes, O_CREAT | O_TRUNC | O_RDWR, 0600);
  179. Assert_true(fd >= 0);
  180. Assert_true(write(fd, name->bytes, name->len) == ((ssize_t)name->len));
  181. ctx->fd = fd;
  182. unlink(textName->bytes);
  183. }
  184. Socket_Server_t* ss = NULL;
  185. Err_assert(Socket_server(&ss, name->bytes, alloc));
  186. Socket_serverOnConnect(ss, onConnectionParent, ctx);
  187. Iface_plumb(&ctx->iface, ss->iface);
  188. const char* path = Process_getPath(alloc);
  189. Assert_true(path != NULL);
  190. #ifdef win32
  191. Assert_true(CString_strstr(path, ":\\") == path + 1); /* C:\ */
  192. Assert_true(CString_strstr(path, ".exe"));
  193. #elif openbsd
  194. Assert_true(path[0] == 'b'); // Process_getPath returns relative paths on openbsd
  195. #elif netbsd
  196. Assert_true(path[0] == 'b'); // Process_getPath returns relative paths on netbsd too
  197. #else
  198. Assert_true(path[0] == '/');
  199. #endif
  200. const char* args[] = { "Process_test", "child", name->bytes, NULL };
  201. Assert_true(!Process_spawn(path, args, alloc, onChildExit));
  202. Timeout_setTimeout(timeout, NULL, 10000, eb, alloc);
  203. EventBase_beginLoop(eb);
  204. unlink(name->bytes);
  205. return 0;
  206. }