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