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>
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)
82 static int nested = 0;
85 signal(SIGALRM, nq_cleanup);
92 * This is implemented as a function rather than as a macro because the
93 * client-side IPC modules expect logoff() to be defined. They call logoff()
94 * when a problem connecting or staying connected to the server occurs.
102 * Connect sendcommand to the Citadel server running on this computer.
104 void np_attach_to_server(char *host, char *port)
107 char hostbuf[256], portbuf[256];
109 {"sendcommand", NULL};
112 fprintf(stderr, "Attaching to server...\n");
113 strcpy(hostbuf, host);
114 strcpy(portbuf, port);
115 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
117 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
120 CtdlIPC_chat_recv(ipc, buf);
121 fprintf(stderr, "%s\n", &buf[4]);
122 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
123 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
124 fprintf(stderr, "%s\n", buf);
131 void sendcommand_die(void) {
139 int main(int argc, char **argv)
147 char relhome[PATH_MAX]="";
148 char ctdldir[PATH_MAX]=CTDLDIR;
153 CtdlInitBase64Table();
155 strcpy(ctdl_home_directory, DEFAULT_PORT);
159 * Change directories if specified
161 for (a = 1; a < argc; ++a) {
162 if (!strncmp(argv[a], "-h", 2)) {
163 relh=argv[a][2]!='/';
164 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
165 sizeof ctdl_home_directory);
167 safestrncpy(relhome, &argv[a][2],
172 if (!IsEmptyStr(cmd))
174 strcat(cmd, argv[a]);
178 calc_dirs_n_files(relh, home, relhome, ctdldir);
181 signal(SIGINT, cleanup);
182 signal(SIGQUIT, cleanup);
183 signal(SIGHUP, cleanup);
184 signal(SIGTERM, cleanup);
186 fprintf(stderr, "sendcommand: started (pid=%d) "
189 ctdl_home_directory);
193 signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
195 np_attach_to_server(UDS, ctdl_home_directory);
197 setIPCDeathHook(sendcommand_die);
199 fprintf(stderr, "%s\n", cmd);
200 CtdlIPC_chat_send(ipc, cmd);
201 CtdlIPC_chat_recv(ipc, buf);
202 fprintf(stderr, "%s\n", buf);
208 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
210 alarm(5); /* Kick the watchdog timer */
212 } else if (buf[0] == '4') {
214 if (fgets(buf, sizeof buf, stdin) == NULL)
216 if (!IsEmptyStr(buf))
217 if (buf[strlen(buf) - 1] == '\n')
218 buf[strlen(buf) - 1] = 0;
219 if (!IsEmptyStr(buf))
220 if (buf[strlen(buf) - 1] == '\r')
221 buf[strlen(buf) - 1] = 0;
222 if (strcmp(buf, "000"))
223 CtdlIPC_chat_send(ipc, buf);
226 FD_SET(ipc->sock, &read_fd);
227 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
230 printf("select failed: %d", err);
233 if (!(errno == EINTR || errno == EAGAIN))
234 printf("select failed: %d", err);
243 n = read(ipc->sock, rbuf, SIZ);
246 fprintf (stderr, rbuf);
250 alarm(5); /* Kick the watchdog timer */
251 } while (strcmp(buf, "000"));
252 CtdlIPC_chat_send(ipc, "\n");
253 CtdlIPC_chat_send(ipc, "000");
255 alarm(0); /* Shutdown the watchdog timer */
256 fprintf(stderr, "sendcommand: processing ended.\n");
257 if (strcasecmp(cmd, "DOWN"))
259 else /* If we downed the server we can't to do CtdlIPCQuit in cleanup()*/