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)
52 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
53 rv = fscanf(lfp, "%d", &onppid);
55 if (!kill(onppid, 0) || errno == EPERM)
58 lfp = fopen(LOCKFILE, "w");
59 fprintf(lfp, "%ld\n", (long) getpid());
64 void remove_lockfile(void)
70 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
71 * cleanup() . If for some reason sendcommand hangs waiting for the server
72 * to clean up, the alarm clock goes off and the program exits anyway.
73 * The cleanup() routine makes a check to ensure it's not reentering, in
74 * case the ipc module looped it somehow.
76 void nq_cleanup(int e)
79 fprintf(stderr, "\nWatch dog time out.\n");
85 * send binary to server
87 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
89 unsigned int bytes_written = 0;
92 #if defined(HAVE_OPENSSL)
94 serv_write_ssl(ipc, buf, nbytes);
99 while (bytes_written < nbytes) {
100 retval = write(ipc->sock, &buf[bytes_written],
101 nbytes - bytes_written);
103 connection_died(ipc, 0);
106 bytes_written += retval;
113 static int nested = 0;
116 signal(SIGALRM, nq_cleanup);
117 serv_write(ipc, "\n", 1);
124 * This is implemented as a function rather than as a macro because the
125 * client-side IPC modules expect logoff() to be defined. They call logoff()
126 * when a problem connecting or staying connected to the server occurs.
134 * Connect sendcommand to the Citadel server running on this computer.
136 void np_attach_to_server(char *host, char *port)
139 char hostbuf[256], portbuf[256];
141 {"sendcommand", NULL};
144 fprintf(stderr, "Attaching to server...\n");
145 strcpy(hostbuf, host);
146 strcpy(portbuf, port);
147 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
149 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
152 CtdlIPC_chat_recv(ipc, buf);
153 fprintf(stderr, "%s\n", &buf[4]);
154 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
155 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
156 fprintf(stderr, "%s\n", buf);
163 void sendcommand_die(void) {
172 int main(int argc, char **argv)
181 char relhome[PATH_MAX]="";
182 char ctdldir[PATH_MAX]=CTDLDIR;
186 int server_shutting_down = 0;
188 strcpy(ctdl_home_directory, DEFAULT_PORT);
192 * Change directories if specified
194 for (a = 1; a < argc; ++a) {
195 if (!strncmp(argv[a], "-h", 2)) {
196 relh=argv[a][2]!='/';
197 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
198 sizeof ctdl_home_directory);
200 safestrncpy(relhome, &argv[a][2],
203 } else if (!strncmp(argv[a], "-w", 2)) {
204 watchdog = atoi(&argv[a][2]);
208 if (!IsEmptyStr(cmd))
210 strcat(cmd, argv[a]);
214 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
217 signal(SIGINT, cleanup);
218 signal(SIGQUIT, cleanup);
219 signal(SIGHUP, cleanup);
220 signal(SIGTERM, cleanup);
222 fprintf(stderr, "sendcommand: started (pid=%d) "
225 ctdl_home_directory);
229 signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
231 np_attach_to_server(UDS, ctdl_home_directory);
233 setIPCDeathHook(sendcommand_die);
235 fprintf(stderr, "%s\n", cmd);
236 CtdlIPC_chat_send(ipc, cmd);
237 CtdlIPC_chat_recv(ipc, buf);
238 fprintf(stderr, "%s\n", buf);
243 if (!strncasecmp(&buf[1], "31", 2)) {
244 server_shutting_down = 1;
248 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
250 alarm(watchdog); /* Kick the watchdog timer */
252 } else if (buf[0] == '4') {
254 if (fgets(buf, sizeof buf, stdin) == NULL)
256 if (!IsEmptyStr(buf))
257 if (buf[strlen(buf) - 1] == '\n')
258 buf[strlen(buf) - 1] = 0;
259 if (!IsEmptyStr(buf))
260 if (buf[strlen(buf) - 1] == '\r')
261 buf[strlen(buf) - 1] = 0;
262 if (strcmp(buf, "000"))
263 CtdlIPC_chat_send(ipc, buf);
266 FD_SET(ipc->sock, &read_fd);
267 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
270 printf("select failed: %d", err);
273 if (!(errno == EINTR || errno == EAGAIN))
274 printf("select failed: %d", err);
283 n = read(ipc->sock, rbuf, SIZ);
286 fprintf (stderr, rbuf);
290 alarm(watchdog); /* Kick the watchdog timer */
291 } while (strcmp(buf, "000"));
292 CtdlIPC_chat_send(ipc, "\n");
293 CtdlIPC_chat_send(ipc, "000");
295 alarm(0); /* Shutdown the watchdog timer */
296 fprintf(stderr, "sendcommand: processing ended.\n");
298 /* Clean up and log off ... unless the server indicated that the command
299 * we sent is shutting it down, in which case we want to just cut the
300 * connection and exit.
302 if (server_shutting_down) {
314 void stty_ctdl(int cmd) {