Process_test.c 7.1 KB

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