diff options
author | Sebastian Benoit <benno@cvs.openbsd.org> | 2019-02-18 21:34:55 +0000 |
---|---|---|
committer | Sebastian Benoit <benno@cvs.openbsd.org> | 2019-02-18 21:34:55 +0000 |
commit | bde034a382776f3c396eef446206fe579292673e (patch) | |
tree | cc19160b5f11f9dcb25f390e1b1f89f36bb5b3f2 /usr.bin/rsync/main.c | |
parent | 574102a6ff9a74c28f2f7825a5f76cbd68781424 (diff) |
sync with kristaps up to Sun Feb 17 2019
339cf5998c0c022623cd68de50722b6c14543952 Push "error trail" further into code.
baf58ce5fe1bc6ce431b3b0ac8264b83ae8c7d02 Document all arguments. Add
common -av usage. Remove bits about not supporting anything but
files/dirs.
821a811a8c80e52fb56b241fc65a16cae1b4fb2c Disambiguate as prodded by deraadt@
6c4475b8f226e9031ec0ec1b3f14f7d347132c87 Add -h to usage string
4d344ae6156873b44c95de0c1ed629e637c2d7ab Clarify error message
language, use service name instead of port, specify that the socket is
SOCK_STREAM. From deraadt@. Tweaked for lowercase messages.
f3ec049e76257fc96bcdc872f1d3b967b98f3eb6 In consideration to benno@'s
comments, let the mktemp functions propogate an errno handled by the
caller. Also keep the original line lengths. While in mktemp.c, make
some defines into an enum.
e116c2bd00e634b56e4276120135915ceaa31cf2 Put the FSM of the sender
into its own function. Put dry_run ack and end of phase ack into the
send buffer too, further reducing the possibility of deadlock.
c7745aa4c7394ca89d841f8ee76782256d694340 Make the sender write loop be
fully non-blocking. This frees us of deadlocking the protocol because
the sender will always be able to pull down data.
93c7b4843e80aeac2ec6ae6ffc395df4deaf4a31 Remove "yoda" notation to be
more in tune with OpenBSD. Most found by deraadt@.
Diffstat (limited to 'usr.bin/rsync/main.c')
-rw-r--r-- | usr.bin/rsync/main.c | 100 |
1 files changed, 55 insertions, 45 deletions
diff --git a/usr.bin/rsync/main.c b/usr.bin/rsync/main.c index 358dbe2d9a8..f72bbcde936 100644 --- a/usr.bin/rsync/main.c +++ b/usr.bin/rsync/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.27 2019/02/17 20:11:42 deraadt Exp $ */ +/* $Id: main.c,v 1.28 2019/02/18 21:34:54 benno Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -98,18 +98,18 @@ fargs_parse(size_t argc, char *argv[], struct opts *opts) /* Allocations. */ if ((f = calloc(1, sizeof(struct fargs))) == NULL) - err(1, "calloc"); + err(EXIT_FAILURE, "calloc"); f->sourcesz = argc - 1; if ((f->sources = calloc(f->sourcesz, sizeof(char *))) == NULL) - err(1, "calloc"); + err(EXIT_FAILURE, "calloc"); for (i = 0; i < argc - 1; i++) if ((f->sources[i] = strdup(argv[i])) == NULL) - err(1, "strdup"); + err(EXIT_FAILURE, "strdup"); if ((f->sink = strdup(argv[i])) == NULL) - err(1, "strdup"); + err(EXIT_FAILURE, "strdup"); /* * Test files for its locality. @@ -124,32 +124,32 @@ fargs_parse(size_t argc, char *argv[], struct opts *opts) if (fargs_is_remote(f->sink)) { f->mode = FARGS_SENDER; if ((f->host = strdup(f->sink)) == NULL) - err(1, "strdup"); + err(EXIT_FAILURE, "strdup"); } if (fargs_is_remote(f->sources[0])) { if (f->host != NULL) - errx(1, "both source and " + errx(EXIT_FAILURE, "both source and " "destination cannot be remote files"); f->mode = FARGS_RECEIVER; if ((f->host = strdup(f->sources[0])) == NULL) - err(1, "strdup"); + err(EXIT_FAILURE, "strdup"); } if (f->host != NULL) { if (strncasecmp(f->host, "rsync://", 8) == 0) { - /* rsync://host/module[/path] */ + /* rsync://host[:port]/module[/path] */ f->remote = 1; len = strlen(f->host) - 8 + 1; memmove(f->host, f->host + 8, len); if ((cp = strchr(f->host, '/')) == NULL) - errx(1, "rsync protocol " + errx(EXIT_FAILURE, "rsync protocol " "requires a module name"); *cp++ = '\0'; f->module = cp; if ((cp = strchr(f->module, '/')) != NULL) *cp = '\0'; - if ((cp = strchr(f->host, ':'))) { + if ((cp = strchr(f->host, ':')) != NULL) { /* host:port --> extract port */ *cp++ = '\0'; opts->port = cp; @@ -169,9 +169,9 @@ fargs_parse(size_t argc, char *argv[], struct opts *opts) } } if ((len = strlen(f->host)) == 0) - errx(1, "empty remote host"); + errx(EXIT_FAILURE, "empty remote host"); if (f->remote && strlen(f->module) == 0) - errx(1, "empty remote module"); + errx(EXIT_FAILURE, "empty remote module"); } /* Make sure we have the same "hostspec" for all files. */ @@ -181,7 +181,7 @@ fargs_parse(size_t argc, char *argv[], struct opts *opts) for (i = 0; i < f->sourcesz; i++) { if (!fargs_is_remote(f->sources[i])) continue; - errx(1, "remote file in " + errx(EXIT_FAILURE, "remote file in " "list of local sources: %s", f->sources[i]); } @@ -191,22 +191,22 @@ fargs_parse(size_t argc, char *argv[], struct opts *opts) !fargs_is_daemon(f->sources[i])) continue; if (fargs_is_daemon(f->sources[i])) - errx(1, "remote " + errx(EXIT_FAILURE, "remote " "daemon in list of " "remote sources: %s", f->sources[i]); - errx(1, "local file in " + errx(EXIT_FAILURE, "local file in " "list of remote sources: %s", f->sources[i]); } } else { if (f->mode != FARGS_RECEIVER) - errx(1, "sender mode for remote " + errx(EXIT_FAILURE, "sender mode for remote " "daemon receivers not yet supported"); for (i = 0; i < f->sourcesz; i++) { if (fargs_is_daemon(f->sources[i])) continue; - errx(1, "non-remote daemon file " + errx(EXIT_FAILURE, "non-remote daemon file " "in list of remote daemon sources: " "%s", f->sources[i]); } @@ -248,11 +248,18 @@ fargs_parse(size_t argc, char *argv[], struct opts *opts) strncasecmp(cp, "rsync://", 8) == 0) { /* rsync://path */ cp += 8; - if ((ccp = strchr(cp, ':'))) /* skip :port */ + + /* + * FIXME: broken. + * URIs can allow colons too. + * Fix this after merge. + */ + + if ((ccp = strchr(cp, ':')) != NULL) /* skip :port */ *ccp = '\0'; if (strncmp(cp, f->host, len) || (cp[len] != '/' && cp[len] != '\0')) - errx(1, "different remote " + errx(EXIT_FAILURE, "different remote " "host: %s", f->sources[i]); memmove(f->sources[i], f->sources[i] + len + 8 + 1, @@ -265,7 +272,7 @@ fargs_parse(size_t argc, char *argv[], struct opts *opts) /* host::path */ if (strncmp(cp, f->host, len) || (cp[len] != ':' && cp[len] != '\0')) - errx(1, "different remote " + errx(EXIT_FAILURE, "different remote " "host: %s", f->sources[i]); memmove(f->sources[i], f->sources[i] + len + 2, j - len - 1); @@ -276,7 +283,7 @@ fargs_parse(size_t argc, char *argv[], struct opts *opts) /* host:path */ if (strncmp(cp, f->host, len) || (cp[len] != ':' && cp[len] != '\0')) - errx(1, "different remote " + errx(EXIT_FAILURE, "different remote " "host: %s", f->sources[i]); memmove(f->sources[i], f->sources[i] + len + 1, j - len); @@ -291,7 +298,7 @@ main(int argc, char *argv[]) { struct opts opts; pid_t child; - int fds[2], rc = 0, c, st; + int fds[2], c, st; struct fargs *fargs; struct option lopts[] = { { "port", required_argument, NULL, 3 }, @@ -329,7 +336,7 @@ main(int argc, char *argv[]) if (pledge("stdio unix rpath wpath cpath dpath inet fattr chown dns getpw proc exec unveil", NULL) == -1) - err(1, "pledge"); + err(EXIT_FAILURE, "pledge"); memset(&opts, 0, sizeof(struct opts)); @@ -351,6 +358,7 @@ main(int argc, char *argv[]) break; case 'e': opts.ssh_prog = optarg; + /* Ignore. */ break; case 'g': opts.preserve_gids = 1; @@ -404,7 +412,7 @@ main(int argc, char *argv[]) goto usage; if (opts.port == NULL) - opts.port = "rsync"; + opts.port = RSYNC_SERVICE; /* * This is what happens when we're started with the "hidden" @@ -414,8 +422,9 @@ main(int argc, char *argv[]) if (opts.server) { if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil", NULL) == -1) - err(1, "pledge"); - return rsync_server(&opts, (size_t)argc, argv); + err(EXIT_FAILURE, "pledge"); + c = rsync_server(&opts, (size_t)argc, argv); + return c ? EXIT_SUCCESS : EXIT_FAILURE; } /* @@ -441,39 +450,39 @@ main(int argc, char *argv[]) assert(fargs->mode == FARGS_RECEIVER); if (pledge("stdio unix rpath wpath cpath dpath inet fattr chown dns getpw unveil", NULL) == -1) - err(1, "pledge"); - rc = rsync_socket(&opts, fargs); + err(EXIT_FAILURE, "pledge"); + c = rsync_socket(&opts, fargs); fargs_free(fargs); - return rc; + return c ? EXIT_SUCCESS : EXIT_FAILURE; } /* Drop the dns/inet possibility. */ if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw proc exec unveil", NULL) == -1) - err(1, "pledge"); + err(EXIT_FAILURE, "pledge"); /* Create a bidirectional socket and start our child. */ if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, fds) == -1) - err(1, "socketpair"); + err(EXIT_FAILURE, "socketpair"); if ((child = fork()) == -1) { close(fds[0]); close(fds[1]); - err(1, "fork"); + err(EXIT_FAILURE, "fork"); } /* Drop the fork possibility. */ if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw exec unveil", NULL) == -1) - err(1, "pledge"); + err(EXIT_FAILURE, "pledge"); if (child == 0) { close(fds[0]); fds[0] = -1; if (pledge("stdio exec", NULL) == -1) - err(1, "pledge"); + err(EXIT_FAILURE, "pledge"); rsync_child(&opts, fds[1], fargs); /* NOTREACHED */ } @@ -481,8 +490,8 @@ main(int argc, char *argv[]) close(fds[1]); fds[1] = -1; if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil", NULL) == -1) - err(1, "pledge"); - rc = rsync_client(&opts, fds[0], fargs); + err(EXIT_FAILURE, "pledge"); + c = rsync_client(&opts, fds[0], fargs); fargs_free(fargs); /* @@ -491,22 +500,23 @@ main(int argc, char *argv[]) * So close the connection here so that they don't hang. */ - if (!rc) { + if (!c) { close(fds[0]); fds[0] = -1; } if (waitpid(child, &st, 0) == -1) - err(1, "waitpid"); - if (!(WIFEXITED(st) && WEXITSTATUS(st) == 0)) - rc = 0; + err(EXIT_FAILURE, "waitpid"); + if (!(WIFEXITED(st) && WEXITSTATUS(st) == EXIT_SUCCESS)) + c = 0; if (fds[0] != -1) close(fds[0]); - return rc; + return c ? EXIT_SUCCESS : EXIT_FAILURE; usage: - fprintf(stderr, "usage: %s [-Daglnoprtv] " - "[-e ssh-prog] [--delete] [--rsync-path=prog] src ... dst\n", + fprintf(stderr, "usage: %s [-Daghlnoprtv] " + "[-e ssh-prog] [--delete] " + "[--port=port] [--rsync-path=prog] src ... dst\n", getprogname()); - return 1; + return EXIT_FAILURE; } |