summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2005-07-09 21:57:00 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2005-07-09 21:57:00 (GMT)
commit7aa182fa09daf8b857ac53ca043e54eeccf1a3ff (patch)
tree62158c99e6ef0b48b72c0dc79b7f2b5ecc274560
parent674ed483d8afb3aedacc747f5359bf5f98b5b30a (diff)
downloadflock-7aa182fa09daf8b857ac53ca043e54eeccf1a3ff.zip
flock-7aa182fa09daf8b857ac53ca043e54eeccf1a3ff.tar.gz
flock-7aa182fa09daf8b857ac53ca043e54eeccf1a3ff.tar.bz2
flock-7aa182fa09daf8b857ac53ca043e54eeccf1a3ff.tar.xz
Clean up itimer/sighandler properly
-rw-r--r--flock.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/flock.c b/flock.c
index 649903a..f08a685 100644
--- a/flock.c
+++ b/flock.c
@@ -107,7 +107,7 @@ static char * strtotimeval(const char *str, struct timeval *tv)
int main(int argc, char *argv[])
{
- struct itimerval timeout;
+ struct itimerval timeout, old_timer;
int have_timeout = 0;
int type = LOCK_EX;
int block = 0;
@@ -119,6 +119,7 @@ int main(int argc, char *argv[])
char *eon;
char **cmd_argv = NULL;
const char *filename = NULL;
+ struct sigaction sa, old_sa;
program = argv[0];
@@ -209,15 +210,13 @@ int main(int argc, char *argv[])
}
if ( have_timeout ) {
- struct sigaction sa;
-
memset(&sa, 0, sizeof sa);
sa.sa_handler = timeout_handler;
sa.sa_flags = SA_ONESHOT;
- sigaction(SIGALRM, &sa, NULL);
+ sigaction(SIGALRM, &sa, &old_sa);
- setitimer(ITIMER_REAL, &timeout, NULL);
+ setitimer(ITIMER_REAL, &timeout, &old_timer);
}
while ( flock(fd, type|block) ) {
@@ -237,6 +236,11 @@ int main(int argc, char *argv[])
}
}
+ if ( have_timeout ) {
+ setitimer(ITIMER_REAL, &old_timer, NULL); /* Cancel itimer */
+ sigaction(SIGALRM, &old_sa, NULL); /* Cancel signal handler */
+ }
+
status = 0;
if ( cmd_argv ) {