ef76015de83a46e3339abf6af8fa6b332bd7ef74
[citadel.git] / citadel / 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 = 5;
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_specified = 1;
201                         home=1;
202                 } else if (!strncmp(argv[a], "-w", 2)) {
203                         watchdog = atoi(&argv[a][2]);
204                         if (watchdog<1)
205                                 watchdog=1;
206                 } else {
207                         if (!IsEmptyStr(cmd))
208                                 strcat(cmd, " ");
209                         strcat(cmd, argv[a]);
210                 }
211         }
212
213         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
214         get_config();
215
216         signal(SIGINT, cleanup);
217         signal(SIGQUIT, cleanup);
218         signal(SIGHUP, cleanup);
219         signal(SIGTERM, cleanup);
220
221         fprintf(stderr, "sendcommand: started (pid=%d) "
222                         "running in %s\n",
223                         (int) getpid(),
224                         ctdl_home_directory);
225         fflush(stderr);
226
227         alarm(watchdog);
228         signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
229         
230         np_attach_to_server(UDS, ctdl_home_directory);
231         fflush(stderr);
232         setIPCDeathHook(sendcommand_die);
233
234         fprintf(stderr, "%s\n", cmd);
235         CtdlIPC_chat_send(ipc, cmd);
236         CtdlIPC_chat_recv(ipc, buf);
237         fprintf(stderr, "%s\n", buf);
238
239         tv.tv_sec = 0;
240         tv.tv_usec = 1000;
241
242         if (!strncasecmp(&buf[1], "31", 2)) {
243                 server_shutting_down = 1;
244         }
245
246         if (buf[0] == '1') {
247                 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
248                         printf("%s\n", buf);
249                         alarm(watchdog); /* Kick the watchdog timer */
250                 }
251         } else if (buf[0] == '4') {
252                 do {
253                         if (fgets(buf, sizeof buf, stdin) == NULL)
254                                 strcpy(buf, "000");
255                         if (!IsEmptyStr(buf))
256                                 if (buf[strlen(buf) - 1] == '\n')
257                                         buf[strlen(buf) - 1] = 0;
258                         if (!IsEmptyStr(buf))
259                                 if (buf[strlen(buf) - 1] == '\r')
260                                         buf[strlen(buf) - 1] = 0;
261                         if (strcmp(buf, "000"))
262                                 CtdlIPC_chat_send(ipc, buf);
263                         
264                         FD_ZERO(&read_fd);
265                         FD_SET(ipc->sock, &read_fd);
266                         ret = select(ipc->sock+1, &read_fd, NULL, NULL,  &tv);
267                         err = errno;
268                         if (err!=0)
269                                 printf("select failed: %d", err);
270
271                         if (ret == -1) {
272                                 if (!(errno == EINTR || errno == EAGAIN))
273                                         printf("select failed: %d", err);
274                                 return 1;
275                         }
276
277                         if (ret != 0) {
278                                 size_t n;
279                                 char rbuf[SIZ];
280
281                                 rbuf[0] = '\0';
282                                 n = read(ipc->sock, rbuf, SIZ);
283                                 if (n>0) {
284                                         rbuf[n]='\0';
285                                         fprintf (stderr, rbuf);
286                                         fflush (stdout);
287                                 }
288                         }
289                         alarm(watchdog); /* Kick the watchdog timer */
290                 } while (strcmp(buf, "000"));
291                 CtdlIPC_chat_send(ipc, "\n");
292                 CtdlIPC_chat_send(ipc, "000");
293         }
294         alarm(0);       /* Shutdown the watchdog timer */
295         fprintf(stderr, "sendcommand: processing ended.\n");
296
297         /* Clean up and log off ... unless the server indicated that the command
298          * we sent is shutting it down, in which case we want to just cut the
299          * connection and exit.
300          */
301         if (server_shutting_down) {
302                 nq_cleanup(0);
303         }
304         else {
305                 cleanup(0);
306         }
307         return 0;
308 }