* New parameter to CtdlSaveMsgPointersInRoom() to suppress the reference count adjust...
[citadel.git] / citadel / utils / sendcommand.c
1 /*
2  * $Id$
3  *
4  * Command-line utility to transmit a server command.
5  *
6  * Copyright (c) 1987-2010 by the citadel.org team
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "ctdl_module.h"
24
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <ctype.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <signal.h>
46 #include <errno.h>
47 #include <limits.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "citadel_ipc.h"
51 #include "server.h"
52 #include "config.h"
53
54 #define LOCKFILE "/tmp/LCK.sendcommand"
55
56 static CtdlIPC *ipc = NULL;
57
58 /*
59  * make sure only one copy of sendcommand runs at a time, using lock files
60  */
61 int set_lockfile(void)
62 {
63         FILE *lfp;
64         int onppid;
65         int rv;
66
67         if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
68                 rv = fscanf(lfp, "%d", &onppid);
69                 fclose(lfp);
70                 if (!kill(onppid, 0) || errno == EPERM)
71                         return 1;
72         }
73         lfp = fopen(LOCKFILE, "w");
74         fprintf(lfp, "%ld\n", (long) getpid());
75         fclose(lfp);
76         return (0);
77 }
78
79 void remove_lockfile(void)
80 {
81         unlink(LOCKFILE);
82 }
83
84 /*
85  * Why both cleanup() and nq_cleanup() ?  Notice the alarm() call in
86  * cleanup() .  If for some reason sendcommand hangs waiting for the server
87  * to clean up, the alarm clock goes off and the program exits anyway.
88  * The cleanup() routine makes a check to ensure it's not reentering, in
89  * case the ipc module looped it somehow.
90  */
91 void nq_cleanup(int e)
92 {
93         if (e == SIGALRM)
94                 fprintf(stderr, "\nWatch dog time out.\n");
95         remove_lockfile();
96         exit(e);
97 }
98
99 /*
100  * send binary to server
101  */
102 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
103 {
104         unsigned int bytes_written = 0;
105         int retval;
106 /*
107 #if defined(HAVE_OPENSSL)
108         if (ipc->ssl) {
109                 serv_write_ssl(ipc, buf, nbytes);
110                 return;
111         }
112 #endif
113 */
114         while (bytes_written < nbytes) {
115                 retval = write(ipc->sock, &buf[bytes_written],
116                                nbytes - bytes_written);
117                 if (retval < 1) {
118                         connection_died(ipc, 0);
119                         return;
120                 }
121                 bytes_written += retval;
122         }
123 }
124
125
126 void cleanup(int e)
127 {
128         static int nested = 0;
129
130         alarm(30);
131         signal(SIGALRM, nq_cleanup);
132         serv_write(ipc, "\n", 1);
133         if (nested++ < 1)
134                 CtdlIPCQuit(ipc);
135         nq_cleanup(e);
136 }
137
138 /*
139  * This is implemented as a function rather than as a macro because the
140  * client-side IPC modules expect logoff() to be defined.  They call logoff()
141  * when a problem connecting or staying connected to the server occurs.
142  */
143 void logoff(int e)
144 {
145         cleanup(e);
146 }
147
148 /*
149  * Connect sendcommand to the Citadel server running on this computer.
150  */
151 void np_attach_to_server(char *host, char *port)
152 {
153         char buf[SIZ];
154         char hostbuf[256], portbuf[256];
155         char *args[] =
156         {"sendcommand", NULL};
157         int r;
158
159         fprintf(stderr, "Attaching to server...\n");
160         strcpy(hostbuf, host);
161         strcpy(portbuf, port);
162         ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
163         if (!ipc) {
164                 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
165                 exit(3);
166         }
167         CtdlIPC_chat_recv(ipc, buf);
168         fprintf(stderr, "%s\n", &buf[4]);
169         snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
170         r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
171         fprintf(stderr, "%s\n", buf);
172         if (r / 100 != 2) {
173                 cleanup(2);
174         }
175 }
176
177
178 void sendcommand_die(void) {
179         exit(0);
180 }
181
182
183
184 int main(int argc, char **argv)
185 {
186         int a;
187         char cmd[SIZ];
188         char buf[SIZ];
189         int watchdog = 60;
190
191         int relh=0;
192         int home=0;
193         char relhome[PATH_MAX]="";
194         char ctdldir[PATH_MAX]=CTDLDIR;
195         fd_set read_fd;
196         struct timeval tv;
197         int ret;
198         int server_shutting_down = 0;
199         
200         strcpy(ctdl_home_directory, DEFAULT_PORT);
201
202         strcpy(cmd, "");
203         /*
204          * Change directories if specified
205          */
206         for (a = 1; a < argc; ++a) {
207                 if (!strncmp(argv[a], "-h", 2)) {
208                         relh=argv[a][2]!='/';
209                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
210                                                                    sizeof ctdl_home_directory);
211                         else
212                                 safestrncpy(relhome, &argv[a][2],
213                                                         sizeof relhome);
214                         home=1;
215                 } else if (!strncmp(argv[a], "-w", 2)) {
216                         watchdog = atoi(&argv[a][2]);
217                         if (watchdog<1)
218                                 watchdog=1;
219                 } else {
220                         if (!IsEmptyStr(cmd))
221                                 strcat(cmd, " ");
222                         strcat(cmd, argv[a]);
223                 }
224         }
225
226         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
227         get_config();
228
229         signal(SIGINT, cleanup);
230         signal(SIGQUIT, cleanup);
231         signal(SIGHUP, cleanup);
232         signal(SIGTERM, cleanup);
233
234         fprintf(stderr, "sendcommand: started (pid=%d) "
235                         "running in %s\n",
236                         (int) getpid(),
237                         ctdl_home_directory);
238         fflush(stderr);
239
240         alarm(watchdog);
241         signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
242         
243         np_attach_to_server(UDS, ctdl_home_directory);
244         fflush(stderr);
245         setIPCDeathHook(sendcommand_die);
246
247         fprintf(stderr, "%s\n", cmd);
248         CtdlIPC_chat_send(ipc, cmd);
249         CtdlIPC_chat_recv(ipc, buf);
250         fprintf(stderr, "%s\n", buf);
251
252         tv.tv_sec = 0;
253         tv.tv_usec = 1000;
254
255         if (!strncasecmp(&buf[1], "31", 2)) {
256                 server_shutting_down = 1;
257         }
258
259         if (buf[0] == '1') {
260                 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
261                         printf("%s\n", buf);
262                         alarm(watchdog); /* Kick the watchdog timer */
263                 }
264         } else if (buf[0] == '4') {
265                 do {
266                         if (fgets(buf, sizeof buf, stdin) == NULL)
267                                 strcpy(buf, "000");
268                         if (!IsEmptyStr(buf))
269                                 if (buf[strlen(buf) - 1] == '\n')
270                                         buf[strlen(buf) - 1] = 0;
271                         if (!IsEmptyStr(buf))
272                                 if (buf[strlen(buf) - 1] == '\r')
273                                         buf[strlen(buf) - 1] = 0;
274                         if (strcmp(buf, "000"))
275                                 CtdlIPC_chat_send(ipc, buf);
276                         
277                         FD_ZERO(&read_fd);
278                         FD_SET(ipc->sock, &read_fd);
279                         ret = select(ipc->sock+1, &read_fd, NULL, NULL,  &tv);
280                         if (ret == -1) {
281                                 if (!(errno == EINTR || errno == EAGAIN))
282                                         fprintf(stderr, "select() failed: %s", strerror(errno));
283                                 return(1);
284                         }
285
286                         if (ret != 0) {
287                                 size_t n;
288                                 char rbuf[SIZ];
289
290                                 rbuf[0] = '\0';
291                                 n = read(ipc->sock, rbuf, SIZ);
292                                 if (n>0) {
293                                         rbuf[n]='\0';
294                                         fprintf(stderr, rbuf);
295                                         fflush (stdout);
296                                 }
297                         }
298                         alarm(watchdog); /* Kick the watchdog timer */
299                 } while (strcmp(buf, "000"));
300                 CtdlIPC_chat_send(ipc, "\n");
301                 CtdlIPC_chat_send(ipc, "000");
302         }
303         alarm(0);       /* Shutdown the watchdog timer */
304         fprintf(stderr, "sendcommand: processing ended.\n");
305
306         /* Clean up and log off ... unless the server indicated that the command
307          * we sent is shutting it down, in which case we want to just cut the
308          * connection and exit.
309          */
310         if (server_shutting_down) {
311                 nq_cleanup(0);
312         }
313         else {
314                 cleanup(0);
315         }
316         return 0;
317 }
318
319 /*
320  * Stub function
321  */
322 void stty_ctdl(int cmd) {
323 }
324
325