| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- /* A CLAP effect whose editor starts child processes as it closes, for testing what LYNX hands to the child processes
- * of the plugins it hosts (issue #359, round 190th; Linux):
- * cc -shared -fPIC -O2 -Ithird_party/clap/include tools/testplugin/lynx_test_child.c -o "LYNX Test Child.clap"
- * LYNX_TEST_CHILD_REPORT=/abs/child.txt LYNX --editortest "LYNX Test Child.clap" report.txt
- * Otherwise a 0.5 gain (in -> out). Its editor (CLAP gui, X11) draws nothing. When the host destroys the editor, on the
- * thread that does it, it runs these and appends one line each to $LYNX_TEST_CHILD_REPORT:
- * probe= children (fork + exec of grep and ls, no shell) report their blocked signals, their ignored signals and the descriptors they
- * inherited (they should get an empty mask, default dispositions and only 0 / 1 / 2)
- * term= a child that waits for SIGTERM and exits 0 on it: it must have exited within 3 s of the kill
- * eof= a child that reads its stdin (a pipe) until the end: it must end within 3 s of our closing the pipe
- * nofork= a child that does not exec (fork only, as JUCE's web view when it runs in the forked copy): SIGTERM must
- * end it within 3 s (in LYNX it ran LYNX's own SIGTERM handler, which wrote to LYNX's pipe: the child
- * lived on and the host process took the signal as its own and ended)
- * juce= JUCE 8's GtkChildProcess::killChild (juce_WebBrowserComponent_linux.cpp), as Xfer Shrub 1.07 runs it
- * when its editor closes: a web-view helper that is still there after 1.5 s is sent SIGTERM in a loop
- * that ends only on WIFEXITED, so once the helper died of the signal the loop spun for ever (100 % CPU in
- * kill + waitpid, the #359 hang). Here the loop is cut after 3 s and reported as "spin".
- * Each line is "<name>=OK ..." or "<name>=FAIL ...". LYNX_TEST_CHILD=<name> runs only that one.
- */
- #define _GNU_SOURCE
- #include <clap/clap.h>
- #include <dirent.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <time.h>
- #include <unistd.h>
- static const char* const features[] = { CLAP_PLUGIN_FEATURE_AUDIO_EFFECT, CLAP_PLUGIN_FEATURE_UTILITY, NULL };
- static const clap_plugin_descriptor_t descriptor = {
- CLAP_VERSION_INIT, "org.lynx.test-child", "LYNX Test Child", "LYNX", "https://github.com/Ka1-S/LYNX-SHARE", "", "", "1.0.0",
- "Starts child processes as its editor closes (host signal / descriptor hygiene tests)", features
- };
- static double now_ms (void)
- {
- struct timespec t;
- clock_gettime (CLOCK_MONOTONIC, &t);
- return t.tv_sec * 1000.0 + t.tv_nsec / 1.0e6;
- }
- static void sleep_ms (int ms)
- {
- struct timespec t = { ms / 1000, (long) (ms % 1000) * 1000000L };
- while (nanosleep (&t, &t) != 0 && errno == EINTR) {}
- }
- static int wanted (const char* name)
- {
- const char* only = getenv ("LYNX_TEST_CHILD");
- return only == NULL || *only == 0 || strcmp (only, name) == 0;
- }
- static void report (const char* line)
- {
- const char* path = getenv ("LYNX_TEST_CHILD_REPORT");
- if (path == NULL || *path == 0)
- return;
- const int fd = open (path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644);
- if (fd < 0)
- return;
- (void) ! write (fd, line, strlen (line));
- (void) ! write (fd, "\n", 1);
- close (fd);
- }
- /* fork + exec /bin/sh -c script, with stdin / stdout from the given descriptors (-1: inherited as they are) */
- static pid_t spawn_sh (const char* script, int in, int out)
- {
- const pid_t pid = fork();
- if (pid == 0)
- {
- if (in >= 0 && in != 0) dup2 (in, 0);
- if (out >= 0 && out != 1) dup2 (out, 1);
- execl ("/bin/sh", "sh", "-c", script, (char*) NULL);
- _exit (127);
- }
- return pid;
- }
- /* waits up to ms for the child; 1 = reaped (status set), 0 = still there, -1 = error */
- static int wait_for (pid_t pid, int* status, int ms)
- {
- const double until = now_ms() + ms;
- for (;;)
- {
- const pid_t r = waitpid (pid, status, WNOHANG);
- if (r == pid) return 1;
- if (r < 0 && errno != EINTR) return -1;
- if (now_ms() > until) return 0;
- sleep_ms (10);
- }
- }
- static void reap_hard (pid_t pid)
- {
- int status = 0;
- kill (pid, SIGKILL);
- (void) wait_for (pid, &status, 3000);
- }
- /* signals whose state a host must not hand on (SIGINT / SIGQUIT: a shell's background job gets them ignored) */
- static const int watched[] = { SIGHUP, SIGPIPE, SIGTERM, SIGCHLD, SIGALRM, SIGUSR1, SIGUSR2, SIGPROF, SIGCONT, SIGWINCH };
- /* runs argv (fork + exec, no shell) with stdout into out[used...]; 1 = it ran and exited 0 */
- static int run_into (const char* const* argv, char* out, size_t size, size_t* used)
- {
- int fds[2];
- if (pipe2 (fds, O_CLOEXEC) != 0) return 0;
- const pid_t pid = fork();
- if (pid == 0)
- {
- dup2 (fds[1], 1);
- execv (argv[0], (char* const*) argv);
- _exit (127);
- }
- close (fds[1]);
- for (;;)
- {
- const ssize_t n = read (fds[0], out + *used, size - 1 - *used);
- if (n > 0) { *used += (size_t) n; continue; }
- if (n < 0 && errno == EINTR) continue;
- break;
- }
- close (fds[0]);
- int status = 0;
- if (pid < 0) return 0;
- if (wait_for (pid, &status, 3000) != 1) { reap_hard (pid); return 0; }
- return WIFEXITED (status) && WEXITSTATUS (status) == 0;
- }
- static void run_probe (void)
- {
- char out[4096] = { 0 }, line[600];
- size_t used = 0;
- /* straight from here, no shell (dash clears the signal mask it inherits): grep reads its own status, ls lists its
- own descriptors (its directory one aside) */
- const char* const grep[] = { "/bin/grep", "-E", "^Sig(Blk|Ign):", "/proc/self/status", NULL };
- const char* const ls[] = { "/bin/ls", "-l", "/proc/self/fd", NULL };
- if (! run_into (grep, out, sizeof (out), &used) || ! run_into (ls, out, sizeof (out), &used)) { report ("probe=FAIL the probe did not finish"); return; }
- unsigned long long blocked = 0, ignored = 0;
- const char* b = strstr (out, "SigBlk:");
- const char* i = strstr (out, "SigIgn:");
- if (b == NULL || i == NULL) { report ("probe=FAIL no /proc status"); return; }
- blocked = strtoull (b + 7, NULL, 16);
- ignored = strtoull (i + 7, NULL, 16);
- char bad[400] = { 0 };
- for (size_t k = 0; k < sizeof (watched) / sizeof (watched[0]); ++k)
- {
- const unsigned long long bit = 1ULL << (watched[k] - 1);
- char one[40];
- if (blocked & bit) { snprintf (one, sizeof (one), " blocked:%d", watched[k]); strncat (bad, one, sizeof (bad) - strlen (bad) - 1); }
- if (ignored & bit) { snprintf (one, sizeof (one), " ignored:%d", watched[k]); strncat (bad, one, sizeof (bad) - strlen (bad) - 1); }
- }
- /* the inherited descriptors: "lrwx------ 1 user user 64 date N -> target" lines, N > 2 */
- char fdlist[2000] = { 0 };
- int leaked = 0;
- for (const char* arrow = strstr (out, " -> "); arrow != NULL; arrow = strstr (arrow + 4, " -> "))
- {
- const char* num = arrow;
- while (num > out && num[-1] != ' ' && num[-1] != '\n')
- --num;
- const int fd = atoi (num);
- const char* e = strchr (arrow, '\n');
- const size_t len = e != NULL ? (size_t) (e - arrow - 4) : strlen (arrow + 4);
- if (fd <= 2 || (strncmp (arrow + 4, "/proc/", 6) == 0 && len >= 3 && strncmp (arrow + 4 + len - 3, "/fd", 3) == 0))
- continue; /* (ls's own directory) */
- char one[300];
- snprintf (one, sizeof (one), " [%d %.*s]", fd, (int) (len < 250 ? len : 250), arrow + 4);
- if (strlen (fdlist) + strlen (one) + 1 < sizeof (fdlist))
- strcat (fdlist, one);
- ++leaked;
- }
- snprintf (line, sizeof (line), "probe=%s blocked=%llx ignored=%llx%s inherited_fds=%d%s", (bad[0] == 0 && leaked == 0) ? "OK" : "FAIL",
- blocked, ignored, bad, leaked, fdlist);
- report (line);
- }
- static void run_term (void)
- {
- char line[200];
- const pid_t pid = spawn_sh ("trap 'exit 0' TERM; while :; do sleep 0.05; done", -1, -1);
- if (pid < 0) { report ("term=FAIL fork"); return; }
- sleep_ms (300);
- kill (pid, SIGTERM);
- const double t0 = now_ms();
- int status = 0;
- const int r = wait_for (pid, &status, 3000);
- if (r != 1) { reap_hard (pid); report ("term=FAIL the child did not end on SIGTERM within 3 s"); return; }
- snprintf (line, sizeof (line), "term=%s exited=%d code=%d signal=%d ms=%.0f", WIFEXITED (status) && WEXITSTATUS (status) == 0 ? "OK" : "FAIL",
- WIFEXITED (status) ? 1 : 0, WIFEXITED (status) ? WEXITSTATUS (status) : -1, WIFSIGNALED (status) ? WTERMSIG (status) : 0, now_ms() - t0);
- report (line);
- }
- static void run_eof (void)
- {
- char line[200];
- int fds[2];
- if (pipe2 (fds, O_CLOEXEC) != 0) { report ("eof=FAIL pipe"); return; }
- const pid_t pid = spawn_sh ("cat > /dev/null; exit 0", fds[0], -1);
- close (fds[0]);
- if (pid < 0) { close (fds[1]); report ("eof=FAIL fork"); return; }
- (void) ! write (fds[1], "x\n", 2);
- sleep_ms (100);
- close (fds[1]);
- const double t0 = now_ms();
- int status = 0;
- 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; }
- snprintf (line, sizeof (line), "eof=%s code=%d ms=%.0f", WIFEXITED (status) && WEXITSTATUS (status) == 0 ? "OK" : "FAIL",
- WIFEXITED (status) ? WEXITSTATUS (status) : -1, now_ms() - t0);
- report (line);
- }
- static void run_nofork (void)
- {
- char line[200];
- const pid_t pid = fork();
- if (pid == 0)
- {
- for (;;)
- pause();
- }
- if (pid < 0) { report ("nofork=FAIL fork"); return; }
- sleep_ms (200);
- kill (pid, SIGTERM);
- int status = 0;
- 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; }
- snprintf (line, sizeof (line), "nofork=%s signal=%d", WIFSIGNALED (status) && WTERMSIG (status) == SIGTERM ? "OK" : "FAIL",
- WIFSIGNALED (status) ? WTERMSIG (status) : 0);
- report (line);
- }
- /* JUCE 8.0's killChild, as compiled into the plugins that use juce::WebBrowserComponent on Linux, with a 3 s cut-off */
- static void run_juce (void)
- {
- char line[200];
- /* a "web view helper" that takes longer than JUCE's 1.5 s to quit, and dies of SIGTERM (its default action) */
- const pid_t childProcess = spawn_sh ("exec sleep 30", -1, -1);
- if (childProcess < 0) { report ("juce=FAIL fork"); return; }
- const double t0 = now_ms();
- int status = 0, result = 0;
- result = waitpid (childProcess, &status, WNOHANG);
- for (int i = 0; i < 15 && (! WIFEXITED (status) || result != childProcess); ++i)
- {
- sleep_ms (100);
- result = waitpid (childProcess, &status, WNOHANG);
- }
- status = 0;
- long loops = 0;
- int spun = 0;
- if (! WIFEXITED (status) || result != childProcess)
- {
- for (;;)
- {
- kill (childProcess, SIGTERM);
- waitpid (childProcess, &status, 0);
- if (WIFEXITED (status))
- break;
- if ((++loops & 1023) == 0 && now_ms() - t0 > 4500) { spun = 1; break; }
- }
- }
- snprintf (line, sizeof (line), "juce=%s loops=%ld ms=%.0f", spun ? "FAIL spin (kill + waitpid for ever, issue #359)" : "OK", loops, now_ms() - t0);
- report (line);
- }
- static void on_editor_closed (void)
- {
- if (wanted ("probe")) run_probe();
- if (wanted ("term")) run_term();
- if (wanted ("eof")) run_eof();
- if (wanted ("nofork")) run_nofork();
- if (wanted ("juce")) run_juce();
- report ("done=1");
- }
- static uint32_t ports_count (const clap_plugin_t* p, bool in) { (void) p; (void) in; return 1; }
- static bool ports_get (const clap_plugin_t* p, uint32_t index, bool in, clap_audio_port_info_t* info)
- {
- (void) p;
- if (index != 0) return false;
- memset (info, 0, sizeof (*info));
- info->id = in ? 0 : 1;
- strcpy (info->name, in ? "In" : "Out");
- info->flags = CLAP_AUDIO_PORT_IS_MAIN;
- info->channel_count = 2;
- info->port_type = CLAP_PORT_STEREO;
- info->in_place_pair = CLAP_INVALID_ID;
- return true;
- }
- static const clap_plugin_audio_ports_t audio_ports = { ports_count, ports_get };
- static bool p_init (const clap_plugin_t* p) { (void) p; return true; }
- static void p_destroy (const clap_plugin_t* p) { free ((void*) p); }
- 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; }
- static void p_deactivate (const clap_plugin_t* p) { (void) p; }
- static bool p_start (const clap_plugin_t* p) { (void) p; return true; }
- static void p_stop (const clap_plugin_t* p) { (void) p; }
- static void p_reset (const clap_plugin_t* p) { (void) p; }
- static clap_process_status p_process (const clap_plugin_t* p, const clap_process_t* proc)
- {
- (void) p;
- if (proc->audio_inputs_count < 1 || proc->audio_outputs_count < 1) return CLAP_PROCESS_CONTINUE;
- for (uint32_t c = 0; c < 2 && c < proc->audio_inputs[0].channel_count && c < proc->audio_outputs[0].channel_count; ++c)
- for (uint32_t i = 0; i < proc->frames_count; ++i)
- proc->audio_outputs[0].data32[c][i] = 0.5f * proc->audio_inputs[0].data32[c][i];
- return CLAP_PROCESS_CONTINUE;
- }
- 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; }
- 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; }
- static bool g_create (const clap_plugin_t* p, const char* api, bool floating) { return g_api (p, api, floating); }
- static void g_destroy (const clap_plugin_t* p) { (void) p; on_editor_closed(); }
- static bool g_scale (const clap_plugin_t* p, double s) { (void) p; (void) s; return true; }
- static bool g_size (const clap_plugin_t* p, uint32_t* w, uint32_t* h) { (void) p; *w = 240; *h = 120; return true; }
- static bool g_can_resize (const clap_plugin_t* p) { (void) p; return false; }
- static bool g_hints (const clap_plugin_t* p, clap_gui_resize_hints_t* h) { (void) p; (void) h; return false; }
- static bool g_adjust (const clap_plugin_t* p, uint32_t* w, uint32_t* h) { (void) p; *w = 240; *h = 120; return true; }
- static bool g_set_size (const clap_plugin_t* p, uint32_t w, uint32_t h) { (void) p; (void) w; (void) h; return true; }
- static bool g_parent (const clap_plugin_t* p, const clap_window_t* window) { (void) p; (void) window; return true; }
- static bool g_transient (const clap_plugin_t* p, const clap_window_t* w) { (void) p; (void) w; return false; }
- static void g_title (const clap_plugin_t* p, const char* t) { (void) p; (void) t; }
- static bool g_show (const clap_plugin_t* p) { (void) p; return true; }
- static bool g_hide (const clap_plugin_t* p) { (void) p; return true; }
- 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,
- g_set_size, g_parent, g_transient, g_title, g_show, g_hide };
- static const void* p_extension (const clap_plugin_t* p, const char* id)
- {
- (void) p;
- if (strcmp (id, CLAP_EXT_GUI) == 0)
- return &gui;
- return strcmp (id, CLAP_EXT_AUDIO_PORTS) == 0 ? &audio_ports : NULL;
- }
- static void p_main_thread (const clap_plugin_t* p) { (void) p; }
- static uint32_t f_count (const clap_plugin_factory_t* f) { (void) f; return 1; }
- static const clap_plugin_descriptor_t* f_descriptor (const clap_plugin_factory_t* f, uint32_t i) { (void) f; return i == 0 ? &descriptor : NULL; }
- static const clap_plugin_t* f_create (const clap_plugin_factory_t* f, const clap_host_t* host, const char* id)
- {
- (void) f; (void) host;
- if (strcmp (id, descriptor.id) != 0) return NULL;
- clap_plugin_t* p = (clap_plugin_t*) calloc (1, sizeof (clap_plugin_t));
- p->desc = &descriptor;
- p->init = p_init; p->destroy = p_destroy; p->activate = p_activate; p->deactivate = p_deactivate;
- p->start_processing = p_start; p->stop_processing = p_stop; p->reset = p_reset; p->process = p_process;
- p->get_extension = p_extension; p->on_main_thread = p_main_thread;
- return p;
- }
- static const clap_plugin_factory_t factory = { f_count, f_descriptor, f_create };
- static bool e_init (const char* path) { (void) path; return true; }
- static void e_deinit (void) {}
- static const void* e_factory (const char* id) { return strcmp (id, CLAP_PLUGIN_FACTORY_ID) == 0 ? &factory : NULL; }
- CLAP_EXPORT const clap_plugin_entry_t clap_entry = { CLAP_VERSION_INIT, e_init, e_deinit, e_factory };
|