* Bind unix socket prior to TCP socket for citadel protocol
[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 "/tmp/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 *args[] = { "sendcommand", NULL } ;
92
93         fprintf(stderr, "Attaching to server...\n");
94         attach_to_server(1, args, hostbuf, portbuf);
95         serv_gets(buf);
96         fprintf(stderr, "%s\n",&buf[4]);
97         sprintf(buf,"IPGM %d", config.c_ipgm_secret);
98         serv_puts(buf);
99         serv_gets(buf);
100         fprintf(stderr, "%s\n",&buf[4]);
101         if (buf[0]!='2') {
102                 cleanup(2);
103                 }
104         }
105
106
107
108 /*
109  * main
110  */
111 int main(int argc, char **argv)
112 {
113         int a;
114         char cmd[256];
115         char buf[256];
116
117         strcpy(bbs_home_directory, BBSDIR);
118
119         strcpy(cmd, "");
120         /*
121          * Change directories if specified
122          */
123         for (a=1; a<argc; ++a) {
124                 if (!strncmp(argv[a], "-h", 2)) {
125                         strcpy(bbs_home_directory, argv[a]);
126                         strcpy(bbs_home_directory, &bbs_home_directory[2]);
127                         home_specified = 1;
128                         }
129                 else {
130                         if (strlen(cmd)>0) strcat(cmd, " ");
131                         strcat(cmd, argv[a]);
132                         }
133                 }
134
135         get_config();
136
137         signal(SIGINT,cleanup);
138         signal(SIGQUIT,cleanup);
139         signal(SIGHUP,cleanup);
140         signal(SIGTERM,cleanup);
141
142         fprintf(stderr, "sendcommand: started.  pid=%ld\n",(long)getpid());
143         fflush(stderr);
144         np_attach_to_server();
145         fflush(stderr);
146
147         fprintf(stderr, "%s\n", cmd);
148         serv_puts(cmd);
149         serv_gets(buf);
150         fprintf(stderr, "%s\n", buf);
151
152         if (buf[0]=='1') {
153                 while (serv_gets(buf), strcmp(buf, "000")) {
154                         printf("%s\n", buf);
155                         }
156                 }
157         else if (buf[0]=='4') {
158                 do {
159                         if (fgets(buf, 255, stdin)==NULL) strcpy(buf, "000");
160                         if (strcmp(buf, "000")) serv_puts(buf);
161                         } while (strcmp(buf, "000"));
162                 }
163
164         fprintf(stderr, "sendcommand: processing ended.\n");
165         cleanup(0);
166         return 0;
167         }