Thu 09 of Sep, 2010 [13:14 UTC]  
Menu

TimeoutPatch

It would not be good if your server waits for a password when you might be hundreds of miles away...
edit print PDF
Português Brasileiro
So here is a small patch for a timeout...

 --- setup.c 2004-03-07 22:56:34.000000000 +0100
 +++ setup.c 2004-10-12 22:05:45.000000000 +0200
 @@ -8,12 +8,19 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
 +#include <signal.h>
 
 #include "setup.h"
 #include "blockdev.h"
 
 static int memory_unsafe = 0;
 
 +void catch_alarm(int sig_num)
 +{
 + printf("Operation timed out! Exiting\n\n");
 + exit(0);
 +}
 +
 static int setup_enter(struct setup_backend *backend)
 {
 int r;
 @@ -59,6 +66,9 @@
 char *pass = NULL;
 int buflen, i;
 
 + signal(SIGALRM, catch_alarm);
 + alarm(30);
 +
 if (isatty(fd)) {
 char *pass2 = getpass(prompt); /* FIXME */
 if (!pass2)
 @@ -86,8 +96,11 @@
 break;
 }
 
 + alarm(0);
 +
 if (pass)
 pass[i] = '\0';
 +
 return pass;
 }


Created by: tribble last modification: Tuesday 06 of July, 2010 [01:52:09 UTC] by Anonymous


Posted messages

Top Hide all
author message
timeout.sh
on: Fri 02 of Dec, 2005 [20:35 UTC] score: 0.00
This could also be done with a wrapper script...

The script requires the setsid program (/usr/bin/setsid under Debian) and will probably kill a calling shell if run with 1 argument... The setsid simply calls setsid(2), so I don't see why it isn't included in busybox... but it isn't as of today (Dec 2005).

I am not sure if signal 2 (SIGINT) should be trapped because the bash man page says something about this being handled to enable the wait command.

I am not sure if this requires JOB CONTROL or not.
#! /bin/sh

# We reuse the script to have a program to pass to setsid(3).
# Another script should be used here instead,
# but this allows this script to be self contained
if [ $# -eq 1 ] ; then
	trap 'kill -15 -$$' 1 2 3 15
	sleep "$1" && kill -USR1 $PPID 2>/dev/null &
	# Wait so that sleep doesn't block us from being interrupted
	wait $!
	exit
fi
if [ $# -lt 2 ] ; then
	echo "Usage: $0   [arguments...]"
	exit 1
fi

# Once the timeout has expired,
# allow the script to kill the program and cleanup
timeout() {
	exec setsid "$0" "$1"
}

# Replace the subshell with the process as a process leader
execute() {
	exec setsid "$@"
}

# SIGUSR1 handler
timeup() {
	[ $progPID -gt 0 ] && kill $progPID && progPID=0
	sleeperPID=0
}

# EXIT, SIGHUP, SIGQUIT, SIGTERM handler
# SIGINT might be needed by the default handler to make wait work
cleanup() {
	trap - 0 1 3 10 15
	for pid in $sleeperPID $progPID ; do
		[ $pid -gt 0 ] && kill -15 -$pid
	done
}

# Setup traps
trap timeup 10
trap cleanup 0 1 3 15
# Start the timeout
timeout "$1" &
sleeperPID=$!

shift
# Start the command
execute "$@" &
progPID=$!

# Wait for the command to complete
wait $progPID && progPID=0




Page: 1/1
1