4 * Command-line utility to transmit a server command.
11 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
32 #include <libcitadel.h>
34 #include "citadel_ipc.h"
38 #define LOCKFILE "/tmp/LCK.sendcommand"
40 static CtdlIPC *ipc = NULL;
43 * make sure only one copy of sendcommand runs at a time, using lock files
45 int set_lockfile(void)
50 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
51 fscanf(lfp, "%d", &onppid);
53 if (!kill(onppid, 0) || errno == EPERM)
56 lfp = fopen(LOCKFILE, "w");
57 fprintf(lfp, "%ld\n", (long) getpid());
62 void remove_lockfile(void)
68 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
69 * cleanup() . If for some reason sendcommand hangs waiting for the server
70 * to clean up, the alarm clock goes off and the program exits anyway.
71 * The cleanup() routine makes a check to ensure it's not reentering, in
72 * case the ipc module looped it somehow.
74 void nq_cleanup(int e)
81 * send binary to server
83 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
85 unsigned int bytes_written = 0;
88 #if defined(HAVE_OPENSSL)
90 serv_write_ssl(ipc, buf, nbytes);
95 while (bytes_written < nbytes) {
96 retval = write(ipc->sock, &buf[bytes_written],
97 nbytes - bytes_written);
99 connection_died(ipc, 0);
102 bytes_written += retval;
109 static int nested = 0;
112 signal(SIGALRM, nq_cleanup);
113 serv_write(ipc, "\n", 1);
120 * This is implemented as a function rather than as a macro because the
121 * client-side IPC modules expect logoff() to be defined. They call logoff()
122 * when a problem connecting or staying connected to the server occurs.
130 * Connect sendcommand to the Citadel server running on this computer.
132 void np_attach_to_server(char *host, char *port)
135 char hostbuf[256], portbuf[256];
137 {"sendcommand", NULL};
140 fprintf(stderr, "Attaching to server...\n");
141 strcpy(hostbuf, host);
142 strcpy(portbuf, port);
143 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
145 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
148 CtdlIPC_chat_recv(ipc, buf);
149 fprintf(stderr, "%s\n", &buf[4]);
150 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
151 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
152 fprintf(stderr, "%s\n", buf);
159 void sendcommand_die(void) {
164 void check_exit_code(int code, void *arg)
167 fprintf(stderr, "\nWatch dog time out.\n");
174 int main(int argc, char **argv)
183 char relhome[PATH_MAX]="";
184 char ctdldir[PATH_MAX]=CTDLDIR;
188 int server_shutting_down = 0;
190 on_exit (check_exit_code, NULL);
192 strcpy(ctdl_home_directory, DEFAULT_PORT);
196 * Change directories if specified
198 for (a = 1; a < argc; ++a) {
199 if (!strncmp(argv[a], "-h", 2)) {
200 relh=argv[a][2]!='/';
201 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
202 sizeof ctdl_home_directory);
204 safestrncpy(relhome, &argv[a][2],
208 } else if (!strncmp(argv[a], "-w", 2)) {
209 watchdog = atoi(&argv[a][2]);
213 if (!IsEmptyStr(cmd))
215 strcat(cmd, argv[a]);
219 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
222 signal(SIGINT, cleanup);
223 signal(SIGQUIT, cleanup);
224 signal(SIGHUP, cleanup);
225 signal(SIGTERM, cleanup);
227 fprintf(stderr, "sendcommand: started (pid=%d) "
230 ctdl_home_directory);
234 signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
236 np_attach_to_server(UDS, ctdl_home_directory);
238 setIPCDeathHook(sendcommand_die);
240 fprintf(stderr, "%s\n", cmd);
241 CtdlIPC_chat_send(ipc, cmd);
242 CtdlIPC_chat_recv(ipc, buf);
243 fprintf(stderr, "%s\n", buf);
248 if (!strncasecmp(&buf[1], "31", 2)) {
249 server_shutting_down = 1;
253 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
255 alarm(watchdog); /* Kick the watchdog timer */
257 } else if (buf[0] == '4') {
259 if (fgets(buf, sizeof buf, stdin) == NULL)
261 if (!IsEmptyStr(buf))
262 if (buf[strlen(buf) - 1] == '\n')
263 buf[strlen(buf) - 1] = 0;
264 if (!IsEmptyStr(buf))
265 if (buf[strlen(buf) - 1] == '\r')
266 buf[strlen(buf) - 1] = 0;
267 if (strcmp(buf, "000"))
268 CtdlIPC_chat_send(ipc, buf);
271 FD_SET(ipc->sock, &read_fd);
272 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
275 printf("select failed: %d", err);
278 if (!(errno == EINTR || errno == EAGAIN))
279 printf("select failed: %d", err);
288 n = read(ipc->sock, rbuf, SIZ);
291 fprintf (stderr, rbuf);
295 alarm(watchdog); /* Kick the watchdog timer */
296 } while (strcmp(buf, "000"));
297 CtdlIPC_chat_send(ipc, "\n");
298 CtdlIPC_chat_send(ipc, "000");
300 alarm(0); /* Shutdown the watchdog timer */
301 fprintf(stderr, "sendcommand: processing ended.\n");
303 /* Clean up and log off ... unless the server indicated that the command
304 * we sent is shutting it down, in which case we want to just cut the
305 * connection and exit.
307 if (server_shutting_down) {