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(char *host, char *port)
108 char hostbuf[256], portbuf[256];
110 {"sendcommand", NULL};
113 fprintf(stderr, "Attaching to server...\n");
114 strcpy(hostbuf, host);
115 strcpy(portbuf, port);
116 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
118 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
121 CtdlIPC_chat_recv(ipc, buf);
122 fprintf(stderr, "%s\n", &buf[4]);
123 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
124 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
125 fprintf(stderr, "%s\n", buf);
132 void sendcommand_die(void) {
140 int main(int argc, char **argv)
148 char relhome[PATH_MAX]="";
149 char ctdldir[PATH_MAX]=CTDLDIR;
154 strcpy(ctdl_home_directory, DEFAULT_PORT);
158 * Change directories if specified
160 for (a = 1; a < argc; ++a) {
161 if (!strncmp(argv[a], "-h", 2)) {
162 relh=argv[a][2]!='/';
163 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
164 sizeof ctdl_home_directory);
166 safestrncpy(relhome, &argv[a][2],
173 strcat(cmd, argv[a]);
177 calc_dirs_n_files(relh, home, relhome, ctdldir);
180 signal(SIGINT, cleanup);
181 signal(SIGQUIT, cleanup);
182 signal(SIGHUP, cleanup);
183 signal(SIGTERM, cleanup);
185 fprintf(stderr, "sendcommand: started (pid=%d) "
188 ctdl_home_directory);
190 np_attach_to_server(UDS, ctdl_home_directory);
192 setIPCDeathHook(sendcommand_die);
194 fprintf(stderr, "%s\n", cmd);
195 CtdlIPC_chat_send(ipc, cmd);
196 CtdlIPC_chat_recv(ipc, buf);
197 fprintf(stderr, "%s\n", buf);
203 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
206 } else if (buf[0] == '4') {
208 if (fgets(buf, sizeof buf, stdin) == NULL)
211 if (buf[strlen(buf) - 1] == '\n')
212 buf[strlen(buf) - 1] = 0;
214 if (buf[strlen(buf) - 1] == '\r')
215 buf[strlen(buf) - 1] = 0;
216 if (strcmp(buf, "000"))
217 CtdlIPC_chat_send(ipc, buf);
220 FD_SET(ipc->sock, &read_fd);
221 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
224 printf("select failed: %d", err);
227 if (!(errno == EINTR || errno == EAGAIN))
228 printf("select failed: %d", err);
237 n = read(ipc->sock, rbuf, SIZ);
243 } while (strcmp(buf, "000"));
244 CtdlIPC_chat_send(ipc, "\n");
245 CtdlIPC_chat_send(ipc, "000");
247 fprintf(stderr, "sendcommand: processing ended.\n");