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.

183 lines
4.6KB

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