Files
lotherkandClaude Sonnet 5.5 cc770d000e dtfile: a mount no longer freezes the window; Connecting dialog, Cancel, Dtfile*connectTimeout (t0080 item 4)
mcde_sys_net_mount ran in the Xt callback and waited for sshfs/mount_afp
without a limit: a server that is gone froze the window for as long as the
tool lived (measured: the call returns after 20.0 s with a tool that sleeps
20 s; with one that never ends, never).

McdeSys: mcde_sys_net_mount_start/_job_fd/_job_finish/_job_cancel.  The tool
runs under a runner process (its own process group) that writes the answer
to a pipe: dtfile sets SIGCHLD to SIG_IGN (Main.c), where waitpid gives no
status.  mcde_sys_net_mount and _start share one plan (MountPlanFor).
test_job (make check, 5 of 5 pass) covers success, failure, a missing tool,
a password on stdin, and cancel with the tool AND what it started gone, with
SIGCHLD default and ignored; without the group kill it fails (mutant run).

dtfile: _McdeNetMount (NetView.c) for all three mounts (network view,
Connect to Server, column view): a modal "Connecting ..." dialog with Cancel
(also close button and Escape), the descriptor as Xt input (no timer
polling), one time-out, Dtfile*connectTimeout, 20 s, 3..600 (app-defaults,
man page).

Measured (real dtfile on Xvfb, fake sshfs that sleeps 40 s, connectTimeout
4): the dialog shows and repaints when moved; the time-out gives "Could not
connect: The server did not answer within 4 seconds.", Escape gives
"Could not connect: Cancelled."; sleep processes while connecting 1, after
the time-out 0, after Escape 0; no dialog left.  One first run showed a 40 s
stall of the whole session after Return (import blocked too); not
reproduced in three further runs, cause unknown.  Cross compile for NetBSD
(x86_64--netbsd-gcc, the build's flags): netmount.c and NetView.c without a
warning; common.c's old settimeofday declaration warning is not new.  Not
done: the NFS and AFP tools (only sshfs was faked), the afp://10.255.255.1/
case of the task with a real mount_afp.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 00:28:27 +00:00

179 lines
4.6 KiB
C

/*
* test_job.c -- McdeSys: a tool run as a job, not waited for (make check)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Connecting to a server that is gone keeps sshfs or mount_afp busy for
* minutes; dtfile starts it as a job and keeps drawing (tasksd t0080, item 4).
* What is checked: a quick tool is reported through the descriptor, with its
* success or failure; a password reaches the tool's standard input; a tool that
* runs on is still running when asked (the descriptor is not readable) and
* mcde_sys_job_cancel ends it AND what it started (the same process group);
* and all of it with SIGCHLD ignored, as dtfile has it -- waitpid(2) then
* gives no status, the pipe does.
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <Mcde/McdeSys.h>
#include "private.h" /* mcde_sys_job_start: the library's, not the API */
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static double
now(void)
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (double) t.tv_sec + (double) t.tv_nsec / 1e9;
}
/* is the job's descriptor readable within MS milliseconds */
static int
ready(McdeSysJob *job, int ms)
{
struct pollfd p;
p.fd = mcde_sys_job_fd(job);
p.events = POLLIN;
return poll(&p, 1, ms) > 0;
}
/* run ARGV (with INPUT) as a job to its end; the result of finish, or -99 */
static int
whole(char *const argv[], const char *input, double *seconds)
{
McdeSysJob *job;
double t0 = now();
if ((job = mcde_sys_job_start(argv, input)) == NULL)
return -99;
if (!ready(job, 10000)) {
(void) mcde_sys_job_cancel(job, "too slow");
return -98;
}
*seconds = now() - t0;
return mcde_sys_job_finish(job);
}
static int
alive(pid_t pid)
{
return kill(pid, 0) == 0 || errno == EPERM;
}
static void
runs(const char *how)
{
char *ok[] = { "/bin/sh", "-c", "exit 0", NULL };
char *bad[] = { "/bin/sh", "-c", "exit 3", NULL };
char *none[] = { "/nonexistent/tool", NULL };
char *in[] = { "/bin/sh", "-c", "read x; test \"$x\" = hunter2", NULL };
char *wrong[] = { "/bin/sh", "-c", "read x; test \"$x\" = other", NULL };
double s = 0;
int r;
printf("-- %s\n", how);
r = whole(ok, NULL, &s);
CHECK(r == MCDE_SYS_OK);
CHECK(s < 2.0);
r = whole(bad, NULL, &s);
CHECK(r == MCDE_SYS_EFAIL);
CHECK(strstr(mcde_sys_error(), "status 3") != NULL);
/* a tool that is not there never starts a job's answer: it is an answer */
r = whole(none, NULL, &s);
CHECK(r == MCDE_SYS_ENOTSUP);
CHECK(strstr(mcde_sys_error(), "/nonexistent/tool") != NULL);
r = whole(in, "hunter2\n", &s);
CHECK(r == MCDE_SYS_OK);
r = whole(wrong, "hunter2\n", &s);
CHECK(r == MCDE_SYS_EFAIL);
}
static void
cancels(void)
{
/* the shell starts a sleep of its own and waits for it: the runner,
* the shell and the sleep are one process group */
char *argv[] = { "/bin/sh", "-c",
"echo $$ > \"$JOBTEST_PID\"; sleep 300 & echo $! >> \"$JOBTEST_PID\"; wait",
NULL };
char file[] = "/tmp/test_job.XXXXXX";
char line[64];
pid_t shell = 0, sleeper = 0;
McdeSysJob *job;
FILE *f;
double t0;
int fd, i;
printf("-- cancel\n");
if ((fd = mkstemp(file)) < 0) {
printf("FAIL mkstemp\n");
failures++;
return;
}
close(fd);
setenv("JOBTEST_PID", file, 1);
job = mcde_sys_job_start(argv, NULL);
CHECK(job != NULL);
if (job == NULL)
return;
/* running on: the descriptor stays quiet while the tool runs */
CHECK(!ready(job, 700));
if ((f = fopen(file, "r")) != NULL) {
if (fgets(line, sizeof line, f) != NULL)
shell = (pid_t) atoi(line);
if (fgets(line, sizeof line, f) != NULL)
sleeper = (pid_t) atoi(line);
fclose(f);
}
CHECK(shell > 1 && sleeper > 1);
CHECK(alive(shell) && alive(sleeper));
t0 = now();
CHECK(mcde_sys_job_cancel(job, "Gave up.") == MCDE_SYS_EFAIL);
CHECK(strcmp(mcde_sys_error(), "Gave up.") == 0);
CHECK(now() - t0 < 2.0);
/* the sleeper's parent was the shell; init takes it over: give the
* kernel a moment to tell */
for (i = 0; i < 20 && alive(sleeper); i++) {
struct timespec pause = { 0, 50000000 };
nanosleep(&pause, NULL);
}
CHECK(!alive(shell));
CHECK(!alive(sleeper));
unlink(file);
}
int
main(void)
{
runs("SIGCHLD default");
signal(SIGCHLD, SIG_IGN); /* what dtfile does (Main.c) */
runs("SIGCHLD ignored");
cancels();
if (failures == 0)
printf("test_job: all passed\n");
return failures != 0;
}