2 * Command-line utility to transmit a server command.
4 * Copyright (c) 1987-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "ctdl_module.h"
25 #include <sys/types.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
46 #include <libcitadel.h>
48 #include "citadel_ipc.h"
52 static CtdlIPC *ipc = NULL;
55 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
56 * cleanup() . If for some reason sendcommand hangs waiting for the server
57 * to clean up, the alarm clock goes off and the program exits anyway.
58 * The cleanup() routine makes a check to ensure it's not reentering, in
59 * case the ipc module looped it somehow.
61 void nq_cleanup(int e)
64 fprintf(stderr, "\nWatch dog time out.\n");
69 * send binary to server
71 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
73 unsigned int bytes_written = 0;
76 #if defined(HAVE_OPENSSL)
78 serv_write_ssl(ipc, buf, nbytes);
83 while (bytes_written < nbytes) {
84 retval = write(ipc->sock, &buf[bytes_written],
85 nbytes - bytes_written);
87 connection_died(ipc, 0);
90 bytes_written += retval;
97 static int nested = 0;
100 signal(SIGALRM, nq_cleanup);
101 serv_write(ipc, "\n", 1);
108 * This is implemented as a function rather than as a macro because the
109 * client-side IPC modules expect logoff() to be defined. They call logoff()
110 * when a problem connecting or staying connected to the server occurs.
118 * Connect sendcommand to the Citadel server running on this computer.
120 void np_attach_to_server(char *host, char *port)
123 char hostbuf[256], portbuf[256];
125 {"sendcommand", NULL};
128 fprintf(stderr, "Attaching to server...\n");
129 strcpy(hostbuf, host);
130 strcpy(portbuf, port);
131 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
133 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
136 CtdlIPC_chat_recv(ipc, buf);
137 fprintf(stderr, "%s\n", &buf[4]);
138 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
139 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
140 fprintf(stderr, "%s\n", buf);
147 void sendcommand_die(void) {
153 int main(int argc, char **argv)
162 char relhome[PATH_MAX]="";
163 char ctdldir[PATH_MAX]=CTDLDIR;
167 int server_shutting_down = 0;
169 strcpy(ctdl_home_directory, DEFAULT_PORT);
173 * Change directories if specified
175 for (a = 1; a < argc; ++a) {
176 if (!strncmp(argv[a], "-h", 2)) {
177 relh=argv[a][2]!='/';
178 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
179 sizeof ctdl_home_directory);
181 safestrncpy(relhome, &argv[a][2],
184 } else if (!strncmp(argv[a], "-w", 2)) {
185 watchdog = atoi(&argv[a][2]);
189 if (!IsEmptyStr(cmd))
191 strcat(cmd, argv[a]);
195 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
198 signal(SIGINT, cleanup);
199 signal(SIGQUIT, cleanup);
200 signal(SIGHUP, cleanup);
201 signal(SIGTERM, cleanup);
203 fprintf(stderr, "sendcommand: started (pid=%d) "
206 ctdl_home_directory);
210 signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
212 np_attach_to_server(UDS, ctdl_home_directory);
214 setIPCDeathHook(sendcommand_die);
216 fprintf(stderr, "%s\n", cmd);
217 CtdlIPC_chat_send(ipc, cmd);
218 CtdlIPC_chat_recv(ipc, buf);
219 fprintf(stderr, "%s\n", buf);
224 if (!strncasecmp(&buf[1], "31", 2)) {
225 server_shutting_down = 1;
229 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
231 alarm(watchdog); /* Kick the watchdog timer */
233 } else if (buf[0] == '4') {
235 if (fgets(buf, sizeof buf, stdin) == NULL)
237 if (!IsEmptyStr(buf))
238 if (buf[strlen(buf) - 1] == '\n')
239 buf[strlen(buf) - 1] = 0;
240 if (!IsEmptyStr(buf))
241 if (buf[strlen(buf) - 1] == '\r')
242 buf[strlen(buf) - 1] = 0;
243 if (strcmp(buf, "000"))
244 CtdlIPC_chat_send(ipc, buf);
247 FD_SET(ipc->sock, &read_fd);
248 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
250 if (!(errno == EINTR || errno == EAGAIN))
251 fprintf(stderr, "select() failed: %s", strerror(errno));
260 n = read(ipc->sock, rbuf, SIZ);
263 fprintf(stderr, "%s", rbuf);
267 alarm(watchdog); /* Kick the watchdog timer */
268 } while (strcmp(buf, "000"));
269 CtdlIPC_chat_send(ipc, "\n");
270 CtdlIPC_chat_send(ipc, "000");
272 alarm(0); /* Shutdown the watchdog timer */
273 fprintf(stderr, "sendcommand: processing ended.\n");
275 /* Clean up and log off ... unless the server indicated that the command
276 * we sent is shutting it down, in which case we want to just cut the
277 * connection and exit.
279 if (server_shutting_down) {
291 void stty_ctdl(int cmd) {