2 * Copyright (c) 2003-2006, Maxime Henrion <mux@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/select.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
72 static void killer_start(struct killer *, struct mux *);
73 static void *killer_run(void *);
74 static void killer_stop(struct killer *);
76 static int proto_waitconnect(int);
77 static int proto_greet(struct config *);
78 static int proto_negproto(struct config *);
79 static int proto_fileattr(struct config *);
80 static int proto_xchgcoll(struct config *);
81 static struct mux *proto_mux(struct config *);
83 static int proto_escape(struct stream *, const char *);
84 static void proto_unescape(char *);
87 proto_waitconnect(int s)
91 int error, rv, soerror;
97 rv = select(s + 1, &readfd, NULL, NULL, NULL);
98 } while (rv == -1 && errno == EINTR);
101 /* Check that the connection was really successful. */
102 len = sizeof(soerror);
103 error = getsockopt(s, SOL_SOCKET, SO_ERROR, &soerror, &len);
105 /* We have no choice but faking an error here. */
106 errno = ECONNREFUSED;
116 /* Connect to the CVSup server. */
118 proto_connect(struct config *config, int family, uint16_t port)
120 char addrbuf[NI_MAXHOST];
121 /* Enough to hold sizeof("cvsup") or any port number. */
123 struct addrinfo *res, *ai, hints;
128 snprintf(servname, sizeof(servname), "%d", port);
130 strncpy(servname, "cvsup", sizeof(servname) - 1);
131 servname[sizeof(servname) - 1] = '\0';
133 memset(&hints, 0, sizeof(hints));
134 hints.ai_family = family;
135 hints.ai_socktype = SOCK_STREAM;
136 error = getaddrinfo(config->host, servname, &hints, &res);
138 * Try with the hardcoded port number for OSes that don't
139 * have cvsup defined in the /etc/services file.
141 if (error == EAI_SERVICE) {
142 strncpy(servname, "5999", sizeof(servname) - 1);
143 servname[sizeof(servname) - 1] = '\0';
144 error = getaddrinfo(config->host, servname, &hints, &res);
147 lprintf(0, "Name lookup failure for \"%s\": %s\n", config->host,
148 gai_strerror(error));
149 return (STATUS_TRANSIENTFAILURE);
151 for (ai = res; ai != NULL; ai = ai->ai_next) {
152 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
155 if (config->laddr != NULL) {
157 (void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
159 error = bind(s, config->laddr,
163 error = connect(s, ai->ai_addr, ai->ai_addrlen);
164 if (error && errno == EINTR)
165 error = proto_waitconnect(s);
170 (void)getnameinfo(ai->ai_addr, ai->ai_addrlen, addrbuf,
171 sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
172 if (s == -1 || error) {
173 lprintf(0, "Cannot connect to %s: %s\n", addrbuf,
177 lprintf(1, "Connected to %s\n", addrbuf);
180 return (STATUS_SUCCESS);
183 return (STATUS_TRANSIENTFAILURE);
186 /* Greet the server. */
188 proto_greet(struct config *config)
190 char *line, *cmd, *msg, *swver;
194 line = stream_getln(s, NULL);
195 cmd = proto_get_ascii(&line);
198 if (strcmp(cmd, "OK") == 0) {
199 (void)proto_get_ascii(&line); /* major number */
200 (void)proto_get_ascii(&line); /* minor number */
201 swver = proto_get_ascii(&line);
202 } else if (strcmp(cmd, "!") == 0) {
203 msg = proto_get_rest(&line);
206 lprintf(-1, "Rejected by server: %s\n", msg);
207 return (STATUS_TRANSIENTFAILURE);
210 lprintf(2, "Server software version: %s\n",
211 swver != NULL ? swver : ".");
212 return (STATUS_SUCCESS);
214 lprintf(-1, "Invalid greeting from server\n");
215 return (STATUS_FAILURE);
218 /* Negotiate protocol version with the server. */
220 proto_negproto(struct config *config)
223 char *cmd, *line, *msg;
227 proto_printf(s, "PROTO %d %d %s\n", PROTO_MAJ, PROTO_MIN, PROTO_SWVER);
229 line = stream_getln(s, NULL);
230 cmd = proto_get_ascii(&line);
231 if (cmd == NULL || line == NULL)
233 if (strcmp(cmd, "!") == 0) {
234 msg = proto_get_rest(&line);
235 lprintf(-1, "Protocol negotiation failed: %s\n", msg);
237 } else if (strcmp(cmd, "PROTO") != 0)
239 error = proto_get_int(&line, &maj, 10);
241 error = proto_get_int(&line, &min, 10);
244 if (maj != PROTO_MAJ || min != PROTO_MIN) {
245 lprintf(-1, "Server protocol version %d.%d not supported "
246 "by client\n", maj, min);
247 return (STATUS_FAILURE);
249 return (STATUS_SUCCESS);
251 lprintf(-1, "Invalid PROTO command from server\n");
252 return (STATUS_FAILURE);
256 * File attribute support negotiation.
259 proto_fileattr(struct config *config)
261 fattr_support_t support;
264 int error, i, n, attr;
267 lprintf(2, "Negotiating file attribute support\n");
268 proto_printf(s, "ATTR %d\n", FT_NUMBER);
269 for (i = 0; i < FT_NUMBER; i++)
270 proto_printf(s, "%x\n", fattr_supported(i));
271 proto_printf(s, ".\n");
273 line = stream_getln(s, NULL);
276 cmd = proto_get_ascii(&line);
277 error = proto_get_int(&line, &n, 10);
278 if (error || line != NULL || strcmp(cmd, "ATTR") != 0 || n > FT_NUMBER)
280 for (i = 0; i < n; i++) {
281 line = stream_getln(s, NULL);
284 error = proto_get_int(&line, &attr, 16);
287 support[i] = fattr_supported(i) & attr;
289 for (i = n; i < FT_NUMBER; i++)
291 line = stream_getln(s, NULL);
292 if (line == NULL || strcmp(line, ".") != 0)
294 memcpy(config->fasupport, support, sizeof(config->fasupport));
295 return (STATUS_SUCCESS);
297 lprintf(-1, "Protocol error negotiating attribute support\n");
298 return (STATUS_FAILURE);
302 * Exchange collection information.
305 proto_xchgcoll(struct config *config)
309 struct globtree *diraccept, *dirrefuse;
310 struct globtree *fileaccept, *filerefuse;
311 char *line, *cmd, *collname, *pat;
312 char *msg, *release, *ident, *rcskey, *prefix;
314 int error, flags, options;
317 lprintf(2, "Exchanging collection information\n");
318 STAILQ_FOREACH(coll, &config->colls, co_next) {
319 if (coll->co_options & CO_SKIP)
321 proto_printf(s, "COLL %s %s %o %d\n", coll->co_name,
322 coll->co_release, coll->co_umask, coll->co_options);
323 for (i = 0; i < pattlist_size(coll->co_accepts); i++) {
324 proto_printf(s, "ACC %s\n",
325 pattlist_get(coll->co_accepts, i));
327 for (i = 0; i < pattlist_size(coll->co_refusals); i++) {
328 proto_printf(s, "REF %s\n",
329 pattlist_get(coll->co_refusals, i));
331 proto_printf(s, ".\n");
333 proto_printf(s, ".\n");
336 STAILQ_FOREACH(coll, &config->colls, co_next) {
337 if (coll->co_options & CO_SKIP)
339 coll->co_norsync = globtree_false();
340 line = stream_getln(s, NULL);
343 cmd = proto_get_ascii(&line);
344 collname = proto_get_ascii(&line);
345 release = proto_get_ascii(&line);
346 error = proto_get_int(&line, &options, 10);
347 if (error || line != NULL)
349 if (strcmp(cmd, "COLL") != 0 ||
350 strcmp(collname, coll->co_name) != 0 ||
351 strcmp(release, coll->co_release) != 0)
354 (coll->co_options | (options & CO_SERVMAYSET)) &
355 ~(~options & CO_SERVMAYCLEAR);
356 while ((line = stream_getln(s, NULL)) != NULL) {
357 if (strcmp(line, ".") == 0)
359 cmd = proto_get_ascii(&line);
362 if (strcmp(cmd, "!") == 0) {
363 msg = proto_get_rest(&line);
366 lprintf(-1, "Server message: %s\n", msg);
367 } else if (strcmp(cmd, "PRFX") == 0) {
368 prefix = proto_get_ascii(&line);
369 if (prefix == NULL || line != NULL)
371 coll->co_cvsroot = xstrdup(prefix);
372 } else if (strcmp(cmd, "KEYALIAS") == 0) {
373 ident = proto_get_ascii(&line);
374 rcskey = proto_get_ascii(&line);
375 if (rcskey == NULL || line != NULL)
377 error = keyword_alias(coll->co_keyword, ident,
381 } else if (strcmp(cmd, "KEYON") == 0) {
382 ident = proto_get_ascii(&line);
383 if (ident == NULL || line != NULL)
385 error = keyword_enable(coll->co_keyword, ident);
388 } else if (strcmp(cmd, "KEYOFF") == 0) {
389 ident = proto_get_ascii(&line);
390 if (ident == NULL || line != NULL)
392 error = keyword_disable(coll->co_keyword,
396 } else if (strcmp(cmd, "NORS") == 0) {
397 pat = proto_get_ascii(&line);
398 if (pat == NULL || line != NULL)
400 coll->co_norsync = globtree_or(coll->co_norsync,
401 globtree_match(pat, FNM_PATHNAME));
402 } else if (strcmp(cmd, "RNORS") == 0) {
403 pat = proto_get_ascii(&line);
404 if (pat == NULL || line != NULL)
406 coll->co_norsync = globtree_or(coll->co_norsync,
407 globtree_match(pat, FNM_PATHNAME |
414 keyword_prepare(coll->co_keyword);
416 diraccept = globtree_true();
417 fileaccept = globtree_true();
418 dirrefuse = globtree_false();
419 filerefuse = globtree_false();
421 if (pattlist_size(coll->co_accepts) > 0) {
422 globtree_free(diraccept);
423 globtree_free(fileaccept);
424 diraccept = globtree_false();
425 fileaccept = globtree_false();
426 flags = FNM_PATHNAME | FNM_LEADING_DIR |
428 for (i = 0; i < pattlist_size(coll->co_accepts); i++) {
429 pat = pattlist_get(coll->co_accepts, i);
430 diraccept = globtree_or(diraccept,
431 globtree_match(pat, flags));
434 if (coll->co_options & CO_CHECKOUTMODE &&
435 (len == 0 || pat[len - 1] != '*')) {
436 /* We must modify the pattern so that it
437 refers to the RCS file, rather than
438 the checked-out file. */
439 xasprintf(&pat, "%s,v", pat);
440 fileaccept = globtree_or(fileaccept,
441 globtree_match(pat, flags));
444 fileaccept = globtree_or(fileaccept,
445 globtree_match(pat, flags));
450 for (i = 0; i < pattlist_size(coll->co_refusals); i++) {
451 pat = pattlist_get(coll->co_refusals, i);
452 dirrefuse = globtree_or(dirrefuse,
453 globtree_match(pat, 0));
455 if (coll->co_options & CO_CHECKOUTMODE &&
456 (len == 0 || pat[len - 1] != '*')) {
457 /* We must modify the pattern so that it refers
458 to the RCS file, rather than the checked-out
460 xasprintf(&pat, "%s,v", pat);
461 filerefuse = globtree_or(filerefuse,
462 globtree_match(pat, 0));
465 filerefuse = globtree_or(filerefuse,
466 globtree_match(pat, 0));
470 coll->co_dirfilter = globtree_and(diraccept,
471 globtree_not(dirrefuse));
472 coll->co_filefilter = globtree_and(fileaccept,
473 globtree_not(filerefuse));
475 /* Set up a mask of file attributes that we don't want to sync
477 if (!(coll->co_options & CO_SETOWNER))
478 coll->co_attrignore |= FA_OWNER | FA_GROUP;
479 if (!(coll->co_options & CO_SETMODE))
480 coll->co_attrignore |= FA_MODE;
481 if (!(coll->co_options & CO_SETFLAGS))
482 coll->co_attrignore |= FA_FLAGS;
484 return (STATUS_SUCCESS);
486 lprintf(-1, "Protocol error during collection exchange\n");
487 return (STATUS_FAILURE);
491 proto_mux(struct config *config)
494 struct stream *s, *wr;
495 struct chan *chan0, *chan1;
499 lprintf(2, "Establishing multiplexed-mode data connection\n");
500 proto_printf(s, "MUX\n");
502 m = mux_open(config->socket, &chan0);
504 lprintf(-1, "Cannot open the multiplexer\n");
509 lprintf(-1, "ChannelMux.Listen failed: %s\n", strerror(errno));
513 wr = stream_open(chan0, NULL, (stream_writefn_t *)chan_write, NULL);
514 proto_printf(wr, "CHAN %d\n", id);
516 chan1 = chan_accept(m, id);
518 lprintf(-1, "ChannelMux.Accept failed: %s\n", strerror(errno));
522 config->chan0 = chan0;
523 config->chan1 = chan1;
528 * Initializes the connection to the CVSup server, that is handle
529 * the protocol negotiation, logging in, exchanging file attributes
530 * support and collections information, and finally run the update
534 proto_run(struct config *config)
536 struct thread_args lister_args;
537 struct thread_args detailer_args;
538 struct thread_args updater_args;
539 struct thread_args *args;
540 struct killer killer;
541 struct threads *workers;
546 * We pass NULL for the close() function because we'll reuse
547 * the socket after the stream is closed.
549 config->server = stream_open_fd(config->socket, stream_read_fd,
550 stream_write_fd, NULL);
551 status = proto_greet(config);
552 if (status == STATUS_SUCCESS)
553 status = proto_negproto(config);
554 if (status == STATUS_SUCCESS)
555 status = auth_login(config);
556 if (status == STATUS_SUCCESS)
557 status = proto_fileattr(config);
558 if (status == STATUS_SUCCESS)
559 status = proto_xchgcoll(config);
560 if (status != STATUS_SUCCESS)
563 /* Multi-threaded action starts here. */
564 m = proto_mux(config);
566 return (STATUS_FAILURE);
568 stream_close(config->server);
569 config->server = NULL;
570 config->fixups = fixups_new();
571 killer_start(&killer, m);
573 /* Start the worker threads. */
574 workers = threads_new();
576 args->config = config;
580 args->wr = stream_open(config->chan0,
581 NULL, (stream_writefn_t *)chan_write, NULL);
582 threads_create(workers, lister, args);
584 args = &detailer_args;
585 args->config = config;
588 args->rd = stream_open(config->chan0,
589 (stream_readfn_t *)chan_read, NULL, NULL);
590 args->wr = stream_open(config->chan1,
591 NULL, (stream_writefn_t *)chan_write, NULL);
592 threads_create(workers, detailer, args);
594 args = &updater_args;
595 args->config = config;
598 args->rd = stream_open(config->chan1,
599 (stream_readfn_t *)chan_read, NULL, NULL);
601 threads_create(workers, updater, args);
603 lprintf(2, "Running\n");
604 /* Wait for all the worker threads to finish. */
605 status = STATUS_SUCCESS;
606 for (i = 0; i < 3; i++) {
607 args = threads_wait(workers);
608 if (args->rd != NULL)
609 stream_close(args->rd);
610 if (args->wr != NULL)
611 stream_close(args->wr);
612 if (args->status != STATUS_SUCCESS) {
613 assert(args->errmsg != NULL);
614 if (status == STATUS_SUCCESS) {
615 status = args->status;
616 /* Shutdown the multiplexer to wake up all
617 the other threads. */
618 mux_shutdown(m, args->errmsg, status);
623 threads_free(workers);
624 if (status == STATUS_SUCCESS) {
625 lprintf(2, "Shutting down connection to server\n");
626 chan_close(config->chan0);
627 chan_close(config->chan1);
628 chan_wait(config->chan0);
629 chan_wait(config->chan1);
630 mux_shutdown(m, NULL, STATUS_SUCCESS);
632 killer_stop(&killer);
633 fixups_free(config->fixups);
634 status = mux_close(m);
635 if (status == STATUS_SUCCESS) {
636 lprintf(1, "Finished successfully\n");
637 } else if (status == STATUS_INTERRUPTED) {
638 lprintf(-1, "Interrupted\n");
639 if (killer.killedby != -1)
640 kill(getpid(), killer.killedby);
646 * Write a string into the stream, escaping characters as needed.
647 * Characters escaped:
656 proto_escape(struct stream *wr, const char *s)
662 /* Handle characters that need escaping. */
664 len = strcspn(s, " \t\r\n\\");
665 n = stream_write(wr, s, len);
671 n = stream_write(wr, "\\_", 2);
674 n = stream_write(wr, "\\t", 2);
677 n = stream_write(wr, "\\r", 2);
680 n = stream_write(wr, "\\n", 2);
683 n = stream_write(wr, "\\\\", 2);
694 * A simple printf() implementation specifically tailored for csup.
695 * List of the supported formats:
698 * %d or %i Print an int as decimal.
699 * %x Print an int as hexadecimal.
700 * %o Print an int as octal.
701 * %t Print a time_t as decimal.
702 * %s Print a char * escaping some characters as needed.
703 * %S Print a char * without escaping.
704 * %f Print an encoded struct fattr *.
705 * %F Print an encoded struct fattr *, specifying the supported
709 proto_printf(struct stream *wr, const char *format, ...)
711 fattr_support_t *support;
726 va_start(ap, format);
727 while ((cp = strchr(fmt, '%')) != NULL) {
729 n = stream_write(wr, fmt, cp - fmt);
740 rv = stream_printf(wr, "%c", c);
744 val = va_arg(ap, int);
745 rv = stream_printf(wr, "%d", val);
748 val = va_arg(ap, int);
749 rv = stream_printf(wr, "%x", val);
752 val = va_arg(ap, int);
753 rv = stream_printf(wr, "%o", val);
756 off = va_arg(ap, off_t);
757 rv = stream_printf(wr, "%" PRId64, off);
760 s = va_arg(ap, char *);
762 rv = stream_printf(wr, "%s", s);
765 s = va_arg(ap, char *);
767 rv = proto_escape(wr, s);
770 longval = (long long)va_arg(ap, time_t);
771 rv = stream_printf(wr, "%lld", longval);
774 fa = va_arg(ap, struct fattr *);
775 attr = fattr_encode(fa, NULL, 0);
776 rv = proto_escape(wr, attr);
780 fa = va_arg(ap, struct fattr *);
781 support = va_arg(ap, fattr_support_t *);
782 ignore = va_arg(ap, int);
783 attr = fattr_encode(fa, *support, ignore);
784 rv = proto_escape(wr, attr);
788 size = va_arg(ap, size_t);
789 rv = stream_printf(wr, "%zu", size);
793 n = stream_write(wr, "%", 1);
807 rv = stream_printf(wr, "%s", fmt);
819 * Unescape the string, see proto_escape().
822 proto_unescape(char *s)
827 while ((cp = strchr(cp, '\\')) != NULL) {
848 while (*cp2 != '\0') {
856 * Get an ascii token in the string.
859 proto_get_ascii(char **s)
863 ret = strsep(s, " ");
866 /* Make sure we disallow 0-length fields. */
876 * Get the rest of the string.
879 proto_get_rest(char **s)
895 proto_get_int(char **s, int *val, int base)
900 cp = proto_get_ascii(s);
903 error = asciitoint(cp, val, base);
908 * Get a size_t token.
911 proto_get_sizet(char **s, size_t *val, int base)
913 unsigned long long tmp;
916 cp = proto_get_ascii(s);
920 tmp = strtoll(cp, &end, base);
921 if (errno || *end != '\0')
928 * Get a time_t token.
930 * Ideally, we would use an intmax_t and strtoimax() here, but strtoll()
931 * is more portable and 64bits should be enough for a timestamp.
934 proto_get_time(char **s, time_t *val)
939 cp = proto_get_ascii(s);
943 tmp = strtoll(cp, &end, 10);
944 if (errno || *end != '\0')
950 /* Start the killer thread. It is used to protect against some signals
951 during the multi-threaded run so that we can gracefully fail. */
953 killer_start(struct killer *k, struct mux *m)
959 sigemptyset(&k->sigset);
960 sigaddset(&k->sigset, SIGINT);
961 sigaddset(&k->sigset, SIGHUP);
962 sigaddset(&k->sigset, SIGTERM);
963 sigaddset(&k->sigset, SIGPIPE);
964 pthread_sigmask(SIG_BLOCK, &k->sigset, NULL);
965 error = pthread_create(&k->thread, NULL, killer_run, k);
967 err(1, "pthread_create");
970 /* The main loop of the killer thread. */
972 killer_run(void *arg)
979 error = sigwait(&k->sigset, &sig);
981 if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) {
982 if (k->killedby == -1) {
984 /* Ensure we don't get canceled during the shutdown. */
985 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
986 mux_shutdown(k->mux, "Cleaning up ...",
988 pthread_setcancelstate(old, NULL);
994 /* Stop the killer thread. */
996 killer_stop(struct killer *k)
1001 error = pthread_cancel(k->thread);
1003 pthread_join(k->thread, &val);
1004 assert(val == PTHREAD_CANCELED);
1005 pthread_sigmask(SIG_UNBLOCK, &k->sigset, NULL);