jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.7KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #ifndef ADDON_DIR
  17. #include "config.h"
  18. #endif
  19. #include "JackChannel.h"
  20. #include "JackLibGlobals.h"
  21. #include "JackServerLaunch.h"
  22. using namespace Jack;
  23. #ifndef WIN32
  24. /* Exec the JACK server in this process. Does not return. */
  25. static void start_server_aux(const char* server_name)
  26. {
  27. FILE* fp = 0;
  28. char filename[255];
  29. char arguments[255];
  30. char buffer[255];
  31. char* command = 0;
  32. size_t pos = 0;
  33. size_t result = 0;
  34. char** argv = 0;
  35. int i = 0;
  36. int good = 0;
  37. int ret;
  38. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  39. fp = fopen(filename, "r");
  40. if (!fp) {
  41. fp = fopen("/etc/jackdrc", "r");
  42. }
  43. /* if still not found, check old config name for backwards compatability */
  44. if (!fp) {
  45. fp = fopen("/etc/jackd.conf", "r");
  46. }
  47. if (fp) {
  48. arguments[0] = '\0';
  49. ret = fscanf(fp, "%s", buffer);
  50. while (ret != 0 && ret != EOF) {
  51. strcat(arguments, buffer);
  52. strcat(arguments, " ");
  53. ret = fscanf(fp, "%s", buffer);
  54. }
  55. if (strlen(arguments) > 0) {
  56. good = 1;
  57. }
  58. fclose(fp);
  59. }
  60. if (!good) {
  61. command = (char*)(JACK_LOCATION "/jackd");
  62. strncpy(arguments, JACK_LOCATION "/jackd -T -d "JACK_DEFAULT_DRIVER, 255);
  63. } else {
  64. result = strcspn(arguments, " ");
  65. command = (char*)malloc(result + 1);
  66. strncpy(command, arguments, result);
  67. command[result] = '\0';
  68. }
  69. argv = (char**)malloc(255);
  70. while (1) {
  71. /* insert -T and -nserver_name in front of arguments */
  72. if (i == 1) {
  73. argv[i] = (char*)malloc(strlen ("-T") + 1);
  74. strcpy (argv[i++], "-T");
  75. if (server_name) {
  76. size_t optlen = strlen("-n");
  77. char* buf = (char*)malloc(optlen + strlen(server_name) + 1);
  78. strcpy(buf, "-n");
  79. strcpy(buf + optlen, server_name);
  80. argv[i++] = buf;
  81. }
  82. }
  83. result = strcspn(arguments + pos, " ");
  84. if (result == 0) {
  85. break;
  86. }
  87. argv[i] = (char*)malloc(result + 1);
  88. strncpy(argv[i], arguments + pos, result);
  89. argv[i][result] = '\0';
  90. pos += result + 1;
  91. ++i;
  92. }
  93. argv[i] = 0;
  94. execv(command, argv);
  95. /* If execv() succeeds, it does not return. There's no point
  96. * in calling jack_error() here in the child process. */
  97. fprintf(stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror(errno));
  98. }
  99. static int start_server(const char* server_name, jack_options_t options)
  100. {
  101. if ((options & JackNoStartServer) || getenv("JACK_NO_START_SERVER")) {
  102. return 1;
  103. }
  104. /* The double fork() forces the server to become a child of
  105. * init, which will always clean up zombie process state on
  106. * termination. This even works in cases where the server
  107. * terminates but this client does not.
  108. *
  109. * Since fork() is usually implemented using copy-on-write
  110. * virtual memory tricks, the overhead of the second fork() is
  111. * probably relatively small.
  112. */
  113. switch (fork()) {
  114. case 0: /* child process */
  115. switch (fork()) {
  116. case 0: /* grandchild process */
  117. start_server_aux(server_name);
  118. _exit(99); /* exec failed */
  119. case -1:
  120. _exit(98);
  121. default:
  122. _exit(0);
  123. }
  124. case -1: /* fork() error */
  125. return 1; /* failed to start server */
  126. }
  127. /* only the original parent process goes here */
  128. return 0; /* (probably) successful */
  129. }
  130. int server_connect(char* server_name)
  131. {
  132. JackClientChannelInterface* channel = JackGlobals::MakeClientChannel();
  133. int res = channel->ServerCheck(server_name);
  134. delete channel;
  135. return res;
  136. }
  137. int try_start_server(jack_varargs_t* va, jack_options_t options, jack_status_t* status)
  138. {
  139. if (server_connect(va->server_name) < 0) {
  140. int trys;
  141. if (start_server(va->server_name, options)) {
  142. int my_status1 = *status | JackFailure | JackServerFailed;
  143. *status = (jack_status_t)my_status1;
  144. return -1;
  145. }
  146. trys = 5;
  147. do {
  148. sleep(1);
  149. if (--trys < 0) {
  150. int my_status1 = *status | JackFailure | JackServerFailed;
  151. *status = (jack_status_t)my_status1;
  152. return -1;
  153. }
  154. } while (server_connect(va->server_name) < 0);
  155. int my_status1 = *status | JackServerStarted;
  156. *status = (jack_status_t)my_status1;
  157. }
  158. return 0;
  159. }
  160. #endif