* Removed references to strucmp() and struncmp(), replaced them with
[citadel.git] / citadel / sendcommand.c
1 /*
2  * $Id$
3  */
4
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <signal.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include "citadel.h"
19 #include "tools.h"
20 #include "ipc.h"
21 #include "config.h"
22
23 #define LOCKFILE "/var/lock/LCK.sendcommand"
24
25 struct config config;
26 extern int home_specified;
27
28
29 /*
30  * make sure only one copy of sendcommand runs at a time, using lock files
31  */
32 int set_lockfile(void) {
33         FILE *lfp;
34         int onppid;
35
36         if ((lfp = fopen(LOCKFILE,"r")) != NULL) {
37                 fscanf(lfp,"%d",&onppid);
38                 fclose(lfp);
39                 if (!kill(onppid, 0) || errno == EPERM) return 1;
40                 }
41
42         lfp=fopen(LOCKFILE,"w");
43         fprintf(lfp,"%ld\n",(long)getpid());
44         fclose(lfp);
45         return(0);
46         }
47
48 void remove_lockfile(void) {
49         unlink(LOCKFILE);
50         }
51
52 /*
53  * Why both cleanup() and nq_cleanup() ?  Notice the alarm() call in
54  * cleanup() .  If for some reason sendcommand hangs waiting for the server
55  * to clean up, the alarm clock goes off and the program exits anyway.
56  * The cleanup() routine makes a check to ensure it's not reentering, in
57  * case the ipc module looped it somehow.
58  */
59 void nq_cleanup(int e)
60 {
61         remove_lockfile();
62         exit(e);
63         }
64
65 void cleanup(int e)
66 {
67         static int nested = 0;
68
69         alarm(30);
70         signal(SIGALRM,nq_cleanup);
71         if (nested++ < 1) serv_puts("QUIT");
72         nq_cleanup(e);
73         }
74
75 /*
76  * This is implemented as a function rather than as a macro because the
77  * client-side IPC modules expect logoff() to be defined.  They call logoff()
78  * when a problem connecting or staying connected to the server occurs.
79  */
80 void logoff(int e)
81 {
82         cleanup(e);
83         }
84
85 /*
86  * Connect sendcommand to the Citadel server running on this computer.
87  */
88 void np_attach_to_server(void) {
89         char hostbuf[256], portbuf[256];
90         char buf[256];
91         char portname[8];
92         char *args[] = { "sendcommand", NULL, NULL, NULL } ;
93
94         fprintf(stderr, "Attaching to server...\n");
95         sprintf(portname, "%d", config.c_port_number);
96         args[2] = portname;
97         attach_to_server(3, 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         }