4 * Command-line utility to transmit a server command.
8 #include "ctdl_module.h"
12 #include <sys/types.h>
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
33 #include <libcitadel.h>
35 #include "citadel_ipc.h"
39 #define LOCKFILE "/tmp/LCK.sendcommand"
41 static CtdlIPC *ipc = NULL;
44 * make sure only one copy of sendcommand runs at a time, using lock files
46 int set_lockfile(void)
51 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
52 fscanf(lfp, "%d", &onppid);
54 if (!kill(onppid, 0) || errno == EPERM)
57 lfp = fopen(LOCKFILE, "w");
58 fprintf(lfp, "%ld\n", (long) getpid());
63 void remove_lockfile(void)
69 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
70 * cleanup() . If for some reason sendcommand hangs waiting for the server
71 * to clean up, the alarm clock goes off and the program exits anyway.
72 * The cleanup() routine makes a check to ensure it's not reentering, in
73 * case the ipc module looped it somehow.
75 void nq_cleanup(int e)
78 fprintf(stderr, "\nWatch dog time out.\n");
84 * send binary to server
86 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
88 unsigned int bytes_written = 0;
91 #if defined(HAVE_OPENSSL)
93 serv_write_ssl(ipc, buf, nbytes);
98 while (bytes_written < nbytes) {
99 retval = write(ipc->sock, &buf[bytes_written],
100 nbytes - bytes_written);
102 connection_died(ipc, 0);
105 bytes_written += retval;
112 static int nested = 0;
115 signal(SIGALRM, nq_cleanup);
116 serv_write(ipc, "\n", 1);
123 * This is implemented as a function rather than as a macro because the
124 * client-side IPC modules expect logoff() to be defined. They call logoff()
125 * when a problem connecting or staying connected to the server occurs.
133 * Connect sendcommand to the Citadel server running on this computer.
135 void np_attach_to_server(char *host, char *port)
138 char hostbuf[256], portbuf[256];
140 {"sendcommand", NULL};
143 fprintf(stderr, "Attaching to server...\n");
144 strcpy(hostbuf, host);
145 strcpy(portbuf, port);
146 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
148 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
151 CtdlIPC_chat_recv(ipc, buf);
152 fprintf(stderr, "%s\n", &buf[4]);
153 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
154 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
155 fprintf(stderr, "%s\n", buf);
162 void sendcommand_die(void) {
171 int main(int argc, char **argv)
180 char relhome[PATH_MAX]="";
181 char ctdldir[PATH_MAX]=CTDLDIR;
185 int server_shutting_down = 0;
187 strcpy(ctdl_home_directory, DEFAULT_PORT);
191 * Change directories if specified
193 for (a = 1; a < argc; ++a) {
194 if (!strncmp(argv[a], "-h", 2)) {
195 relh=argv[a][2]!='/';
196 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
197 sizeof ctdl_home_directory);
199 safestrncpy(relhome, &argv[a][2],
202 } else if (!strncmp(argv[a], "-w", 2)) {
203 watchdog = atoi(&argv[a][2]);
207 if (!IsEmptyStr(cmd))
209 strcat(cmd, argv[a]);
213 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
216 signal(SIGINT, cleanup);
217 signal(SIGQUIT, cleanup);
218 signal(SIGHUP, cleanup);
219 signal(SIGTERM, cleanup);
221 fprintf(stderr, "sendcommand: started (pid=%d) "
224 ctdl_home_directory);
228 signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
230 np_attach_to_server(UDS, ctdl_home_directory);
232 setIPCDeathHook(sendcommand_die);
234 fprintf(stderr, "%s\n", cmd);
235 CtdlIPC_chat_send(ipc, cmd);
236 CtdlIPC_chat_recv(ipc, buf);
237 fprintf(stderr, "%s\n", buf);
242 if (!strncasecmp(&buf[1], "31", 2)) {
243 server_shutting_down = 1;
247 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
249 alarm(watchdog); /* Kick the watchdog timer */
251 } else if (buf[0] == '4') {
253 if (fgets(buf, sizeof buf, stdin) == NULL)
255 if (!IsEmptyStr(buf))
256 if (buf[strlen(buf) - 1] == '\n')
257 buf[strlen(buf) - 1] = 0;
258 if (!IsEmptyStr(buf))
259 if (buf[strlen(buf) - 1] == '\r')
260 buf[strlen(buf) - 1] = 0;
261 if (strcmp(buf, "000"))
262 CtdlIPC_chat_send(ipc, buf);
265 FD_SET(ipc->sock, &read_fd);
266 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
269 printf("select failed: %d", err);
272 if (!(errno == EINTR || errno == EAGAIN))
273 printf("select failed: %d", err);
282 n = read(ipc->sock, rbuf, SIZ);
285 fprintf (stderr, rbuf);
289 alarm(watchdog); /* Kick the watchdog timer */
290 } while (strcmp(buf, "000"));
291 CtdlIPC_chat_send(ipc, "\n");
292 CtdlIPC_chat_send(ipc, "000");
294 alarm(0); /* Shutdown the watchdog timer */
295 fprintf(stderr, "sendcommand: processing ended.\n");
297 /* Clean up and log off ... unless the server indicated that the command
298 * we sent is shutting it down, in which case we want to just cut the
299 * connection and exit.
301 if (server_shutting_down) {
313 void stty_ctdl(int cmd) {