lynx_test_child.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* A CLAP effect whose editor starts child processes as it closes, for testing what LYNX hands to the child processes
  2. * of the plugins it hosts (issue #359, round 190th; Linux):
  3. * cc -shared -fPIC -O2 -Ithird_party/clap/include tools/testplugin/lynx_test_child.c -o "LYNX Test Child.clap"
  4. * LYNX_TEST_CHILD_REPORT=/abs/child.txt LYNX --editortest "LYNX Test Child.clap" report.txt
  5. * Otherwise a 0.5 gain (in -> out). Its editor (CLAP gui, X11) draws nothing. When the host destroys the editor, on the
  6. * thread that does it, it runs these and appends one line each to $LYNX_TEST_CHILD_REPORT:
  7. * probe= children (fork + exec of grep and ls, no shell) report their blocked signals, their ignored signals and the descriptors they
  8. * inherited (they should get an empty mask, default dispositions and only 0 / 1 / 2)
  9. * term= a child that waits for SIGTERM and exits 0 on it: it must have exited within 3 s of the kill
  10. * eof= a child that reads its stdin (a pipe) until the end: it must end within 3 s of our closing the pipe
  11. * nofork= a child that does not exec (fork only, as JUCE's web view when it runs in the forked copy): SIGTERM must
  12. * end it within 3 s (in LYNX it ran LYNX's own SIGTERM handler, which wrote to LYNX's pipe: the child
  13. * lived on and the host process took the signal as its own and ended)
  14. * juce= JUCE 8's GtkChildProcess::killChild (juce_WebBrowserComponent_linux.cpp), as Xfer Shrub 1.07 runs it
  15. * when its editor closes: a web-view helper that is still there after 1.5 s is sent SIGTERM in a loop
  16. * that ends only on WIFEXITED, so once the helper died of the signal the loop spun for ever (100 % CPU in
  17. * kill + waitpid, the #359 hang). Here the loop is cut after 3 s and reported as "spin".
  18. * Each line is "<name>=OK ..." or "<name>=FAIL ...". LYNX_TEST_CHILD=<name> runs only that one.
  19. */
  20. #define _GNU_SOURCE
  21. #include <clap/clap.h>
  22. #include <dirent.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <signal.h>
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <sys/wait.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. static const char* const features[] = { CLAP_PLUGIN_FEATURE_AUDIO_EFFECT, CLAP_PLUGIN_FEATURE_UTILITY, NULL };
  35. static const clap_plugin_descriptor_t descriptor = {
  36. CLAP_VERSION_INIT, "org.lynx.test-child", "LYNX Test Child", "LYNX", "https://github.com/Ka1-S/LYNX-SHARE", "", "", "1.0.0",
  37. "Starts child processes as its editor closes (host signal / descriptor hygiene tests)", features
  38. };
  39. static double now_ms (void)
  40. {
  41. struct timespec t;
  42. clock_gettime (CLOCK_MONOTONIC, &t);
  43. return t.tv_sec * 1000.0 + t.tv_nsec / 1.0e6;
  44. }
  45. static void sleep_ms (int ms)
  46. {
  47. struct timespec t = { ms / 1000, (long) (ms % 1000) * 1000000L };
  48. while (nanosleep (&t, &t) != 0 && errno == EINTR) {}
  49. }
  50. static int wanted (const char* name)
  51. {
  52. const char* only = getenv ("LYNX_TEST_CHILD");
  53. return only == NULL || *only == 0 || strcmp (only, name) == 0;
  54. }
  55. static void report (const char* line)
  56. {
  57. const char* path = getenv ("LYNX_TEST_CHILD_REPORT");
  58. if (path == NULL || *path == 0)
  59. return;
  60. const int fd = open (path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644);
  61. if (fd < 0)
  62. return;
  63. (void) ! write (fd, line, strlen (line));
  64. (void) ! write (fd, "\n", 1);
  65. close (fd);
  66. }
  67. /* fork + exec /bin/sh -c script, with stdin / stdout from the given descriptors (-1: inherited as they are) */
  68. static pid_t spawn_sh (const char* script, int in, int out)
  69. {
  70. const pid_t pid = fork();
  71. if (pid == 0)
  72. {
  73. if (in >= 0 && in != 0) dup2 (in, 0);
  74. if (out >= 0 && out != 1) dup2 (out, 1);
  75. execl ("/bin/sh", "sh", "-c", script, (char*) NULL);
  76. _exit (127);
  77. }
  78. return pid;
  79. }
  80. /* waits up to ms for the child; 1 = reaped (status set), 0 = still there, -1 = error */
  81. static int wait_for (pid_t pid, int* status, int ms)
  82. {
  83. const double until = now_ms() + ms;
  84. for (;;)
  85. {
  86. const pid_t r = waitpid (pid, status, WNOHANG);
  87. if (r == pid) return 1;
  88. if (r < 0 && errno != EINTR) return -1;
  89. if (now_ms() > until) return 0;
  90. sleep_ms (10);
  91. }
  92. }
  93. static void reap_hard (pid_t pid)
  94. {
  95. int status = 0;
  96. kill (pid, SIGKILL);
  97. (void) wait_for (pid, &status, 3000);
  98. }
  99. /* signals whose state a host must not hand on (SIGINT / SIGQUIT: a shell's background job gets them ignored) */
  100. static const int watched[] = { SIGHUP, SIGPIPE, SIGTERM, SIGCHLD, SIGALRM, SIGUSR1, SIGUSR2, SIGPROF, SIGCONT, SIGWINCH };
  101. /* runs argv (fork + exec, no shell) with stdout into out[used...]; 1 = it ran and exited 0 */
  102. static int run_into (const char* const* argv, char* out, size_t size, size_t* used)
  103. {
  104. int fds[2];
  105. if (pipe2 (fds, O_CLOEXEC) != 0) return 0;
  106. const pid_t pid = fork();
  107. if (pid == 0)
  108. {
  109. dup2 (fds[1], 1);
  110. execv (argv[0], (char* const*) argv);
  111. _exit (127);
  112. }
  113. close (fds[1]);
  114. for (;;)
  115. {
  116. const ssize_t n = read (fds[0], out + *used, size - 1 - *used);
  117. if (n > 0) { *used += (size_t) n; continue; }
  118. if (n < 0 && errno == EINTR) continue;
  119. break;
  120. }
  121. close (fds[0]);
  122. int status = 0;
  123. if (pid < 0) return 0;
  124. if (wait_for (pid, &status, 3000) != 1) { reap_hard (pid); return 0; }
  125. return WIFEXITED (status) && WEXITSTATUS (status) == 0;
  126. }
  127. static void run_probe (void)
  128. {
  129. char out[4096] = { 0 }, line[600];
  130. size_t used = 0;
  131. /* straight from here, no shell (dash clears the signal mask it inherits): grep reads its own status, ls lists its
  132. own descriptors (its directory one aside) */
  133. const char* const grep[] = { "/bin/grep", "-E", "^Sig(Blk|Ign):", "/proc/self/status", NULL };
  134. const char* const ls[] = { "/bin/ls", "-l", "/proc/self/fd", NULL };
  135. if (! run_into (grep, out, sizeof (out), &used) || ! run_into (ls, out, sizeof (out), &used)) { report ("probe=FAIL the probe did not finish"); return; }
  136. unsigned long long blocked = 0, ignored = 0;
  137. const char* b = strstr (out, "SigBlk:");
  138. const char* i = strstr (out, "SigIgn:");
  139. if (b == NULL || i == NULL) { report ("probe=FAIL no /proc status"); return; }
  140. blocked = strtoull (b + 7, NULL, 16);
  141. ignored = strtoull (i + 7, NULL, 16);
  142. char bad[400] = { 0 };
  143. for (size_t k = 0; k < sizeof (watched) / sizeof (watched[0]); ++k)
  144. {
  145. const unsigned long long bit = 1ULL << (watched[k] - 1);
  146. char one[40];
  147. if (blocked & bit) { snprintf (one, sizeof (one), " blocked:%d", watched[k]); strncat (bad, one, sizeof (bad) - strlen (bad) - 1); }
  148. if (ignored & bit) { snprintf (one, sizeof (one), " ignored:%d", watched[k]); strncat (bad, one, sizeof (bad) - strlen (bad) - 1); }
  149. }
  150. /* the inherited descriptors: "lrwx------ 1 user user 64 date N -> target" lines, N > 2 */
  151. char fdlist[2000] = { 0 };
  152. int leaked = 0;
  153. for (const char* arrow = strstr (out, " -> "); arrow != NULL; arrow = strstr (arrow + 4, " -> "))
  154. {
  155. const char* num = arrow;
  156. while (num > out && num[-1] != ' ' && num[-1] != '\n')
  157. --num;
  158. const int fd = atoi (num);
  159. const char* e = strchr (arrow, '\n');
  160. const size_t len = e != NULL ? (size_t) (e - arrow - 4) : strlen (arrow + 4);
  161. if (fd <= 2 || (strncmp (arrow + 4, "/proc/", 6) == 0 && len >= 3 && strncmp (arrow + 4 + len - 3, "/fd", 3) == 0))
  162. continue; /* (ls's own directory) */
  163. char one[300];
  164. snprintf (one, sizeof (one), " [%d %.*s]", fd, (int) (len < 250 ? len : 250), arrow + 4);
  165. if (strlen (fdlist) + strlen (one) + 1 < sizeof (fdlist))
  166. strcat (fdlist, one);
  167. ++leaked;
  168. }
  169. snprintf (line, sizeof (line), "probe=%s blocked=%llx ignored=%llx%s inherited_fds=%d%s", (bad[0] == 0 && leaked == 0) ? "OK" : "FAIL",
  170. blocked, ignored, bad, leaked, fdlist);
  171. report (line);
  172. }
  173. static void run_term (void)
  174. {
  175. char line[200];
  176. const pid_t pid = spawn_sh ("trap 'exit 0' TERM; while :; do sleep 0.05; done", -1, -1);
  177. if (pid < 0) { report ("term=FAIL fork"); return; }
  178. sleep_ms (300);
  179. kill (pid, SIGTERM);
  180. const double t0 = now_ms();
  181. int status = 0;
  182. const int r = wait_for (pid, &status, 3000);
  183. if (r != 1) { reap_hard (pid); report ("term=FAIL the child did not end on SIGTERM within 3 s"); return; }
  184. snprintf (line, sizeof (line), "term=%s exited=%d code=%d signal=%d ms=%.0f", WIFEXITED (status) && WEXITSTATUS (status) == 0 ? "OK" : "FAIL",
  185. WIFEXITED (status) ? 1 : 0, WIFEXITED (status) ? WEXITSTATUS (status) : -1, WIFSIGNALED (status) ? WTERMSIG (status) : 0, now_ms() - t0);
  186. report (line);
  187. }
  188. static void run_eof (void)
  189. {
  190. char line[200];
  191. int fds[2];
  192. if (pipe2 (fds, O_CLOEXEC) != 0) { report ("eof=FAIL pipe"); return; }
  193. const pid_t pid = spawn_sh ("cat > /dev/null; exit 0", fds[0], -1);
  194. close (fds[0]);
  195. if (pid < 0) { close (fds[1]); report ("eof=FAIL fork"); return; }
  196. (void) ! write (fds[1], "x\n", 2);
  197. sleep_ms (100);
  198. close (fds[1]);
  199. const double t0 = now_ms();
  200. int status = 0;
  201. if (wait_for (pid, &status, 3000) != 1) { reap_hard (pid); report ("eof=FAIL the child did not see the end of its stdin within 3 s"); return; }
  202. snprintf (line, sizeof (line), "eof=%s code=%d ms=%.0f", WIFEXITED (status) && WEXITSTATUS (status) == 0 ? "OK" : "FAIL",
  203. WIFEXITED (status) ? WEXITSTATUS (status) : -1, now_ms() - t0);
  204. report (line);
  205. }
  206. static void run_nofork (void)
  207. {
  208. char line[200];
  209. const pid_t pid = fork();
  210. if (pid == 0)
  211. {
  212. for (;;)
  213. pause();
  214. }
  215. if (pid < 0) { report ("nofork=FAIL fork"); return; }
  216. sleep_ms (200);
  217. kill (pid, SIGTERM);
  218. int status = 0;
  219. if (wait_for (pid, &status, 3000) != 1) { reap_hard (pid); report ("nofork=FAIL a forked copy lived on after SIGTERM (it ran the host's handler)"); return; }
  220. snprintf (line, sizeof (line), "nofork=%s signal=%d", WIFSIGNALED (status) && WTERMSIG (status) == SIGTERM ? "OK" : "FAIL",
  221. WIFSIGNALED (status) ? WTERMSIG (status) : 0);
  222. report (line);
  223. }
  224. /* JUCE 8.0's killChild, as compiled into the plugins that use juce::WebBrowserComponent on Linux, with a 3 s cut-off */
  225. static void run_juce (void)
  226. {
  227. char line[200];
  228. /* a "web view helper" that takes longer than JUCE's 1.5 s to quit, and dies of SIGTERM (its default action) */
  229. const pid_t childProcess = spawn_sh ("exec sleep 30", -1, -1);
  230. if (childProcess < 0) { report ("juce=FAIL fork"); return; }
  231. const double t0 = now_ms();
  232. int status = 0, result = 0;
  233. result = waitpid (childProcess, &status, WNOHANG);
  234. for (int i = 0; i < 15 && (! WIFEXITED (status) || result != childProcess); ++i)
  235. {
  236. sleep_ms (100);
  237. result = waitpid (childProcess, &status, WNOHANG);
  238. }
  239. status = 0;
  240. long loops = 0;
  241. int spun = 0;
  242. if (! WIFEXITED (status) || result != childProcess)
  243. {
  244. for (;;)
  245. {
  246. kill (childProcess, SIGTERM);
  247. waitpid (childProcess, &status, 0);
  248. if (WIFEXITED (status))
  249. break;
  250. if ((++loops & 1023) == 0 && now_ms() - t0 > 4500) { spun = 1; break; }
  251. }
  252. }
  253. snprintf (line, sizeof (line), "juce=%s loops=%ld ms=%.0f", spun ? "FAIL spin (kill + waitpid for ever, issue #359)" : "OK", loops, now_ms() - t0);
  254. report (line);
  255. }
  256. static void on_editor_closed (void)
  257. {
  258. if (wanted ("probe")) run_probe();
  259. if (wanted ("term")) run_term();
  260. if (wanted ("eof")) run_eof();
  261. if (wanted ("nofork")) run_nofork();
  262. if (wanted ("juce")) run_juce();
  263. report ("done=1");
  264. }
  265. static uint32_t ports_count (const clap_plugin_t* p, bool in) { (void) p; (void) in; return 1; }
  266. static bool ports_get (const clap_plugin_t* p, uint32_t index, bool in, clap_audio_port_info_t* info)
  267. {
  268. (void) p;
  269. if (index != 0) return false;
  270. memset (info, 0, sizeof (*info));
  271. info->id = in ? 0 : 1;
  272. strcpy (info->name, in ? "In" : "Out");
  273. info->flags = CLAP_AUDIO_PORT_IS_MAIN;
  274. info->channel_count = 2;
  275. info->port_type = CLAP_PORT_STEREO;
  276. info->in_place_pair = CLAP_INVALID_ID;
  277. return true;
  278. }
  279. static const clap_plugin_audio_ports_t audio_ports = { ports_count, ports_get };
  280. static bool p_init (const clap_plugin_t* p) { (void) p; return true; }
  281. static void p_destroy (const clap_plugin_t* p) { free ((void*) p); }
  282. static bool p_activate (const clap_plugin_t* p, double r, uint32_t a, uint32_t b) { (void) p; (void) r; (void) a; (void) b; return true; }
  283. static void p_deactivate (const clap_plugin_t* p) { (void) p; }
  284. static bool p_start (const clap_plugin_t* p) { (void) p; return true; }
  285. static void p_stop (const clap_plugin_t* p) { (void) p; }
  286. static void p_reset (const clap_plugin_t* p) { (void) p; }
  287. static clap_process_status p_process (const clap_plugin_t* p, const clap_process_t* proc)
  288. {
  289. (void) p;
  290. if (proc->audio_inputs_count < 1 || proc->audio_outputs_count < 1) return CLAP_PROCESS_CONTINUE;
  291. for (uint32_t c = 0; c < 2 && c < proc->audio_inputs[0].channel_count && c < proc->audio_outputs[0].channel_count; ++c)
  292. for (uint32_t i = 0; i < proc->frames_count; ++i)
  293. proc->audio_outputs[0].data32[c][i] = 0.5f * proc->audio_inputs[0].data32[c][i];
  294. return CLAP_PROCESS_CONTINUE;
  295. }
  296. static bool g_api (const clap_plugin_t* p, const char* api, bool floating) { (void) p; return ! floating && strcmp (api, CLAP_WINDOW_API_X11) == 0; }
  297. static bool g_preferred (const clap_plugin_t* p, const char** api, bool* floating) { (void) p; *api = CLAP_WINDOW_API_X11; *floating = false; return true; }
  298. static bool g_create (const clap_plugin_t* p, const char* api, bool floating) { return g_api (p, api, floating); }
  299. static void g_destroy (const clap_plugin_t* p) { (void) p; on_editor_closed(); }
  300. static bool g_scale (const clap_plugin_t* p, double s) { (void) p; (void) s; return true; }
  301. static bool g_size (const clap_plugin_t* p, uint32_t* w, uint32_t* h) { (void) p; *w = 240; *h = 120; return true; }
  302. static bool g_can_resize (const clap_plugin_t* p) { (void) p; return false; }
  303. static bool g_hints (const clap_plugin_t* p, clap_gui_resize_hints_t* h) { (void) p; (void) h; return false; }
  304. static bool g_adjust (const clap_plugin_t* p, uint32_t* w, uint32_t* h) { (void) p; *w = 240; *h = 120; return true; }
  305. static bool g_set_size (const clap_plugin_t* p, uint32_t w, uint32_t h) { (void) p; (void) w; (void) h; return true; }
  306. static bool g_parent (const clap_plugin_t* p, const clap_window_t* window) { (void) p; (void) window; return true; }
  307. static bool g_transient (const clap_plugin_t* p, const clap_window_t* w) { (void) p; (void) w; return false; }
  308. static void g_title (const clap_plugin_t* p, const char* t) { (void) p; (void) t; }
  309. static bool g_show (const clap_plugin_t* p) { (void) p; return true; }
  310. static bool g_hide (const clap_plugin_t* p) { (void) p; return true; }
  311. static const clap_plugin_gui_t gui = { g_api, g_preferred, g_create, g_destroy, g_scale, g_size, g_can_resize, g_hints, g_adjust,
  312. g_set_size, g_parent, g_transient, g_title, g_show, g_hide };
  313. static const void* p_extension (const clap_plugin_t* p, const char* id)
  314. {
  315. (void) p;
  316. if (strcmp (id, CLAP_EXT_GUI) == 0)
  317. return &gui;
  318. return strcmp (id, CLAP_EXT_AUDIO_PORTS) == 0 ? &audio_ports : NULL;
  319. }
  320. static void p_main_thread (const clap_plugin_t* p) { (void) p; }
  321. static uint32_t f_count (const clap_plugin_factory_t* f) { (void) f; return 1; }
  322. static const clap_plugin_descriptor_t* f_descriptor (const clap_plugin_factory_t* f, uint32_t i) { (void) f; return i == 0 ? &descriptor : NULL; }
  323. static const clap_plugin_t* f_create (const clap_plugin_factory_t* f, const clap_host_t* host, const char* id)
  324. {
  325. (void) f; (void) host;
  326. if (strcmp (id, descriptor.id) != 0) return NULL;
  327. clap_plugin_t* p = (clap_plugin_t*) calloc (1, sizeof (clap_plugin_t));
  328. p->desc = &descriptor;
  329. p->init = p_init; p->destroy = p_destroy; p->activate = p_activate; p->deactivate = p_deactivate;
  330. p->start_processing = p_start; p->stop_processing = p_stop; p->reset = p_reset; p->process = p_process;
  331. p->get_extension = p_extension; p->on_main_thread = p_main_thread;
  332. return p;
  333. }
  334. static const clap_plugin_factory_t factory = { f_count, f_descriptor, f_create };
  335. static bool e_init (const char* path) { (void) path; return true; }
  336. static void e_deinit (void) {}
  337. static const void* e_factory (const char* id) { return strcmp (id, CLAP_PLUGIN_FACTORY_ID) == 0 ? &factory : NULL; }
  338. CLAP_EXPORT const clap_plugin_entry_t clap_entry = { CLAP_VERSION_INIT, e_init, e_deinit, e_factory };