do_start_process.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Actually start a process. All of the arguments given to this
  3. * function are strings that are used for the "argv" array. However,
  4. * if those strings contain spaces, the given argument is split into
  5. * multiple argv entries without spaces. Similarly, if an argument is
  6. * the empty string, it is skipped. This function has the inherent
  7. * limitation that it does NOT allow passing command line arguments
  8. * with spaces to the new process.
  9. *
  10. * @param lsocks array of listen sockets to dup starting at fd3 (systemd-style), or NULL
  11. * @param first_arg first argument for argv (may be an empty string)
  12. * @param ... more arguments, NULL terminated
  13. * @return PID of the started process, -1 on error
  14. */
  15. static struct GNUNET_OS_Process *
  16. do_start_process (const int *lsocks,
  17. const char *first_arg, ...)
  18. {
  19. va_list ap;
  20. char **argv;
  21. unsigned int argv_size;
  22. const char *arg;
  23. const char *rpos;
  24. char *pos;
  25. char *cp;
  26. const char *last;
  27. struct GNUNET_OS_Process *proc;
  28. argv_size = 1;
  29. va_start (ap, first_arg);
  30. arg = first_arg;
  31. last = NULL;
  32. do
  33. {
  34. rpos = arg;
  35. while ('\0' != *rpos)
  36. {
  37. if (' ' == *rpos)
  38. {
  39. if (last != NULL)
  40. argv_size++;
  41. last = NULL;
  42. while (' ' == *rpos)
  43. rpos++;
  44. }
  45. if ( (last == NULL) && (*rpos != '\0') )
  46. last = rpos;
  47. if (*rpos != '\0')
  48. rpos++;
  49. }
  50. if (last != NULL)
  51. argv_size++;
  52. }
  53. while (NULL != (arg = (va_arg (ap, const char*))));
  54. va_end (ap);
  55. argv = GNUNET_malloc (argv_size * sizeof (char *));
  56. argv_size = 0;
  57. va_start (ap, first_arg);
  58. arg = first_arg;
  59. last = NULL;
  60. do
  61. {
  62. cp = GNUNET_strdup (arg);
  63. pos = cp;
  64. while ('\0' != *pos)
  65. {
  66. if (' ' == *pos)
  67. {
  68. *pos = '\0';
  69. if (last != NULL)
  70. argv[argv_size++] = GNUNET_strdup (last);
  71. last = NULL;
  72. pos++;
  73. while (' ' == *pos)
  74. pos++;
  75. }
  76. if ( (last == NULL) && (*pos != '\0') )
  77. last = pos;
  78. if (*pos != '\0')
  79. pos++;
  80. }
  81. if (last != NULL)
  82. argv[argv_size++] = GNUNET_strdup (last);
  83. last = NULL;
  84. GNUNET_free (cp);
  85. }
  86. while (NULL != (arg = (va_arg (ap, const char*))));
  87. va_end (ap);
  88. argv[argv_size] = NULL;
  89. proc = GNUNET_OS_start_process_v (lsocks, argv[0], argv);
  90. while (argv_size > 0)
  91. GNUNET_free (argv[--argv_size]);
  92. GNUNET_free (argv);
  93. return proc;
  94. }