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)
40 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
41 fscanf(lfp, "%d", &onppid);
43 if (!kill(onppid, 0) || errno == EPERM)
46 lfp = fopen(LOCKFILE, "w");
47 fprintf(lfp, "%ld\n", (long) getpid());
52 void remove_lockfile(void)
58 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
59 * cleanup() . If for some reason sendcommand hangs waiting for the server
60 * to clean up, the alarm clock goes off and the program exits anyway.
61 * The cleanup() routine makes a check to ensure it's not reentering, in
62 * case the ipc module looped it somehow.
64 void nq_cleanup(int e)
72 static int nested = 0;
75 signal(SIGALRM, nq_cleanup);
82 * This is implemented as a function rather than as a macro because the
83 * client-side IPC modules expect logoff() to be defined. They call logoff()
84 * when a problem connecting or staying connected to the server occurs.
92 * Connect sendcommand to the Citadel server running on this computer.
94 void np_attach_to_server(void)
96 char hostbuf[SIZ], portbuf[SIZ];
99 {"sendcommand", NULL};
101 fprintf(stderr, "Attaching to server...\n");
102 attach_to_server(1, args, hostbuf, portbuf);
104 fprintf(stderr, "%s\n", &buf[4]);
105 sprintf(buf, "IPGM %d", config.c_ipgm_secret);
108 fprintf(stderr, "%s\n", &buf[4]);
119 int main(int argc, char **argv)
125 strcpy(bbs_home_directory, BBSDIR);
129 * Change directories if specified
131 for (a = 1; a < argc; ++a) {
132 if (!strncmp(argv[a], "-h", 2)) {
133 strcpy(bbs_home_directory, argv[a]);
134 strcpy(bbs_home_directory, &bbs_home_directory[2]);
139 strcat(cmd, argv[a]);
145 signal(SIGINT, cleanup);
146 signal(SIGQUIT, cleanup);
147 signal(SIGHUP, cleanup);
148 signal(SIGTERM, cleanup);
150 fprintf(stderr, "sendcommand: started. pid=%ld\n", (long) getpid());
151 fprintf(stderr, "Running from %s\n", bbs_home_directory);
153 np_attach_to_server();
156 fprintf(stderr, "%s\n", cmd);
159 fprintf(stderr, "%s\n", buf);
162 while (serv_gets(buf), strcmp(buf, "000")) {
165 } else if (buf[0] == '4') {
167 if (fgets(buf, 255, stdin) == NULL)
170 if (buf[strlen(buf) - 1] == '\n')
171 buf[strlen(buf) - 1] = 0;
173 if (buf[strlen(buf) - 1] == '\r')
174 buf[strlen(buf) - 1] = 0;
175 if (strcmp(buf, "000"))
177 } while (strcmp(buf, "000"));
180 fprintf(stderr, "sendcommand: processing ended.\n");