From: short <> Date: Tue, 9 Aug 2005 06:57:40 +0000 (+0000) Subject: Some prevention of stale locks by unlocking it whenever possible. X-Git-Url: http://git.jankratochvil.net/?p=inetdmx.git;a=commitdiff_plain;h=be1836412ce05f1ed3902fa90d433a95e4ff8281 Some prevention of stale locks by unlocking it whenever possible. - Not sure if it helps anything and if it is even White Box Linux bug at all. --- diff --git a/inetdmx.c b/inetdmx.c index 13efc7f..006a4f2 100644 --- a/inetdmx.c +++ b/inetdmx.c @@ -77,6 +77,9 @@ static char *opt_command; static void fatal(const char *fmt,...) G_GNUC_PRINTF(1,2) G_GNUC_NORETURN; +/* for atexit(3) function */ +static int verror_quiet; + static void verror(const char *fmt,va_list ap) G_GNUC_PRINTF(1,0); static void verror(const char *fmt,va_list ap) { @@ -84,6 +87,8 @@ int use_syslog=opt_syslog,use_stderr=opt_stderr; char *string; const char *const double_error="Error printing error message"; + if (verror_quiet) + return; if (!use_syslog && !use_stderr) use_stderr=1; if (-1==vasprintf(&string,fmt,ap)) { @@ -237,13 +242,25 @@ static void lock_touch(void) static void lock_close(void) { - if (lock_fd==-1) + if (lock_fd==-1 || !opt_lock) return; + /* It should not be needed but some stale locks were seen on: + * White Box Linux kernel-smp-2.6.9-5.0.5.EL + */ + if (flock(lock_fd,LOCK_UN|LOCK_NB)) + fatal("Error unlocking lock file \"%s\": %m",opt_lock); if (close(lock_fd)) fatal("Error closing lock file \"%s\": %m",opt_lock); lock_fd=-1; } +static void lock_close_atexit(void) +{ + /* Prevent some crashes of malloc(3) etc. */ + verror_quiet=1; + lock_close(); +} + static int connect_try(void) { int fdtcp; @@ -497,6 +514,8 @@ char *s; else program_name=argv[0]; + atexit(lock_close_atexit); + optarg=NULL; optind=0; /* FIXME: Possible portability problem. */ while ((optc=getopt_long(argc,argv,"01T:i:Sel:p:Ih",longopts,NULL))!=EOF) switch (optc) { long l;