X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Futils%2Fsendcommand.c;h=71764bcbbbce69e2a84edf081ca5860dd52b15e7;hb=57b37d4ed168b176d594bc149e5d16a1b9f0517a;hp=faeb469c1bc2ee273f1996867af849d7b27a67e4;hpb=c61307c63378edc18a9adbd628f5fbfc4a78025c;p=citadel.git diff --git a/citadel/utils/sendcommand.c b/citadel/utils/sendcommand.c index faeb469c1..71764bcbb 100644 --- a/citadel/utils/sendcommand.c +++ b/citadel/utils/sendcommand.c @@ -1,8 +1,15 @@ /* - * $Id$ - * * Command-line utility to transmit a server command. * + * Copyright (c) 1987-2012 by the citadel.org team + * + * This program is open source software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3. + * + * 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. */ #include "ctdl_module.h" @@ -36,35 +43,8 @@ #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 @@ -76,7 +56,6 @@ void nq_cleanup(int e) { if (e == SIGALRM) fprintf(stderr, "\nWatch dog time out.\n"); - remove_lockfile(); exit(e); } @@ -86,15 +65,8 @@ void nq_cleanup(int e) void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes) { unsigned int bytes_written = 0; - int retval; -/* -#if defined(HAVE_OPENSSL) - if (ipc->ssl) { - serv_write_ssl(ipc, buf, nbytes); - return; - } -#endif -*/ + int retval = 0; + while (bytes_written < nbytes) { retval = write(ipc->sock, &buf[bytes_written], nbytes - bytes_written); @@ -113,8 +85,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); } @@ -165,9 +141,6 @@ void sendcommand_die(void) { -/* - * main - */ int main(int argc, char **argv) { int a; @@ -181,7 +154,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); @@ -193,19 +166,19 @@ int main(int argc, char **argv) for (a = 1; a < argc; ++a) { if (!strncmp(argv[a], "-h", 2)) { relh=argv[a][2]!='/'; - if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2], - sizeof ctdl_home_directory); - else - safestrncpy(relhome, &argv[a][2], - sizeof relhome); + if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2], sizeof ctdl_home_directory); + else { + safestrncpy(relhome, &argv[a][2], sizeof relhome); + } home=1; } else if (!strncmp(argv[a], "-w", 2)) { watchdog = atoi(&argv[a][2]); if (watchdog<1) watchdog=1; } else { - if (!IsEmptyStr(cmd)) + if (!IsEmptyStr(cmd)) { strcat(cmd, " "); + } strcat(cmd, argv[a]); } } @@ -264,14 +237,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) { @@ -282,7 +251,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); } }