4 * Command-line utility to transmit a server command.
11 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
34 #include "citadel_ipc.h"
36 #include "serv_extensions.h"
39 #define LOCKFILE "/tmp/LCK.sendcommand"
41 static CtdlIPC *ipc = NULL;
44 * make sure only one copy of sendcommand runs at a time, using lock files
46 int set_lockfile(void)
51 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
52 fscanf(lfp, "%d", &onppid);
54 if (!kill(onppid, 0) || errno == EPERM)
57 lfp = fopen(LOCKFILE, "w");
58 fprintf(lfp, "%ld\n", (long) getpid());
63 void remove_lockfile(void)
69 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
70 * cleanup() . If for some reason sendcommand hangs waiting for the server
71 * to clean up, the alarm clock goes off and the program exits anyway.
72 * The cleanup() routine makes a check to ensure it's not reentering, in
73 * case the ipc module looped it somehow.
75 void nq_cleanup(int e)
83 static int nested = 0;
86 signal(SIGALRM, nq_cleanup);
93 * This is implemented as a function rather than as a macro because the
94 * client-side IPC modules expect logoff() to be defined. They call logoff()
95 * when a problem connecting or staying connected to the server occurs.
103 * Connect sendcommand to the Citadel server running on this computer.
105 void np_attach_to_server(void)
107 char hostbuf[SIZ], portbuf[SIZ];
110 {"sendcommand", NULL};
113 strcpy(hostbuf, UDS); /* Only run on a unix domain socket */
114 strcpy(portbuf, "."); /* IPGM will refuse to run on the network */
115 fprintf(stderr, "Attaching to server...\n");
116 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
117 CtdlIPC_chat_recv(ipc, buf);
118 fprintf(stderr, "%s\n", &buf[4]);
119 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
120 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
121 fprintf(stderr, "%s\n", buf);
132 int main(int argc, char **argv)
138 strcpy(bbs_home_directory, BBSDIR);
142 * Change directories if specified
144 for (a = 1; a < argc; ++a) {
145 if (!strncmp(argv[a], "-h", 2)) {
146 strcpy(bbs_home_directory, argv[a]);
147 strcpy(bbs_home_directory, &bbs_home_directory[2]);
152 strcat(cmd, argv[a]);
158 signal(SIGINT, cleanup);
159 signal(SIGQUIT, cleanup);
160 signal(SIGHUP, cleanup);
161 signal(SIGTERM, cleanup);
163 fprintf(stderr, "sendcommand: started (pid=%d) "
168 np_attach_to_server();
171 fprintf(stderr, "%s\n", cmd);
172 CtdlIPC_chat_send(ipc, cmd);
173 CtdlIPC_chat_recv(ipc, buf);
174 fprintf(stderr, "%s\n", buf);
177 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
180 } else if (buf[0] == '4') {
182 if (fgets(buf, sizeof buf, stdin) == NULL)
185 if (buf[strlen(buf) - 1] == '\n')
186 buf[strlen(buf) - 1] = 0;
188 if (buf[strlen(buf) - 1] == '\r')
189 buf[strlen(buf) - 1] = 0;
190 if (strcmp(buf, "000"))
191 CtdlIPC_chat_send(ipc, buf);
192 } while (strcmp(buf, "000"));
193 CtdlIPC_chat_send(ipc, "000");
195 fprintf(stderr, "sendcommand: processing ended.\n");