subprocess_posix.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/support/port_platform.h>
  19. #ifdef GPR_POSIX_SUBPROCESS
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include <stdbool.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/wait.h>
  29. #include <unistd.h>
  30. #include <grpc/support/alloc.h>
  31. #include <grpc/support/log.h>
  32. #include "src/core/lib/gprpp/memory.h"
  33. #include "test/core/util/subprocess.h"
  34. struct gpr_subprocess {
  35. int pid;
  36. bool joined;
  37. };
  38. const char* gpr_subprocess_binary_extension() { return ""; }
  39. gpr_subprocess* gpr_subprocess_create(int argc, const char** argv) {
  40. gpr_subprocess* r;
  41. int pid;
  42. char** exec_args;
  43. pid = fork();
  44. if (pid == -1) {
  45. return nullptr;
  46. } else if (pid == 0) {
  47. exec_args = static_cast<char**>(
  48. gpr_malloc((static_cast<size_t>(argc) + 1) * sizeof(char*)));
  49. memcpy(exec_args, argv, static_cast<size_t>(argc) * sizeof(char*));
  50. exec_args[argc] = nullptr;
  51. execv(exec_args[0], exec_args);
  52. /* if we reach here, an error has occurred */
  53. gpr_log(GPR_ERROR, "execv '%s' failed: %s", exec_args[0], strerror(errno));
  54. _exit(1);
  55. } else {
  56. r = grpc_core::Zalloc<gpr_subprocess>();
  57. r->pid = pid;
  58. return r;
  59. }
  60. }
  61. void gpr_subprocess_destroy(gpr_subprocess* p) {
  62. if (!p->joined) {
  63. kill(p->pid, SIGKILL);
  64. gpr_subprocess_join(p);
  65. }
  66. gpr_free(p);
  67. }
  68. int gpr_subprocess_join(gpr_subprocess* p) {
  69. int status;
  70. retry:
  71. if (waitpid(p->pid, &status, 0) == -1) {
  72. if (errno == EINTR) {
  73. goto retry;
  74. }
  75. gpr_log(GPR_ERROR, "waitpid failed for pid %d: %s", p->pid,
  76. strerror(errno));
  77. return -1;
  78. }
  79. p->joined = true;
  80. return status;
  81. }
  82. void gpr_subprocess_interrupt(gpr_subprocess* p) {
  83. if (!p->joined) {
  84. kill(p->pid, SIGINT);
  85. }
  86. }
  87. #endif /* GPR_POSIX_SUBPROCESS */