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