Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option for preventing DB connections leaks #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ src/Makefile.in
src/rlwrap
stamp-h1
tools/
test-suite.log
test/testit.log
test/testit.trs
filters/RlwrapFilter.3pm
36 changes: 26 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ int history_duplicate_avoidance_policy =
char *history_format = NULL; /* -F option: format to append to history entries */
char *forget_regexp = NULL; /* -g option: keep matching input out of history */
int pass_on_sigINT_as_sigTERM = FALSE; /* -I option: send a SIGTERM to client when a SIGINT is received */
int seconds_before_exiting = 0; /* -L option: set the number of seconds before the program stops */
char *multi_line_tmpfile_ext = NULL; /* -M option: tmpfile extension for multi-line editor */
int nowarn = FALSE; /* -n option: suppress warnings */
int commands_children_not_wrapped = FALSE; /* -N option: always use direct mode when <command> is waiting */
Expand Down Expand Up @@ -96,11 +97,11 @@ static void test_main(void);

/* options */
#ifdef GETOPT_GROKS_OPTIONAL_ARGS
static char optstring[] = "+:a::Ab:cC:d::D:e:f:F:g:hH:iIl:nNM:m::oO:p::P:q:rRs:S:t:TUvw:Wz:";
static char optstring[] = "+:a::Ab:cC:d::D:e:f:F:g:hH:iIl:L:nNM:m::oO:p::P:q:rRs:S:t:TUvw:Wz:";
/* +: is not really documented. configure checks wheteher it works as expected
if not, GETOPT_GROKS_OPTIONAL_ARGS is undefined. @@@ */
#else
static char optstring[] = "+:a:Ab:cC:d:D:e:f:F:g:hH:iIl:nNM:m:oO:p:P:q:rRs:S:t:TUvw:Wz:";
static char optstring[] = "+:a:Ab:cC:d:D:e:f:F:g:hH:iIl:L:nNM:m:oO:p:P:q:rRs:S:t:TUvw:Wz:";
#endif

#ifdef HAVE_GETOPT_LONG
Expand All @@ -121,6 +122,7 @@ static struct option longopts[] = {
{"case-insensitive", no_argument, NULL, 'i'},
{"pass-sigint-as-sigterm", no_argument, NULL, 'I'},
{"logfile", required_argument, NULL, 'l'},
{"limit-timeout-seconds", required_argument, NULL, 'L'},
{"multi-line", optional_argument, NULL, 'm'},
{"multi-line-ext", required_argument, NULL, 'M'},
{"no-warnings", no_argument, NULL, 'n'},
Expand All @@ -133,12 +135,12 @@ static struct option longopts[] = {
{"remember", no_argument, NULL, 'r'},
{"renice", no_argument, NULL, 'R'},
{"histsize", required_argument, NULL, 's'},
{"substitute-prompt", required_argument, NULL, 'S'},
{"set-terminal-name", required_argument, NULL, 't'},
{"substitute-prompt", required_argument, NULL, 'S'},
{"set-terminal-name", required_argument, NULL, 't'},
{"test-terminal", no_argument, NULL, 'T'},
{"mirror-arguments", no_argument, NULL, 'U'},
{"version", no_argument, NULL, 'v'},
{"wait-before-prompt", required_argument, NULL, 'w'},
{"wait-before-prompt", required_argument, NULL, 'w'},
{"polling", no_argument, NULL, 'W'},
{"filter", required_argument, NULL, 'z'},
{0, 0, 0, 0}
Expand Down Expand Up @@ -235,15 +237,17 @@ main_loop()
int promptlen = 0;
int leave_prompt_alone;
sigset_t no_signals_blocked;
int seen_EOF = FALSE;
int seen_EOF = FALSE;


struct timespec select_timeout, *select_timeoutptr;
struct timespec immediately = { 0, 0 }; /* zero timeout when child is dead */
struct timespec wait_a_little = {0, 0xBadf00d }; /* tv_usec field will be filled in when initialising */
struct timespec *forever = NULL;
wait_a_little.tv_nsec = 1000 * 1000 * wait_before_prompt;

struct timeval start_time= { 0, 0 };
struct timeval next_time= { 0, 0 };
unsigned long elapsed_time=0;

sigemptyset(&no_signals_blocked);

Expand All @@ -253,9 +257,20 @@ main_loop()
pass_through_filter(TAG_OUTPUT,""); /* If something is wrong with filter, get the error NOW */
set_echo(FALSE); /* This will also put the terminal in CBREAK mode */
test_main();

gettimeofday(&start_time, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using clock_gettime(CLOCK_MONOTONIC) instead gettimeofday. Using gettimeofday can result in erratic behavior if NTP decides to adjust the system clock, for example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks !
I was tempted to use CLOCK_MONOTONIC_RAW, but it's Linux specific


/* ------------------------------ main loop -------------------------------*/
while (TRUE) {

/*Check if we reached the possible set timeout value*/
if (seconds_before_exiting > 0 && gettimeofday(&next_time, NULL) == 0) {
if (next_time.tv_sec - start_time.tv_sec >= seconds_before_exiting) {
DPRINTF1(DEBUG_TERMIO, "we reached timeout session value of %d seconds, disconnecting !!", seconds_before_exiting);
cleanup_rlwrap_and_exit(EXIT_SUCCESS);
}
}

/* listen on both stdin and pty_fd */
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
Expand Down Expand Up @@ -390,7 +405,7 @@ main_loop()
cleanup_rlwrap_and_exit(EXIT_SUCCESS);
} else if (errno == EINTR) { /* interrupted by signal ...*/
continue; /* ... don't worry */
} else if (! seen_EOF) { /* maybe command has just died (and SIGCHLD, whose handler sets command_is_dead is not */
} else if (! seen_EOF) { /* maybe command has just died (and SIGCHLD, whose handler sets command_is_dead is not */
mymicrosleep(50); /* yet caught) Therefore we wait a bit, */
seen_EOF = TRUE; /* set a flag */
continue; /* and try one more time (hopefully catching the signal this time round */
Expand Down Expand Up @@ -788,6 +803,7 @@ read_options_and_command_name(int argc, char **argv)
break;
case 'I': pass_on_sigINT_as_sigTERM = TRUE; break;
case 'l': open_logfile(optarg); break;
case 'L': seconds_before_exiting = abs(my_atoi(optarg)); break;
case 'n': nowarn = TRUE; break;
case 'm':
#ifndef HAVE_SYSTEM
Expand Down Expand Up @@ -1044,6 +1060,6 @@ static void test_main() {
exit(0);
}

#endif
}
#endif
}

1 change: 1 addition & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ usage(int status)
print_option('i', "case-insensitive", NULL, FALSE, NULL);
print_option('I', "pass-sigint-as-sigterm", NULL, FALSE, NULL);
print_option('l', "logfile", "file", FALSE, NULL);
print_option('L', "seconds_before_exiting", "N", FALSE, "(sec)");
print_option('m', "multi-line", "newline substitute", TRUE, NULL);
print_option('M', "multi-line-ext", ".ext", FALSE, NULL);
print_option('n', "no-warnings", NULL, FALSE, NULL);
Expand Down