* When saving a Task, if the status is COMPLETED then also set PERCENT-COMPLETE:100...
[citadel.git] / citadel / sendcommand.c
blob2f8a3cfdb86e907f135525c0c382b93d080b7f99
1 /*
2 * $Id$
4 * Command-line utility to transmit a server command.
6 */
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <ctype.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 # include <sys/time.h>
24 # else
25 # include <time.h>
26 # endif
27 #endif
29 #include <signal.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #include "citadel_ipc.h"
35 #include "server.h"
36 #include "config.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)
47 FILE *lfp;
48 int onppid;
50 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
51 fscanf(lfp, "%d", &onppid);
52 fclose(lfp);
53 if (!kill(onppid, 0) || errno == EPERM)
54 return 1;
56 lfp = fopen(LOCKFILE, "w");
57 fprintf(lfp, "%ld\n", (long) getpid());
58 fclose(lfp);
59 return (0);
62 void remove_lockfile(void)
64 unlink(LOCKFILE);
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)
76 if (e == SIGALRM)
77 fprintf(stderr, "\nWatch dog time out.\n");
78 remove_lockfile();
79 exit(e);
83 * send binary to server
85 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
87 unsigned int bytes_written = 0;
88 int retval;
90 #if defined(HAVE_OPENSSL)
91 if (ipc->ssl) {
92 serv_write_ssl(ipc, buf, nbytes);
93 return;
95 #endif
97 while (bytes_written < nbytes) {
98 retval = write(ipc->sock, &buf[bytes_written],
99 nbytes - bytes_written);
100 if (retval < 1) {
101 connection_died(ipc, 0);
102 return;
104 bytes_written += retval;
109 void cleanup(int e)
111 static int nested = 0;
113 alarm(30);
114 signal(SIGALRM, nq_cleanup);
115 serv_write(ipc, "\n", 1);
116 if (nested++ < 1)
117 CtdlIPCQuit(ipc);
118 nq_cleanup(e);
122 * This is implemented as a function rather than as a macro because the
123 * client-side IPC modules expect logoff() to be defined. They call logoff()
124 * when a problem connecting or staying connected to the server occurs.
126 void logoff(int e)
128 cleanup(e);
132 * Connect sendcommand to the Citadel server running on this computer.
134 void np_attach_to_server(char *host, char *port)
136 char buf[SIZ];
137 char hostbuf[256], portbuf[256];
138 char *args[] =
139 {"sendcommand", NULL};
140 int r;
142 fprintf(stderr, "Attaching to server...\n");
143 strcpy(hostbuf, host);
144 strcpy(portbuf, port);
145 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
146 if (!ipc) {
147 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
148 exit(3);
150 CtdlIPC_chat_recv(ipc, buf);
151 fprintf(stderr, "%s\n", &buf[4]);
152 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
153 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
154 fprintf(stderr, "%s\n", buf);
155 if (r / 100 != 2) {
156 cleanup(2);
161 void sendcommand_die(void) {
162 exit(0);
168 * main
170 int main(int argc, char **argv)
172 int a;
173 char cmd[SIZ];
174 char buf[SIZ];
175 int watchdog = 5;
177 int relh=0;
178 int home=0;
179 char relhome[PATH_MAX]="";
180 char ctdldir[PATH_MAX]=CTDLDIR;
181 fd_set read_fd;
182 struct timeval tv;
183 int ret, err;
184 int server_shutting_down = 0;
186 strcpy(ctdl_home_directory, DEFAULT_PORT);
188 strcpy(cmd, "");
190 * Change directories if specified
192 for (a = 1; a < argc; ++a) {
193 if (!strncmp(argv[a], "-h", 2)) {
194 relh=argv[a][2]!='/';
195 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
196 sizeof ctdl_home_directory);
197 else
198 safestrncpy(relhome, &argv[a][2],
199 sizeof relhome);
200 home=1;
201 } else if (!strncmp(argv[a], "-w", 2)) {
202 watchdog = atoi(&argv[a][2]);
203 if (watchdog<1)
204 watchdog=1;
205 } else {
206 if (!IsEmptyStr(cmd))
207 strcat(cmd, " ");
208 strcat(cmd, argv[a]);
212 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
213 get_config();
215 signal(SIGINT, cleanup);
216 signal(SIGQUIT, cleanup);
217 signal(SIGHUP, cleanup);
218 signal(SIGTERM, cleanup);
220 fprintf(stderr, "sendcommand: started (pid=%d) "
221 "running in %s\n",
222 (int) getpid(),
223 ctdl_home_directory);
224 fflush(stderr);
226 alarm(watchdog);
227 signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
229 np_attach_to_server(UDS, ctdl_home_directory);
230 fflush(stderr);
231 setIPCDeathHook(sendcommand_die);
233 fprintf(stderr, "%s\n", cmd);
234 CtdlIPC_chat_send(ipc, cmd);
235 CtdlIPC_chat_recv(ipc, buf);
236 fprintf(stderr, "%s\n", buf);
238 tv.tv_sec = 0;
239 tv.tv_usec = 1000;
241 if (!strncasecmp(&buf[1], "31", 2)) {
242 server_shutting_down = 1;
245 if (buf[0] == '1') {
246 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
247 printf("%s\n", buf);
248 alarm(watchdog); /* Kick the watchdog timer */
250 } else if (buf[0] == '4') {
251 do {
252 if (fgets(buf, sizeof buf, stdin) == NULL)
253 strcpy(buf, "000");
254 if (!IsEmptyStr(buf))
255 if (buf[strlen(buf) - 1] == '\n')
256 buf[strlen(buf) - 1] = 0;
257 if (!IsEmptyStr(buf))
258 if (buf[strlen(buf) - 1] == '\r')
259 buf[strlen(buf) - 1] = 0;
260 if (strcmp(buf, "000"))
261 CtdlIPC_chat_send(ipc, buf);
263 FD_ZERO(&read_fd);
264 FD_SET(ipc->sock, &read_fd);
265 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
266 err = errno;
267 if (err!=0)
268 printf("select failed: %d", err);
270 if (ret == -1) {
271 if (!(errno == EINTR || errno == EAGAIN))
272 printf("select failed: %d", err);
273 return 1;
276 if (ret != 0) {
277 size_t n;
278 char rbuf[SIZ];
280 rbuf[0] = '\0';
281 n = read(ipc->sock, rbuf, SIZ);
282 if (n>0) {
283 rbuf[n]='\0';
284 fprintf (stderr, rbuf);
285 fflush (stdout);
288 alarm(watchdog); /* Kick the watchdog timer */
289 } while (strcmp(buf, "000"));
290 CtdlIPC_chat_send(ipc, "\n");
291 CtdlIPC_chat_send(ipc, "000");
293 alarm(0); /* Shutdown the watchdog timer */
294 fprintf(stderr, "sendcommand: processing ended.\n");
296 /* Clean up and log off ... unless the server indicated that the command
297 * we sent is shutting it down, in which case we want to just cut the
298 * connection and exit.
300 if (server_shutting_down) {
301 nq_cleanup(0);
303 else {
304 cleanup(0);
306 return 0;
310 * Stub function
312 void stty_ctdl(int cmd) {