A few more API clean ups. Mainly revolving around user_ops.c
[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
51         if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
52                 fscanf(lfp, "%d", &onppid);
53                 fclose(lfp);
54                 if (!kill(onppid, 0) || errno == EPERM)
55                         return 1;
56         }
57         lfp = fopen(LOCKFILE, "w");
58         fprintf(lfp, "%ld\n", (long) getpid());
59         fclose(lfp);
60         return (0);
61 }
62
63 void remove_lockfile(void)
64 {
65         unlink(LOCKFILE);
66 }
67
68 /*
69  * Why both cleanup() and nq_cleanup() ?  Notice the alarm() call in
70  * cleanup() .  If for some reason sendcommand hangs waiting for the server
71  * to clean up, the alarm clock goes off and the program exits anyway.
72  * The cleanup() routine makes a check to ensure it's not reentering, in
73  * case the ipc module looped it somehow.
74  */
75 void nq_cleanup(int e)
76 {
77         if (e == SIGALRM)
78                 fprintf(stderr, "\nWatch dog time out.\n");
79         remove_lockfile();
80         exit(e);
81 }
82
83 /*
84  * send binary to server
85  */
86 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
87 {
88         unsigned int bytes_written = 0;
89         int retval;
90 /*
91 #if defined(HAVE_OPENSSL)
92         if (ipc->ssl) {
93                 serv_write_ssl(ipc, buf, nbytes);
94                 return;
95         }
96 #endif
97 */
98         while (bytes_written < nbytes) {
99                 retval = write(ipc->sock, &buf[bytes_written],
100                                nbytes - bytes_written);
101                 if (retval < 1) {
102                         connection_died(ipc, 0);
103                         return;
104                 }
105                 bytes_written += retval;
106         }
107 }
108
109
110 void cleanup(int e)
111 {
112         static int nested = 0;
113
114         alarm(30);
115         signal(SIGALRM, nq_cleanup);
116         serv_write(ipc, "\n", 1);
117         if (nested++ < 1)
118                 CtdlIPCQuit(ipc);
119         nq_cleanup(e);
120 }
121
122 /*
123  * This is implemented as a function rather than as a macro because the
124  * client-side IPC modules expect logoff() to be defined.  They call logoff()
125  * when a problem connecting or staying connected to the server occurs.
126  */
127 void logoff(int e)
128 {
129         cleanup(e);
130 }
131
132 /*
133  * Connect sendcommand to the Citadel server running on this computer.
134  */
135 void np_attach_to_server(char *host, char *port)
136 {
137         char buf[SIZ];
138         char hostbuf[256], portbuf[256];
139         char *args[] =
140         {"sendcommand", NULL};
141         int r;
142
143         fprintf(stderr, "Attaching to server...\n");
144         strcpy(hostbuf, host);
145         strcpy(portbuf, port);
146         ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
147         if (!ipc) {
148                 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
149                 exit(3);
150         }
151         CtdlIPC_chat_recv(ipc, buf);
152         fprintf(stderr, "%s\n", &buf[4]);
153         snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
154         r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
155         fprintf(stderr, "%s\n", buf);
156         if (r / 100 != 2) {
157                 cleanup(2);
158         }
159 }
160
161
162 void sendcommand_die(void) {
163         exit(0);
164 }
165
166
167
168 /*
169  * main
170  */
171 int main(int argc, char **argv)
172 {
173         int a;
174         char cmd[SIZ];
175         char buf[SIZ];
176         int watchdog = 60;
177
178         int relh=0;
179         int home=0;
180         char relhome[PATH_MAX]="";
181         char ctdldir[PATH_MAX]=CTDLDIR;
182         fd_set read_fd;
183         struct timeval tv;
184         int ret, err;
185         int server_shutting_down = 0;
186         
187         strcpy(ctdl_home_directory, DEFAULT_PORT);
188
189         strcpy(cmd, "");
190         /*
191          * Change directories if specified
192          */
193         for (a = 1; a < argc; ++a) {
194                 if (!strncmp(argv[a], "-h", 2)) {
195                         relh=argv[a][2]!='/';
196                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
197                                                                    sizeof ctdl_home_directory);
198                         else
199                                 safestrncpy(relhome, &argv[a][2],
200                                                         sizeof relhome);
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 }
309
310 /*
311  * Stub function
312  */
313 void stty_ctdl(int cmd) {
314 }
315
316