4 * Command-line utility to transmit a server command.
11 #include <sys/types.h>
26 #define LOCKFILE "/tmp/LCK.sendcommand"
29 extern int home_specified;
33 * make sure only one copy of sendcommand runs at a time, using lock files
35 int set_lockfile(void) {
39 if ((lfp = fopen(LOCKFILE,"r")) != NULL) {
40 fscanf(lfp,"%d",&onppid);
42 if (!kill(onppid, 0) || errno == EPERM) return 1;
45 lfp=fopen(LOCKFILE,"w");
46 fprintf(lfp,"%ld\n",(long)getpid());
51 void remove_lockfile(void) {
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.
62 void nq_cleanup(int e)
70 static int nested = 0;
73 signal(SIGALRM,nq_cleanup);
74 if (nested++ < 1) serv_puts("QUIT");
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.
89 * Connect sendcommand to the Citadel server running on this computer.
91 void np_attach_to_server(void) {
92 char hostbuf[256], portbuf[256];
94 char *args[] = { "sendcommand", NULL } ;
96 fprintf(stderr, "Attaching to server...\n");
97 attach_to_server(1, args, hostbuf, portbuf);
99 fprintf(stderr, "%s\n",&buf[4]);
100 sprintf(buf,"IPGM %d", config.c_ipgm_secret);
103 fprintf(stderr, "%s\n",&buf[4]);
114 int main(int argc, char **argv)
120 strcpy(bbs_home_directory, BBSDIR);
124 * Change directories if specified
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]);
133 if (strlen(cmd)>0) strcat(cmd, " ");
134 strcat(cmd, argv[a]);
140 signal(SIGINT,cleanup);
141 signal(SIGQUIT,cleanup);
142 signal(SIGHUP,cleanup);
143 signal(SIGTERM,cleanup);
145 fprintf(stderr, "sendcommand: started. pid=%ld\n",(long)getpid());
147 np_attach_to_server();
150 fprintf(stderr, "%s\n", cmd);
153 fprintf(stderr, "%s\n", buf);
156 while (serv_gets(buf), strcmp(buf, "000")) {
160 else if (buf[0]=='4') {
162 if (fgets(buf, 255, stdin)==NULL) strcpy(buf, "000");
163 if (strcmp(buf, "000")) serv_puts(buf);
164 } while (strcmp(buf, "000"));
167 fprintf(stderr, "sendcommand: processing ended.\n");