-/* $Id$ */
+/*
+ * $Id$
+ * Latest:
+ * http://cvs.jankratochvil.net/viewcvs/nethome/src/inetdmx.c?rev=HEAD
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; you must use version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
#define _GNU_SOURCE 1
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)
{
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)) {
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;
opt_start_command_timeout,opt_port,opt_command);
}
-/* Returns: Is fresh? */
-static int lock_create_and_time_check(int lock_mode)
+/* Returns: Is fresh or does not exist? */
+static int lock_open_and_time_check(int lock_mode)
{
struct stat statbuf;
if (!opt_lock)
return 0;
+ if (access(opt_lock,R_OK|W_OK)) {
+ if (errno==ENOENT)
+ return 1;
+ fatal("Error checking existance of the lock file \"%s\": %m",opt_lock);
+ }
if (!lock_create(lock_mode|LOCK_NB))
- exit(EXIT_SUCCESS);
+ return 0;
if (lock_fd==-1 || fstat(lock_fd,&statbuf))
fatal("Error fstat(2)ting lock file \"%s\": %m",opt_lock);
return statbuf.st_mtime>=time(NULL)-opt_idle_server_timeout;
if (opt_idle_server_timeout!=DEFAULT_IDLE_SERVER_TIMEOUT && !opt_lock)
fatal("-l|--lock is a required argument for -i|--idle-server-timeout of -1|--start");
- is_fresh=lock_create_and_time_check(LOCK_SH);
+ is_fresh=lock_open_and_time_check(LOCK_SH);
lock_close();
if (is_fresh)
exit(EXIT_SUCCESS);
- lock_create_and_time_check(LOCK_EX);
+ lock_open_and_time_check(LOCK_EX);
system_checked(opt_command);
lock_delete_and_close();
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;