different exit codes for different conditions.
fd = open_socket_out(host, rsync_port);
if (fd == -1) {
- exit_cleanup(1);
+ exit_cleanup(RERR_SOCKETIO);
}
server_options(sargs,&sargc);
if (!ret) {
rprintf(FERROR,"Error parsing options (unsupported option?) - aborting\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
start_server(fd, fd, argc, argp);
extern int remote_version;
if (!lp_load(config_file, 0)) {
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
set_socket_options(fd,"SO_KEEPALIVE");
if (!lp_load(config_file, 1)) {
fprintf(stderr,"failed to load config file %s\n", config_file);
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
log_open();
if ((f = fopen(lp_pid_file(), "w")) == NULL) {
cleanup_set_pid(0);
fprintf(stderr,"failed to create pid file %s\n", pid_file);
- exit_cleanup(1);
+ exit_cleanup(RERR_FILEIO);
}
fprintf(f, "%d\n", pid);
fclose(f);
remote_version > MAX_PROTOCOL_VERSION) {
rprintf(FERROR,"protocol version mismatch - is your shell clean?\n");
rprintf(FERROR,"(see the rsync man page for an explanation)\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_PROTOCOL);
}
if (verbose > 2)
--- /dev/null
+/* error codes returned by rsync */
+
+#define RERR_SYNTAX 1 /* syntax or usage error */
+#define RERR_PROTOCOL 2 /* protocol incompatibility */
+#define RERR_FILESELECT 3 /* errors selecting input/output files, dirs */
+#define RERR_NOSUPPORT 4 /* requested action not supported */
+
+#define RERR_SOCKETIO 10 /* error in socket IO */
+#define RERR_FILEIO 11 /* error in file IO */
+#define RERR_STREAMIO 12 /* error in rsync protocol data stream */
+#define RERR_MESSAGEIO 13 /* errors with program diagnostics */
+#define RERR_IPC 14 /* error in IPC code */
+
+#define RERR_SIGNAL 20 /* status returned when sent SIGUSR1, SIGINT */
+#define RERR_WAITCHILD 21 /* some error returned by waitpid() */
+#define RERR_MALLOC 22 /* error allocating core memory buffers */
+
+#define RERR_TIMEOUT 30 /* timeout in data send/receive */
if (!f) {
if (fatal) {
rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILEIO);
}
return list;
}
if (exclude_list[i]->include) {
if (remote_version < 19) {
rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_NOSUPPORT);
}
write_int(f,l+2);
write_buf(f,"+ ",2);
}
if (munmap(map->map, map->p_len) != 0) {
rprintf(FERROR,"munmap failed : %s\n", strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_MALLOC);
}
/* align the mmap region on a nice boundary back a bit from
if (do_lseek(map->fd,offset,SEEK_SET) != offset) {
rprintf(FERROR,"lseek failed in map_ptr\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_FILEIO);
}
if ((nread=read(map->fd,map->p,len)) != len) {
if (pop_dir(olddir) != 0) {
rprintf(FERROR,"pop_dir %s : %s\n",
dir,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILESELECT);
}
}
}
if (last_io && io_timeout && (t-last_io) >= io_timeout) {
rprintf(FERROR,"io timeout after %d second - exiting\n",
(int)(t-last_io));
- exit_cleanup(1);
+ exit_cleanup(RERR_TIMEOUT);
}
}
if (eof_error) {
rprintf(FERROR,"unexpected EOF in read_timeout\n");
}
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
rprintf(FERROR,"read error: %s\n", strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
return ret;
if (tag != FERROR && tag != FINFO) {
rprintf(FERROR,"unexpected tag %d\n", tag);
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
if (remaining > sizeof(line)-1) {
rprintf(FERROR,"multiplexing overflow %d\n\n",
remaining);
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
read_loop(fd, line, remaining);
#ifdef NO_INT64
rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_UNSUPPORTED);
#else
if (remote_version >= 16) {
readfd(f,b,8);
if (ret <= 0) {
rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
blocked = 0;
len = vslprintf(buf, sizeof(buf)-1, format, ap);
va_end(ap);
- if (len < 0) exit_cleanup(1);
+ if (len < 0) exit_cleanup(RERR_STREAMIO);
write_sbuf(fd, buf);
}
io_flush();
if (read_buffer_len) {
fprintf(stderr,"ERROR: data in read buffer at mplx start\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
io_multiplexing_in = 1;
return;
}
*s = strdup(v);
- if (!*s) exit_cleanup(1);
+ if (!*s) exit_cleanup(RERR_MALLOC);
}
len = vslprintf(buf, sizeof(buf)-1, format, ap);
va_end(ap);
- if (len < 0) exit_cleanup(1);
+ if (len < 0) exit_cleanup(RERR_MESSAGEIO);
- if (len > sizeof(buf)-1) exit_cleanup(1);
+ if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
buf[len] = 0;
f = stdout;
}
- if (!f) exit_cleanup(1);
+ if (!f) exit_cleanup(RERR_MESSAGEIO);
- if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
+ if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
}
f = stdout;
}
- if (!f) exit_cleanup(1);
+ if (!f) exit_cleanup(RERR_MESSAGEIO);
fflush(f);
}
if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
p[0]);
- exit_cleanup(1);
+ exit_cleanup(RERR_MESSAGEIO);
}
if (l != 2) {
(double)stats.total_read,
(double)stats.total_size);
} else {
- rprintf(FLOG,"transfer interrupted\n");
+ rprintf(FLOG,"transfer interrupted (code %d)\n", code);
}
}
if (!push_dir(name, 0)) {
rprintf(FERROR,"push_dir %s : %s (1)\n",
name,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILESELECT);
}
return NULL;
}
if (flist->count > 1) {
rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_FILESELECT);
}
return name;
}
if (do_mkdir(name,0777 & ~orig_umask) != 0) {
rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILEIO);
} else {
if (verbose > 0)
rprintf(FINFO,"created directory %s\n",name);
if (!push_dir(name, 0)) {
rprintf(FERROR,"push_dir %s : %s (2)\n",
name,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILESELECT);
}
return NULL;
if (!relative_paths && !push_dir(dir, 0)) {
rprintf(FERROR,"push_dir %s: %s (3)\n",dir,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILESELECT);
}
argc--;
argv++;
if (pipe(recv_pipe) < 0) {
rprintf(FERROR,"pipe failed in do_recv\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_SOCKETIO);
}
io_flush();
if (!am_daemon && !push_dir(dir, 0)) {
rprintf(FERROR,"push_dir %s : %s (4)\n",
dir,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILESELECT);
}
}
flist = recv_file_list(f_in);
if (!flist || flist->count == 0) {
rprintf(FERROR,"server_recv: nothing to do\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_FILESELECT);
}
if (argc > 0) {
if (argc < 1) {
usage(FERROR);
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
am_sender = 0;
if (argc < 2) {
usage(FERROR);
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
if (local_server) {
if (!am_sender && argc > 1) {
usage(FERROR);
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
static RETSIGTYPE sigusr1_handler(int val) {
- exit_cleanup(1);
+ exit_cleanup(RERR_SIGNAL);
}
int main(int argc,char *argv[])
if (argc < 2) {
usage(FERROR);
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
/* we set a 0 umask so that correct file permissions can be
orig_umask = (int)umask(0);
if (!parse_arguments(argc, argv)) {
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
argc -= optind;
if (argc < 1) {
usage(FERROR);
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
if (dry_run)
#ifndef SUPPORT_LINKS
if (!am_server && preserve_links) {
rprintf(FERROR,"ERROR: symbolic links not supported\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_UNSUPPORTED);
}
#endif
case OPT_SENDER:
if (!am_server) {
usage(FERROR);
- exit_cleanup(1);
+ exit_cleanup(RERR_SYNTAX);
}
am_sender = 1;
break;
if (fd != -1 && write_file(fd,data,i) != i) {
rprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILEIO);
}
offset += i;
continue;
if (fd != -1 && write_file(fd,map,len) != len) {
rprintf(FERROR,"write failed on %s : %s\n",
fname,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILEIO);
}
offset += len;
}
if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
rprintf(FERROR,"write failed on %s : %s\n",
fname,strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_FILEIO);
}
sum_end(file_sum1);
if (i < 0 || i >= flist->count) {
rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
i, flist->count);
- exit_cleanup(1);
+ exit_cleanup(RERR_PROTOCOL);
}
file = flist->files[i];
void sig_int(void)
{
- exit_cleanup(1);
+ exit_cleanup(RERR_SIGNAL);
}
#define FINFO 2
#define FLOG 3
+#include "errcode.h"
+
#include "config.h"
#if HAVE_REMSH
if (i < 0 || i >= flist->count) {
rprintf(FERROR,"Invalid file index %d (count=%d)\n",
i, flist->count);
- exit_cleanup(1);
+ exit_cleanup(RERR_PROTOCOL);
}
file = flist->files[i];
/* open an incoming socket */
s = open_socket_in(SOCK_STREAM, port);
if (s == -1)
- exit_cleanup(1);
+ exit_cleanup(RERR_SOCKETIO);
/* ready to listen */
if (listen(s, 5) == -1) {
close(s);
- exit_cleanup(1);
+ exit_cleanup(RERR_SOCKETIO);
}
initialised = 1;
if (getpeername(fd, &sa, &length)) {
- exit_cleanup(1);
+ exit_cleanup(RERR_SOCKETIO);
}
strlcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf)-1);
strcpy(name_buf,def);
if (getpeername(fd, &sa, &length)) {
- exit_cleanup(1);
+ exit_cleanup(RERR_SOCKETIO);
}
/* Look up the remote host name. */
Z_DEFLATED, -15, 8,
Z_DEFAULT_STRATEGY) != Z_OK) {
rprintf(FERROR, "compression init failed\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
if ((obuf = malloc(MAX_DATA_COUNT+2)) == NULL)
out_of_memory("send_deflated_token");
r = deflate(&tx_strm, flush);
if (r != Z_OK) {
rprintf(FERROR, "deflate returned %d\n", r);
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
if (nb == 0 || tx_strm.avail_out == 0) {
n = MAX_DATA_COUNT - tx_strm.avail_out;
if (r != Z_OK || tx_strm.avail_in != 0) {
rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
r, tx_strm.avail_in);
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
}
}
rx_strm.zfree = NULL;
if (inflateInit2(&rx_strm, -15) != Z_OK) {
rprintf(FERROR, "inflate init failed\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
if ((cbuf = malloc(MAX_DATA_COUNT)) == NULL
|| (dbuf = malloc(CHUNK_SIZE)) == NULL)
if (r != Z_OK && r != Z_BUF_ERROR) {
rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
r, n);
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
if (n != 0 && r != Z_BUF_ERROR) {
/* have to return some more data and
*/
if (!inflateSyncPoint(&rx_strm)) {
rprintf(FERROR, "decompressor lost sync!\n");
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
rx_strm.avail_in = 4;
rx_strm.next_in = (Bytef *)cbuf;
n = CHUNK_SIZE - rx_strm.avail_out;
if (r != Z_OK) {
rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
if (rx_strm.avail_in == 0)
recv_state = r_inflated;
r = inflate(&rx_strm, Z_SYNC_FLUSH);
if (r != Z_OK) {
rprintf(FERROR, "inflate (token) returned %d\n", r);
- exit_cleanup(1);
+ exit_cleanup(RERR_STREAMIO);
}
} while (len || rx_strm.avail_out == 0);
}
if (pipe(to_child_pipe) < 0 ||
pipe(from_child_pipe) < 0) {
rprintf(FERROR,"pipe: %s\n",strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_IPC);
}
pid = do_fork();
if (pid < 0) {
rprintf(FERROR,"fork: %s\n",strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_IPC);
}
if (pid == 0)
close(from_child_pipe[0]) < 0 ||
dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_IPC);
}
if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
execvp(command[0], command);
rprintf(FERROR,"Failed to exec %s : %s\n",
command[0],strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_IPC);
}
if (close(from_child_pipe[1]) < 0 ||
close(to_child_pipe[0]) < 0) {
rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_IPC);
}
*f_in = from_child_pipe[0];
if (pipe(to_child_pipe) < 0 ||
pipe(from_child_pipe) < 0) {
rprintf(FERROR,"pipe: %s\n",strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_IPC);
}
pid = do_fork();
if (pid < 0) {
rprintf(FERROR,"fork: %s\n",strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_IPC);
}
if (pid == 0) {
close(from_child_pipe[0]) < 0 ||
dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_IPC);
}
if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
if (close(from_child_pipe[1]) < 0 ||
close(to_child_pipe[0]) < 0) {
rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
- exit_cleanup(1);
+ exit_cleanup(RERR_IPC);
}
*f_in = from_child_pipe[0];
void out_of_memory(char *str)
{
rprintf(FERROR,"ERROR: out of memory in %s\n",str);
- exit_cleanup(1);
+ exit_cleanup(RERR_MALLOC);
}
void overflow(char *str)
{
rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
- exit_cleanup(1);
+ exit_cleanup(RERR_MALLOC);
}