2 * Command-line utility to transmit a server command.
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #include <sys/types.h>
26 #include <sys/socket.h>
29 #include "include/citadel_dirs.h"
36 int uds_connectsock(char *sockpath)
39 struct sockaddr_un addr;
41 memset(&addr, 0, sizeof(addr));
42 addr.sun_family = AF_UNIX;
43 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
45 s = socket(AF_UNIX, SOCK_STREAM, 0);
47 fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
51 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
52 fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
62 * input binary data from socket
64 void serv_read(char *buf, int bytes)
70 rlen = read(serv_sock, &buf[len], bytes - len);
80 * send binary to server
82 void serv_write(char *buf, int nbytes)
84 int bytes_written = 0;
86 while (bytes_written < nbytes) {
87 retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
91 bytes_written = bytes_written + retval;
98 * input string from socket - implemented in terms of serv_read()
100 void serv_gets(char *buf)
104 /* Read one character at a time.
107 serv_read(&buf[i], 1);
108 if (buf[i] == '\n' || i == (SIZ-1))
112 /* If we got a long line, discard characters until the newline.
115 while (buf[i] != '\n') {
116 serv_read(&buf[i], 1);
120 /* Strip all trailing nonprintables (crlf)
127 * send line to server - implemented in terms of serv_write()
129 void serv_puts(char *buf)
131 serv_write(buf, strlen(buf));
139 * Main loop. Do things and have fun.
141 int main(int argc, char **argv)
149 char relhome[PATH_MAX]="";
150 char ctdldir[PATH_MAX]=CTDLDIR;
152 /* Parse command line */
153 while ((a = getopt(argc, argv, "h:w:")) != EOF) {
158 strncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
160 strncpy(relhome, optarg, sizeof relhome);
165 watchdog = atoi(optarg);
167 fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
172 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
174 fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server at %s\n",
176 file_citadel_admin_socket
181 serv_sock = uds_connectsock(file_citadel_admin_socket);
184 fprintf(stderr, "%s\n", buf);
187 for (a=optind; a<argc; ++a) {
191 strcat(buf, argv[a]);
194 fprintf(stderr, "%s\n", buf);
197 fprintf(stderr, "%s\n", buf);
201 if ((xfermode == '4') || (xfermode == '8')) { /* send text */
202 while (fgets(buf, sizeof buf, stdin)) {
203 buf[strlen(buf)-1] = 0;
205 alarm(watchdog); /* reset the watchdog timer */
210 if ((xfermode == '1') || (xfermode == '8')) { /* receive text */
211 while (serv_gets(buf), strcmp(buf, "000")) {
213 alarm(watchdog); /* reset the watchdog timer */
217 if (xfermode == '6') { /* receive binary */
218 size_t len = atoi(&buf[4]);
219 size_t bytes_remaining = len;
221 while (bytes_remaining > 0) {
222 size_t this_block = bytes_remaining;
223 if (this_block > SIZ) this_block = SIZ;
224 serv_read(buf, this_block);
225 fwrite(buf, this_block, 1, stdout);
226 bytes_remaining -= this_block;
231 alarm(0); /* cancel the watchdog timer */
232 fprintf(stderr, "sendcommand: processing ended.\n");
233 if (xfermode == '5') {