811775535b0bc2433ff4ae567a19dad15586e076
[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 #include <time.h>
18 #include <signal.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include "citadel.h"
22 #include "tools.h"
23 #include "ipc.h"
24 #include "config.h"
25
26 #define LOCKFILE "/tmp/LCK.sendcommand"
27
28 struct config config;
29 extern int home_specified;
30
31
32 /*
33  * make sure only one copy of sendcommand runs at a time, using lock files
34  */
35 int set_lockfile(void)
36 {
37         FILE *lfp;
38         int onppid;
39
40         if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
41                 fscanf(lfp, "%d", &onppid);
42                 fclose(lfp);
43                 if (!kill(onppid, 0) || errno == EPERM)
44                         return 1;
45         }
46         lfp = fopen(LOCKFILE, "w");
47         fprintf(lfp, "%ld\n", (long) getpid());
48         fclose(lfp);
49         return (0);
50 }
51
52 void remove_lockfile(void)
53 {
54         unlink(LOCKFILE);
55 }
56
57 /*
58  * Why both cleanup() and nq_cleanup() ?  Notice the alarm() call in
59  * cleanup() .  If for some reason sendcommand hangs waiting for the server
60  * to clean up, the alarm clock goes off and the program exits anyway.
61  * The cleanup() routine makes a check to ensure it's not reentering, in
62  * case the ipc module looped it somehow.
63  */
64 void nq_cleanup(int e)
65 {
66         remove_lockfile();
67         exit(e);
68 }
69
70 void cleanup(int e)
71 {
72         static int nested = 0;
73
74         alarm(30);
75         signal(SIGALRM, nq_cleanup);
76         if (nested++ < 1)
77                 serv_puts("QUIT");
78         nq_cleanup(e);
79 }
80
81 /*
82  * This is implemented as a function rather than as a macro because the
83  * client-side IPC modules expect logoff() to be defined.  They call logoff()
84  * when a problem connecting or staying connected to the server occurs.
85  */
86 void logoff(int e)
87 {
88         cleanup(e);
89 }
90
91 /*
92  * Connect sendcommand to the Citadel server running on this computer.
93  */
94 void np_attach_to_server(void)
95 {
96         char hostbuf[256], portbuf[256];
97         char buf[256];
98         char *args[] =
99         {"sendcommand", NULL};
100
101         fprintf(stderr, "Attaching to server...\n");
102         attach_to_server(1, args, hostbuf, portbuf);
103         serv_gets(buf);
104         fprintf(stderr, "%s\n", &buf[4]);
105         sprintf(buf, "IPGM %d", config.c_ipgm_secret);
106         serv_puts(buf);
107         serv_gets(buf);
108         fprintf(stderr, "%s\n", &buf[4]);
109         if (buf[0] != '2') {
110                 cleanup(2);
111         }
112 }
113
114
115
116 /*
117  * main
118  */
119 int main(int argc, char **argv)
120 {
121         int a;
122         char cmd[256];
123         char buf[256];
124
125         strcpy(bbs_home_directory, BBSDIR);
126
127         strcpy(cmd, "");
128         /*
129          * Change directories if specified
130          */
131         for (a = 1; a < argc; ++a) {
132                 if (!strncmp(argv[a], "-h", 2)) {
133                         strcpy(bbs_home_directory, argv[a]);
134                         strcpy(bbs_home_directory, &bbs_home_directory[2]);
135                         home_specified = 1;
136                 } else {
137                         if (strlen(cmd) > 0)
138                                 strcat(cmd, " ");
139                         strcat(cmd, argv[a]);
140                 }
141         }
142
143         get_config();
144
145         signal(SIGINT, cleanup);
146         signal(SIGQUIT, cleanup);
147         signal(SIGHUP, cleanup);
148         signal(SIGTERM, cleanup);
149
150         fprintf(stderr, "sendcommand: started.  pid=%ld\n", (long) getpid());
151         fprintf(stderr, "Running from %s\n", bbs_home_directory);
152         fflush(stderr);
153         np_attach_to_server();
154         fflush(stderr);
155
156         fprintf(stderr, "%s\n", cmd);
157         serv_puts(cmd);
158         serv_gets(buf);
159         fprintf(stderr, "%s\n", buf);
160
161         if (buf[0] == '1') {
162                 while (serv_gets(buf), strcmp(buf, "000")) {
163                         printf("%s\n", buf);
164                 }
165         } else if (buf[0] == '4') {
166                 do {
167                         if (fgets(buf, 255, stdin) == NULL)
168                                 strcpy(buf, "000");
169                         if (strlen(buf) > 0)
170                                 if (buf[strlen(buf) - 1] == '\n')
171                                         buf[strlen(buf) - 1] = 0;
172                         if (strlen(buf) > 0)
173                                 if (buf[strlen(buf) - 1] == '\r')
174                                         buf[strlen(buf) - 1] = 0;
175                         if (strcmp(buf, "000"))
176                                 serv_puts(buf);
177                 } while (strcmp(buf, "000"));
178                 serv_puts("000");
179         }
180         fprintf(stderr, "sendcommand: processing ended.\n");
181         cleanup(0);
182         return 0;
183 }