Process_Linux.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #define _POSIX_C_SOURCE 200112L
  16. #include "memory/Allocator.h"
  17. #include "memory/MallocAllocator.h"
  18. #include "util/platform/libc/strlen.h"
  19. #include "util/Process.h"
  20. #include "util/Bits.h"
  21. #include <stdint.h>
  22. #include <unistd.h>
  23. int Process_spawn(char* binaryPath, char** args)
  24. {
  25. int pid = fork();
  26. if (pid < 0) {
  27. return -1;
  28. } else if (pid == 0) {
  29. char** argv;
  30. {
  31. int argCount;
  32. for (argCount = 0; args[argCount]; argCount++);
  33. struct Allocator* alloc = MallocAllocator_new((argCount + 2) * sizeof(char*));
  34. argv = Allocator_calloc(alloc, (argCount + 2), sizeof(char*));
  35. }
  36. for (int i = 1; args[i-1]; i++) {
  37. argv[i] = args[i-1];
  38. }
  39. argv[0] = binaryPath;
  40. // Goodbye :)
  41. setsid();
  42. execvp(binaryPath, argv);
  43. _exit(72);
  44. }
  45. return 0;
  46. }
  47. char* Process_getPath(struct Allocator* alloc)
  48. {
  49. char buff[1024] = {0};
  50. ssize_t pathSize = readlink("/proc/self/exe", buff, 1023);
  51. if (pathSize < 1) {
  52. return NULL;
  53. }
  54. uint32_t length = strlen(buff);
  55. char* output = Allocator_calloc(alloc, length + 1, 1);
  56. Bits_memcpy(output, buff, length);
  57. return output;
  58. }