c7439dc0dbca681423020307810a64c8ff905f56
[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 "citadel.h"
33 #include "tools.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         remove_lockfile();
77         exit(e);
78 }
79
80 void cleanup(int e)
81 {
82         static int nested = 0;
83
84         alarm(30);
85         signal(SIGALRM, nq_cleanup);
86         if (nested++ < 1)
87                 CtdlIPCQuit(ipc);
88         nq_cleanup(e);
89 }
90
91 /*
92  * This is implemented as a function rather than as a macro because the
93  * client-side IPC modules expect logoff() to be defined.  They call logoff()
94  * when a problem connecting or staying connected to the server occurs.
95  */
96 void logoff(int e)
97 {
98         cleanup(e);
99 }
100
101 /*
102  * Connect sendcommand to the Citadel server running on this computer.
103  */
104 void np_attach_to_server(char *host, char *port)
105 {
106         char buf[SIZ];
107         char hostbuf[256], portbuf[256];
108         char *args[] =
109         {"sendcommand", NULL};
110         int r;
111
112         fprintf(stderr, "Attaching to server...\n");
113         strcpy(hostbuf, host);
114         strcpy(portbuf, port);
115         ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
116         if (!ipc) {
117                 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
118                 exit(3);
119         }
120         CtdlIPC_chat_recv(ipc, buf);
121         fprintf(stderr, "%s\n", &buf[4]);
122         snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
123         r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
124         fprintf(stderr, "%s\n", buf);
125         if (r / 100 != 2) {
126                 cleanup(2);
127         }
128 }
129
130
131 void sendcommand_die(void) {
132         exit(0);
133 }
134
135
136 /*
137  * main
138  */
139 int main(int argc, char **argv)
140 {
141         int a;
142         char cmd[SIZ];
143         char buf[SIZ];
144
145         int relh=0;
146         int home=0;
147         char relhome[PATH_MAX]="";
148         char ctdldir[PATH_MAX]=CTDLDIR;
149         fd_set read_fd;
150         struct timeval tv;
151         int ret, err;
152
153         CtdlInitBase64Table();
154
155         strcpy(ctdl_home_directory, DEFAULT_PORT);
156
157         strcpy(cmd, "");
158         /*
159          * Change directories if specified
160          */
161         for (a = 1; a < argc; ++a) {
162                 if (!strncmp(argv[a], "-h", 2)) {
163                         relh=argv[a][2]!='/';
164                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
165                                                                    sizeof ctdl_home_directory);
166                         else
167                                 safestrncpy(relhome, &argv[a][2],
168                                                         sizeof relhome);
169                         home_specified = 1;
170                         home=1;
171                 } else {
172                         if (!IsEmptyStr(cmd))
173                                 strcat(cmd, " ");
174                         strcat(cmd, argv[a]);
175                 }
176         }
177
178         calc_dirs_n_files(relh, home, relhome, ctdldir);
179         get_config();
180
181         signal(SIGINT, cleanup);
182         signal(SIGQUIT, cleanup);
183         signal(SIGHUP, cleanup);
184         signal(SIGTERM, cleanup);
185
186         fprintf(stderr, "sendcommand: started (pid=%d) "
187                         "running in %s\n",
188                         (int) getpid(),
189                         ctdl_home_directory);
190         fflush(stderr);
191
192         alarm(5);
193         signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
194         
195         np_attach_to_server(UDS, ctdl_home_directory);
196         fflush(stderr);
197         setIPCDeathHook(sendcommand_die);
198
199         fprintf(stderr, "%s\n", cmd);
200         CtdlIPC_chat_send(ipc, cmd);
201         CtdlIPC_chat_recv(ipc, buf);
202         fprintf(stderr, "%s\n", buf);
203
204         tv.tv_sec = 0;
205         tv.tv_usec = 1000;
206
207         if (buf[0] == '1') {
208                 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
209                         printf("%s\n", buf);
210                         alarm(5); /* Kick the watchdog timer */
211                 }
212         } else if (buf[0] == '4') {
213                 do {
214                         if (fgets(buf, sizeof buf, stdin) == NULL)
215                                 strcpy(buf, "000");
216                         if (!IsEmptyStr(buf))
217                                 if (buf[strlen(buf) - 1] == '\n')
218                                         buf[strlen(buf) - 1] = 0;
219                         if (!IsEmptyStr(buf))
220                                 if (buf[strlen(buf) - 1] == '\r')
221                                         buf[strlen(buf) - 1] = 0;
222                         if (strcmp(buf, "000"))
223                                 CtdlIPC_chat_send(ipc, buf);
224                         
225                         FD_ZERO(&read_fd);
226                         FD_SET(ipc->sock, &read_fd);
227                         ret = select(ipc->sock+1, &read_fd, NULL, NULL,  &tv);
228                         err = errno;
229                         if (err!=0)
230                                 printf("select failed: %d", err);
231
232                         if (ret == -1) {
233                                 if (!(errno == EINTR || errno == EAGAIN))
234                                         printf("select failed: %d", err);
235                                 return 1;
236                         }
237
238                         if (ret != 0) {
239                                 size_t n;
240                                 char rbuf[SIZ];
241
242                                 rbuf[0] = '\0';
243                                 n = read(ipc->sock, rbuf, SIZ);
244                                 if (n>0) {
245                                         rbuf[n]='\0';
246                                         fprintf (stderr, rbuf);
247                                         fflush (stdout);
248                                 }
249                         }
250                         alarm(5); /* Kick the watchdog timer */
251                 } while (strcmp(buf, "000"));
252                 CtdlIPC_chat_send(ipc, "\n");
253                 CtdlIPC_chat_send(ipc, "000");
254         }
255         alarm(0);       /* Shutdown the watchdog timer */
256         fprintf(stderr, "sendcommand: processing ended.\n");
257         if (strcasecmp(cmd, "DOWN"))
258                 cleanup(0);
259         else    /* If we downed the server we can't to do CtdlIPCQuit in cleanup()*/
260                 nq_cleanup(0);
261         return 0;
262 }