From 55434759f6729dafc21a12349db00c5fbb4c2d5c Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Wed, 7 Oct 2009 01:07:54 +0000 Subject: [PATCH] Add support for pausing and changing the speed in interactive mode. --- sudoreplay.c | 68 ++++++++++++++++++++++-- sudoreplay.cat | 128 +++++++++++++++++++++++++++++++++++----------- sudoreplay.man.in | 26 +++++----- sudoreplay.pod | 19 +++++++ 4 files changed, 195 insertions(+), 46 deletions(-) diff --git a/sudoreplay.c b/sudoreplay.c index 0c315ba55..5576009d7 100644 --- a/sudoreplay.c +++ b/sudoreplay.c @@ -148,6 +148,7 @@ static struct search_node *node_stack[32]; static int stack_top; extern void *emalloc __P((size_t)); +extern void *emalloc2 __P((size_t, size_t)); extern void *erealloc __P((void *, size_t)); extern void efree __P((void *)); extern time_t get_date __P((char *)); @@ -171,6 +172,7 @@ void cleanup __P((int)); static int list_sessions __P((int, char **, const char *, const char *, const char *)); static int parse_expr __P((struct search_node **, char **)); +static void check_input __P((int, double *)); static void delay __P((double)); static void usage __P((void)); @@ -188,15 +190,15 @@ main(argc, argv) int argc; char **argv; { - int ch, plen, listonly = 0; + int ch, plen, ttyfd, interactive = 0, listonly = 0; const char *id, *user = NULL, *pattern = NULL, *tty = NULL; char path[PATH_MAX], buf[LINE_MAX], *cp, *ep; FILE *tfile, *sfile, *lfile; sigaction_t sa; - double seconds; unsigned long nbytes; size_t len, nread; ssize_t nwritten; + double seconds; double speed = 1.0; double max_wait = 0; double to_wait; @@ -292,7 +294,12 @@ main(argc, argv) sa.sa_handler = SIG_IGN; (void) sigaction(SIGTSTP, &sa, NULL); (void) sigaction(SIGQUIT, &sa, NULL); - term_cbreak(STDOUT_FILENO); + + ttyfd = open(_PATH_TTY, O_RDWR|O_NOCTTY, 0); + if (ttyfd != -1) { + term_cbreak(ttyfd); + interactive = isatty(STDOUT_FILENO); + } /* * Timing file consists of line of the format: "%f %d\n" @@ -309,6 +316,9 @@ main(argc, argv) if (errno == ERANGE && nbytes == ULONG_MAX) error(1, "invalid timing file byte count: %s", cp); + if (interactive) + check_input(ttyfd, &speed); + /* Adjust delay using speed factor and clamp to max_wait */ to_wait = seconds / speed; if (max_wait && to_wait > max_wait) @@ -722,6 +732,58 @@ list_sessions(argc, argv, pattern, user, tty) return(0); } +/* + * Check input for ' ', '<', '>' + * pause, slow, fast + */ +static void +check_input(ttyfd, speed) + int ttyfd; + double *speed; +{ + fd_set *fdsr; + int flags, nready; + struct timeval tv; + char ch; + ssize_t n; + + fdsr = (fd_set *)emalloc2(howmany(ttyfd + 1, NFDBITS), sizeof(fd_mask)); + FD_SET(ttyfd, fdsr); + tv.tv_sec = 0; + tv.tv_usec = 0; + + nready = select(ttyfd + 1, fdsr, NULL, NULL, &tv); + if (nready == 1) { + flags = fcntl(ttyfd, F_GETFL, 0); + if (flags != -1) + (void) fcntl(ttyfd, F_SETFL, flags | O_NONBLOCK); + do { + n = read(ttyfd, &ch, 1); + if (n == 1) { + switch (ch) { + case ' ': + /* wait for another character */ + if (flags != -1) + (void) fcntl(ttyfd, F_SETFL, flags); + n = read(ttyfd, &ch, 1); + if (flags != -1) + (void) fcntl(ttyfd, F_SETFL, flags | O_NONBLOCK); + break; + case '<': + *speed /= 2; + break; + case '>': + *speed *= 2; + break; + } + } + } while (n == 1); + if (flags != -1) + (void) fcntl(ttyfd, F_SETFL, flags); + } + free(fdsr); +} + static void usage() { diff --git a/sudoreplay.cat b/sudoreplay.cat index 15399c88d..56dc10a83 100644 --- a/sudoreplay.cat +++ b/sudoreplay.cat @@ -23,6 +23,16 @@ DDEESSCCRRIIPPTTIIOONN In list mode, ssuuddoorreeppllaayy can be used to find the ID of a session based on a number of criteria such as the user, tty or command run. + In replay mode, if the standard output has not been redirected, + ssuuddoorreeppllaayy will act on the following keys: + + ' ' (space) + Pause output; press any key to resume. + + '<' Reduce the playback speed by one half. + + '>' Double the playback speed. + OOPPTTIIOONNSS ssuuddoorreeppllaayy accepts the following command line options: @@ -48,20 +58,10 @@ OOPPTTIIOONNSS Evaluates to true if the command was run with the specified current working directory. - fromdate _d_a_t_e - Evaluates to true if the command was run on or - after _d_a_t_e. See "Date and time format" for a - description of supported date and time formats. - - group _r_u_n_a_s___g_r_o_u_p - Evaluates to true if the command was run with the - specified _r_u_n_a_s___g_r_o_u_p. Note that unless a - _r_u_n_a_s___g_r_o_u_p was explicitly specified when ssuuddoo was - run this field will be empty in the log. -1.7.2 September 19, 2009 1 +1.7.2 October 6, 2009 1 @@ -70,6 +70,17 @@ OOPPTTIIOONNSS SUDOREPLAY(1m) MAINTENANCE COMMANDS SUDOREPLAY(1m) + fromdate _d_a_t_e + Evaluates to true if the command was run on or + after _d_a_t_e. See "Date and time format" for a + description of supported date and time formats. + + group _r_u_n_a_s___g_r_o_u_p + Evaluates to true if the command was run with the + specified _r_u_n_a_s___g_r_o_u_p. Note that unless a + _r_u_n_a_s___g_r_o_u_p was explicitly specified when ssuuddoo was + run this field will be empty in the log. + runas _r_u_n_a_s___u_s_e_r Evaluates to true if the command was run as the specified _r_u_n_a_s___u_s_e_r. Note that ssuuddoo runs commands @@ -113,28 +124,27 @@ SUDOREPLAY(1m) MAINTENANCE COMMANDS SUDOREPLAY(1m) seconds it will wait between key presses or program output. This can be used to slow down or speed up the display. For example, a _s_p_e_e_d___f_a_c_t_o_r of _2 would make the output twice as - fast whereas a _s_p_e_e_d___f_a_c_t_o_r of <.5> would make the output - twice as slow. - - -V The --VV (version) option causes ssuuddoorreeppllaayy to print its - version number and exit. - DDaattee aanndd ttiimmee ffoorrmmaatt - The time and date may be specified multiple ways, common formats - include: +1.7.2 October 6, 2009 2 -1.7.2 September 19, 2009 2 +SUDOREPLAY(1m) MAINTENANCE COMMANDS SUDOREPLAY(1m) + fast whereas a _s_p_e_e_d___f_a_c_t_o_r of <.5> would make the output + twice as slow. -SUDOREPLAY(1m) MAINTENANCE COMMANDS SUDOREPLAY(1m) + -V The --VV (version) option causes ssuuddoorreeppllaayy to print its + version number and exit. + DDaattee aanndd ttiimmee ffoorrmmaatt + The time and date may be specified multiple ways, common formats + include: HH:MM:SS am MM/DD/CCYY timezone 24 hour time may be used in place of am/pm. @@ -181,26 +191,24 @@ SUDOREPLAY(1m) MAINTENANCE COMMANDS SUDOREPLAY(1m) 10:01 am 9/17/2009 10:01 am, September 17, 2009. - 10:01 am - 10:01 am on the current day. - - 10 10:00 am on the current day. - 9/17/2009 - 00:00 am, September 17, 2009. +1.7.2 October 6, 2009 3 -1.7.2 September 19, 2009 3 - +SUDOREPLAY(1m) MAINTENANCE COMMANDS SUDOREPLAY(1m) + 10:01 am + 10:01 am on the current day. -SUDOREPLAY(1m) MAINTENANCE COMMANDS SUDOREPLAY(1m) + 10 10:00 am on the current day. + 9/17/2009 + 00:00 am, September 17, 2009. 10:01 am Sep 17, 2009 10:01 am, September 17, 2009. @@ -249,6 +257,17 @@ SSUUPPPPOORRTT http://www.sudo.ws/mailman/listinfo/sudo-users to subscribe or search the archives. + + +1.7.2 October 6, 2009 4 + + + + + +SUDOREPLAY(1m) MAINTENANCE COMMANDS SUDOREPLAY(1m) + + DDIISSCCLLAAIIMMEERR ssuuddoorreeppllaayy is provided ``AS IS'' and any express or implied warranties, including, but not limited to, the implied warranties of @@ -259,6 +278,53 @@ DDIISSCCLLAAIIMMEERR -1.7.2 September 19, 2009 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1.7.2 October 6, 2009 5 diff --git a/sudoreplay.man.in b/sudoreplay.man.in index 6f49ff001..6ff18363d 100644 --- a/sudoreplay.man.in +++ b/sudoreplay.man.in @@ -14,18 +14,10 @@ .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" .\" $Sudo$ -.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05) +.\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.07) .\" .\" Standard preamble: .\" ======================================================================== -.de Sh \" Subsection heading -.br -.if t .Sp -.ne 5 -.PP -\fB\\$1\fR -.PP -.. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp @@ -69,7 +61,7 @@ .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for -.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index +.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ @@ -148,7 +140,7 @@ .\" ======================================================================== .\" .IX Title "SUDOREPLAY @mansectsu@" -.TH SUDOREPLAY @mansectsu@ "September 19, 2009" "1.7.2" "MAINTENANCE COMMANDS" +.TH SUDOREPLAY @mansectsu@ "October 6, 2009" "1.7.2" "MAINTENANCE COMMANDS" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -172,6 +164,16 @@ session logging enabled. .PP In list mode, \fBsudoreplay\fR can be used to find the \s-1ID\s0 of a session based on a number of criteria such as the user, tty or command run. +.PP +In replay mode, if the standard output has not been redirected, +\&\fBsudoreplay\fR will act on the following keys: +.IP "' ' (space)" 8 +.IX Item "' ' (space)" +Pause output; press any key to resume. +.IP "'<'" 8 +Reduce the playback speed by one half. +.IP "'>'" 8 +Double the playback speed. .SH "OPTIONS" .IX Header "OPTIONS" \&\fBsudoreplay\fR accepts the following command line options: @@ -255,7 +257,7 @@ a \fIspeed_factor\fR of <.5> would make the output twice as slow. .IX Item "-V" The \fB\-V\fR (version) option causes \fBsudoreplay\fR to print its version number and exit. -.Sh "Date and time format" +.SS "Date and time format" .IX Subsection "Date and time format" The time and date may be specified multiple ways, common formats include: .IP "\s-1HH:MM:SS\s0 am \s-1MM/DD/CCYY\s0 timezone" 8 diff --git a/sudoreplay.pod b/sudoreplay.pod index 46f9d0921..42c12187b 100644 --- a/sudoreplay.pod +++ b/sudoreplay.pod @@ -39,6 +39,25 @@ session logging enabled. In list mode, B can be used to find the ID of a session based on a number of criteria such as the user, tty or command run. +In replay mode, if the standard output has not been redirected, +B will act on the following keys: + +=over 8 + +=item ' ' (space) + +Pause output; press any key to resume. + +=item '<' + +Reduce the playback speed by one half. + +=item '>' + +Double the playback speed. + +=back + =head1 OPTIONS B accepts the following command line options: -- 2.40.0