Skip to content

Commit

Permalink
lib/pty-session: add generic PTY container code
Browse files Browse the repository at this point in the history
The idea is to consolidate script(1), scriptlive(1) and su(1) --pty
and use the same code everywhere.

TODO: add callbacks for stdin/out logging (necessary for script(1)).

Signed-off-by: Karel Zak <kzak@redhat.com>
  • Loading branch information
karelzak committed Oct 8, 2019
1 parent 33869e5 commit 6954895
Show file tree
Hide file tree
Showing 4 changed files with 654 additions and 1 deletion.
7 changes: 7 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ have_security_openpam_h=$ac_cv_header_security_openpam_h
have_shadow_h=$ac_cv_header_shadow_h
have_sys_signalfd_h=$ac_cv_header_sys_signalfd_h
have_utmpx_h=$ac_cv_header_utmpx_h
have_pty_h=$ac_cv_header_pty_h

AS_CASE([$linux_os:$have_linux_version_h],
[yes:no],
Expand Down Expand Up @@ -743,6 +744,12 @@ AS_IF([test "x$with_util" = xno], [
UL_CHECK_LIB([util], [openpty])
])

AS_IF([test "x$have_pty_h" = xyes -a "x$have_sys_signalfd_h" = xyes -a "x$have_util" = xyes], [
AM_CONDITIONAL([HAVE_PTY], [true])
AC_DEFINE([HAVE_PTY], [1], [have PTY support])
], [
AM_CONDITIONAL([HAVE_PTY], [false])
])

AC_CHECK_TYPES([union semun], [], [], [[
#include <sys/sem.h>
Expand Down
53 changes: 53 additions & 0 deletions include/pty-session.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This code is in the public domain; do with it what you wish.
*
* Written by Karel Zak <kzak@redhat.com> in Jul 2019
*/
#ifndef UTIL_LINUX_PTY_SESSION_H
#define UTIL_LINUX_PTY_SESSION_H

#include <pty.h>
#include <termios.h>
#include <signal.h>

struct ul_pty_callbacks {
void (*child_wait)(void *);
void (*child_sigstop)(void *);
};

struct ul_pty {
struct termios stdin_attrs; /* stdin and slave terminal runtime attributes */
int master; /* parent side */
int slave; /* child side */
int sigfd; /* signalfd() */
int poll_timeout;
struct winsize win; /* terminal window size */
sigset_t orgsig; /* original signal mask */

int delivered_signal;

struct ul_pty_callbacks callbacks;
void *callback_data;

pid_t child;

unsigned int isterm:1; /* is stdin terminal? */
};

void ul_pty_init_debug(int mask);
struct ul_pty *ul_new_pty(int is_stdin_tty);

sigset_t *ul_pty_get_orig_sigset(struct ul_pty *pty);
int ul_pty_get_delivered_signal(struct ul_pty *pty);

void ul_pty_set_callback_data(struct ul_pty *pty, void *data);
void ul_pty_set_child(struct ul_pty *pty, pid_t child);

struct ul_pty_callbacks *ul_pty_get_callbacks(struct ul_pty *pty);
int ul_pty_is_running(struct ul_pty *pty);
int ul_pty_setup(struct ul_pty *pty);
void ul_pty_cleanup(struct ul_pty *pty);
void ul_pty_init_slave(struct ul_pty *pty);
int ul_pty_proxy_master(struct ul_pty *pty);

#endif /* UTIL_LINUX_PTY_H */
9 changes: 8 additions & 1 deletion lib/Makemodule.am
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ check_PROGRAMS += \
test_timeutils



if LINUX
if HAVE_CPU_SET_T
check_PROGRAMS += test_cpuset
Expand Down Expand Up @@ -144,6 +143,14 @@ test_path_LDADD = $(LDADD)
endif
endif

if HAVE_PTY
check_PROGRAMS += test_pty
test_pty_SOURCES = lib/pty-session.c \
include/pty-session.h
test_pty_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_PTY
test_pty_LDADD = $(LDADD) libcommon.la -lutil
endif

if LINUX
test_cpuset_SOURCES = lib/cpuset.c
test_cpuset_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_CPUSET
Expand Down

0 comments on commit 6954895

Please sign in to comment.