summaryrefslogtreecommitdiffstats
path: root/usr.bin/csup/main.c
blob: 1363d149f25866da6a052356932a50936c40f0fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*-
 * Copyright (c) 2003-2006, Maxime Henrion <mux@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/file.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "config.h"
#include "fattr.h"
#include "misc.h"
#include "proto.h"
#include "stream.h"

#define	USAGE_OPTFMT	"    %-12s %s\n"
#define	USAGE_OPTFMTSUB	"    %-14s %s\n", ""

int verbose = 1;

static void
usage(char *argv0)
{

	lprintf(-1, "Usage: %s [options] supfile\n", basename(argv0));
	lprintf(-1, "  Options:\n");
	lprintf(-1, USAGE_OPTFMT, "-1", "Don't retry automatically on failure "
	    "(same as \"-r 0\")");
	lprintf(-1, USAGE_OPTFMT, "-4", "Force usage of IPv4 addresses");
	lprintf(-1, USAGE_OPTFMT, "-6", "Force usage of IPv6 addresses");
	lprintf(-1, USAGE_OPTFMT, "-a",
		"Require server to authenticate itself to us");
	lprintf(-1, USAGE_OPTFMT, "-A addr",
	    "Bind local socket to a specific address");
	lprintf(-1, USAGE_OPTFMT, "-b base",
	    "Override supfile's \"base\" directory");
	lprintf(-1, USAGE_OPTFMT, "-c collDir",
	    "Subdirectory of \"base\" for collections (default \"sup\")");
	lprintf(-1, USAGE_OPTFMT, "-d delLimit",
	    "Allow at most \"delLimit\" file deletions (default unlimited)");
	lprintf(-1, USAGE_OPTFMT, "-h host",
	    "Override supfile's \"host\" name");
	lprintf(-1, USAGE_OPTFMT, "-i pattern",
	    "Include only files/directories matching pattern.");
	lprintf(-1, USAGE_OPTFMTSUB,
	    "May be repeated for an OR operation.  Default is");
	lprintf(-1, USAGE_OPTFMTSUB, "to include each entire collection.");
	lprintf(-1, USAGE_OPTFMT, "-k",
	    "Keep bad temporary files when fixups are required");
	lprintf(-1, USAGE_OPTFMT, "-l lockfile",
	    "Lock file during update; fail if already locked");
	lprintf(-1, USAGE_OPTFMT, "-L n",
	    "Verbosity level (0..2, default 1)");
	lprintf(-1, USAGE_OPTFMT, "-p port",
	    "Alternate server port (default 5999)");
	lprintf(-1, USAGE_OPTFMT, "-r n",
	    "Maximum retries on transient errors (default unlimited)");
	lprintf(-1, USAGE_OPTFMT, "-s",
	    "Don't stat client files; trust the checkouts file");
	lprintf(-1, USAGE_OPTFMT, "-v", "Print version and exit");
	lprintf(-1, USAGE_OPTFMT, "-z", "Enable compression for all "
	    "collections");
	lprintf(-1, USAGE_OPTFMT, "-Z", "Disable compression for all "
	    "collections");
}

int
main(int argc, char *argv[])
{
	struct tm tm;
	struct backoff_timer *timer;
	struct config *config;
	struct coll *override;
	struct addrinfo *res;
	struct sockaddr *laddr;
	socklen_t laddrlen;
	struct stream *lock;
	char *argv0, *file, *lockfile;
	int family, error, lockfd, lflag, overridemask;
	int c, i, deletelim, port, retries, status, reqauth;
	time_t nexttry;

	error = 0;
	family = PF_UNSPEC;
	deletelim = -1;
	port = 0;
	lflag = 0;
	lockfd = 0;
	nexttry = 0;
	retries = -1;
	argv0 = argv[0];
	laddr = NULL;
	laddrlen = 0;
	lockfile = NULL;
	override = coll_new(NULL);
	overridemask = 0;
	reqauth = 0;

	while ((c = getopt(argc, argv,
	    "146aA:b:c:d:gh:i:kl:L:p:P:r:svzZ")) != -1) {
		switch (c) {
		case '1':
			retries = 0;
			break;
		case '4':
			family = AF_INET;
			break;
		case '6':
			family = AF_INET6;
			break;
		case 'a':
			/* Require server authentication */
			reqauth = 1;
			break;
		case 'A':
			error = getaddrinfo(optarg, NULL, NULL, &res);
			if (error) {
				lprintf(-1, "%s: %s\n", optarg,
				    gai_strerror(error));
				return (1);
			}
			laddrlen = res->ai_addrlen;
			laddr = xmalloc(laddrlen);
			memcpy(laddr, res->ai_addr, laddrlen);
			freeaddrinfo(res);
			break;
		case 'b':
			if (override->co_base != NULL)
				free(override->co_base);
			override->co_base = xstrdup(optarg);
			break;
		case 'c':
			override->co_colldir = optarg;
			break;
		case 'd':
			error = asciitoint(optarg, &deletelim, 0);
			if (error || deletelim < 0) {
				lprintf(-1, "Invalid deletion limit\n");
				usage(argv0);
				return (1);
			}
			break;
		case 'g':
			/* For compatibility. */
			break;
		case 'h':
			if (override->co_host != NULL)
				free(override->co_host);
			override->co_host = xstrdup(optarg);
			break;
		case 'i':
			pattlist_add(override->co_accepts, optarg);
			break;
		case 'k':
			override->co_options |= CO_KEEPBADFILES;
			overridemask |= CO_KEEPBADFILES;
			break;
		case 'l':
			lockfile = optarg;
			lflag = 1;
			lockfd = open(lockfile,
			    O_CREAT | O_WRONLY | O_TRUNC, 0700);
			if (lockfd != -1) {
				error = flock(lockfd, LOCK_EX | LOCK_NB);
				if (error == -1 && errno == EWOULDBLOCK) {
					if (lockfd != -1)
						close(lockfd);
					lprintf(-1, "\"%s\" is already locked "
					    "by another process\n", lockfile);
					return (1);
				}
			}
			if (lockfd == -1 || error == -1) {
				if (lockfd != -1)
					close(lockfd);
				lprintf(-1, "Error locking \"%s\": %s\n",
				    lockfile, strerror(errno));
				return (1);
			}
			lock = stream_open_fd(lockfd,
			    NULL, stream_write_fd, NULL);
			(void)stream_printf(lock, "%10ld\n", (long)getpid());
			stream_close(lock);
			break;
		case 'L':
			error = asciitoint(optarg, &verbose, 0);
			if (error) {
				lprintf(-1, "Invalid verbosity\n");
				usage(argv0);
				return (1);
			}
			break;
		case 'p':
			/* Use specified server port. */
			error = asciitoint(optarg, &port, 0);
			if (error) {
				lprintf(-1, "Invalid server port\n");
				usage(argv0);
				return (1);
			}
			if (port <= 0 || port >= 65536) {
				lprintf(-1, "Invalid port %d\n", port);
				return (1);
			}
			if (port < 1024) {
				lprintf(-1, "Reserved port %d not permitted\n",
				    port);
				return (1);
			}
			break;
		case 'P':
			/* For compatibility. */
			if (strcmp(optarg, "m") != 0) {
				lprintf(-1,
				    "Client only supports multiplexed mode\n");
				return (1);
			}
			break;
		case 'r':
			error = asciitoint(optarg, &retries, 0);
			if (error || retries < 0) {
				lprintf(-1, "Invalid retry limit\n");
				usage(argv0);
				return (1);
			}
			break;
		case 's':
			override->co_options |= CO_TRUSTSTATUSFILE;
			overridemask |= CO_TRUSTSTATUSFILE;
			break;
		case 'v':
			lprintf(0, "CVSup client written in C\n");
			lprintf(0, "Software version: %s\n", PROTO_SWVER);
			lprintf(0, "Protocol version: %d.%d\n",
			    PROTO_MAJ, PROTO_MIN);
			return (0);
			break;
		case 'z':
			/* Force compression on all collections. */
			override->co_options |= CO_COMPRESS;
			overridemask |= CO_COMPRESS;
			break;
		case 'Z':
			/* Disables compression on all collections. */
			override->co_options &= ~CO_COMPRESS;
			overridemask &= ~CO_COMPRESS;
			break;
		case '?':
		default:
			usage(argv0);
			return (1);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc < 1) {
		usage(argv0);
		return (1);
	}

	file = argv[0];
	lprintf(2, "Parsing supfile \"%s\"\n", file);
	config = config_init(file, override, overridemask);
	coll_free(override);
	if (config == NULL)
		return (1);

	if (config_checkcolls(config) == 0) {
		lprintf(-1, "No collections selected\n");
		return (1);
	}

	if (laddr != NULL) {
		config->laddr = laddr;
		config->laddrlen = laddrlen;
	}
	config->deletelim = deletelim;
	config->reqauth = reqauth;
	lprintf(2, "Connecting to %s\n", config->host);

	i = 0;
	fattr_init();	/* Initialize the fattr API. */
	timer = bt_new(300, 7200, 2.0, 0.1);
	for (;;) {
		status = proto_connect(config, family, port);
		if (status == STATUS_SUCCESS) {
			status = proto_run(config);
			if (status != STATUS_TRANSIENTFAILURE)
				break;
		}
		if (retries >= 0 && i >= retries)
			break;
		nexttry = time(0) + bt_get(timer);
		localtime_r(&nexttry, &tm);
		lprintf(1, "Will retry at %02d:%02d:%02d\n",
		    tm.tm_hour, tm.tm_min, tm.tm_sec);
		bt_pause(timer);
		lprintf(1, "Retrying\n");
		i++;
	}
	bt_free(timer);
	fattr_fini();
	if (lflag) {
		unlink(lockfile);
		flock(lockfd, LOCK_UN);
		close(lockfd);
	}
	config_free(config);
	if (status != STATUS_SUCCESS)
		return (1);
	return (0);
}
OpenPOWER on IntegriCloud