4 * Command-line utility to transmit a server command.
6 * Copyright (c) 1987-2010 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "ctdl_module.h"
27 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
48 #include <libcitadel.h>
50 #include "citadel_ipc.h"
54 #define LOCKFILE "/tmp/LCK.sendcommand"
56 static CtdlIPC *ipc = NULL;
59 * make sure only one copy of sendcommand runs at a time, using lock files
61 int set_lockfile(void)
67 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
68 rv = fscanf(lfp, "%d", &onppid);
70 if (!kill(onppid, 0) || errno == EPERM)
73 lfp = fopen(LOCKFILE, "w");
74 fprintf(lfp, "%ld\n", (long) getpid());
79 void remove_lockfile(void)
85 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
86 * cleanup() . If for some reason sendcommand hangs waiting for the server
87 * to clean up, the alarm clock goes off and the program exits anyway.
88 * The cleanup() routine makes a check to ensure it's not reentering, in
89 * case the ipc module looped it somehow.
91 void nq_cleanup(int e)
94 fprintf(stderr, "\nWatch dog time out.\n");
100 * send binary to server
102 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
104 unsigned int bytes_written = 0;
107 #if defined(HAVE_OPENSSL)
109 serv_write_ssl(ipc, buf, nbytes);
114 while (bytes_written < nbytes) {
115 retval = write(ipc->sock, &buf[bytes_written],
116 nbytes - bytes_written);
118 connection_died(ipc, 0);
121 bytes_written += retval;
128 static int nested = 0;
131 signal(SIGALRM, nq_cleanup);
132 serv_write(ipc, "\n", 1);
139 * This is implemented as a function rather than as a macro because the
140 * client-side IPC modules expect logoff() to be defined. They call logoff()
141 * when a problem connecting or staying connected to the server occurs.
149 * Connect sendcommand to the Citadel server running on this computer.
151 void np_attach_to_server(char *host, char *port)
154 char hostbuf[256], portbuf[256];
156 {"sendcommand", NULL};
159 fprintf(stderr, "Attaching to server...\n");
160 strcpy(hostbuf, host);
161 strcpy(portbuf, port);
162 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
164 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
167 CtdlIPC_chat_recv(ipc, buf);
168 fprintf(stderr, "%s\n", &buf[4]);
169 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
170 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
171 fprintf(stderr, "%s\n", buf);
178 void sendcommand_die(void) {
184 int main(int argc, char **argv)
193 char relhome[PATH_MAX]="";
194 char ctdldir[PATH_MAX]=CTDLDIR;
198 int server_shutting_down = 0;
200 strcpy(ctdl_home_directory, DEFAULT_PORT);
204 * Change directories if specified
206 for (a = 1; a < argc; ++a) {
207 if (!strncmp(argv[a], "-h", 2)) {
208 relh=argv[a][2]!='/';
209 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
210 sizeof ctdl_home_directory);
212 safestrncpy(relhome, &argv[a][2],
215 } else if (!strncmp(argv[a], "-w", 2)) {
216 watchdog = atoi(&argv[a][2]);
220 if (!IsEmptyStr(cmd))
222 strcat(cmd, argv[a]);
226 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
229 signal(SIGINT, cleanup);
230 signal(SIGQUIT, cleanup);
231 signal(SIGHUP, cleanup);
232 signal(SIGTERM, cleanup);
234 fprintf(stderr, "sendcommand: started (pid=%d) "
237 ctdl_home_directory);
241 signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
243 np_attach_to_server(UDS, ctdl_home_directory);
245 setIPCDeathHook(sendcommand_die);
247 fprintf(stderr, "%s\n", cmd);
248 CtdlIPC_chat_send(ipc, cmd);
249 CtdlIPC_chat_recv(ipc, buf);
250 fprintf(stderr, "%s\n", buf);
255 if (!strncasecmp(&buf[1], "31", 2)) {
256 server_shutting_down = 1;
260 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
262 alarm(watchdog); /* Kick the watchdog timer */
264 } else if (buf[0] == '4') {
266 if (fgets(buf, sizeof buf, stdin) == NULL)
268 if (!IsEmptyStr(buf))
269 if (buf[strlen(buf) - 1] == '\n')
270 buf[strlen(buf) - 1] = 0;
271 if (!IsEmptyStr(buf))
272 if (buf[strlen(buf) - 1] == '\r')
273 buf[strlen(buf) - 1] = 0;
274 if (strcmp(buf, "000"))
275 CtdlIPC_chat_send(ipc, buf);
278 FD_SET(ipc->sock, &read_fd);
279 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
281 if (!(errno == EINTR || errno == EAGAIN))
282 fprintf(stderr, "select() failed: %s", strerror(errno));
291 n = read(ipc->sock, rbuf, SIZ);
294 fprintf(stderr, rbuf);
298 alarm(watchdog); /* Kick the watchdog timer */
299 } while (strcmp(buf, "000"));
300 CtdlIPC_chat_send(ipc, "\n");
301 CtdlIPC_chat_send(ipc, "000");
303 alarm(0); /* Shutdown the watchdog timer */
304 fprintf(stderr, "sendcommand: processing ended.\n");
306 /* Clean up and log off ... unless the server indicated that the command
307 * we sent is shutting it down, in which case we want to just cut the
308 * connection and exit.
310 if (server_shutting_down) {
322 void stty_ctdl(int cmd) {