fix all the <time.h> vs. <sys/time.h> issues, hopefully
[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 "ipc.h"
35 #include "config.h"
36
37 #define LOCKFILE "/tmp/LCK.sendcommand"
38
39 struct config config;
40 extern int home_specified;
41
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                 serv_puts("QUIT");
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(void)
106 {
107         char hostbuf[SIZ], portbuf[SIZ];
108         char buf[SIZ];
109         char *args[] =
110         {"sendcommand", NULL};
111
112         fprintf(stderr, "Attaching to server...\n");
113         attach_to_server(1, args, hostbuf, portbuf);
114         serv_gets(buf);
115         fprintf(stderr, "%s\n", &buf[4]);
116         sprintf(buf, "IPGM %d", config.c_ipgm_secret);
117         serv_puts(buf);
118         serv_gets(buf);
119         fprintf(stderr, "%s\n", &buf[4]);
120         if (buf[0] != '2') {
121                 cleanup(2);
122         }
123 }
124
125
126
127 /*
128  * main
129  */
130 int main(int argc, char **argv)
131 {
132         int a;
133         char cmd[SIZ];
134         char buf[SIZ];
135
136         strcpy(bbs_home_directory, BBSDIR);
137
138         strcpy(cmd, "");
139         /*
140          * Change directories if specified
141          */
142         for (a = 1; a < argc; ++a) {
143                 if (!strncmp(argv[a], "-h", 2)) {
144                         strcpy(bbs_home_directory, argv[a]);
145                         strcpy(bbs_home_directory, &bbs_home_directory[2]);
146                         home_specified = 1;
147                 } else {
148                         if (strlen(cmd) > 0)
149                                 strcat(cmd, " ");
150                         strcat(cmd, argv[a]);
151                 }
152         }
153
154         get_config();
155
156         signal(SIGINT, cleanup);
157         signal(SIGQUIT, cleanup);
158         signal(SIGHUP, cleanup);
159         signal(SIGTERM, cleanup);
160
161         fprintf(stderr, "sendcommand: started.  pid=%ld\n", (long) getpid());
162         fprintf(stderr, "Running from %s\n", bbs_home_directory);
163         fflush(stderr);
164         np_attach_to_server();
165         fflush(stderr);
166
167         fprintf(stderr, "%s\n", cmd);
168         serv_puts(cmd);
169         serv_gets(buf);
170         fprintf(stderr, "%s\n", buf);
171
172         if (buf[0] == '1') {
173                 while (serv_gets(buf), strcmp(buf, "000")) {
174                         printf("%s\n", buf);
175                 }
176         } else if (buf[0] == '4') {
177                 do {
178                         if (fgets(buf, sizeof buf, stdin) == NULL)
179                                 strcpy(buf, "000");
180                         if (strlen(buf) > 0)
181                                 if (buf[strlen(buf) - 1] == '\n')
182                                         buf[strlen(buf) - 1] = 0;
183                         if (strlen(buf) > 0)
184                                 if (buf[strlen(buf) - 1] == '\r')
185                                         buf[strlen(buf) - 1] = 0;
186                         if (strcmp(buf, "000"))
187                                 serv_puts(buf);
188                 } while (strcmp(buf, "000"));
189                 serv_puts("000");
190         }
191         fprintf(stderr, "sendcommand: processing ended.\n");
192         cleanup(0);
193         return 0;
194 }