cc47df4af80cff048d7fc0dfd761412858ee9898
[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         strcpy(ctdl_home_directory, DEFAULT_PORT);
154
155         strcpy(cmd, "");
156         /*
157          * Change directories if specified
158          */
159         for (a = 1; a < argc; ++a) {
160                 if (!strncmp(argv[a], "-h", 2)) {
161                         relh=argv[a][2]!='/';
162                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
163                                                                    sizeof ctdl_home_directory);
164                         else
165                                 safestrncpy(relhome, &argv[a][2],
166                                                         sizeof relhome);
167                         home_specified = 1;
168                         home=1;
169                 } else {
170                         if (strlen(cmd) > 0)
171                                 strcat(cmd, " ");
172                         strcat(cmd, argv[a]);
173                 }
174         }
175
176         calc_dirs_n_files(relh, home, relhome, ctdldir);
177         get_config();
178
179         signal(SIGINT, cleanup);
180         signal(SIGQUIT, cleanup);
181         signal(SIGHUP, cleanup);
182         signal(SIGTERM, cleanup);
183
184         fprintf(stderr, "sendcommand: started (pid=%d) "
185                         "running in %s\n",
186                         (int) getpid(),
187                         ctdl_home_directory);
188         fflush(stderr);
189         np_attach_to_server(UDS, ctdl_home_directory);
190         fflush(stderr);
191         setIPCDeathHook(sendcommand_die);
192
193         fprintf(stderr, "%s\n", cmd);
194         CtdlIPC_chat_send(ipc, cmd);
195         CtdlIPC_chat_recv(ipc, buf);
196         fprintf(stderr, "%s\n", buf);
197
198         tv.tv_sec = 0;
199         tv.tv_usec = 1000;
200
201         if (buf[0] == '1') {
202                 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
203                         printf("%s\n", buf);
204                 }
205         } else if (buf[0] == '4') {
206                 do {
207                         if (fgets(buf, sizeof buf, stdin) == NULL)
208                                 strcpy(buf, "000");
209                         if (strlen(buf) > 0)
210                                 if (buf[strlen(buf) - 1] == '\n')
211                                         buf[strlen(buf) - 1] = 0;
212                         if (strlen(buf) > 0)
213                                 if (buf[strlen(buf) - 1] == '\r')
214                                         buf[strlen(buf) - 1] = 0;
215                         if (strcmp(buf, "000"))
216                                 CtdlIPC_chat_send(ipc, buf);
217                         
218                         FD_ZERO(&read_fd);
219                         FD_SET(ipc->sock, &read_fd);
220                         ret = select(ipc->sock+1, &read_fd, NULL, NULL,  &tv);
221                         err = errno;
222                         if (err!=0)
223                                 printf("select failed: %d", err);
224
225                         if (ret == -1) {
226                                 if (!(errno == EINTR || errno == EAGAIN))
227                                         printf("select failed: %d", err);
228                                 return 1;
229                         }
230
231                         if (ret != 0) {
232                                 size_t n;
233                                 char rbuf[SIZ];
234
235                                 rbuf[0] = '\0';
236                                 n = read(ipc->sock, rbuf, SIZ);
237                                 if (n>0) {
238                                         rbuf[n]='\0';
239                                         printf (rbuf);
240                                 }
241                         }
242                 } while (strcmp(buf, "000"));
243                 CtdlIPC_chat_send(ipc, "\n");
244                 CtdlIPC_chat_send(ipc, "000");
245         }
246         fprintf(stderr, "sendcommand: processing ended.\n");
247         cleanup(0);
248         return 0;
249 }