[BACK]Return to client.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rsync

Annotation of src/usr.bin/rsync/client.c, Revision 1.16

1.16    ! claudio     1: /*     $Id: client.c,v 1.15 2019/05/08 20:00:25 benno Exp $ */
1.1       benno       2: /*
                      3:  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <sys/stat.h>
                     18:
                     19: #include <assert.h>
                     20: #include <inttypes.h>
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23: #include <unistd.h>
1.11      deraadt    24: #include <err.h>
1.1       benno      25:
                     26: #include "extern.h"
                     27:
                     28: /*
                     29:  * The rsync client runs on the operator's local machine.
                     30:  * It can either be in sender or receiver mode.
                     31:  * In the former, it synchronises local files from a remote sink.
                     32:  * In the latter, the remote sink synchronses to the local files.
1.13      deraadt    33:  * Returns exit code 0 on success, 1 on failure, 2 on failure with
                     34:  * incompatible protocols.
1.1       benno      35:  */
                     36: int
                     37: rsync_client(const struct opts *opts, int fd, const struct fargs *f)
                     38: {
                     39:        struct sess      sess;
1.13      deraadt    40:        int              rc = 1;
1.1       benno      41:
                     42:        /* Standard rsync preamble, sender side. */
1.11      deraadt    43:
                     44:        if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil",
                     45:            NULL) == -1)
1.16    ! claudio    46:                err(ERR_IPC, "pledge");
1.1       benno      47:
                     48:        memset(&sess, 0, sizeof(struct sess));
                     49:        sess.opts = opts;
                     50:        sess.lver = RSYNC_PROTOCOL;
                     51:
1.3       deraadt    52:        if (!io_write_int(&sess, fd, sess.lver)) {
1.15      benno      53:                ERRX1("io_write_int");
1.1       benno      54:                goto out;
1.3       deraadt    55:        } else if (!io_read_int(&sess, fd, &sess.rver)) {
1.15      benno      56:                ERRX1("io_read_int");
1.1       benno      57:                goto out;
1.3       deraadt    58:        } else if (!io_read_int(&sess, fd, &sess.seed)) {
1.15      benno      59:                ERRX1("io_read_int");
1.1       benno      60:                goto out;
                     61:        }
                     62:
                     63:        if (sess.rver < sess.lver) {
1.15      benno      64:                ERRX("remote protocol %d is older than our own %d: unsupported",
1.13      deraadt    65:                    sess.rver, sess.lver);
                     66:                rc = 2;
1.1       benno      67:                goto out;
                     68:        }
                     69:
1.15      benno      70:        LOG2("client detected client version %d, server version %d, seed %d",
1.14      deraadt    71:            sess.lver, sess.rver, sess.seed);
1.1       benno      72:
                     73:        sess.mplex_reads = 1;
                     74:
                     75:        /*
                     76:         * Now we need to get our list of files.
                     77:         * Senders (and locals) send; receivers receive.
                     78:         */
                     79:
1.12      deraadt    80:        if (f->mode != FARGS_RECEIVER) {
1.15      benno      81:                LOG2("client starting sender: %s",
1.4       deraadt    82:                    f->host == NULL ? "(local)" : f->host);
1.5       deraadt    83:                if (!rsync_sender(&sess, fd, fd, f->sourcesz,
                     84:                    f->sources)) {
1.15      benno      85:                        ERRX1("rsync_sender");
1.1       benno      86:                        goto out;
                     87:                }
                     88:        } else {
1.15      benno      89:                LOG2("client starting receiver: %s",
1.4       deraadt    90:                    f->host == NULL ? "(local)" : f->host);
1.3       deraadt    91:                if (!rsync_receiver(&sess, fd, fd, f->sink)) {
1.15      benno      92:                        ERRX1("rsync_receiver");
1.1       benno      93:                        goto out;
                     94:                }
                     95:        }
                     96:
                     97: #if 0
                     98:        /* Probably the EOF. */
                     99:        if (io_read_check(&sess, fd))
1.15      benno     100:                WARNX("data remains in read pipe");
1.1       benno     101: #endif
                    102:
1.13      deraadt   103:        rc = 0;
1.1       benno     104: out:
                    105:        return rc;
                    106: }