Fixed --stop if the lock file does not exist.
[inetdmx.git] / inetdmx.c
index ebab3bf..99077b9 100644 (file)
--- a/inetdmx.c
+++ b/inetdmx.c
@@ -1,4 +1,21 @@
-/* $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
@@ -72,11 +89,14 @@ static int opt_syslog;
 static int opt_stderr;
 static const char *opt_lock;
 static int opt_ignore_spawned_command_output;
-static const char *opt_command;
+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 +104,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)) {
@@ -135,6 +157,15 @@ int rc;
        return string;
 }
 
+static void *xmalloc(size_t size)
+{
+void *r;
+
+       if ((r=malloc(size)))
+               return r;
+       fatal("Error allocing %lu bytes",(unsigned long)size);
+}
+
 static void usage(void)
 {
        fprintf(stderr,"\
@@ -228,13 +259,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;
@@ -276,13 +319,6 @@ int fdi,fdo;
        pollfdi_name[0]=conn0_name;
        pollfdi_name[1]=conn1_name;
        for (;;) {
-               for (fdi=0;fdi<G_N_ELEMENTS(pollfdi);fdi++)
-                       if (pollfdi[fdi].events)
-                               break;
-               if (fdi>=G_N_ELEMENTS(pollfdi)) {
-                       lock_close();
-                       exit(EXIT_SUCCESS);
-                       }
                if (0>=poll(pollfdi,G_N_ELEMENTS(pollfdi),-1))
                        fatal("poll(%s socket,%s socket): %m",pollfdi_name[0],pollfdi_name[1]);
                for (fdi=0;fdi<G_N_ELEMENTS(pollfdi);fdi++)
@@ -307,8 +343,8 @@ int fdi,fdo;
                                        fatal("read(%s socket): %m",pollfdi_name[fdi]);
                                        }
                                if (got==0) {
-                                       pollfdi[fdi].events&=~POLLIN;
-                                       break;
+                                       lock_close();
+                                       exit(EXIT_SUCCESS);
                                        }
                                for (fdo=0;fdo<G_N_ELEMENTS(pollfdi);fdo++) {
                                        if (fdi==fdo)
@@ -434,16 +470,21 @@ int retry;
                        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;
@@ -470,12 +511,12 @@ int is_fresh;
        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();
 
@@ -486,12 +527,17 @@ int main(int argc,char **argv) G_GNUC_NORETURN;
 int main(int argc,char **argv)
 {
 char optc;
+size_t opt_command_len;
+int i;
+char *s;
 
        if ((program_name=strrchr(argv[0],'/')))
                program_name++;
        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;
@@ -556,9 +602,21 @@ char *endptr;
 
        if (optind>=argc)
                fatal("<start-server-command/stop-server-command> is a required argument");
-       if (optind+1<argc)
-               fatal("Too many arguments, <start-server-command/stop-server-command> may need quoting");
-       opt_command=argv[optind];
+       opt_command_len=0;
+       for (i=optind;i<argc;i++)
+               opt_command_len+=strlen(argv[i])+1;
+       opt_command=xmalloc(opt_command_len);
+       s=opt_command;
+       for (i=optind;i<argc;i++) {
+size_t argv_i_len=strlen(argv[i]);
+
+               if (s>opt_command)
+                       *s++=' ';
+               memcpy(s,argv[i],argv_i_len);
+               s+=argv_i_len;
+               }
+       *s++=0;
+       assert(s==opt_command+opt_command_len);
 
        if (!opt_syslog && !opt_stderr)
                opt_stderr=1;