centralized filename calculation
[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 "serv_extensions.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         remove_lockfile();
78         exit(e);
79 }
80
81 void cleanup(int e)
82 {
83         static int nested = 0;
84
85         alarm(30);
86         signal(SIGALRM, nq_cleanup);
87         if (nested++ < 1)
88                 CtdlIPCQuit(ipc);
89         nq_cleanup(e);
90 }
91
92 /*
93  * This is implemented as a function rather than as a macro because the
94  * client-side IPC modules expect logoff() to be defined.  They call logoff()
95  * when a problem connecting or staying connected to the server occurs.
96  */
97 void logoff(int e)
98 {
99         cleanup(e);
100 }
101
102 /*
103  * Connect sendcommand to the Citadel server running on this computer.
104  */
105 void np_attach_to_server(char *host, char *port)
106 {
107         char buf[SIZ];
108         char hostbuf[256], portbuf[256];
109         char *args[] =
110         {"sendcommand", NULL};
111         int r;
112
113         fprintf(stderr, "Attaching to server...\n");
114         strcpy(hostbuf, host);
115         strcpy(portbuf, port);
116         ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
117         if (!ipc) {
118                 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
119                 exit(3);
120         }
121         CtdlIPC_chat_recv(ipc, buf);
122         fprintf(stderr, "%s\n", &buf[4]);
123         snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
124         r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
125         fprintf(stderr, "%s\n", buf);
126         if (r / 100 != 2) {
127                 cleanup(2);
128         }
129 }
130
131
132 void sendcommand_die(void) {
133         exit(0);
134 }
135
136
137 /*
138  * main
139  */
140 int main(int argc, char **argv)
141 {
142         int a;
143         char cmd[SIZ];
144         char buf[SIZ];
145
146         int relh=0;
147         int home=0;
148         char relhome[PATH_MAX]="";
149         char ctdldir[PATH_MAX]=CTDLDIR;
150
151         strcpy(ctdl_home_directory, DEFAULT_PORT);
152
153         strcpy(cmd, "");
154         /*
155          * Change directories if specified
156          */
157         for (a = 1; a < argc; ++a) {
158                 if (!strncmp(argv[a], "-h", 2)) {
159                         relh=argv[a][2]!='/';
160                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
161                                                                    sizeof ctdl_home_directory);
162                         else
163                                 safestrncpy(relhome, &argv[a][2],
164                                                         sizeof relhome);
165                         home_specified = 1;
166                         home=1;
167                 } else {
168                         if (strlen(cmd) > 0)
169                                 strcat(cmd, " ");
170                         strcat(cmd, argv[a]);
171                 }
172         }
173
174         calc_dirs_n_files(relh, home, relhome, ctdldir);
175         get_config();
176
177         signal(SIGINT, cleanup);
178         signal(SIGQUIT, cleanup);
179         signal(SIGHUP, cleanup);
180         signal(SIGTERM, cleanup);
181
182         fprintf(stderr, "sendcommand: started (pid=%d) "
183                         "running in %s\n",
184                         (int) getpid(),
185                         ctdl_home_directory);
186         fflush(stderr);
187         np_attach_to_server(UDS, ctdl_home_directory);
188         fflush(stderr);
189         setIPCDeathHook(sendcommand_die);
190
191         fprintf(stderr, "%s\n", cmd);
192         CtdlIPC_chat_send(ipc, cmd);
193         CtdlIPC_chat_recv(ipc, buf);
194         fprintf(stderr, "%s\n", buf);
195
196         if (buf[0] == '1') {
197                 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
198                         printf("%s\n", buf);
199                 }
200         } else if (buf[0] == '4') {
201                 do {
202                         if (fgets(buf, sizeof buf, stdin) == NULL)
203                                 strcpy(buf, "000");
204                         if (strlen(buf) > 0)
205                                 if (buf[strlen(buf) - 1] == '\n')
206                                         buf[strlen(buf) - 1] = 0;
207                         if (strlen(buf) > 0)
208                                 if (buf[strlen(buf) - 1] == '\r')
209                                         buf[strlen(buf) - 1] = 0;
210                         if (strcmp(buf, "000"))
211                                 CtdlIPC_chat_send(ipc, buf);
212                 } while (strcmp(buf, "000"));
213                 CtdlIPC_chat_send(ipc, "000");
214         }
215         fprintf(stderr, "sendcommand: processing ended.\n");
216         cleanup(0);
217         return 0;
218 }