DTCH(1) General Commands Manual DTCH(1)

dtchdetached sessions

dtch [-s] name [command ...]

dtch -a name

dtch spawns a command in a detachable session. If no command is given, the value of SHELL is used. The dtch process should be run as a background job or with nohup(1).

To attach to an existing session, pass the -a flag. To detach from the session, type ^Q.

The arguments are as follows:

Attach to an existing session.
Sink the output of command while detached.

~/.dtch
Directory of UNIX-domain sockets for each session.

dtch foo vim &
dtch -a foo
August 12, 2019 OpenBSD 7.4

dtch.c in git

/* Copyright (C) 2017-2019  June McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>

#if defined __FreeBSD__
#include <libutil.h>
#elif defined __linux__
#include <pty.h>
#else
#include <util.h>
#endif

static char _;
static struct iovec iov = { .iov_base = &_, .iov_len = 1 };

static ssize_t sendfd(int sock, int fd) {
	size_t len = CMSG_SPACE(sizeof(int));
	char buf[len];
	struct msghdr msg = {
		.msg_iov = &iov,
		.msg_iovlen = 1,
		.msg_control = buf,
		.msg_controllen = len,
	};

	struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type = SCM_RIGHTS;
	*(int *)CMSG_DATA(cmsg) = fd;

	return sendmsg(sock, &msg, 0);
}

static int recvfd(int sock) {
	size_t len = CMSG_SPACE(sizeof(int));
	char buf[len];
	struct msghdr msg = {
		.msg_iov = &iov,
		.msg_iovlen = 1,
		.msg_control = buf,
		.msg_controllen = len,
	};
	if (0 > recvmsg(sock, &msg, 0)) return -1;

	struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
	if (!cmsg || cmsg->cmsg_type != SCM_RIGHTS) {
		errno = ENOMSG;
		return -1;
	}
	return *(int *)CMSG_DATA(cmsg);
}

static struct sockaddr_un addr = { .sun_family = AF_UNIX };

static void handler(int sig) {
	unlink(addr.sun_path);
	_exit(-sig);
}

static void detach(int server, bool sink, char *argv[]) {
	int pty;
	pid_t pid = forkpty(&pty, NULL, NULL, NULL);
	if (pid < 0) err(1, "forkpty");

	if (!pid) {
		execvp(argv[0], argv);
		err(127, "%s", argv[0]);
	}

	signal(SIGINT, handler);
	signal(SIGTERM, handler);

	int error = listen(server, 0);
	if (error) err(1, "listen");

	struct pollfd fds[] = {
		{ .events = POLLIN, .fd = server },
		{ .events = POLLIN, .fd = pty },
	};
	while (0 < poll(fds, (sink ? 2 : 1), -1)) {
		if (fds[0].revents) {
			int client = accept(server, NULL, NULL);
			if (client < 0) err(1, "accept");

			ssize_t len = sendfd(client, pty);
			if (len < 0) warn("sendfd");

			len = recv(client, &_, sizeof(_), 0);
			if (len < 0) warn("recv");

			close(client);
		}

		if (fds[1].revents) {
			char buf[4096];
			ssize_t len = read(pty, buf, sizeof(buf));
			if (len < 0) err(1, "read");
		}

		int status;
		pid_t dead = waitpid(pid, &status, WNOHANG);
		if (dead < 0) err(1, "waitpid");
		if (dead) {
			unlink(addr.sun_path);
			exit(WIFEXITED(status) ? WEXITSTATUS(status) : -WTERMSIG(status));
		}
	}
	err(1, "poll");
}

static struct termios saveTerm;
static void restoreTerm(void) {
	tcsetattr(STDIN_FILENO, TCSADRAIN, &saveTerm);
	fprintf(stderr, "\33c"); // RIS
	warnx("detached");
}

static void nop(int sig) {
	(void)sig;
}

static void attach(int client) {
	int error;

	int pty = recvfd(client);
	if (pty < 0) err(1, "recvfd");
	warnx("attached");

	struct winsize window;
	error = ioctl(STDIN_FILENO, TIOCGWINSZ, &window);
	if (error) err(1, "ioctl");

	struct winsize redraw = { .ws_row = 1, .ws_col = 1 };
	error = ioctl(pty, TIOCSWINSZ, &redraw);
	if (error) err(1, "ioctl");

	error = ioctl(pty, TIOCSWINSZ, &window);
	if (error) err(1, "ioctl");

	error = tcgetattr(STDIN_FILENO, &saveTerm);
	if (error) err(1, "tcgetattr");
	atexit(restoreTerm);

	struct termios raw = saveTerm;
	cfmakeraw(&raw);
	error = tcsetattr(STDIN_FILENO, TCSADRAIN, &raw);
	if (error) err(1, "tcsetattr");

	signal(SIGWINCH, nop);

	char buf[4096];
	struct pollfd fds[] = {
		{ .events = POLLIN, .fd = STDIN_FILENO },
		{ .events = POLLIN, .fd = pty },
	};
	for (;;) {
		int nfds = poll(fds, 2, -1);
		if (nfds < 0) {
			if (errno != EINTR) err(1, "poll");

			error = ioctl(STDIN_FILENO, TIOCGWINSZ, &window);
			if (error) err(1, "ioctl");

			error = ioctl(pty, TIOCSWINSZ, &window);
			if (error) err(1, "ioctl");

			continue;
		}

		if (fds[0].revents) {
			ssize_t len = read(STDIN_FILENO, buf, sizeof(buf));
			if (len < 0) err(1, "read");
			if (!len) break;

			if (len == 1 && buf[0] == CTRL('Q')) break;

			len = write(pty, buf, len);
			if (len < 0) err(1, "write");
		}

		if (fds[1].revents) {
			ssize_t len = read(pty, buf, sizeof(buf));
			if (len < 0) err(1, "read");
			if (!len) break;

			len = write(STDOUT_FILENO, buf, len);
			if (len < 0) err(1, "write");
		}
	}
}

int main(int argc, char *argv[]) {
	int error;

	bool atch = false;
	bool sink = false;

	int opt;
	while (0 < (opt = getopt(argc, argv, "as"))) {
		switch (opt) {
			break; case 'a': atch = true;
			break; case 's': sink = true;
			break; default:  return 1;
		}
	}
	if (optind == argc) errx(1, "no session name");
	const char *name = argv[optind++];

	if (optind == argc) {
		argv[--optind] = getenv("SHELL");
		if (!argv[optind]) errx(1, "SHELL unset");
	}

	const char *home = getenv("HOME");
	if (!home) errx(1, "HOME unset");

	int fd = open(home, 0);
	if (fd < 0) err(1, "%s", home);

	error = mkdirat(fd, ".dtch", 0700);
	if (error && errno != EEXIST) err(1, "%s/.dtch", home);

	close(fd);

	int sock = socket(PF_UNIX, SOCK_STREAM, 0);
	if (sock < 0) err(1, "socket");
	fcntl(sock, F_SETFD, FD_CLOEXEC);

	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/.dtch/%s", home, name);

	if (atch) {
		error = connect(sock, (struct sockaddr *)&addr, SUN_LEN(&addr));
		if (error) err(1, "%s", addr.sun_path);
		attach(sock);
	} else {
		error = bind(sock, (struct sockaddr *)&addr, SUN_LEN(&addr));
		if (error) err(1, "%s", addr.sun_path);
		detach(sock, sink, &argv[optind]);
	}
}