diff --git a/AGENTS.md b/AGENTS.md index 62177d02..78134fe0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -483,9 +483,16 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt - **Network in the column view**: `~/Network` lists the hosts DNS-SD finds (`mcde_sys_net_browse`) instead of files; a host is a column of its NFS exports and SMB shares (`mcde_sys_net_shares`), and a share chosen is - mounted on `~/Network/HOST/NAME` (`mcde_sys_net_mount`) and opened. The - mount needs root; as a user it says why in a message. The sidebar's eject - buttons and a "Computer > Network" root are not built. + mounted on `~/Network/HOST/NAME` and opened. The mount needs root; as a + user it says why in a message. The sidebar's eject + buttons and a "Computer > Network" root are not built. Every mount from + dtfile goes through `_McdeNetMount` (`NetView.c`): a McdeSys job + (`mcde_sys_net_mount_start`; the answer comes through a pipe, because dtfile + ignores SIGCHLD), a modal "Connecting ..." dialog with Cancel, and the + resource `Dtfile*connectTimeout` (20 s); a dead server no longer freezes + the window (tasksd t0080, measured: the tool and the dialog are gone after + the time-out and after Cancel). A mount in an Xt callback is never + `mcde_sys_net_mount`. - **Disk Utility**: the toolbar carries Mount, Unmount, Eject, Verify, Info, Log, Refresh and Unlock; the detail block under the list shows device, mount point, format, capacity, available and used from diff --git a/include/Mcde/McdeSys.h b/include/Mcde/McdeSys.h index 5e7daf5d..9183b986 100644 --- a/include/Mcde/McdeSys.h +++ b/include/Mcde/McdeSys.h @@ -301,6 +301,22 @@ int mcde_sys_net_shares(const char *host, int kind, char (*names)[256], * PASSWORD may be NULL */ int mcde_sys_net_mount(const char *url, const char *mountpoint, const char *password); +/* The same without waiting: a server that is gone keeps sshfs or mount_afp + * busy for minutes, and a program that waits for it does not draw. The + * connection runs as a job in a process of its own. Its descriptor + * (mcde_sys_job_fd) becomes readable when the tool has ended: put it into the + * event loop (XtAppAddInput) and then call mcde_sys_job_finish, which gives + * the result as mcde_sys_net_mount would have and frees the job. + * mcde_sys_job_cancel ends the tool (and its helpers) instead and frees the + * job; a program that gives up (Cancel, a time-out) calls it with the REASON + * it gives the user, which becomes mcde_sys_error(); it returns a failure. + * NULL from _start: not started, mcde_sys_error() says why. */ +typedef struct McdeSysJob McdeSysJob; +McdeSysJob *mcde_sys_net_mount_start(const char *url, const char *mountpoint, + const char *password); +int mcde_sys_job_fd(const McdeSysJob *job); +int mcde_sys_job_finish(McdeSysJob *job); +int mcde_sys_job_cancel(McdeSysJob *job, const char *reason); /* disconnect what is mounted at MOUNTPOINT */ int mcde_sys_net_unmount(const char *mountpoint); diff --git a/lib/McdeSys/Makefile.am b/lib/McdeSys/Makefile.am index dcf31c7b..c2216e8b 100644 --- a/lib/McdeSys/Makefile.am +++ b/lib/McdeSys/Makefile.am @@ -17,7 +17,7 @@ endif libMcdeSys_la_LDFLAGS = -version-info 1:0:0 # what the system tells about itself, for a look by hand: make check runs it -check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes +check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job test_sysinfo_SOURCES = test_sysinfo.c test_sysinfo_CPPFLAGS = -I$(top_srcdir)/include test_sysinfo_LDADD = libMcdeSys.la @@ -30,4 +30,7 @@ test_updatesource_LDADD = libMcdeSys.la test_processes_SOURCES = test_processes.c test_processes_CPPFLAGS = -I$(top_srcdir)/include test_processes_LDADD = libMcdeSys.la -TESTS = test_sysinfo test_netmount test_updatesource test_processes +test_job_SOURCES = test_job.c +test_job_CPPFLAGS = -I$(top_srcdir)/include +test_job_LDADD = libMcdeSys.la +TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job diff --git a/lib/McdeSys/common.c b/lib/McdeSys/common.c index 3630354a..f8ca039c 100644 --- a/lib/McdeSys/common.c +++ b/lib/McdeSys/common.c @@ -256,6 +256,125 @@ Capture(char *const argv[], char *buf, size_t len, int seconds) argv[0], WIFEXITED(st) ? WEXITSTATUS(st) : -1); } +/* + * Jobs: a tool that may take minutes (a server that is gone keeps sshfs and + * mount_afp busy until their own time-out) run without the program waiting + * for it. The tool is started by a process of its own, the runner, which + * does what mcde_sys_run does and writes the answer to a pipe before it + * ends. The program watches the pipe's read end (XtAppAddInput, poll(2)): + * readable means the answer is there -- or the runner is gone. The pipe, + * and not waitpid(2), carries the answer because a program may have set + * SIGCHLD to SIG_IGN (dtfile does), and then waitpid gives no status. The + * runner leads a process group of its own: cancelling ends it and the tool. + */ +struct McdeSysJob { + pid_t pid; /* the runner */ + int fd; /* the read end of the pipe */ +}; + +typedef struct { + int status; + char message[256]; +} JobAnswer; + +McdeSysJob * +mcde_sys_job_start(char *const argv[], const char *input) +{ + McdeSysJob *job; + int fd[2], i; + pid_t pid; + + if ((job = malloc(sizeof *job)) == NULL) { + (void) mcde_sys_fail(MCDE_SYS_EFAIL, "Out of memory."); + return NULL; + } + if (pipe(fd) != 0) { + (void) mcde_sys_fail(MCDE_SYS_EFAIL, "No pipe: %s.", + strerror(errno)); + free(job); + return NULL; + } + /* neither end goes into the tool: only the runner holds the write + * end, so the end of file means the runner has ended */ + for (i = 0; i < 2; i++) + (void) fcntl(fd[i], F_SETFD, FD_CLOEXEC); + pid = fork(); + if (pid < 0) { + (void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot start %s: %s.", + argv[0], strerror(errno)); + (void) close(fd[0]); + (void) close(fd[1]); + free(job); + return NULL; + } + if (pid == 0) { + JobAnswer a; + + (void) setpgid(0, 0); + (void) close(fd[0]); + memset(&a, 0, sizeof a); + a.status = input != NULL ? mcde_sys_run_input(argv, input) : + mcde_sys_run(argv); + if (a.status != MCDE_SYS_OK) + snprintf(a.message, sizeof a.message, "%s", + mcde_sys_error()); + /* one write, less than PIPE_BUF: all of it or none */ + (void) write(fd[1], &a, sizeof a); + _exit(0); + } + (void) setpgid(pid, pid); /* the runner does it too: no race */ + (void) close(fd[1]); + job->pid = pid; + job->fd = fd[0]; + return job; +} + +int +mcde_sys_job_fd(const McdeSysJob *job) +{ + return job->fd; +} + +int +mcde_sys_job_finish(McdeSysJob *job) +{ + JobAnswer a; + ssize_t n; + int st; + + do + n = read(job->fd, &a, sizeof a); + while (n < 0 && errno == EINTR); + (void) close(job->fd); + /* the runner is ending; ECHILD: SIGCHLD is ignored, it is gone */ + while (waitpid(job->pid, &st, 0) < 0 && errno == EINTR) + ; + free(job); + if (n != (ssize_t) sizeof a) + return mcde_sys_fail(MCDE_SYS_EFAIL, "The program that " + "connects ended without an answer."); + if (a.status == MCDE_SYS_OK) + return MCDE_SYS_OK; + a.message[sizeof a.message - 1] = '\0'; + return mcde_sys_fail(a.status, "%s", a.message); +} + +int +mcde_sys_job_cancel(McdeSysJob *job, const char *reason) +{ + int st; + + /* SIGKILL for the group: the runner and the tool. A daemon that the + * tool has started (afpfsd) is in a session of its own and stays. */ + (void) kill(-job->pid, SIGKILL); + (void) close(job->fd); + while (waitpid(job->pid, &st, 0) < 0 && errno == EINTR) + ; + free(job); + return mcde_sys_fail(MCDE_SYS_EFAIL, "%s", reason != NULL ? reason : + "Cancelled."); +} + int mcde_sys_capture(char *const argv[], char *buf, size_t len) { diff --git a/lib/McdeSys/netmount.c b/lib/McdeSys/netmount.c index 145e8ef9..9a25fbc3 100644 --- a/lib/McdeSys/netmount.c +++ b/lib/McdeSys/netmount.c @@ -147,15 +147,31 @@ ParseUrl(const char *url, char *scheme, char *user, char *host, char *path) "0123456789._-") == strlen(user) && strchr(path, '\n') == NULL; } -int -mcde_sys_net_mount(const char *url, const char *mountpoint, - const char *password) +/* the command that connects URL to MOUNTPOINT: the tool, its arguments, and + * what goes to its standard input (a password for sshfs). Kept in one + * structure so that mcde_sys_net_mount (waits) and mcde_sys_net_mount_start + * (does not) run the same thing. A password is in here: the caller clears + * the plan when it has used it. */ +typedef struct { + char target[900]; + char input[300]; + char *argv[12]; + int has_input; +} MountPlan; + +/* fills PLAN; MCDE_SYS_OK, or a failure STATUS with its sentence */ +static int +MountPlanFor(const char *url, const char *mountpoint, const char *password, + MountPlan *plan) { - char scheme[16], user[64], host[256], path[512], target[900]; + char scheme[16], user[64], host[256], path[512]; const char *tool; - char *argv[12]; + char **argv = plan->argv; + char *target = plan->target; + size_t tsize = sizeof plan->target; int n = 0; + memset(plan, 0, sizeof *plan); if (url == NULL || mountpoint == NULL || mountpoint[0] != '/' || !ParseUrl(url, scheme, user, host, path)) return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no address " @@ -164,7 +180,7 @@ mcde_sys_net_mount(const char *url, const char *mountpoint, if ((tool = mcde_sys_which(sshfsPaths)) == NULL) return mcde_sys_fail(MCDE_SYS_ENOTSUP, "sshfs is not " "there."); - snprintf(target, sizeof target, "%s%s%s:%s", user, + snprintf(target, tsize, "%s%s%s:%s", user, user[0] != '\0' ? "@" : "", host, path[0] != '\0' ? path : ""); argv[n++] = (char *) tool; @@ -176,31 +192,26 @@ mcde_sys_net_mount(const char *url, const char *mountpoint, if (password != NULL) { argv[n++] = "-o"; argv[n++] = "password_stdin"; + snprintf(plan->input, sizeof plan->input, "%s\n", + password); + plan->has_input = 1; } else { argv[n++] = "-o"; argv[n++] = "BatchMode=yes"; } argv[n] = NULL; - if (password != NULL) { - char input[300]; - - snprintf(input, sizeof input, "%s\n", password); - n = mcde_sys_run_input(argv, input); - memset(input, 0, sizeof input); - return n; - } - return mcde_sys_run(argv); + return MCDE_SYS_OK; } if (strcmp(scheme, "nfs") == 0) { if ((tool = mcde_sys_which(mountNfsPaths)) == NULL) return mcde_sys_fail(MCDE_SYS_ENOTSUP, "No NFS client."); - snprintf(target, sizeof target, "%s:%s", host, + snprintf(target, tsize, "%s:%s", host, path[0] != '\0' ? path : "/"); argv[n++] = (char *) tool; argv[n++] = target; argv[n++] = (char *) mountpoint; argv[n] = NULL; - return mcde_sys_run(argv); + return MCDE_SYS_OK; } if (strcmp(scheme, "afp") == 0) { if ((tool = mcde_sys_which(mountAfpPaths)) == NULL) @@ -209,7 +220,7 @@ mcde_sys_net_mount(const char *url, const char *mountpoint, if (password != NULL && strpbrk(password, "@/:") != NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, "This password " "cannot go into an AFP address."); - snprintf(target, sizeof target, "afp://%s%s%s%s%s%s", + snprintf(target, tsize, "afp://%s%s%s%s%s%s", user, password != NULL ? ":" : "", password != NULL ? password : "", user[0] != '\0' || password != NULL ? "@" : "", host, path); @@ -217,14 +228,41 @@ mcde_sys_net_mount(const char *url, const char *mountpoint, argv[n++] = target; argv[n++] = (char *) mountpoint; argv[n] = NULL; - n = mcde_sys_run(argv); - memset(target, 0, sizeof target); - return n; + return MCDE_SYS_OK; } return mcde_sys_fail(MCDE_SYS_ENOTSUP, "%s:// cannot be connected to " "here yet.", scheme); } +int +mcde_sys_net_mount(const char *url, const char *mountpoint, + const char *password) +{ + MountPlan plan; + int n; + + if ((n = MountPlanFor(url, mountpoint, password, &plan)) != MCDE_SYS_OK) + return n; + n = plan.has_input ? mcde_sys_run_input(plan.argv, plan.input) : + mcde_sys_run(plan.argv); + memset(&plan, 0, sizeof plan); + return n; +} + +McdeSysJob * +mcde_sys_net_mount_start(const char *url, const char *mountpoint, + const char *password) +{ + MountPlan plan; + McdeSysJob *job; + + if (MountPlanFor(url, mountpoint, password, &plan) != MCDE_SYS_OK) + return NULL; + job = mcde_sys_job_start(plan.argv, plan.has_input ? plan.input : NULL); + memset(&plan, 0, sizeof plan); + return job; +} + int mcde_sys_net_unmount(const char *mountpoint) { diff --git a/lib/McdeSys/private.h b/lib/McdeSys/private.h index 94600910..741ce6eb 100644 --- a/lib/McdeSys/private.h +++ b/lib/McdeSys/private.h @@ -23,6 +23,10 @@ const char *mcde_sys_which(const char *const *paths); /* the same, ending the child after SECONDS (tools that ask the network) */ int mcde_sys_capture_limited(char *const argv[], char *buf, size_t len, int seconds); +/* run ARGV (with INPUT on its standard input, if not NULL) in a job that the + * caller does not wait for (common.c; McdeSys.h: mcde_sys_job_fd, _finish, + * _cancel); NULL, with mcde_sys_error(), if it cannot start */ +McdeSysJob *mcde_sys_job_start(char *const argv[], const char *input); /* run ARGV and write INPUT to its standard input (for chpasswd(8)) */ int mcde_sys_run_input(char *const argv[], const char *input); /* the value after KEY on the first line of FILE that starts with it */ diff --git a/lib/McdeSys/test_job.c b/lib/McdeSys/test_job.c new file mode 100644 index 00000000..17e49bd6 --- /dev/null +++ b/lib/McdeSys/test_job.c @@ -0,0 +1,178 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#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; +} diff --git a/programs/dtfile/Columns.c b/programs/dtfile/Columns.c index 5f6437ae..fd73143a 100644 --- a/programs/dtfile/Columns.c +++ b/programs/dtfile/Columns.c @@ -539,8 +539,8 @@ ColumnChosen(Widget w, XtPointer client, XtPointer call) (void)snprintf(mp, sizeof mp, "%s/%s/%s", NetRoot(), host, wins[k].col[ci].names[entry]); (void)mkdir(mp, 0755); - if (mcde_sys_net_mount(url, mp, NULL) == MCDE_SYS_OK && - IsDir(mp)) { + if (_McdeNetMount(rec->shell, url, mp, NULL) == + MCDE_SYS_OK && IsDir(mp)) { while (wins[k].ncol > ci + 1) FreeColumn(&wins[k].col[--wins[k].ncol]); Preview(k, mp); diff --git a/programs/dtfile/Connect.c b/programs/dtfile/Connect.c index 311edeba..46cee150 100644 --- a/programs/dtfile/Connect.c +++ b/programs/dtfile/Connect.c @@ -277,7 +277,8 @@ ConnectNow(Widget w, XtPointer client, XtPointer call) } Normalise(text, url, sizeof url); XtFree(text); - if (_McdeNetConnectAddress(url, mp, sizeof mp, err, sizeof err)) { + if (_McdeNetConnectAddress(conn.shell, url, mp, sizeof mp, err, + sizeof err)) { RecentAdd(url); XtPopdown(conn.shell); if (conn.rec != NULL) diff --git a/programs/dtfile/Connect.h b/programs/dtfile/Connect.h index fe827150..af1b8fad 100644 --- a/programs/dtfile/Connect.h +++ b/programs/dtfile/Connect.h @@ -16,8 +16,8 @@ extern void _McdeConnectShow(FileMgrRec *rec); /* the address MOUNTED through the system layer: 1 with MP (the mount point) * and an empty ERR, 0 with ERR the reason. NetView.c knows the parse and - * the mount. */ -extern int _McdeNetConnectAddress(const char *url, char *mp, size_t mplen, - char *err, size_t errlen); + * the mount; PARENT is the window the "Connecting ..." dialog stands over. */ +extern int _McdeNetConnectAddress(Widget parent, const char *url, char *mp, + size_t mplen, char *err, size_t errlen); #endif /* _Connect_h */ diff --git a/programs/dtfile/Dtfile b/programs/dtfile/Dtfile index c330ca1e..1a0704c8 100644 --- a/programs/dtfile/Dtfile +++ b/programs/dtfile/Dtfile @@ -25,6 +25,7 @@ Dtfile*searchPath: DT/Dtfile Dtfile*promptUser: True Dtfile*moveThreshold: 10 +Dtfile*connectTimeout: 20 Dtfile*showEmptySet: True Dtfile*filter_files*file_name_text.columns: %|nls-13-#30#| diff --git a/programs/dtfile/Navigation.h b/programs/dtfile/Navigation.h index 0318b678..e3174f2f 100644 --- a/programs/dtfile/Navigation.h +++ b/programs/dtfile/Navigation.h @@ -31,6 +31,12 @@ extern char *_McdeColumnsChosen(FileMgrRec *rec); /* the network view (NetView.c) */ extern void _McdeNetCreate(FileMgrRec *rec, Widget parent, Widget work); extern void _McdeNetShow(FileMgrRec *rec, Boolean on); +/* connect URL to the folder MP as mcde_sys_net_mount does (McdeSys), with the + * window drawing meanwhile: a modal "Connecting ..." dialog with Cancel over + * PARENT, ended by Cancel or Dtfile*connectTimeout; MCDE_SYS_OK or a failure + * with mcde_sys_error() (NetView.c) */ +extern int _McdeNetMount(Widget parent, const char *url, const char *mp, + const char *password); /* "Connect to Server": shows the network view with the address field, as * Command+K asks for it */ extern void _McdeNetConnect(FileMgrRec *rec); diff --git a/programs/dtfile/NetView.c b/programs/dtfile/NetView.c index 3842fe75..7c48ea33 100644 --- a/programs/dtfile/NetView.c +++ b/programs/dtfile/NetView.c @@ -14,10 +14,14 @@ * terminal by SSH, its NFS exports, its SMB shares (listed; this * computer cannot open them yet). * - * Connecting mounts the share on ~/Network/HOST[/NAME] (mcde_sys_net_mount) - * and opens that folder in the window: the view goes back to the icons and + * Connecting mounts the share on ~/Network/HOST[/NAME] (_McdeNetMount) and + * opens that folder in the window: the view goes back to the icons and * dtfile goes there (_McdeNavGoTo). When the server wants a password, a * dialog asks for it (the field shows stars) and the mount is tried again. + * A server that is gone keeps the mount tool busy for minutes: the mount + * runs as a job (McdeSys), a dialog "Connecting ..." with Cancel stands in + * front of the window, which goes on drawing, and the time-out is the + * resource Dtfile*connectTimeout (seconds, 20). * Finding the computers takes a few seconds: it runs after the view is * drawn. * @@ -39,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -176,6 +181,145 @@ Mounted(const char *path) a.st_dev != b.st_dev; } +/* ---- the mount, without freezing the window ------------------------------ */ + +#define CONNECT_SECONDS 20 +#define CONNECT_SECONDS_MIN 3 +#define CONNECT_SECONDS_MAX 600 + +typedef struct { + Boolean done; /* leave the loop */ + Boolean ended; /* the tool has ended: its answer is there */ + Boolean cancelled; /* the user said Cancel */ + XtInputId input; + XtIntervalId timer; +} MountWait; + +static void +WaitInput(XtPointer client, int *source, XtInputId *id) +{ + MountWait *w = (MountWait *) client; + + (void) source; + XtRemoveInput(*id); /* it stays readable: once is enough */ + w->input = 0; + w->ended = True; + w->done = True; +} + +static void +WaitTimer(XtPointer client, XtIntervalId *id) +{ + MountWait *w = (MountWait *) client; + + (void) id; + w->timer = 0; + w->done = True; +} + +static void +WaitCancel(Widget wid, XtPointer client, XtPointer call) +{ + MountWait *w = (MountWait *) client; + + (void) wid; + (void) call; + w->cancelled = True; + w->done = True; +} + +/* Dtfile*connectTimeout, in seconds; a value that is no number or lies + * outside 3 to 600 is the default (resources: the pattern of File.c) */ +static int +ConnectSeconds(Widget w) +{ + Display *dpy = XtDisplay(w); + String name, cls; + char nm[128], cl[128], *type, *end; + XrmValue v; + long n; + + XtGetApplicationNameAndClass(dpy, &name, &cls); + (void) snprintf(nm, sizeof nm, "%s.connectTimeout", name); + (void) snprintf(cl, sizeof cl, "%s.ConnectTimeout", cls); + if (!XrmGetResource(XrmGetDatabase(dpy), nm, cl, &type, &v) || + v.addr == NULL) + return CONNECT_SECONDS; + n = strtol(v.addr, &end, 10); + if (end == v.addr || *end != '\0' || n < CONNECT_SECONDS_MIN || + n > CONNECT_SECONDS_MAX) + return CONNECT_SECONDS; + return (int) n; +} + +/* + * What mcde_sys_net_mount does, with the window drawing meanwhile: the mount + * is a job; a modal "Connecting ..." dialog with Cancel (also the close + * button and Escape) stands over PARENT, and the events are processed until + * the tool has ended (its descriptor is an Xt input: no timer polls it), the + * user cancelled or Dtfile*connectTimeout has passed. Returns what + * mcde_sys_net_mount returns, MCDE_SYS_OK or a failure with + * mcde_sys_error(). A job that is given up is ended, the tool with it. + */ +int +_McdeNetMount(Widget parent, const char *url, const char *mountpoint, + const char *password) +{ + McdeSysJob *job; + MountWait w; + Widget dialog, shell; + XtAppContext app = XtWidgetToApplicationContext(parent); + Display *dpy = XtDisplay(parent); + XmString msg, title; + char text[700]; + Atom del; + Arg args[8]; + int n = 0, seconds = ConnectSeconds(parent); + + if ((job = mcde_sys_net_mount_start(url, mountpoint, password)) == NULL) + return MCDE_SYS_EFAIL; /* mcde_sys_error() says why */ + memset(&w, 0, sizeof w); + (void) snprintf(text, sizeof text, "Connecting to %s ...", url); + msg = XmStringCreateLocalized(text); + title = XmStringCreateLocalized((char *) "Connecting"); + XtSetArg(args[n], XmNmessageString, msg); n++; + XtSetArg(args[n], XmNdialogTitle, title); n++; + XtSetArg(args[n], XmNdialogStyle, XmDIALOG_PRIMARY_APPLICATION_MODAL); n++; + XtSetArg(args[n], XmNdefaultButtonType, XmDIALOG_CANCEL_BUTTON); n++; + XtSetArg(args[n], XmNautoUnmanage, False); n++; + dialog = XmCreateWorkingDialog(parent, (char *) "connecting", args, n); + XmStringFree(msg); + XmStringFree(title); + XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_OK_BUTTON)); + XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON)); + XtAddCallback(dialog, XmNcancelCallback, WaitCancel, &w); + shell = XtParent(dialog); + /* the close button of the window manager is Cancel, not a destroy */ + XtVaSetValues(shell, XmNdeleteResponse, XmDO_NOTHING, NULL); + del = XmInternAtom(dpy, (char *) "WM_DELETE_WINDOW", False); + XmAddWMProtocolCallback(shell, del, WaitCancel, &w); + w.input = XtAppAddInput(app, mcde_sys_job_fd(job), + (XtPointer) XtInputReadMask, WaitInput, &w); + w.timer = XtAppAddTimeOut(app, (unsigned long) seconds * 1000UL, + WaitTimer, &w); + XtManageChild(dialog); + while (!w.done) + XtAppProcessEvent(app, XtIMAll); + if (w.input != 0) + XtRemoveInput(w.input); + if (w.timer != 0) + XtRemoveTimeOut(w.timer); + XtUnmanageChild(dialog); + XtDestroyWidget(shell); + if (w.ended) + return mcde_sys_job_finish(job); + if (w.cancelled) + return mcde_sys_job_cancel(job, "Cancelled."); + (void) snprintf(text, sizeof text, "The server did not answer within " + "%d seconds.", seconds); + return mcde_sys_job_cancel(job, text); +} + static void Open(const char *mountpoint); static void AskPassword(void); @@ -190,8 +334,8 @@ Connect(const char *url, const char *mountpoint, const char *password) } snprintf(text, sizeof text, "Connecting to %s ...", url); Note(text); - if (mcde_sys_net_mount(url, mountpoint, password) == MCDE_SYS_OK && - Mounted(mountpoint)) { + if (_McdeNetMount(nv.rec->shell, url, mountpoint, password) == + MCDE_SYS_OK && Mounted(mountpoint)) { Note(""); Open(mountpoint); return; @@ -608,8 +752,8 @@ _McdeNetConnect(FileMgrRec *rec) * MP (the mount point) and an empty ERR, 0 with ERR the reason. */ int -_McdeNetConnectAddress(const char *url, char *mp, size_t mplen, char *err, - size_t errlen) +_McdeNetConnectAddress(Widget parent, const char *url, char *mp, + size_t mplen, char *err, size_t errlen) { const char *p, *end, *path; char host[256]; @@ -640,7 +784,7 @@ _McdeNetConnectAddress(const char *url, char *mp, size_t mplen, char *err, MountPoint(host, path, mp, mplen); if (Mounted(mp)) return 1; - if (mcde_sys_net_mount(url, mp, NULL) != MCDE_SYS_OK) { + if (_McdeNetMount(parent, url, mp, NULL) != MCDE_SYS_OK) { if (err != NULL) (void)snprintf(err, errlen, "Could not connect: %s", mcde_sys_error()); diff --git a/programs/dtfile/dtfile.man b/programs/dtfile/dtfile.man index 7ed30383..7f74860e 100644 --- a/programs/dtfile/dtfile.man +++ b/programs/dtfile/dtfile.man @@ -213,6 +213,7 @@ openDir OpenDir string current restrictMode restrictMode XmRBoolean False rootTitle RootTitle string ROOT moveThreshold MoveThreshold XmRInt 4(pixels) +connectTimeout ConnectTimeout XmRInt 20(seconds) view View string large_icon order Order string alphabetical direction Direction string ascending @@ -283,6 +284,11 @@ directory on the Desktop. .IP "\fBDtfile*moveThreshold:\fP" This is the number of pixels the cursor can move while a button is down before the drag controller recognizes the button down as a drag. +.IP "\fBDtfile*connectTimeout:\fP" +This is the number of seconds Connect to Server and the network view wait for +a server to answer before they give up with "The server did not answer". +Meanwhile a "Connecting" window with a Cancel button is shown and the file +manager keeps drawing. Values below 3 or above 600 are ignored. .IP "\fBDtfile*view:\fP" This resource allows the user to change the default for the way the icons are represented within a view. The default is "large_icon". Possible