1
0

Process.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "util/events/libuv/UvWrapper.h"
  16. #include "memory/Allocator.h"
  17. #include "util/events/libuv/EventBase_pvt.h"
  18. #include "util/events/Process.h"
  19. #include "util/Bits.h"
  20. #include "util/Identity.h"
  21. struct Process_pvt
  22. {
  23. uv_process_t proc;
  24. Process_OnExitCallback onExit;
  25. struct Allocator* alloc;
  26. Identity
  27. };
  28. static void onFree2(uv_handle_t* process)
  29. {
  30. Allocator_onFreeComplete((struct Allocator_OnFreeJob*) process->data);
  31. }
  32. static int onFree(struct Allocator_OnFreeJob* job)
  33. {
  34. struct Process_pvt* p = Identity_check((struct Process_pvt*) job->userData);
  35. uv_process_kill(&p->proc, SIGTERM);
  36. p->proc.data = job;
  37. uv_close((uv_handle_t*)&p->proc, onFree2);
  38. return Allocator_ONFREE_ASYNC;
  39. }
  40. static void onExit(uv_process_t *req, int64_t exit_status, int term_signal)
  41. {
  42. struct Process_pvt* p = Identity_containerOf(req, struct Process_pvt, proc);
  43. if (p->onExit) {
  44. p->onExit(exit_status, term_signal);
  45. }
  46. }
  47. int Process_spawn(char* binaryPath,
  48. char** args,
  49. struct EventBase* base,
  50. struct Allocator* alloc,
  51. Process_OnExitCallback callback)
  52. {
  53. struct EventBase_pvt* ctx = EventBase_privatize(base);
  54. int i;
  55. for (i = 0; args[i]; i++) ;
  56. char** binAndArgs = Allocator_calloc(alloc, sizeof(char*), i+2);
  57. for (i = 0; args[i]; i++) {
  58. binAndArgs[i+1] = args[i];
  59. }
  60. binAndArgs[0] = binaryPath;
  61. binAndArgs[i+1] = NULL;
  62. struct Process_pvt* p = Allocator_calloc(alloc, sizeof(struct Process_pvt), 1);
  63. p->alloc = alloc;
  64. Identity_set(p);
  65. Allocator_onFree(alloc, onFree, p);
  66. uv_stdio_container_t files[] = {
  67. { .flags = UV_IGNORE },
  68. { .flags = UV_INHERIT_FD, .data.fd = 1 },
  69. { .flags = UV_INHERIT_FD, .data.fd = 2 },
  70. };
  71. uv_process_options_t options = {
  72. .file = binaryPath,
  73. .args = binAndArgs,
  74. .flags = UV_PROCESS_WINDOWS_HIDE,
  75. .stdio = files,
  76. .stdio_count = 3,
  77. .exit_cb = onExit
  78. };
  79. p->onExit = callback;
  80. return uv_spawn(ctx->loop, &p->proc, &options);
  81. }
  82. char* Process_getPath(struct Allocator* alloc)
  83. {
  84. char path[4096];
  85. size_t sz = 4096;
  86. uv_exepath(path, &sz);
  87. char* out = Allocator_malloc(alloc, sz+1);
  88. Bits_memcpy(out, path, sz);
  89. out[sz] = 0;
  90. return out;
  91. }