SDL_sysurl.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "../SDL_sysurl.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/wait.h>
  25. #include <errno.h>
  26. #if USE_POSIX_SPAWN
  27. #include <spawn.h>
  28. extern char **environ;
  29. #endif
  30. int SDL_SYS_OpenURL(const char *url)
  31. {
  32. const pid_t pid1 = fork();
  33. if (pid1 == 0) { /* child process */
  34. #if USE_POSIX_SPAWN
  35. pid_t pid2;
  36. const char *args[] = { "xdg-open", url, NULL };
  37. if (posix_spawnp(&pid2, args[0], NULL, NULL, (char **)args, environ) == 0) {
  38. /* Child process doesn't wait for possibly-blocking grandchild. */
  39. _exit(EXIT_SUCCESS);
  40. } else {
  41. _exit(EXIT_FAILURE);
  42. }
  43. #else
  44. pid_t pid2;
  45. /* Clear LD_PRELOAD so Chrome opens correctly when this application is launched by Steam */
  46. unsetenv("LD_PRELOAD");
  47. /* Notice this is vfork and not fork! */
  48. pid2 = vfork();
  49. if (pid2 == 0) { /* Grandchild process will try to launch the url */
  50. execlp("xdg-open", "xdg-open", url, NULL);
  51. _exit(EXIT_FAILURE);
  52. } else if (pid2 < 0) { /* There was an error forking */
  53. _exit(EXIT_FAILURE);
  54. } else {
  55. /* Child process doesn't wait for possibly-blocking grandchild. */
  56. _exit(EXIT_SUCCESS);
  57. }
  58. #endif
  59. } else if (pid1 < 0) {
  60. return SDL_SetError("fork() failed: %s", strerror(errno));
  61. } else {
  62. int status;
  63. if (waitpid(pid1, &status, 0) == pid1) {
  64. if (WIFEXITED(status)) {
  65. if (WEXITSTATUS(status) == 0) {
  66. return 0; /* success! */
  67. } else {
  68. return SDL_SetError("xdg-open reported error or failed to launch: %d", WEXITSTATUS(status));
  69. }
  70. } else {
  71. return SDL_SetError("xdg-open failed for some reason");
  72. }
  73. } else {
  74. return SDL_SetError("Waiting on xdg-open failed: %s", strerror(errno));
  75. }
  76. }
  77. }