110-uloop-allow-reusing-the-existing-environment.patch 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. From: Felix Fietkau <[email protected]>
  2. Date: Wed, 8 Oct 2025 19:11:46 +0200
  3. Subject: [PATCH] uloop: allow reusing the existing environment
  4. When passing null as environment argument, reuse existing environ.
  5. This makes it possible to avoid having to duplicate and convert it
  6. by passing the output of getenv().
  7. Signed-off-by: Felix Fietkau <[email protected]>
  8. ---
  9. --- a/lib/uloop.c
  10. +++ b/lib/uloop.c
  11. @@ -1016,7 +1016,22 @@ uc_uloop_process(uc_vm_t *vm, size_t nar
  12. if (pid == 0) {
  13. argp = calloc(ucv_array_length(arguments) + 2, sizeof(char *));
  14. - envp = calloc(ucv_object_length(env_arg) + 1, sizeof(char *));
  15. + envp = environ;
  16. +
  17. + if (env_arg) {
  18. + envp = calloc(ucv_object_length(env_arg) + 1, sizeof(char *));
  19. + i = 0;
  20. + ucv_object_foreach(env_arg, envk, envv) {
  21. + buf = xprintbuf_new();
  22. +
  23. + ucv_stringbuf_printf(buf, "%s=", envk);
  24. + ucv_to_stringbuf(vm, buf, envv, false);
  25. +
  26. + envp[i++] = buf->buf;
  27. +
  28. + free(buf);
  29. + }
  30. + }
  31. if (!argp || !envp)
  32. _exit(-1);
  33. @@ -1026,19 +1041,6 @@ uc_uloop_process(uc_vm_t *vm, size_t nar
  34. for (i = 0; i < ucv_array_length(arguments); i++)
  35. argp[i+1] = ucv_to_string(vm, ucv_array_get(arguments, i));
  36. - i = 0;
  37. -
  38. - ucv_object_foreach(env_arg, envk, envv) {
  39. - buf = xprintbuf_new();
  40. -
  41. - ucv_stringbuf_printf(buf, "%s=", envk);
  42. - ucv_to_stringbuf(vm, buf, envv, false);
  43. -
  44. - envp[i++] = buf->buf;
  45. -
  46. - free(buf);
  47. - }
  48. -
  49. execvpe((const char *)ucv_string_get(executable),
  50. (char * const *)argp, (char * const *)envp);