]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/pty-session.h
Merge branch 'lsclocks/rtc' of https://github.com/t-8ch/util-linux
[thirdparty/util-linux.git] / include / pty-session.h
1 /*
2 * This code is in the public domain; do with it what you wish.
3 *
4 * Written by Karel Zak <kzak@redhat.com> in Jul 2019
5 */
6 #ifndef UTIL_LINUX_PTY_SESSION_H
7 #define UTIL_LINUX_PTY_SESSION_H
8
9 #include <pty.h>
10 #include <termios.h>
11 #include <signal.h>
12 #include <sys/time.h>
13 #include <sys/stat.h>
14
15 #include <sys/signalfd.h>
16
17 /*
18 * Callbacks -- the first argument is always callback data, see
19 * ul_pty_set_callback_data().
20 */
21 struct ul_pty_callbacks {
22 /*
23 * Optional. Executed on SIGCHLD when ssi_code is EXITED, KILLED or
24 * DUMPED; The callback has to call ul_pty_set_child(pty, (pid_t) -1)
25 * if child is no more alive.
26 */
27 void (*child_wait)(void *, pid_t);
28
29 /*
30 * Used when child_wait() undefined to informa about child status
31 */
32 void (*child_die)(void *, pid_t, int);
33
34 /*
35 * Executed on SIGCHLD when ssi_status is SIGSTOP
36 */
37 void (*child_sigstop)(void *, pid_t);
38
39 /*
40 * Executed in master loop before ul_pty enter poll() and in time set by
41 * ul_pty_set_mainloop_time(). The callback is no used when time is not set.
42 */
43 int (*mainloop)(void *);
44
45 /*
46 * Executed on master or stdin activity, arguments:
47 * 2nd - file descriptor
48 * 3rd - buffer with data
49 * 4th - size of the data
50 */
51 int (*log_stream_activity)(void *, int, char *, size_t);
52
53 /*
54 * Executed on signal, arguments:
55 * 2nd - signal info
56 * 3rd - NULL or signal specific data (e.g. struct winsize on SIGWINCH
57 */
58 int (*log_signal)(void *, struct signalfd_siginfo *, void *);
59
60 /*
61 * Executed on SIGUSR1
62 */
63 int (*flush_logs)(void *);
64 };
65
66 struct ul_pty {
67 struct termios stdin_attrs; /* stdin and slave terminal runtime attributes */
68 int master; /* parent side */
69 int slave; /* child side */
70 int sigfd; /* signalfd() */
71 int poll_timeout;
72 struct winsize win; /* terminal window size */
73 sigset_t orgsig; /* original signal mask */
74
75 int delivered_signal;
76
77 struct ul_pty_callbacks callbacks;
78 void *callback_data;
79
80 pid_t child;
81
82 struct timeval next_callback_time;
83
84 struct ul_pty_child_buffer {
85 struct ul_pty_child_buffer *next;
86 char buf[BUFSIZ];
87 size_t size, cursor;
88 unsigned int final_input:1; /* drain child before writing */
89 } *child_buffer_head, *child_buffer_tail, *free_buffers;
90
91 unsigned int isterm:1, /* is stdin terminal? */
92 slave_echo:1; /* keep ECHO on pty slave */
93 };
94
95 void ul_pty_init_debug(int mask);
96 struct ul_pty *ul_new_pty(int is_stdin_tty);
97 void ul_free_pty(struct ul_pty *pty);
98
99 void ul_pty_slave_echo(struct ul_pty *pty, int enable);
100 int ul_pty_get_delivered_signal(struct ul_pty *pty);
101
102 void ul_pty_set_callback_data(struct ul_pty *pty, void *data);
103 void ul_pty_set_child(struct ul_pty *pty, pid_t child);
104
105 struct ul_pty_callbacks *ul_pty_get_callbacks(struct ul_pty *pty);
106 int ul_pty_is_running(struct ul_pty *pty);
107 int ul_pty_setup(struct ul_pty *pty);
108 int ul_pty_signals_setup(struct ul_pty *pty);
109 void ul_pty_cleanup(struct ul_pty *pty);
110 int ul_pty_chownmod_slave(struct ul_pty *pty, uid_t uid, gid_t gid, mode_t mode);
111 void ul_pty_init_slave(struct ul_pty *pty);
112 int ul_pty_proxy_master(struct ul_pty *pty);
113
114 void ul_pty_set_mainloop_time(struct ul_pty *pty, struct timeval *tv);
115 int ul_pty_get_childfd(struct ul_pty *pty);
116 void ul_pty_wait_for_child(struct ul_pty *pty);
117 pid_t ul_pty_get_child(struct ul_pty *pty);
118 void ul_pty_write_eof_to_child(struct ul_pty *pty);
119
120 #endif /* UTIL_LINUX_PTY_H */