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>
71 static void killer_start(struct killer *, struct mux *);
72 static void *killer_run(void *);
73 static void killer_stop(struct killer *);
75 static int proto_waitconnect(int);
76 static int proto_greet(struct config *);
77 static int proto_negproto(struct config *);
78 static int proto_fileattr(struct config *);
79 static int proto_xchgcoll(struct config *);
80 static struct mux *proto_mux(struct config *);
82 static int proto_escape(struct stream *, const char *);
83 static void proto_unescape(char *);
86 proto_waitconnect(int s)
90 int error, rv, soerror;
96 rv = select(s + 1, &readfd, NULL, NULL, NULL);
97 } while (rv == -1 && errno == EINTR);
100 /* Check that the connection was really successful. */
101 len = sizeof(soerror);
102 error = getsockopt(s, SOL_SOCKET, SO_ERROR, &soerror, &len);
104 /* We have no choice but faking an error here. */
105 errno = ECONNREFUSED;
115 /* Connect to the CVSup server. */
117 proto_connect(struct config *config, int family, uint16_t port)
119 char addrbuf[NI_MAXHOST];
120 /* Enough to hold sizeof("cvsup") or any port number. */
122 struct addrinfo *res, *ai, hints;
127 snprintf(servname, sizeof(servname), "%d", port);
129 strncpy(servname, "cvsup", sizeof(servname) - 1);
130 servname[sizeof(servname) - 1] = '\0';
132 memset(&hints, 0, sizeof(hints));
133 hints.ai_family = family;
134 hints.ai_socktype = SOCK_STREAM;
135 error = getaddrinfo(config->host, servname, &hints, &res);
137 * Try with the hardcoded port number for OSes that don't
138 * have cvsup defined in the /etc/services file.
140 if (error == EAI_SERVICE) {
141 strncpy(servname, "5999", sizeof(servname) - 1);
142 servname[sizeof(servname) - 1] = '\0';
143 error = getaddrinfo(config->host, servname, &hints, &res);
146 lprintf(0, "Name lookup failure for \"%s\": %s\n", config->host,
147 gai_strerror(error));
148 return (STATUS_TRANSIENTFAILURE);
150 for (ai = res; ai != NULL; ai = ai->ai_next) {
151 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
154 if (config->laddr != NULL) {
156 (void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
158 error = bind(s, config->laddr,
162 error = connect(s, ai->ai_addr, ai->ai_addrlen);
163 if (error && errno == EINTR)
164 error = proto_waitconnect(s);
169 (void)getnameinfo(ai->ai_addr, ai->ai_addrlen, addrbuf,
170 sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
171 if (s == -1 || error) {
172 lprintf(0, "Cannot connect to %s: %s\n", addrbuf,
176 lprintf(1, "Connected to %s\n", addrbuf);
179 return (STATUS_SUCCESS);
182 return (STATUS_TRANSIENTFAILURE);
185 /* Greet the server. */
187 proto_greet(struct config *config)
189 char *line, *cmd, *msg, *swver;
193 line = stream_getln(s, NULL);
194 cmd = proto_get_ascii(&line);
197 if (strcmp(cmd, "OK") == 0) {
198 (void)proto_get_ascii(&line); /* major number */
199 (void)proto_get_ascii(&line); /* minor number */
200 swver = proto_get_ascii(&line);
201 } else if (strcmp(cmd, "!") == 0) {
202 msg = proto_get_rest(&line);
205 lprintf(-1, "Rejected by server: %s\n", msg);
206 return (STATUS_TRANSIENTFAILURE);
209 lprintf(2, "Server software version: %s\n",
210 swver != NULL ? swver : ".");
211 return (STATUS_SUCCESS);
213 lprintf(-1, "Invalid greeting from server\n");
214 return (STATUS_FAILURE);
217 /* Negotiate protocol version with the server. */
219 proto_negproto(struct config *config)
222 char *cmd, *line, *msg;
226 proto_printf(s, "PROTO %d %d %s\n", PROTO_MAJ, PROTO_MIN, PROTO_SWVER);
228 line = stream_getln(s, NULL);
229 cmd = proto_get_ascii(&line);
230 if (cmd == NULL || line == NULL)
232 if (strcmp(cmd, "!") == 0) {
233 msg = proto_get_rest(&line);
234 lprintf(-1, "Protocol negotiation failed: %s\n", msg);
236 } else if (strcmp(cmd, "PROTO") != 0)
238 error = proto_get_int(&line, &maj, 10);
240 error = proto_get_int(&line, &min, 10);
243 if (maj != PROTO_MAJ || min != PROTO_MIN) {
244 lprintf(-1, "Server protocol version %d.%d not supported "
245 "by client\n", maj, min);
246 return (STATUS_FAILURE);
248 return (STATUS_SUCCESS);
250 lprintf(-1, "Invalid PROTO command from server\n");
251 return (STATUS_FAILURE);
255 * File attribute support negotiation.
258 proto_fileattr(struct config *config)
260 fattr_support_t support;
263 int error, i, n, attr;
266 lprintf(2, "Negotiating file attribute support\n");
267 proto_printf(s, "ATTR %d\n", FT_NUMBER);
268 for (i = 0; i < FT_NUMBER; i++)
269 proto_printf(s, "%x\n", fattr_supported(i));
270 proto_printf(s, ".\n");
272 line = stream_getln(s, NULL);
275 cmd = proto_get_ascii(&line);
276 error = proto_get_int(&line, &n, 10);
277 if (error || line != NULL || strcmp(cmd, "ATTR") != 0 || n > FT_NUMBER)
279 for (i = 0; i < n; i++) {
280 line = stream_getln(s, NULL);
283 error = proto_get_int(&line, &attr, 16);
286 support[i] = fattr_supported(i) & attr;
288 for (i = n; i < FT_NUMBER; i++)
290 line = stream_getln(s, NULL);
291 if (line == NULL || strcmp(line, ".") != 0)
293 memcpy(config->fasupport, support, sizeof(config->fasupport));
294 return (STATUS_SUCCESS);
296 lprintf(-1, "Protocol error negotiating attribute support\n");
297 return (STATUS_FAILURE);
301 * Exchange collection information.
304 proto_xchgcoll(struct config *config)
308 struct globtree *diraccept, *dirrefuse;
309 struct globtree *fileaccept, *filerefuse;
310 char *line, *cmd, *collname, *pat;
311 char *msg, *release, *ident, *rcskey, *prefix;
313 int error, flags, options;
316 lprintf(2, "Exchanging collection information\n");
317 STAILQ_FOREACH(coll, &config->colls, co_next) {
318 if (coll->co_options & CO_SKIP)
320 proto_printf(s, "COLL %s %s %o %d\n", coll->co_name,
321 coll->co_release, coll->co_umask, coll->co_options);
322 for (i = 0; i < pattlist_size(coll->co_accepts); i++) {
323 proto_printf(s, "ACC %s\n",
324 pattlist_get(coll->co_accepts, i));
326 for (i = 0; i < pattlist_size(coll->co_refusals); i++) {
327 proto_printf(s, "REF %s\n",
328 pattlist_get(coll->co_refusals, i));
330 proto_printf(s, ".\n");
332 proto_printf(s, ".\n");
335 STAILQ_FOREACH(coll, &config->colls, co_next) {
336 if (coll->co_options & CO_SKIP)
338 coll->co_norsync = globtree_false();
339 line = stream_getln(s, NULL);
342 cmd = proto_get_ascii(&line);
343 collname = proto_get_ascii(&line);
344 release = proto_get_ascii(&line);
345 error = proto_get_int(&line, &options, 10);
346 if (error || line != NULL)
348 if (strcmp(cmd, "COLL") != 0 ||
349 strcmp(collname, coll->co_name) != 0 ||
350 strcmp(release, coll->co_release) != 0)
353 (coll->co_options | (options & CO_SERVMAYSET)) &
354 ~(~options & CO_SERVMAYCLEAR);
355 while ((line = stream_getln(s, NULL)) != NULL) {
356 if (strcmp(line, ".") == 0)
358 cmd = proto_get_ascii(&line);
361 if (strcmp(cmd, "!") == 0) {
362 msg = proto_get_rest(&line);
365 lprintf(-1, "Server message: %s\n", msg);
366 } else if (strcmp(cmd, "PRFX") == 0) {
367 prefix = proto_get_ascii(&line);
368 if (prefix == NULL || line != NULL)
370 coll->co_cvsroot = xstrdup(prefix);
371 } else if (strcmp(cmd, "KEYALIAS") == 0) {
372 ident = proto_get_ascii(&line);
373 rcskey = proto_get_ascii(&line);
374 if (rcskey == NULL || line != NULL)
376 error = keyword_alias(coll->co_keyword, ident,
380 } else if (strcmp(cmd, "KEYON") == 0) {
381 ident = proto_get_ascii(&line);
382 if (ident == NULL || line != NULL)
384 error = keyword_enable(coll->co_keyword, ident);
387 } else if (strcmp(cmd, "KEYOFF") == 0) {
388 ident = proto_get_ascii(&line);
389 if (ident == NULL || line != NULL)
391 error = keyword_disable(coll->co_keyword,
395 } else if (strcmp(cmd, "NORS") == 0) {
396 pat = proto_get_ascii(&line);
397 if (pat == NULL || line != NULL)
399 coll->co_norsync = globtree_or(coll->co_norsync,
400 globtree_match(pat, FNM_PATHNAME));
401 } else if (strcmp(cmd, "RNORS") == 0) {
402 pat = proto_get_ascii(&line);
403 if (pat == NULL || line != NULL)
405 coll->co_norsync = globtree_or(coll->co_norsync,
406 globtree_match(pat, FNM_PATHNAME |
413 keyword_prepare(coll->co_keyword);
415 diraccept = globtree_true();
416 fileaccept = globtree_true();
417 dirrefuse = globtree_false();
418 filerefuse = globtree_false();
420 if (pattlist_size(coll->co_accepts) > 0) {
421 globtree_free(diraccept);
422 globtree_free(fileaccept);
423 diraccept = globtree_false();
424 fileaccept = globtree_false();
425 flags = FNM_PATHNAME | FNM_LEADING_DIR |
427 for (i = 0; i < pattlist_size(coll->co_accepts); i++) {
428 pat = pattlist_get(coll->co_accepts, i);
429 diraccept = globtree_or(diraccept,
430 globtree_match(pat, flags));
433 if (coll->co_options & CO_CHECKOUTMODE &&
434 (len == 0 || pat[len - 1] != '*')) {
435 /* We must modify the pattern so that it
436 refers to the RCS file, rather than
437 the checked-out file. */
438 xasprintf(&pat, "%s,v", pat);
439 fileaccept = globtree_or(fileaccept,
440 globtree_match(pat, flags));
443 fileaccept = globtree_or(fileaccept,
444 globtree_match(pat, flags));
449 for (i = 0; i < pattlist_size(coll->co_refusals); i++) {
450 pat = pattlist_get(coll->co_refusals, i);
451 dirrefuse = globtree_or(dirrefuse,
452 globtree_match(pat, 0));
454 if (coll->co_options & CO_CHECKOUTMODE &&
455 (len == 0 || pat[len - 1] != '*')) {
456 /* We must modify the pattern so that it refers
457 to the RCS file, rather than the checked-out
459 xasprintf(&pat, "%s,v", pat);
460 filerefuse = globtree_or(filerefuse,
461 globtree_match(pat, 0));
464 filerefuse = globtree_or(filerefuse,
465 globtree_match(pat, 0));
469 coll->co_dirfilter = globtree_and(diraccept,
470 globtree_not(dirrefuse));
471 coll->co_filefilter = globtree_and(fileaccept,
472 globtree_not(filerefuse));
474 /* Set up a mask of file attributes that we don't want to sync
476 if (!(coll->co_options & CO_SETOWNER))
477 coll->co_attrignore |= FA_OWNER | FA_GROUP;
478 if (!(coll->co_options & CO_SETMODE))
479 coll->co_attrignore |= FA_MODE;
480 if (!(coll->co_options & CO_SETFLAGS))
481 coll->co_attrignore |= FA_FLAGS;
483 return (STATUS_SUCCESS);
485 lprintf(-1, "Protocol error during collection exchange\n");
486 return (STATUS_FAILURE);
490 proto_mux(struct config *config)
493 struct stream *s, *wr;
494 struct chan *chan0, *chan1;
498 lprintf(2, "Establishing multiplexed-mode data connection\n");
499 proto_printf(s, "MUX\n");
501 m = mux_open(config->socket, &chan0);
503 lprintf(-1, "Cannot open the multiplexer\n");
508 lprintf(-1, "ChannelMux.Listen failed: %s\n", strerror(errno));
512 wr = stream_open(chan0, NULL, (stream_writefn_t *)chan_write, NULL);
513 proto_printf(wr, "CHAN %d\n", id);
515 chan1 = chan_accept(m, id);
517 lprintf(-1, "ChannelMux.Accept failed: %s\n", strerror(errno));
521 config->chan0 = chan0;
522 config->chan1 = chan1;
527 * Initializes the connection to the CVSup server, that is handle
528 * the protocol negotiation, logging in, exchanging file attributes
529 * support and collections information, and finally run the update
533 proto_run(struct config *config)
535 struct thread_args lister_args;
536 struct thread_args detailer_args;
537 struct thread_args updater_args;
538 struct thread_args *args;
539 struct killer killer;
540 struct threads *workers;
545 * We pass NULL for the close() function because we'll reuse
546 * the socket after the stream is closed.
548 config->server = stream_open_fd(config->socket, stream_read_fd,
549 stream_write_fd, NULL);
550 status = proto_greet(config);
551 if (status == STATUS_SUCCESS)
552 status = proto_negproto(config);
553 if (status == STATUS_SUCCESS)
554 status = auth_login(config);
555 if (status == STATUS_SUCCESS)
556 status = proto_fileattr(config);
557 if (status == STATUS_SUCCESS)
558 status = proto_xchgcoll(config);
559 if (status != STATUS_SUCCESS)
562 /* Multi-threaded action starts here. */
563 m = proto_mux(config);
565 return (STATUS_FAILURE);
567 stream_close(config->server);
568 config->server = NULL;
569 config->fixups = fixups_new();
570 killer_start(&killer, m);
572 /* Start the worker threads. */
573 workers = threads_new();
575 args->config = config;
579 args->wr = stream_open(config->chan0,
580 NULL, (stream_writefn_t *)chan_write, NULL);
581 threads_create(workers, lister, args);
583 args = &detailer_args;
584 args->config = config;
587 args->rd = stream_open(config->chan0,
588 (stream_readfn_t *)chan_read, NULL, NULL);
589 args->wr = stream_open(config->chan1,
590 NULL, (stream_writefn_t *)chan_write, NULL);
591 threads_create(workers, detailer, args);
593 args = &updater_args;
594 args->config = config;
597 args->rd = stream_open(config->chan1,
598 (stream_readfn_t *)chan_read, NULL, NULL);
600 threads_create(workers, updater, args);
602 lprintf(2, "Running\n");
603 /* Wait for all the worker threads to finish. */
604 status = STATUS_SUCCESS;
605 for (i = 0; i < 3; i++) {
606 args = threads_wait(workers);
607 if (args->rd != NULL)
608 stream_close(args->rd);
609 if (args->wr != NULL)
610 stream_close(args->wr);
611 if (args->status != STATUS_SUCCESS) {
612 assert(args->errmsg != NULL);
613 if (status == STATUS_SUCCESS) {
614 status = args->status;
615 /* Shutdown the multiplexer to wake up all
616 the other threads. */
617 mux_shutdown(m, args->errmsg, status);
622 threads_free(workers);
623 if (status == STATUS_SUCCESS) {
624 lprintf(2, "Shutting down connection to server\n");
625 chan_close(config->chan0);
626 chan_close(config->chan1);
627 chan_wait(config->chan0);
628 chan_wait(config->chan1);
629 mux_shutdown(m, NULL, STATUS_SUCCESS);
631 killer_stop(&killer);
632 fixups_free(config->fixups);
633 status = mux_close(m);
634 if (status == STATUS_SUCCESS) {
635 lprintf(1, "Finished successfully\n");
636 } else if (status == STATUS_INTERRUPTED) {
637 lprintf(-1, "Interrupted\n");
638 if (killer.killedby != -1)
639 kill(getpid(), killer.killedby);
645 * Write a string into the stream, escaping characters as needed.
646 * Characters escaped:
655 proto_escape(struct stream *wr, const char *s)
661 /* Handle characters that need escaping. */
663 len = strcspn(s, " \t\r\n\\");
664 n = stream_write(wr, s, len);
670 n = stream_write(wr, "\\_", 2);
673 n = stream_write(wr, "\\t", 2);
676 n = stream_write(wr, "\\r", 2);
679 n = stream_write(wr, "\\n", 2);
682 n = stream_write(wr, "\\\\", 2);
693 * A simple printf() implementation specifically tailored for csup.
694 * List of the supported formats:
697 * %d or %i Print an int as decimal.
698 * %x Print an int as hexadecimal.
699 * %o Print an int as octal.
700 * %t Print a time_t as decimal.
701 * %s Print a char * escaping some characters as needed.
702 * %S Print a char * without escaping.
703 * %f Print an encoded struct fattr *.
704 * %F Print an encoded struct fattr *, specifying the supported
708 proto_printf(struct stream *wr, const char *format, ...)
710 fattr_support_t *support;
725 va_start(ap, format);
726 while ((cp = strchr(fmt, '%')) != NULL) {
728 n = stream_write(wr, fmt, cp - fmt);
737 rv = stream_printf(wr, "%c", c);
741 val = va_arg(ap, int);
742 rv = stream_printf(wr, "%d", val);
745 val = va_arg(ap, int);
746 rv = stream_printf(wr, "%x", val);
749 val = va_arg(ap, int);
750 rv = stream_printf(wr, "%o", val);
753 off = va_arg(ap, off_t);
754 rv = stream_printf(wr, "%llu", off);
757 s = va_arg(ap, char *);
759 rv = stream_printf(wr, "%s", s);
762 s = va_arg(ap, char *);
764 rv = proto_escape(wr, s);
767 longval = (long long)va_arg(ap, time_t);
768 rv = stream_printf(wr, "%lld", longval);
771 fa = va_arg(ap, struct fattr *);
772 attr = fattr_encode(fa, NULL, 0);
773 rv = proto_escape(wr, attr);
777 fa = va_arg(ap, struct fattr *);
778 support = va_arg(ap, fattr_support_t *);
779 ignore = va_arg(ap, int);
780 attr = fattr_encode(fa, *support, ignore);
781 rv = proto_escape(wr, attr);
785 size = va_arg(ap, size_t);
786 rv = stream_printf(wr, "%zu", size);
790 n = stream_write(wr, "%", 1);
800 rv = stream_printf(wr, "%s", fmt);
810 * Unescape the string, see proto_escape().
813 proto_unescape(char *s)
818 while ((cp = strchr(cp, '\\')) != NULL) {
839 while (*cp2 != '\0') {
847 * Get an ascii token in the string.
850 proto_get_ascii(char **s)
854 ret = strsep(s, " ");
857 /* Make sure we disallow 0-length fields. */
867 * Get the rest of the string.
870 proto_get_rest(char **s)
886 proto_get_int(char **s, int *val, int base)
891 cp = proto_get_ascii(s);
894 error = asciitoint(cp, val, base);
899 * Get a size_t token.
902 proto_get_sizet(char **s, size_t *val, int base)
904 unsigned long long tmp;
907 cp = proto_get_ascii(s);
911 tmp = strtoll(cp, &end, base);
912 if (errno || *end != '\0')
919 * Get a time_t token.
921 * Ideally, we would use an intmax_t and strtoimax() here, but strtoll()
922 * is more portable and 64bits should be enough for a timestamp.
925 proto_get_time(char **s, time_t *val)
930 cp = proto_get_ascii(s);
934 tmp = strtoll(cp, &end, 10);
935 if (errno || *end != '\0')
941 /* Start the killer thread. It is used to protect against some signals
942 during the multi-threaded run so that we can gracefully fail. */
944 killer_start(struct killer *k, struct mux *m)
950 sigemptyset(&k->sigset);
951 sigaddset(&k->sigset, SIGINT);
952 sigaddset(&k->sigset, SIGHUP);
953 sigaddset(&k->sigset, SIGTERM);
954 sigaddset(&k->sigset, SIGPIPE);
955 pthread_sigmask(SIG_BLOCK, &k->sigset, NULL);
956 error = pthread_create(&k->thread, NULL, killer_run, k);
958 err(1, "pthread_create");
961 /* The main loop of the killer thread. */
963 killer_run(void *arg)
970 error = sigwait(&k->sigset, &sig);
972 if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) {
973 if (k->killedby == -1) {
975 /* Ensure we don't get canceled during the shutdown. */
976 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
977 mux_shutdown(k->mux, "Cleaning up ...",
979 pthread_setcancelstate(old, NULL);
985 /* Stop the killer thread. */
987 killer_stop(struct killer *k)
992 error = pthread_cancel(k->thread);
994 pthread_join(k->thread, &val);
995 assert(val == PTHREAD_CANCELED);
996 pthread_sigmask(SIG_UNBLOCK, &k->sigset, NULL);