From 445bbe3469b057c8b518f7c787527128f724cc1b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Oct 2019 20:02:06 +0100 Subject: wayland: track multiple terminals The wayland 'term' member is gone and replaced by a list, 'terms'. This list contains all currently running terminal (windows). --- terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 301319ff..d889cfca 100644 --- a/terminal.c +++ b/terminal.c @@ -472,7 +472,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, goto err; } - wayl->term = term; + tll_push_back(wayl->terms, term); return term; err: -- cgit v1.3 From 9d5926ce12935e51de8c22a93f9f2b1cbfc6c6d8 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Oct 2019 20:03:11 +0100 Subject: term: add term_shutdown() This function unmaps the terminal window, removes itself from the wayland list of terminals and then finally destroys itself. We ensure we don't get any callbacks/events referring to a free:d terminal struct, we close all terminal related FDs and unmap the wayland window. Then, to be really really sure there aren't any (by the FDM) queued up events, we delay the self-destruction to the next FDM poll iteration, by opening an event FD and adding it to the FDM. The callback for the event FD removes the FD from the FDM again, and closes it. And then proceeds to destroy the terminal. --- terminal.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++--------------- terminal.h | 1 + 2 files changed, 103 insertions(+), 32 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index d889cfca..87adae62 100644 --- a/terminal.c +++ b/terminal.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -30,12 +31,8 @@ fdm_ptmx(struct fdm *fdm, int fd, int events, void *data) { struct terminal *term = data; - if (events & EPOLLHUP) { - term->quit = true; - - if (!(events & EPOLLIN)) - return false; - } + if ((events & EPOLLHUP) && !(events & EPOLLIN)) + return term_shutdown(term); assert(events & EPOLLIN); @@ -101,7 +98,10 @@ fdm_ptmx(struct fdm *fdm, int fd, int events, void *data) } } - return !(events & EPOLLHUP); + if (events & EPOLLHUP) + return term_shutdown(term); + + return true; } static bool @@ -494,24 +494,109 @@ close_fds: return NULL; } -int -term_destroy(struct terminal *term) +static void +close_fd(struct terminal *term, int *fd) { - if (term == NULL) - return 0; + if (*fd == -1) + return; + + fdm_del(term->fdm, *fd); + close(*fd); + *fd = -1; +} + +static void +close_fds(struct terminal *term) +{ + close_fd(term, &term->delayed_render_timer.lower_fd); + close_fd(term, &term->delayed_render_timer.upper_fd); + close_fd(term, &term->blink.fd); + close_fd(term, &term->flash.fd); + close_fd(term, &term->ptmx); +} + +static bool +fdm_shutdown(struct fdm *fdm, int fd, int events, void *data) +{ + LOG_DBG("FDM shutdown"); + struct terminal *term = data; + + fdm_del(term->fdm, fd); + close(fd); + + /* + * Now there shouldn't be any more callbacks or events that can + * trigger, meaning it should be safe to destroy the terminal. + */ + + assert(term->wl->focused != term); + assert(term->wl->moused != term); + + tll_foreach(term->wl->terms, it) { + if (it->item == term) { + tll_remove(term->wl->terms, it); + break; + } + } + + term_destroy(term); + return true; +} + +bool +term_shutdown(struct terminal *term) +{ + /* Ensure we don't get any more events */ + close_fds(term); + /* Destroy the wayland window, and consume the trail of Wayland events */ wayl_win_destroy(term->window); + wl_display_roundtrip(term->wl->display); + term->window = NULL; + + /* + * At this point, we no longer receive any more events referring to us, and it _should_ be safe to destroy ourselves now. + * + * However, in the (unlikely) event that there are events already + * queued up by the FDM, we delay the self-destruction to the next + * FDM poll iteration. + * + * This is done by opening an event FD and adding it to the FDM. + */ + + int event_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); + if (event_fd == -1) { + LOG_ERRNO("failed to create terminal shutdown event FD"); + return false; + } - if (term->delayed_render_timer.lower_fd != -1) { - fdm_del(term->fdm, term->delayed_render_timer.lower_fd); - close(term->delayed_render_timer.lower_fd); + if (!fdm_add(term->fdm, event_fd, EPOLLIN, &fdm_shutdown, term)) { + LOG_ERR("failed to add terminal shutdown event FD to the FDM"); + close(event_fd); + return false; } - if (term->delayed_render_timer.upper_fd != -1) { - fdm_del(term->fdm, term->delayed_render_timer.upper_fd); - close(term->delayed_render_timer.upper_fd); + if (write(event_fd, &(uint64_t){1}, sizeof(uint64_t)) != sizeof(uint64_t)) { + LOG_ERRNO("failed to send terminal shutdown event"); + fdm_del(term->fdm, event_fd); + close(event_fd); + return false; } + return true; +} + +int +term_destroy(struct terminal *term) +{ + if (term == NULL) + return 0; + + close_fds(term); + + if (term->window != NULL) + wayl_win_destroy(term->window); + mtx_lock(&term->render.workers.lock); assert(tll_length(term->render.workers.queue) == 0); @@ -546,21 +631,6 @@ term_destroy(struct terminal *term) free(term->search.buf); - if (term->flash.fd != -1) { - fdm_del(term->fdm, term->flash.fd); - close(term->flash.fd); - } - - if (term->blink.fd != -1) { - fdm_del(term->fdm, term->blink.fd); - close(term->blink.fd); - } - - if (term->ptmx != -1) { - fdm_del(term->fdm, term->ptmx); - close(term->ptmx); - } - for (size_t i = 0; i < term->render.workers.count; i++) { if (term->render.workers.threads[i] != 0) thrd_join(term->render.workers.threads[i], NULL); diff --git a/terminal.h b/terminal.h index b16b9b51..5a0473b3 100644 --- a/terminal.h +++ b/terminal.h @@ -293,6 +293,7 @@ struct config; struct terminal *term_init( const struct config *conf, struct fdm *fdm, struct wayland *wayl, int argc, char *const *argv); +bool term_shutdown(struct terminal *term); int term_destroy(struct terminal *term); void term_reset(struct terminal *term, bool hard); -- cgit v1.3 From 58e31728a0c1c7e4c3b0ecdec12041774dc62b54 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Oct 2019 20:22:01 +0100 Subject: term: term_init(): create wayland window *last* * Configure all FDs *completely* before moving on * Start client before creating the wayland window --- terminal.c | 105 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 57 insertions(+), 48 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 87adae62..6699a25b 100644 --- a/terminal.c +++ b/terminal.c @@ -304,7 +304,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, int delay_lower_fd = -1; int delay_upper_fd = -1; - struct terminal *term = NULL; + struct terminal *term = malloc(sizeof(*term)); if ((ptmx = posix_openpt(O_RDWR | O_NOCTTY)) == -1) { LOG_ERRNO("failed to open PTY"); @@ -325,8 +325,43 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, goto close_fds; } + /* Read logic requires non-blocking mode */ + { + int fd_flags = fcntl(ptmx, F_GETFL); + if (fd_flags == -1) { + LOG_ERRNO("failed to set non blocking mode on PTY master"); + goto err; + } + + if (fcntl(ptmx, F_SETFL, fd_flags | O_NONBLOCK) == -1) { + LOG_ERRNO("failed to set non blocking mode on PTY master"); + goto err; + } + } + + if (!fdm_add(fdm, ptmx, EPOLLIN, &fdm_ptmx, term)) { + LOG_ERR("failed to add ptmx to FDM"); + goto err; + } + + if (!fdm_add(fdm, flash_fd, EPOLLIN, &fdm_flash, term)) { + LOG_ERR("failed to add flash timer FD to FDM"); + goto err; + } + + if (!fdm_add(fdm, blink_fd, EPOLLIN, &fdm_blink, term)) { + LOG_ERR("failed to add blink tiemr FD to FDM"); + goto err; + } + + if (!fdm_add(fdm, delay_lower_fd, EPOLLIN, &fdm_delayed_render, term) || + !fdm_add(fdm, delay_upper_fd, EPOLLIN, &fdm_delayed_render, term)) + { + LOG_ERR("failed to add delayed rendering timer FDs to FDM"); + goto err; + } + /* Initialize configure-based terminal attributes */ - term = malloc(sizeof(*term)); *term = (struct terminal) { .fdm = fdm, .quit = false, @@ -410,6 +445,10 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, term->cell_height = (int)ceil(term->fextents.height); LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); + /* Start the slave/client */ + if (slave_spawn(term->ptmx, argc, argv, conf->shell) == -1) + goto err; + /* Initiailze the Wayland window backend */ if ((term->window = wayl_win_init(wayl)) == NULL) goto err; @@ -432,46 +471,6 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, height = max(height, term->cell_height); render_resize(term, width, height); - /* Start the slave/client */ - if (!slave_spawn(term->ptmx, argc, argv, conf->shell)) - goto err; - - /* Read logic requires non-blocking mode */ - { - int fd_flags = fcntl(ptmx, F_GETFL); - if (fd_flags == -1) { - LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto err; - } - - if (fcntl(ptmx, F_SETFL, fd_flags | O_NONBLOCK) == -1) { - LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto err; - } - } - - if (!fdm_add(fdm, ptmx, EPOLLIN, &fdm_ptmx, term)) { - LOG_ERR("failed to add ptmx to FDM"); - goto err; - } - - if (!fdm_add(fdm, flash_fd, EPOLLIN, &fdm_flash, term)) { - LOG_ERR("failed to add flash timer FD to FDM"); - goto err; - } - - if (!fdm_add(fdm, blink_fd, EPOLLIN, &fdm_blink, term)) { - LOG_ERR("failed to add blink tiemr FD to FDM"); - goto err; - } - - if (!fdm_add(fdm, delay_lower_fd, EPOLLIN, &fdm_delayed_render, term) || - !fdm_add(fdm, delay_upper_fd, EPOLLIN, &fdm_delayed_render, term)) - { - LOG_ERR("failed to add delayed rendering timer FDs to FDM"); - goto err; - } - tll_push_back(wayl->terms, term); return term; @@ -480,17 +479,27 @@ err: return NULL; close_fds: - if (ptmx != -1) + if (ptmx != -1) { + fdm_del(fdm, ptmx); close(ptmx); - if (flash_fd != -1) + } + if (flash_fd != -1) { + fdm_del(fdm, flash_fd); close(flash_fd); - if (blink_fd != -1) + } + if (blink_fd != -1) { + fdm_del(fdm, blink_fd); close(blink_fd); - if (delay_lower_fd != -1) + } + if (delay_lower_fd != -1) { + fdm_del(fdm, delay_lower_fd); close(delay_lower_fd); - if (delay_upper_fd != -1) + } + if (delay_upper_fd != -1) { + fdm_del(fdm, delay_upper_fd); close(delay_upper_fd); - assert(term == NULL); + } + free(term); return NULL; } -- cgit v1.3 From c7238ef7f3c6f182414c7456d3d4ed0faf2ee041 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Oct 2019 20:24:54 +0100 Subject: term: assign client PID to term->slave --- terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 6699a25b..7f3b3023 100644 --- a/terminal.c +++ b/terminal.c @@ -446,7 +446,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); /* Start the slave/client */ - if (slave_spawn(term->ptmx, argc, argv, conf->shell) == -1) + if ((term->slave = slave_spawn(term->ptmx, argc, argv, conf->shell)) == -1) goto err; /* Initiailze the Wayland window backend */ -- cgit v1.3 From 644585a3e5e2ca678a7ad6ce74c8629196ae7d62 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Oct 2019 20:25:45 +0100 Subject: term: term_shutdown(): set exit value in wayland --- terminal.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 7f3b3023..04c39937 100644 --- a/terminal.c +++ b/terminal.c @@ -529,6 +529,7 @@ fdm_shutdown(struct fdm *fdm, int fd, int events, void *data) { LOG_DBG("FDM shutdown"); struct terminal *term = data; + struct wayland *wayl = term->wl; fdm_del(term->fdm, fd); close(fd); @@ -538,17 +539,18 @@ fdm_shutdown(struct fdm *fdm, int fd, int events, void *data) * trigger, meaning it should be safe to destroy the terminal. */ - assert(term->wl->focused != term); - assert(term->wl->moused != term); + assert(wayl->focused != term); + assert(wayl->moused != term); - tll_foreach(term->wl->terms, it) { + tll_foreach(wayl->terms, it) { if (it->item == term) { - tll_remove(term->wl->terms, it); + tll_remove(wayl->terms, it); break; } } - term_destroy(term); + wayl->last_exit_value = term_destroy(term); + LOG_WARN("last-exit-value: %d", wayl->last_exit_value); return true; } -- cgit v1.3 From 4ae22b72a1d565fea23f9f9022503b655bad2353 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Oct 2019 20:28:21 +0100 Subject: term: fdm_shutdown(): remove debug output --- terminal.c | 1 - 1 file changed, 1 deletion(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 04c39937..f32586ff 100644 --- a/terminal.c +++ b/terminal.c @@ -550,7 +550,6 @@ fdm_shutdown(struct fdm *fdm, int fd, int events, void *data) } wayl->last_exit_value = term_destroy(term); - LOG_WARN("last-exit-value: %d", wayl->last_exit_value); return true; } -- cgit v1.3 From 2e78dcc5e5cd2d5a2ae6a1f136467fc99a6dfd57 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:24:13 +0100 Subject: Don't use non-blocking FDs We use epoll() to determine when we can read/write FDs so there's absolutely no need for non-blocking. --- terminal.c | 24 +++++------------------- wayland.c | 15 +-------------- 2 files changed, 6 insertions(+), 33 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index f32586ff..5dc75d7a 100644 --- a/terminal.c +++ b/terminal.c @@ -310,35 +310,21 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, LOG_ERRNO("failed to open PTY"); goto close_fds; } - if ((flash_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1) { + if ((flash_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC)) == -1) { LOG_ERRNO("failed to create flash timer FD"); goto close_fds; } - if ((blink_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1) { + if ((blink_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC)) == -1) { LOG_ERRNO("failed to create blink timer FD"); goto close_fds; } - if ((delay_lower_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1 || - (delay_upper_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1) + if ((delay_lower_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC)) == -1 || + (delay_upper_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC)) == -1) { LOG_ERRNO("failed to create delayed rendering timer FDs"); goto close_fds; } - /* Read logic requires non-blocking mode */ - { - int fd_flags = fcntl(ptmx, F_GETFL); - if (fd_flags == -1) { - LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto err; - } - - if (fcntl(ptmx, F_SETFL, fd_flags | O_NONBLOCK) == -1) { - LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto err; - } - } - if (!fdm_add(fdm, ptmx, EPOLLIN, &fdm_ptmx, term)) { LOG_ERR("failed to add ptmx to FDM"); goto err; @@ -574,7 +560,7 @@ term_shutdown(struct terminal *term) * This is done by opening an event FD and adding it to the FDM. */ - int event_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); + int event_fd = eventfd(0, EFD_CLOEXEC); if (event_fd == -1) { LOG_ERRNO("failed to create terminal shutdown event FD"); return false; diff --git a/wayland.c b/wayland.c index 6a1d0fae..21ab1359 100644 --- a/wayland.c +++ b/wayland.c @@ -407,9 +407,6 @@ fdm_repeat(struct fdm *fdm, int fd, int events, void *data) wayl->kbd.repeat.fd, &expiration_count, sizeof(expiration_count)); if (ret < 0) { - if (errno == EAGAIN) - return true; - LOG_ERRNO("failed to read repeat key from repeat timer fd"); return false; } @@ -513,26 +510,16 @@ wayl_init(struct fdm *fdm) goto out; } - wayl->kbd.repeat.fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK); /* All wayland initialization done - make it so */ wl_display_roundtrip(wayl->display); + wayl->kbd.repeat.fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC); if (wayl->kbd.repeat.fd == -1) { LOG_ERRNO("failed to create keyboard repeat timer FD"); goto out; } int wl_fd = wl_display_get_fd(wayl->display); - int fd_flags = fcntl(wl_fd, F_GETFL); - if (fd_flags == -1) { - LOG_ERRNO("failed to set non blocking mode on Wayland display connection"); - goto out; - } - if (fcntl(wl_fd, F_SETFL, fd_flags | O_NONBLOCK) == -1) { - LOG_ERRNO("failed to set non blocking mode on Wayland display connection"); - goto out; - } - if (!fdm_add(fdm, wl_fd, EPOLLIN, &fdm_wayl, wayl)) { LOG_ERR("failed to register Wayland connection with the FDM"); goto out; -- cgit v1.3 From 2286bcf23d3d36f1cfbaa5c66b02ab38542ee628 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:27:45 +0100 Subject: terminal: no need to check for EAGAIN - we don't use non-blocking --- terminal.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 5dc75d7a..29487bdf 100644 --- a/terminal.c +++ b/terminal.c @@ -40,9 +40,6 @@ fdm_ptmx(struct fdm *fdm, int fd, int events, void *data) ssize_t count = read(term->ptmx, buf, sizeof(buf)); if (count < 0) { - if (errno == EAGAIN) - return true; - LOG_ERRNO("failed to read from pseudo terminal"); return false; } @@ -116,9 +113,6 @@ fdm_flash(struct fdm *fdm, int fd, int events, void *data) term->flash.fd, &expiration_count, sizeof(expiration_count)); if (ret < 0) { - if (errno == EAGAIN) - return true; - LOG_ERRNO("failed to read flash timer"); return false; } @@ -144,9 +138,6 @@ fdm_blink(struct fdm *fdm, int fd, int events, void *data) term->blink.fd, &expiration_count, sizeof(expiration_count)); if (ret < 0) { - if (errno == EAGAIN) - return true; - LOG_ERRNO("failed to read blink timer"); return false; } @@ -192,17 +183,18 @@ fdm_delayed_render(struct fdm *fdm, int fd, int events, void *data) if (fd == term->delayed_render_timer.upper_fd) ret2 = read(term->delayed_render_timer.upper_fd, &unused, sizeof(unused)); - if ((ret1 < 0 || ret2 < 0) && errno != EAGAIN) + if ((ret1 < 0 || ret2 < 0)) { LOG_ERRNO("failed to read timeout timer"); - else if (ret1 > 0 || ret2 > 0) { - render_refresh(term); + return false; + } - /* Reset timers */ - term->delayed_render_timer.is_armed = false; - timerfd_settime(term->delayed_render_timer.lower_fd, 0, &(struct itimerspec){.it_value = {0}}, NULL); - timerfd_settime(term->delayed_render_timer.upper_fd, 0, &(struct itimerspec){.it_value = {0}}, NULL); - } else - assert(false); + render_refresh(term); + + /* Reset timers */ + struct itimerspec reset = {{0}}; + timerfd_settime(term->delayed_render_timer.lower_fd, 0, &reset, NULL); + timerfd_settime(term->delayed_render_timer.upper_fd, 0, &reset, NULL); + term->delayed_render_timer.is_armed = false; return true; } -- cgit v1.3 From fb0801fa5632f6a683320baa53d1f395efc0b231 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:28:11 +0100 Subject: terminal: delayed rendering: fdm_delayed_render() may be called with is_armged==false This would happen if *both* timers triggered in the same epoll() call. --- terminal.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 29487bdf..7e7d3b53 100644 --- a/terminal.c +++ b/terminal.c @@ -172,7 +172,8 @@ fdm_delayed_render(struct fdm *fdm, int fd, int events, void *data) return false; struct terminal *term = data; - assert(term->delayed_render_timer.is_armed); + if (!term->delayed_render_timer.is_armed) + return true; uint64_t unused; ssize_t ret1 = 0; -- cgit v1.3 From c824aa2ef57e5e16f9e56ac75d1161c03605b62d Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:29:16 +0100 Subject: terminal: fdm_del() closes the FD --- terminal.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 7e7d3b53..3e4ecf12 100644 --- a/terminal.c +++ b/terminal.c @@ -458,26 +458,12 @@ err: return NULL; close_fds: - if (ptmx != -1) { - fdm_del(fdm, ptmx); - close(ptmx); - } - if (flash_fd != -1) { - fdm_del(fdm, flash_fd); - close(flash_fd); - } - if (blink_fd != -1) { - fdm_del(fdm, blink_fd); - close(blink_fd); - } - if (delay_lower_fd != -1) { - fdm_del(fdm, delay_lower_fd); - close(delay_lower_fd); - } - if (delay_upper_fd != -1) { - fdm_del(fdm, delay_upper_fd); - close(delay_upper_fd); - } + fdm_del(fdm, ptmx); + fdm_del(fdm, flash_fd); + fdm_del(fdm, blink_fd); + fdm_del(fdm, delay_lower_fd); + fdm_del(fdm, delay_upper_fd); + free(term); return NULL; } -- cgit v1.3 From 70b236d66d76abb82948f807cb9926278d380dac Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:30:58 +0100 Subject: term_shutdown(): cleanup * Close FDs, then delay destruction to next epoll() call * Destroy window before destroying terminal --- terminal.c | 84 ++++++++++++++++++++++++++------------------------------------ 1 file changed, 35 insertions(+), 49 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 3e4ecf12..e0c76708 100644 --- a/terminal.c +++ b/terminal.c @@ -468,77 +468,53 @@ close_fds: return NULL; } -static void -close_fd(struct terminal *term, int *fd) -{ - if (*fd == -1) - return; - - fdm_del(term->fdm, *fd); - close(*fd); - *fd = -1; -} - -static void -close_fds(struct terminal *term) -{ - close_fd(term, &term->delayed_render_timer.lower_fd); - close_fd(term, &term->delayed_render_timer.upper_fd); - close_fd(term, &term->blink.fd); - close_fd(term, &term->flash.fd); - close_fd(term, &term->ptmx); -} - static bool fdm_shutdown(struct fdm *fdm, int fd, int events, void *data) { LOG_DBG("FDM shutdown"); struct terminal *term = data; - struct wayland *wayl = term->wl; + /* Kill the event FD */ fdm_del(term->fdm, fd); - close(fd); - /* - * Now there shouldn't be any more callbacks or events that can - * trigger, meaning it should be safe to destroy the terminal. - */ + wayl_win_destroy(term->window); + term->window = NULL; + struct wayland *wayl __attribute__((unused)) = term->wl; assert(wayl->focused != term); assert(wayl->moused != term); - tll_foreach(wayl->terms, it) { - if (it->item == term) { - tll_remove(wayl->terms, it); - break; - } - } - wayl->last_exit_value = term_destroy(term); + int exit_code = term_destroy(term); + return true; } bool term_shutdown(struct terminal *term) { - /* Ensure we don't get any more events */ - close_fds(term); + if (term->is_shutting_down) + return true; - /* Destroy the wayland window, and consume the trail of Wayland events */ - wayl_win_destroy(term->window); - wl_display_roundtrip(term->wl->display); - term->window = NULL; + term->is_shutting_down = true; /* - * At this point, we no longer receive any more events referring to us, and it _should_ be safe to destroy ourselves now. - * - * However, in the (unlikely) event that there are events already - * queued up by the FDM, we delay the self-destruction to the next - * FDM poll iteration. - * - * This is done by opening an event FD and adding it to the FDM. + * Close FDs then postpone self-destruction to the next poll + * iteration, by creating an event FD that we trigger immediately. */ + fdm_del(term->fdm, term->delayed_render_timer.lower_fd); + fdm_del(term->fdm, term->delayed_render_timer.upper_fd); + fdm_del(term->fdm, term->blink.fd); + fdm_del(term->fdm, term->flash.fd); + fdm_del(term->fdm, term->ptmx); + + term->delayed_render_timer.lower_fd = -1; + term->delayed_render_timer.upper_fd = -1; + term->blink.fd = -1; + term->flash.fd = -1; + term->ptmx = -1; + int event_fd = eventfd(0, EFD_CLOEXEC); if (event_fd == -1) { LOG_ERRNO("failed to create terminal shutdown event FD"); @@ -554,7 +530,6 @@ term_shutdown(struct terminal *term) if (write(event_fd, &(uint64_t){1}, sizeof(uint64_t)) != sizeof(uint64_t)) { LOG_ERRNO("failed to send terminal shutdown event"); fdm_del(term->fdm, event_fd); - close(event_fd); return false; } @@ -567,7 +542,18 @@ term_destroy(struct terminal *term) if (term == NULL) return 0; - close_fds(term); + tll_foreach(term->wl->terms, it) { + if (it->item == term) { + tll_remove(term->wl->terms, it); + break; + } + } + + fdm_del(term->fdm, term->delayed_render_timer.lower_fd); + fdm_del(term->fdm, term->delayed_render_timer.upper_fd); + fdm_del(term->fdm, term->blink.fd); + fdm_del(term->fdm, term->flash.fd); + fdm_del(term->fdm, term->ptmx); if (term->window != NULL) wayl_win_destroy(term->window); -- cgit v1.3 From 1e41a25f00aab1038486cff37e286fd5c729a72b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:34:32 +0100 Subject: terminal: call user-defined callback when destroying terminal main uses this to get the exit code of the terminal. --- main.c | 33 +++++++++++++++++++++++---------- terminal.c | 9 ++++++++- terminal.h | 8 +++++++- wayland.h | 1 - 4 files changed, 38 insertions(+), 13 deletions(-) (limited to 'terminal.c') diff --git a/main.c b/main.c index b9e0e5f7..606b0199 100644 --- a/main.c +++ b/main.c @@ -26,14 +26,27 @@ static void print_usage(const char *prog_name) { - printf("Usage: %s [OPTION]...\n", prog_name); + printf("Usage: %s [OPTIONS]...\n", prog_name); printf("\n"); printf("Options:\n"); printf(" -f,--font=FONT comma separated list of fonts in fontconfig format (monospace)\n" " -t,--term=TERM value to set the environment variable TERM to (foot)\n" " -g,--geometry=WIDTHxHEIGHT set initial width and height\n" + " -s,--server run as a server\n" " -v,--version show the version number and quit\n"); - printf("\n"); +} + +struct shutdown_context { + struct terminal **term; + int exit_code; +}; + +static void +term_shutdown_cb(void *data, int exit_code) +{ + struct shutdown_context *ctx = data; + *ctx->term = NULL; + ctx->exit_code = exit_code; } int @@ -136,6 +149,7 @@ main(int argc, char *const *argv) struct fdm *fdm = NULL; struct wayland *wayl = NULL; struct terminal *term = NULL; + struct shutdown_context shutdown_ctx = {.term = &term, .exit_code = EXIT_FAILURE}; if ((fdm = fdm_init()) == NULL) goto out; @@ -143,7 +157,10 @@ main(int argc, char *const *argv) if ((wayl = wayl_init(fdm)) == NULL) goto out; - if ((term = term_init(&conf, fdm, wayl, argc, argv)) == NULL) + if (!as_server && (term = term_init(&conf, fdm, wayl, argc, argv, + &term_shutdown_cb, &shutdown_ctx)) == NULL) + goto out; + goto out; while (tll_length(wayl->terms) > 0) { @@ -157,15 +174,11 @@ main(int argc, char *const *argv) out: shm_fini(); - tll_foreach(wayl->terms, it) - term_destroy(it->item); - - int child_ret = wayl->last_exit_value; - + server_destroy(server); + term_destroy(term); wayl_destroy(wayl); fdm_destroy(fdm); config_free(conf); - return ret == EXIT_SUCCESS ? child_ret : ret; - + return ret == EXIT_SUCCESS && !as_server ? shutdown_ctx.exit_code : ret; } diff --git a/terminal.c b/terminal.c index e0c76708..cbe67b58 100644 --- a/terminal.c +++ b/terminal.c @@ -289,7 +289,8 @@ initialize_fonts(struct terminal *term, const struct config *conf) struct terminal * term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, - int argc, char *const *argv) + int argc, char *const *argv, + void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data) { int ptmx = -1; int flash_fd = -1; @@ -411,6 +412,8 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, .lower_fd = delay_lower_fd, .upper_fd = delay_upper_fd, }, + .shutdown_cb = shutdown_cb, + .shutdown_data = shutdown_data, }; initialize_color_cube(term); @@ -484,8 +487,12 @@ fdm_shutdown(struct fdm *fdm, int fd, int events, void *data) assert(wayl->focused != term); assert(wayl->moused != term); + void (*cb)(void *, int) = term->shutdown_cb; + void *cb_data = term->shutdown_data; int exit_code = term_destroy(term); + if (cb != NULL) + cb(cb_data, exit_code); return true; } diff --git a/terminal.h b/terminal.h index 5a0473b3..3ec2802c 100644 --- a/terminal.h +++ b/terminal.h @@ -287,12 +287,18 @@ struct terminal { int lower_fd; int upper_fd; } delayed_render_timer; + + bool is_shutting_down; + void (*shutdown_cb)(void *data, int exit_code); + void *shutdown_data; }; struct config; struct terminal *term_init( const struct config *conf, struct fdm *fdm, struct wayland *wayl, - int argc, char *const *argv); + int argc, char *const *argv, + void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data); + bool term_shutdown(struct terminal *term); int term_destroy(struct terminal *term); diff --git a/wayland.h b/wayland.h index 7e7bfd4c..951a0ece 100644 --- a/wayland.h +++ b/wayland.h @@ -150,7 +150,6 @@ struct wayland { tll(struct terminal *) terms; struct terminal *focused; struct terminal *moused; - int last_exit_value; /* TODO: exit value from client(s)? */ }; struct wayland *wayl_init(struct fdm *fdm); -- cgit v1.3 From 69d62d3cd2918bee089fc879e381820349497483 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 21:01:15 +0100 Subject: slave: set TERM environment variable in slave process --- main.c | 1 - slave.c | 5 ++++- slave.h | 2 +- terminal.c | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) (limited to 'terminal.c') diff --git a/main.c b/main.c index f13a45d8..c1abe5bc 100644 --- a/main.c +++ b/main.c @@ -161,7 +161,6 @@ main(int argc, char *const *argv) argv += optind; setlocale(LC_ALL, ""); - setenv("TERM", conf.term, 1); struct fdm *fdm = NULL; struct wayland *wayl = NULL; diff --git a/slave.c b/slave.c index 0e762bf0..1f70a0d8 100644 --- a/slave.c +++ b/slave.c @@ -1,5 +1,6 @@ #define _XOPEN_SOURCE 500 #include "slave.h" + #include #include #include @@ -68,7 +69,7 @@ err: pid_t slave_spawn(int ptmx, int argc, char *const *argv, - const char *conf_shell) + const char *term_env, const char *conf_shell) { int fork_pipe[2]; if (pipe2(fork_pipe, O_CLOEXEC) < 0) { @@ -88,6 +89,8 @@ slave_spawn(int ptmx, int argc, char *const *argv, /* Child */ close(fork_pipe[0]); /* Close read end */ + setenv("TERM", term_env, 1); + char **_shell_argv = NULL; char *const *shell_argv = argv; diff --git a/slave.h b/slave.h index cfb18af2..f1a9848a 100644 --- a/slave.h +++ b/slave.h @@ -4,4 +4,4 @@ #include pid_t slave_spawn( - int ptmx, int argc, char *const *argv, const char *conf_shell); + int ptmx, int argc, char *const *argv, const char *term_env, const char *conf_shell); diff --git a/terminal.c b/terminal.c index cbe67b58..d6aaf4ff 100644 --- a/terminal.c +++ b/terminal.c @@ -428,7 +428,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); /* Start the slave/client */ - if ((term->slave = slave_spawn(term->ptmx, argc, argv, conf->shell)) == -1) + if ((term->slave = slave_spawn(term->ptmx, argc, argv, conf->term, conf->shell)) == -1) goto err; /* Initiailze the Wayland window backend */ -- cgit v1.3 From 0bd2ddd8ad60a77e27cdb459da8e319647fd6f59 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 21:03:08 +0100 Subject: term_init(): initialize slave TERM from term_init() argument --- main.c | 2 +- server.c | 4 ++-- terminal.c | 4 ++-- terminal.h | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) (limited to 'terminal.c') diff --git a/main.c b/main.c index c1abe5bc..26a8f300 100644 --- a/main.c +++ b/main.c @@ -174,7 +174,7 @@ main(int argc, char *const *argv) if ((wayl = wayl_init(fdm)) == NULL) goto out; - if (!as_server && (term = term_init(&conf, fdm, wayl, argc, argv, + if (!as_server && (term = term_init(&conf, fdm, wayl, conf.term, argc, argv, &term_shutdown_cb, &shutdown_ctx)) == NULL) goto out; diff --git a/server.c b/server.c index ae6d678e..f48ed262 100644 --- a/server.c +++ b/server.c @@ -117,8 +117,8 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) assert(client->term == NULL); client->term = term_init( - server->conf, server->fdm, server->wayl, client->argc, client->argv, - &term_shutdown_handler, client); + server->conf, server->fdm, server->wayl, server->conf->term, + client->argc, client->argv, &term_shutdown_handler, client); if (client->term == NULL) { LOG_ERR("failed to instantiate new terminal"); diff --git a/terminal.c b/terminal.c index d6aaf4ff..f71d90a6 100644 --- a/terminal.c +++ b/terminal.c @@ -289,7 +289,7 @@ initialize_fonts(struct terminal *term, const struct config *conf) struct terminal * term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, - int argc, char *const *argv, + const char *term_env, int argc, char *const *argv, void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data) { int ptmx = -1; @@ -428,7 +428,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); /* Start the slave/client */ - if ((term->slave = slave_spawn(term->ptmx, argc, argv, conf->term, conf->shell)) == -1) + if ((term->slave = slave_spawn(term->ptmx, argc, argv, term_env, conf->shell)) == -1) goto err; /* Initiailze the Wayland window backend */ diff --git a/terminal.h b/terminal.h index 3ec2802c..11571519 100644 --- a/terminal.h +++ b/terminal.h @@ -296,7 +296,7 @@ struct terminal { struct config; struct terminal *term_init( const struct config *conf, struct fdm *fdm, struct wayland *wayl, - int argc, char *const *argv, + const char *term_env, int argc, char *const *argv, void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data); bool term_shutdown(struct terminal *term); -- cgit v1.3