8ea8fd633e97a7273530a86595992a6c63efd78e
[citadel.git] / citadel / utils / sendcommand.c
1 /*
2  * $Id$
3  *
4  * Command-line utility to transmit a server command.
5  *
6  */
7
8 #include "ctdl_module.h"
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <ctype.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <signal.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <libcitadel.h>
34 #include "citadel.h"
35 #include "citadel_ipc.h"
36 #include "server.h"
37 #include "config.h"
38
39 #define LOCKFILE "/tmp/LCK.sendcommand"
40
41 static CtdlIPC *ipc = NULL;
42
43 /*
44  * make sure only one copy of sendcommand runs at a time, using lock files
45  */
46 int set_lockfile(void)
47 {
48         FILE *lfp;
49         int onppid;
50         int rv;
51
52         if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
53                 rv = fscanf(lfp, "%d", &onppid);
54                 fclose(lfp);
55                 if (!kill(onppid, 0) || errno == EPERM)
56                         return 1;
57         }
58         lfp = fopen(LOCKFILE, "w");
59         fprintf(lfp, "%ld\n", (long) getpid());
60         fclose(lfp);
61         return (0);
62 }
63
64 void remove_lockfile(void)
65 {
66         unlink(LOCKFILE);
67 }
68
69 /*
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.
75  */
76 void nq_cleanup(int e)
77 {
78         if (e == SIGALRM)
79                 fprintf(stderr, "\nWatch dog time out.\n");
80         remove_lockfile();
81         exit(e);
82 }
83
84 /*
85  * send binary to server
86  */
87 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
88 {
89         unsigned int bytes_written = 0;
90         int retval;
91 /*
92 #if defined(HAVE_OPENSSL)
93         if (ipc->ssl) {
94                 serv_write_ssl(ipc, buf, nbytes);
95                 return;
96         }
97 #endif
98 */
99         while (bytes_written < nbytes) {
100                 retval = write(ipc->sock, &buf[bytes_written],
101                                nbytes - bytes_written);
102                 if (retval < 1) {
103                         connection_died(ipc, 0);
104                         return;
105                 }
106                 bytes_written += retval;
107         }
108 }
109
110
111 void cleanup(int e)
112 {
113         static int nested = 0;
114
115         alarm(30);
116         signal(SIGALRM, nq_cleanup);
117         serv_write(ipc, "\n", 1);
118         if (nested++ < 1)
119                 CtdlIPCQuit(ipc);
120         nq_cleanup(e);
121 }
122
123 /*
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.
127  */
128 void logoff(int e)
129 {
130         cleanup(e);
131 }
132
133 /*
134  * Connect sendcommand to the Citadel server running on this computer.
135  */
136 void np_attach_to_server(char *host, char *port)
137 {
138         char buf[SIZ];
139         char hostbuf[256], portbuf[256];
140         char *args[] =
141         {"sendcommand", NULL};
142         int r;
143
144         fprintf(stderr, "Attaching to server...\n");
145         strcpy(hostbuf, host);
146         strcpy(portbuf, port);
147         ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
148         if (!ipc) {
149                 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
150                 exit(3);
151         }
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);
157         if (r / 100 != 2) {
158                 cleanup(2);
159         }
160 }
161
162
163 void sendcommand_die(void) {
164         exit(0);
165 }
166
167
168
169 /*
170  * main
171  */
172 int main(int argc, char **argv)
173 {
174         int a;
175         char cmd[SIZ];
176         char buf[SIZ];
177         int watchdog = 60;
178
179         int relh=0;
180         int home=0;
181         char relhome[PATH_MAX]="";
182         char ctdldir[PATH_MAX]=CTDLDIR;
183         fd_set read_fd;
184         struct timeval tv;
185         int ret, err;
186         int server_shutting_down = 0;
187         
188         strcpy(ctdl_home_directory, DEFAULT_PORT);
189
190         strcpy(cmd, "");
191         /*
192          * Change directories if specified
193          */
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);
199                         else
200                                 safestrncpy(relhome, &argv[a][2],
201                                                         sizeof relhome);
202                         home=1;
203                 } else if (!strncmp(argv[a], "-w", 2)) {
204                         watchdog = atoi(&argv[a][2]);
205                         if (watchdog<1)
206                                 watchdog=1;
207                 } else {
208                         if (!IsEmptyStr(cmd))
209                                 strcat(cmd, " ");
210                         strcat(cmd, argv[a]);
211                 }
212         }
213
214         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
215         get_config();
216
217         signal(SIGINT, cleanup);
218         signal(SIGQUIT, cleanup);
219         signal(SIGHUP, cleanup);
220         signal(SIGTERM, cleanup);
221
222         fprintf(stderr, "sendcommand: started (pid=%d) "
223                         "running in %s\n",
224                         (int) getpid(),
225                         ctdl_home_directory);
226         fflush(stderr);
227
228         alarm(watchdog);
229         signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
230         
231         np_attach_to_server(UDS, ctdl_home_directory);
232         fflush(stderr);
233         setIPCDeathHook(sendcommand_die);
234
235         fprintf(stderr, "%s\n", cmd);
236         CtdlIPC_chat_send(ipc, cmd);
237         CtdlIPC_chat_recv(ipc, buf);
238         fprintf(stderr, "%s\n", buf);
239
240         tv.tv_sec = 0;
241         tv.tv_usec = 1000;
242
243         if (!strncasecmp(&buf[1], "31", 2)) {
244                 server_shutting_down = 1;
245         }
246
247         if (buf[0] == '1') {
248                 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
249                         printf("%s\n", buf);
250                         alarm(watchdog); /* Kick the watchdog timer */
251                 }
252         } else if (buf[0] == '4') {
253                 do {
254                         if (fgets(buf, sizeof buf, stdin) == NULL)
255                                 strcpy(buf, "000");
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);
264                         
265                         FD_ZERO(&read_fd);
266                         FD_SET(ipc->sock, &read_fd);
267                         ret = select(ipc->sock+1, &read_fd, NULL, NULL,  &tv);
268                         err = errno;
269                         if (err!=0)
270                                 printf("select failed: %d", err);
271
272                         if (ret == -1) {
273                                 if (!(errno == EINTR || errno == EAGAIN))
274                                         printf("select failed: %d", err);
275                                 return 1;
276                         }
277
278                         if (ret != 0) {
279                                 size_t n;
280                                 char rbuf[SIZ];
281
282                                 rbuf[0] = '\0';
283                                 n = read(ipc->sock, rbuf, SIZ);
284                                 if (n>0) {
285                                         rbuf[n]='\0';
286                                         fprintf (stderr, rbuf);
287                                         fflush (stdout);
288                                 }
289                         }
290                         alarm(watchdog); /* Kick the watchdog timer */
291                 } while (strcmp(buf, "000"));
292                 CtdlIPC_chat_send(ipc, "\n");
293                 CtdlIPC_chat_send(ipc, "000");
294         }
295         alarm(0);       /* Shutdown the watchdog timer */
296         fprintf(stderr, "sendcommand: processing ended.\n");
297
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.
301          */
302         if (server_shutting_down) {
303                 nq_cleanup(0);
304         }
305         else {
306                 cleanup(0);
307         }
308         return 0;
309 }
310
311 /*
312  * Stub function
313  */
314 void stty_ctdl(int cmd) {
315 }
316
317