SafeTimer: check the IPC struct before trying to send a kill to the server in the...
[citadel.git] / citadel / utils / sendcommand.c
index 5012877018945538de8426e941944eb58ce1d2b6..c0870f62bfb046c2a6fe4c160c85db90f2256d64 100644 (file)
@@ -1,10 +1,24 @@
 /*
- * $Id$
- *
  * Command-line utility to transmit a server command.
  *
+ * Copyright (c) 1987-2010 by the citadel.org team
+ *
+ * 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; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include "ctdl_module.h"
 
 #include <stdlib.h>
 #include <unistd.h>
 #include "server.h"
 #include "config.h"
 
-#define LOCKFILE "/tmp/LCK.sendcommand"
-
 static CtdlIPC *ipc = NULL;
 
-/*
- * make sure only one copy of sendcommand runs at a time, using lock files
- */
-int set_lockfile(void)
-{
-       FILE *lfp;
-       int onppid;
-
-       if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
-               fscanf(lfp, "%d", &onppid);
-               fclose(lfp);
-               if (!kill(onppid, 0) || errno == EPERM)
-                       return 1;
-       }
-       lfp = fopen(LOCKFILE, "w");
-       fprintf(lfp, "%ld\n", (long) getpid());
-       fclose(lfp);
-       return (0);
-}
-
-void remove_lockfile(void)
-{
-       unlink(LOCKFILE);
-}
-
 /*
  * Why both cleanup() and nq_cleanup() ?  Notice the alarm() call in
  * cleanup() .  If for some reason sendcommand hangs waiting for the server
@@ -75,7 +62,6 @@ void nq_cleanup(int e)
 {
        if (e == SIGALRM)
                fprintf(stderr, "\nWatch dog time out.\n");
-       remove_lockfile();
        exit(e);
 }
 
@@ -112,8 +98,12 @@ void cleanup(int e)
 
        alarm(30);
        signal(SIGALRM, nq_cleanup);
-       serv_write(ipc, "\n", 1);
-       if (nested++ < 1)
+       if ((ipc != NULL) && 
+           (ipc->sock > 0))
+               serv_write(ipc, "\n", 1);
+       if ((nested++ < 1) &&
+           (ipc != NULL) &&
+           (ipc->sock > 0))
                CtdlIPCQuit(ipc);
        nq_cleanup(e);
 }
@@ -164,9 +154,6 @@ void sendcommand_die(void) {
 
 
 
-/*
- * main
- */
 int main(int argc, char **argv)
 {
        int a;
@@ -180,7 +167,7 @@ int main(int argc, char **argv)
        char ctdldir[PATH_MAX]=CTDLDIR;
        fd_set read_fd;
        struct timeval tv;
-       int ret, err;
+       int ret;
        int server_shutting_down = 0;
        
        strcpy(ctdl_home_directory, DEFAULT_PORT);
@@ -263,14 +250,10 @@ int main(int argc, char **argv)
                        FD_ZERO(&read_fd);
                        FD_SET(ipc->sock, &read_fd);
                        ret = select(ipc->sock+1, &read_fd, NULL, NULL,  &tv);
-                       err = errno;
-                       if (err!=0)
-                               printf("select failed: %d", err);
-
                        if (ret == -1) {
                                if (!(errno == EINTR || errno == EAGAIN))
-                                       printf("select failed: %d", err);
-                               return 1;
+                                       fprintf(stderr, "select() failed: %s", strerror(errno));
+                               return(1);
                        }
 
                        if (ret != 0) {
@@ -281,7 +264,7 @@ int main(int argc, char **argv)
                                n = read(ipc->sock, rbuf, SIZ);
                                if (n>0) {
                                        rbuf[n]='\0';
-                                       fprintf (stderr, rbuf);
+                                       fprintf(stderr, "%s", rbuf);
                                        fflush (stdout);
                                }
                        }