#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include "extern.h"
static int
fcntl_nonblock(int fd)
{
int fl;
if ((fl = fcntl(fd, F_GETFL, 0)) == -1)
ERR("fcntl: F_GETFL");
else if (fcntl(fd, F_SETFL, fl|O_NONBLOCK) == -1)
ERR("fcntl: F_SETFL");
else
return 1;
return 0;
}
int
rsync_server(const struct opts *opts, size_t argc, char *argv[])
{
struct sess sess;
int fdin = STDIN_FILENO,
fdout = STDOUT_FILENO, rc = 1;
if (pledge("stdio unix rpath wpath cpath dpath fattr chown getpw unveil",
NULL) == -1)
err(ERR_IPC, "pledge");
memset(&sess, 0, sizeof(struct sess));
sess.opts = opts;
if (!fcntl_nonblock(fdin) ||
!fcntl_nonblock(fdout)) {
ERRX1("fcntl_nonblock");
goto out;
}
sess.lver = RSYNC_PROTOCOL;
sess.seed = arc4random();
if (!io_read_int(&sess, fdin, &sess.rver)) {
ERRX1("io_read_int");
goto out;
} else if (!io_write_int(&sess, fdout, sess.lver)) {
ERRX1("io_write_int");
goto out;
} else if (!io_write_int(&sess, fdout, sess.seed)) {
ERRX1("io_write_int");
goto out;
}
sess.mplex_writes = 1;
if (sess.rver < sess.lver) {
ERRX("remote protocol %d is older than our own %d: unsupported",
sess.rver, sess.lver);
rc = 2;
goto out;
}
LOG2("server detected client version %d, server version %d, seed %d",
sess.rver, sess.lver, sess.seed);
if (sess.opts->sender) {
LOG2("server starting sender");
if (strcmp(argv[0], ".")) {
ERRX("first argument must be a standalone period");
goto out;
}
argv++;
argc--;
if (argc == 0) {
ERRX("must have arguments");
goto out;
}
if (!rsync_sender(&sess, fdin, fdout, argc, argv)) {
ERRX1("rsync_sender");
goto out;
}
} else {
LOG2("server starting receiver");
if (argc != 2) {
ERRX("server receiver mode requires two argument");
goto out;
} else if (strcmp(argv[0], ".")) {
ERRX("first argument must be a standalone period");
goto out;
}
if (!rsync_receiver(&sess, fdin, fdout, argv[1])) {
ERRX1("rsync_receiver");
goto out;
}
}
#if 0
if (io_read_check(&sess, fdin))
WARNX("data remains in read pipe");
#endif
rc = 0;
out:
return rc;
}