5012877018945538de8426e941944eb58ce1d2b6
[citadel.git] / citadel / utils / sendcommand.c
1 /*
2  * $Id$
3  *
4  * Command-line utility to transmit a server command.
5  *
6  */
7
8
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>
17
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
28
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"
37
38 #define LOCKFILE "/tmp/LCK.sendcommand"
39
40 static CtdlIPC *ipc = NULL;
41
42 /*
43  * make sure only one copy of sendcommand runs at a time, using lock files
44  */
45 int set_lockfile(void)
46 {
47         FILE *lfp;
48         int onppid;
49
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;
55         }
56         lfp = fopen(LOCKFILE, "w");
57         fprintf(lfp, "%ld\n", (long) getpid());
58         fclose(lfp);
59         return (0);
60 }
61
62 void remove_lockfile(void)
63 {
64         unlink(LOCKFILE);
65 }
66
67 /*
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.
73  */
74 void nq_cleanup(int e)
75 {
76         if (e == SIGALRM)
77                 fprintf(stderr, "\nWatch dog time out.\n");
78         remove_lockfile();
79         exit(e);
80 }
81
82 /*
83  * send binary to server
84  */
85 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
86 {
87         unsigned int bytes_written = 0;
88         int retval;
89 /*
90 #if defined(HAVE_OPENSSL)
91         if (ipc->ssl) {
92                 serv_write_ssl(ipc, buf, nbytes);
93                 return;
94         }
95 #endif
96 */
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;
103                 }
104                 bytes_written += retval;
105         }
106 }
107
108
109 void cleanup(int e)
110 {
111         static int nested = 0;
112
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);
119 }
120
121 /*
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.
125  */
126 void logoff(int e)
127 {
128         cleanup(e);
129 }
130
131 /*
132  * Connect sendcommand to the Citadel server running on this computer.
133  */
134 void np_attach_to_server(char *host, char *port)
135 {
136         char buf[SIZ];
137         char hostbuf[256], portbuf[256];
138         char *args[] =
139         {"sendcommand", NULL};
140         int r;
141
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);
149         }
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);
157         }
158 }
159
160
161 void sendcommand_die(void) {
162         exit(0);
163 }
164
165
166
167 /*
168  * main
169  */
170 int main(int argc, char **argv)
171 {
172         int a;
173         char cmd[SIZ];
174         char buf[SIZ];
175         int watchdog = 60;
176
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;
185         
186         strcpy(ctdl_home_directory, DEFAULT_PORT);
187
188         strcpy(cmd, "");
189         /*
190          * Change directories if specified
191          */
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]);
209                 }
210         }
211
212         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
213         get_config();
214
215         signal(SIGINT, cleanup);
216         signal(SIGQUIT, cleanup);
217         signal(SIGHUP, cleanup);
218         signal(SIGTERM, cleanup);
219
220         fprintf(stderr, "sendcommand: started (pid=%d) "
221                         "running in %s\n",
222                         (int) getpid(),
223                         ctdl_home_directory);
224         fflush(stderr);
225
226         alarm(watchdog);
227         signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
228         
229         np_attach_to_server(UDS, ctdl_home_directory);
230         fflush(stderr);
231         setIPCDeathHook(sendcommand_die);
232
233         fprintf(stderr, "%s\n", cmd);
234         CtdlIPC_chat_send(ipc, cmd);
235         CtdlIPC_chat_recv(ipc, buf);
236         fprintf(stderr, "%s\n", buf);
237
238         tv.tv_sec = 0;
239         tv.tv_usec = 1000;
240
241         if (!strncasecmp(&buf[1], "31", 2)) {
242                 server_shutting_down = 1;
243         }
244
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 */
249                 }
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);
262                         
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);
269
270                         if (ret == -1) {
271                                 if (!(errno == EINTR || errno == EAGAIN))
272                                         printf("select failed: %d", err);
273                                 return 1;
274                         }
275
276                         if (ret != 0) {
277                                 size_t n;
278                                 char rbuf[SIZ];
279
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);
286                                 }
287                         }
288                         alarm(watchdog); /* Kick the watchdog timer */
289                 } while (strcmp(buf, "000"));
290                 CtdlIPC_chat_send(ipc, "\n");
291                 CtdlIPC_chat_send(ipc, "000");
292         }
293         alarm(0);       /* Shutdown the watchdog timer */
294         fprintf(stderr, "sendcommand: processing ended.\n");
295
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.
299          */
300         if (server_shutting_down) {
301                 nq_cleanup(0);
302         }
303         else {
304                 cleanup(0);
305         }
306         return 0;
307 }
308
309 /*
310  * Stub function
311  */
312 void stty_ctdl(int cmd) {
313 }
314
315