xsel

Program for manipulating the X clipboard
git clone https://git.sinitax.com/kfish/xsel
Log | Files | Refs | README | LICENSE | sfeed.txt

commit e8ecbd6089aa0436e5e3544fc43cee14dcfc0d89
parent 839ccd2c646e35ced6388a4b016739e7c56aa699
Author: Zev Weiss <zev@bewilderbeest.net>
Date:   Wed,  3 Sep 2014 03:45:11 -0500

Give --selectionTimeout meaning in --input mode.

Daemon simply exits after the specified amount of time.

Diffstat:
Mxsel.1x | 3++-
Mxsel.c | 61++++++++++++++++++++++++++++++++++++++++++++++---------------
2 files changed, 48 insertions(+), 16 deletions(-)

diff --git a/xsel.1x b/xsel.1x @@ -87,7 +87,8 @@ specify the server to use; see \fIX(1)\fP. .TP \fB\-t\fR \fIms\fR, \fB\-\-selectionTimeout\fR \fIms\fR Specify the timeout in milliseconds within which the selection must be -retrieved. A value of \fB0\fR (zero) specifies no timeout (default). +retrieved. In \fB\-\-input\fR mode, the background process exits after this +amount of time. A value of \fB0\fR (zero) specifies no timeout (default). .PP \fBMiscellaneous options\fR diff --git a/xsel.c b/xsel.c @@ -374,6 +374,20 @@ gotpw: return homedir; } +/* The jmp_buf to longjmp out of the signal handler */ +static sigjmp_buf env_alrm; + +/* + * alarm_handler (sig) + * + * Signal handler for catching SIGALRM. + */ +static void +alarm_handler (int sig) +{ + siglongjmp (env_alrm, 1); +} + /* * set_timer_timeout () * @@ -389,6 +403,30 @@ set_timer_timeout (void) } /* + * set_daemon_timeout () + * + * Set up a timer to cause the daemon to exit after the desired + * amount of time. + */ +static void +set_daemon_timeout (void) +{ + if (signal (SIGALRM, alarm_handler) == SIG_ERR) { + exit_err ("error setting timeout handler"); + } + + set_timer_timeout (); + + if (sigsetjmp (env_alrm, 0) == 0) { + setitimer (ITIMER_REAL, &timer, (struct itimerval *)0); + } else { + print_debug (D_INFO, "daemon exiting after %d ms", timeout / 1000); + exit (0); + } +} + + +/* * become_daemon () * * Perform the required procedure to become a daemon process, as @@ -403,7 +441,12 @@ become_daemon (void) int null_r_fd, null_w_fd, log_fd; char * homedir; - if (no_daemon) return; + if (no_daemon) { + /* If the user has specified a timeout, enforce it even if we don't + * actually daemonize */ + set_daemon_timeout (); + return; + } homedir = get_homedir (); @@ -471,6 +514,8 @@ become_daemon (void) if (dup2 (log_fd, 2) == -1) { exit_err ("error duplicating logfile %s on stderr", logfile); } + + set_daemon_timeout (); } /* @@ -517,20 +562,6 @@ get_timestamp (void) * to the first XEvent.] */ -/* The jmp_buf to longjmp out of the signal handler */ -static sigjmp_buf env_alrm; - -/* - * alarm_handler (sig) - * - * Signal handler for catching SIGALRM. - */ -static void -alarm_handler (int sig) -{ - siglongjmp (env_alrm, 1); -} - /* * get_append_property () *