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>
36 #include "dynloader.h"
39 #define LOCKFILE "/tmp/LCK.sendcommand"
42 * make sure only one copy of sendcommand runs at a time, using lock files
44 int set_lockfile(void)
49 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
50 fscanf(lfp, "%d", &onppid);
52 if (!kill(onppid, 0) || errno == EPERM)
55 lfp = fopen(LOCKFILE, "w");
56 fprintf(lfp, "%ld\n", (long) getpid());
61 void remove_lockfile(void)
67 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
68 * cleanup() . If for some reason sendcommand hangs waiting for the server
69 * to clean up, the alarm clock goes off and the program exits anyway.
70 * The cleanup() routine makes a check to ensure it's not reentering, in
71 * case the ipc module looped it somehow.
73 void nq_cleanup(int e)
81 static int nested = 0;
84 signal(SIGALRM, nq_cleanup);
91 * This is implemented as a function rather than as a macro because the
92 * client-side IPC modules expect logoff() to be defined. They call logoff()
93 * when a problem connecting or staying connected to the server occurs.
101 * Connect sendcommand to the Citadel server running on this computer.
103 void np_attach_to_server(void)
105 char hostbuf[SIZ], portbuf[SIZ];
108 {"sendcommand", NULL};
110 fprintf(stderr, "Attaching to server...\n");
111 attach_to_server(1, args, hostbuf, portbuf);
113 fprintf(stderr, "%s\n", &buf[4]);
114 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
117 fprintf(stderr, "%s\n", &buf[4]);
128 int main(int argc, char **argv)
134 strcpy(bbs_home_directory, BBSDIR);
138 * Change directories if specified
140 for (a = 1; a < argc; ++a) {
141 if (!strncmp(argv[a], "-h", 2)) {
142 strcpy(bbs_home_directory, argv[a]);
143 strcpy(bbs_home_directory, &bbs_home_directory[2]);
148 strcat(cmd, argv[a]);
154 signal(SIGINT, cleanup);
155 signal(SIGQUIT, cleanup);
156 signal(SIGHUP, cleanup);
157 signal(SIGTERM, cleanup);
159 fprintf(stderr, "sendcommand: started (pid=%d) "
164 np_attach_to_server();
167 fprintf(stderr, "%s\n", cmd);
170 fprintf(stderr, "%s\n", buf);
173 while (serv_gets(buf), strcmp(buf, "000")) {
176 } else if (buf[0] == '4') {
178 if (fgets(buf, sizeof buf, stdin) == NULL)
181 if (buf[strlen(buf) - 1] == '\n')
182 buf[strlen(buf) - 1] = 0;
184 if (buf[strlen(buf) - 1] == '\r')
185 buf[strlen(buf) - 1] = 0;
186 if (strcmp(buf, "000"))
188 } while (strcmp(buf, "000"));
191 fprintf(stderr, "sendcommand: processing ended.\n");