terminal-util: introduce terminal_{new,detach}_session helpers

Prompted by #35761
This commit is contained in:
Mike Yuan
2025-01-02 21:14:38 +01:00
parent 217bd588c2
commit 1de1282320
6 changed files with 63 additions and 14 deletions
+16
View File
@@ -498,6 +498,22 @@ int release_terminal(void) {
return r;
}
int terminal_new_session(void) {
/* Make us the new session leader, and set stdin tty to be our controlling terminal.
*
* Why stdin? Well, the ctty logic is relevant for signal delivery mostly, i.e. if people hit C-c
* or the line is hung up. Such events are basically just a form of input, via a side channel
* (that side channel being signal delivery, i.e. SIGINT, SIGHUP et al). Hence we focus on input,
* not output here. */
if (!isatty_safe(STDIN_FILENO))
return -ENXIO;
(void) setsid();
return RET_NERRNO(ioctl(STDIN_FILENO, TIOCSCTTY, 0));
}
int terminal_vhangup_fd(int fd) {
assert(fd >= 0);
return RET_NERRNO(ioctl(fd, TIOCVHANGUP));
+7
View File
@@ -7,6 +7,7 @@
#include <syslog.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include "macro.h"
#include "time-util.h"
@@ -64,6 +65,12 @@ typedef enum AcquireTerminalFlags {
int acquire_terminal(const char *name, AcquireTerminalFlags flags, usec_t timeout);
int release_terminal(void);
int terminal_new_session(void);
static inline void terminal_detach_session(void) {
(void) setsid();
(void) release_terminal();
}
int terminal_vhangup_fd(int fd);
int terminal_vhangup(const char *tty);