SafeTimer: check the IPC struct before trying to send a kill to the server in the...
[citadel.git] / citadel / utils / sendcommand.c
index c259a5b685e44d7eb327bee0aa19eeda85c3a400..c0870f62bfb046c2a6fe4c160c85db90f2256d64 100644 (file)
 #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;
-       int rv;
-
-       if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
-               rv = 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
@@ -90,7 +62,6 @@ void nq_cleanup(int e)
 {
        if (e == SIGALRM)
                fprintf(stderr, "\nWatch dog time out.\n");
-       remove_lockfile();
        exit(e);
 }
 
@@ -127,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);
 }
@@ -289,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);
                                }
                        }