File: got-send-pack.c

package info (click to toggle)
got 0.119-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,448 kB
  • sloc: ansic: 124,378; sh: 50,814; yacc: 4,353; makefile: 2,241; perl: 357
file content (759 lines) | stat: -rw-r--r-- 19,740 bytes parent folder | download | duplicates (2)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/stat.h>

#include <err.h>
#include <stdint.h>
#include <errno.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <zlib.h>
#include <err.h>

#include "got_compat.h"
#include "got_error.h"
#include "got_object.h"
#include "got_path.h"
#include "got_version.h"
#include "got_fetch.h"
#include "got_reference.h"

#include "got_lib_hash.h"
#include "got_lib_delta.h"
#include "got_lib_object.h"
#include "got_lib_object_parse.h"
#include "got_lib_privsep.h"
#include "got_lib_pack.h"
#include "got_lib_pkt.h"
#include "got_lib_gitproto.h"
#include "got_lib_ratelimit.h"
#include "got_lib_poll.h"

#ifndef nitems
#define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
#endif

struct got_object *indexed;
static int chattygot;

static const struct got_capability got_capabilities[] = {
	{ GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
	{ GOT_CAPA_OFS_DELTA, NULL },
#if 0
	{ GOT_CAPA_SIDE_BAND_64K, NULL },
#endif
	{ GOT_CAPA_REPORT_STATUS, NULL },
	{ GOT_CAPA_DELETE_REFS, NULL },
};

static const struct got_error *
send_upload_progress(struct imsgbuf *ibuf, off_t bytes,
    struct got_ratelimit *rl)
{
	const struct got_error *err = NULL;
	int elapsed = 0;

	if (rl) {
		err = got_ratelimit_check(&elapsed, rl);
		if (err || !elapsed)
			return err;
	}

	if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
	    &bytes, sizeof(bytes)) == -1)
		return got_error_from_errno(
		    "imsg_compose SEND_UPLOAD_PROGRESS");

	return got_privsep_flush_imsg(ibuf);
}

static const struct got_error *
send_pack_request(struct imsgbuf *ibuf)
{
	if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
	    NULL, 0) == -1)
		return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
	return got_privsep_flush_imsg(ibuf);
}

static const struct got_error *
send_done(struct imsgbuf *ibuf)
{
	if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
		return got_error_from_errno("imsg_compose SEND_DONE");
	return got_privsep_flush_imsg(ibuf);
}

static const struct got_error *
recv_packfd(int *packfd, struct imsgbuf *ibuf)
{
	const struct got_error *err;
	struct imsg imsg;

	*packfd = -1;

	err = got_privsep_recv_imsg(&imsg, ibuf, 0);
	if (err)
		return err;

	if (imsg.hdr.type == GOT_IMSG_STOP) {
		err = got_error(GOT_ERR_CANCELLED);
		goto done;
	}

	if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
		err = got_error(GOT_ERR_PRIVSEP_MSG);
		goto done;
	}

	if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
		err = got_error(GOT_ERR_PRIVSEP_LEN);
		goto done;
	}

	*packfd = imsg_get_fd(&imsg);
done:
	imsg_free(&imsg);
	return err;
}

static const struct got_error *
send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
{
	const struct got_error *err;
	unsigned char buf[8192];
	ssize_t r;
	off_t wtotal = 0;
	struct got_ratelimit rl;

	if (lseek(packfd, 0L, SEEK_SET) == -1)
		return got_error_from_errno("lseek");

	got_ratelimit_init(&rl, 0, 500);

	for (;;) {
		r = read(packfd, buf, sizeof(buf));
		if (r == -1)
			return got_error_from_errno("read");
		if (r == 0)
			break;
		err = got_poll_write_full(sendfd, buf, r);
		if (err)
			return NULL;
		wtotal += r;
		err = send_upload_progress(ibuf, wtotal, &rl);
		if (err)
			return err;
	}

	return send_upload_progress(ibuf, wtotal, NULL);
}

static const struct got_error *
send_error(const char *buf, size_t len)
{
	static char msg[1024];
	size_t i;

	for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
		if (!isprint((unsigned char)buf[i]))
			return got_error_msg(GOT_ERR_BAD_PACKET,
			    "non-printable error message received from server");
		msg[i] = buf[i];
	}
	msg[i] = '\0';
	return got_error_msg(GOT_ERR_SEND_FAILED, msg);
}

static const struct got_error *
send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
    const char *refname)
{
	struct ibuf *wbuf;
	size_t len, reflen = strlen(refname);

	len = sizeof(struct got_imsg_send_remote_ref) + reflen;
	if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
		return got_error(GOT_ERR_NO_SPACE);

	wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
	if (wbuf == NULL)
		return got_error_from_errno("imsg_create SEND_REMOTE_REF");

	/* Keep in sync with struct got_imsg_send_remote_ref definition! */
	if (imsg_add(wbuf, refid, sizeof(*refid)) == -1)
		return got_error_from_errno("imsg_add SEND_REMOTE_REF");
	if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
		return got_error_from_errno("imsg_add SEND_REMOTE_REF");
	if (imsg_add(wbuf, refname, reflen) == -1)
		return got_error_from_errno("imsg_add SEND_REMOTE_REF");

	imsg_close(ibuf, wbuf);
	return got_privsep_flush_imsg(ibuf);
}

static const struct got_error *
send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
    struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
{
	struct ibuf *wbuf;
	size_t i, len, reflen, errmsglen = 0;
	struct got_pathlist_entry *pe;
	int ref_valid = 0;
	char *eol, *sp;
	const char *errmsg = "";

	eol = strchr(refname, '\n');
	if (eol == NULL) {
		return got_error_msg(GOT_ERR_BAD_PACKET,
		    "unexpected message from server");
	}
	*eol = '\0';

	sp = strchr(refname, ' ');
	if (sp != NULL) {
		*sp++ = '\0';
		errmsg = sp;
		errmsglen = strlen(errmsg);

		for (i = 0; i < errmsglen; ++i) {
			if (!isprint((unsigned char)errmsg[i])) {
				return got_error_msg(GOT_ERR_BAD_PACKET,
				    "non-printable error message received "
				    "from the server");
			}
		}
	}

	reflen = strlen(refname);
	if (!got_ref_name_is_valid(refname)) {
		return got_error_msg(GOT_ERR_BAD_PACKET,
		    "unexpected message from server");
	}

	RB_FOREACH(pe, got_pathlist_head, refs) {
		if (strcmp(refname, pe->path) == 0) {
			ref_valid = 1;
			break;
		}
	}
	if (!ref_valid) {
		RB_FOREACH(pe, got_pathlist_head, delete_refs) {
			if (strcmp(refname, pe->path) == 0) {
				ref_valid = 1;
				break;
			}
		}
	}
	if (!ref_valid) {
		return got_error_msg(GOT_ERR_BAD_PACKET,
		    "unexpected message from server");
	}

	len = sizeof(struct got_imsg_send_ref_status) + reflen + errmsglen;
	if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
		return got_error(GOT_ERR_NO_SPACE);

	wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
	    0, 0, len);
	if (wbuf == NULL)
		return got_error_from_errno("imsg_create SEND_REF_STATUS");

	/* Keep in sync with struct got_imsg_send_ref_status definition! */
	if (imsg_add(wbuf, &success, sizeof(success)) == -1)
		return got_error_from_errno("imsg_add SEND_REF_STATUS");
	if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
		return got_error_from_errno("imsg_add SEND_REF_STATUS");
	if (imsg_add(wbuf, &errmsglen, sizeof(errmsglen)) == -1)
		return got_error_from_errno("imsg_add SEND_REF_STATUS");
	if (imsg_add(wbuf, refname, reflen) == -1)
		return got_error_from_errno("imsg_add SEND_REF_STATUS");
	if (imsg_add(wbuf, errmsg, errmsglen) == -1)
		return got_error_from_errno("imsg_add SEND_REF_STATUS");

	imsg_close(ibuf, wbuf);
	return got_privsep_flush_imsg(ibuf);
}

static const struct got_error *
describe_refchange(int *n, int *sent_my_capabilites,
    const char *my_capabilities, char *buf, size_t bufsize,
    const char *refname, const char *old_hashstr, const char *new_hashstr)
{
	*n = snprintf(buf, bufsize, "%s %s %s",
	    old_hashstr, new_hashstr, refname);
	if (*n < 0 || (size_t)*n >= bufsize)
		return got_error(GOT_ERR_NO_SPACE);

	/*
	 * We must announce our capabilities along with the first
	 * reference. Unfortunately, the protocol requires an embedded
	 * NUL as a separator between reference name and capabilities,
	 * which we have to deal with here.
	 * It also requires a linefeed for terminating packet data.
	 */
	if (!*sent_my_capabilites && my_capabilities != NULL) {
		int m;
		if (*n >= bufsize - 1)
			return got_error(GOT_ERR_NO_SPACE);
		m = snprintf(buf + *n + 1, /* offset after '\0' */
		    bufsize - (*n + 1), "%s\n", my_capabilities);
		if (m < 0 || *n + m >= bufsize)
			return got_error(GOT_ERR_NO_SPACE);
		*n += m;
		*sent_my_capabilites = 1;
	} else {
		*n = strlcat(buf, "\n", bufsize);
		if (*n >= bufsize)
			return got_error(GOT_ERR_NO_SPACE);
	}

	return NULL;
}

static const struct got_error *
send_pack(int fd, struct got_pathlist_head *refs,
    struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
{
	const struct got_error *err = NULL;
	char buf[GOT_PKT_MAX];
	const unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
	char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
	char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
	struct got_pathlist_head their_refs;
	int is_firstpkt = 1;
	int n, nsent = 0;
	int packfd = -1;
	char *id_str = NULL, *refname = NULL;
	struct got_object_id *id = NULL;
	char *server_capabilities = NULL, *my_capabilities = NULL;
	struct got_pathlist_entry *pe;
	int sent_my_capabilites = 0;

	RB_INIT(&their_refs);

	if (RB_EMPTY(refs) && RB_EMPTY(delete_refs))
		return got_error(GOT_ERR_SEND_EMPTY);

	while (1) {
		err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot,
		    INFTIM);
		if (err)
			goto done;
		if (n == 0)
			break;
		if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
			err = send_error(&buf[4], n - 4);
			goto done;
		}
		free(id_str);
		free(refname);
		err = got_gitproto_parse_refline(&id_str, &refname,
		    &server_capabilities, buf, n);
		if (err)
			goto done;
		if (is_firstpkt) {
			if (server_capabilities == NULL) {
				server_capabilities = strdup("");
				if (server_capabilities == NULL) {
					err = got_error_from_errno("strdup");
					goto done;
				}
			}
			if (chattygot && server_capabilities[0] != '\0')
				fprintf(stderr, "%s: server capabilities: %s\n",
				    getprogname(), server_capabilities);
			err = got_gitproto_match_capabilities(&my_capabilities,
			    NULL, server_capabilities, got_capabilities,
			    nitems(got_capabilities));
			if (err)
				goto done;
			if (chattygot)
				fprintf(stderr, "%s: my capabilities:%s\n",
				    getprogname(),
				    my_capabilities ? my_capabilities : "");
			is_firstpkt = 0;
		}
		if (strstr(refname, "^{}")) {
			if (chattygot) {
				fprintf(stderr, "%s: ignoring %s\n",
				    getprogname(), refname);
			}
			continue;
		}

		id = malloc(sizeof(*id));
		if (id == NULL) {
			err = got_error_from_errno("malloc");
			goto done;
		}
		if (!got_parse_object_id(id, id_str, GOT_HASH_SHA1)) {
			err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
			goto done;
		}
		err = send_their_ref(ibuf, id, refname);
		if (err)
			goto done;

		err = got_pathlist_insert(&pe, &their_refs, refname, id);
		if (err || pe == NULL) {
			free(refname);
			free(id);
			if (err)
				goto done;
		}


		if (chattygot)
			fprintf(stderr, "%s: remote has %s %s\n",
			    getprogname(), refname, id_str);
		free(id_str);
		id_str = NULL;
		refname = NULL; /* do not free; owned by their_refs */
		id = NULL; /* do not free; owned by their_refs */
	}

	if (!RB_EMPTY(delete_refs)) {
		if (my_capabilities == NULL ||
		    strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
			err = got_error(GOT_ERR_CAPA_DELETE_REFS);
			goto done;
		}
	}

	RB_FOREACH(pe, got_pathlist_head, delete_refs) {
		const char *refname = pe->path;
		struct got_pathlist_entry *their_pe;
		struct got_object_id *their_id = NULL;

		RB_FOREACH(their_pe, got_pathlist_head, &their_refs) {
			const char *their_refname = their_pe->path;
			if (got_path_cmp(refname, their_refname,
			    strlen(refname), strlen(their_refname)) == 0) {
				their_id = their_pe->data;
				break;
			}
		}
		if (their_id == NULL) {
			err = got_error_fmt(GOT_ERR_NOT_REF,
			    "%s does not exist in remote repository",
			    refname);
			goto done;
		}

		got_object_id_hex(their_id, old_hashstr,
		    sizeof(old_hashstr));
		got_sha1_digest_to_str(zero_id, new_hashstr,
		    sizeof(new_hashstr));
		err = describe_refchange(&n, &sent_my_capabilites,
		    my_capabilities, buf, sizeof(buf), refname,
		    old_hashstr, new_hashstr);
		if (err)
			goto done;
		err = got_pkt_writepkt(fd, buf, n, chattygot);
		if (err)
			goto done;
		if (chattygot) {
			fprintf(stderr, "%s: deleting %s %s\n",
			    getprogname(), refname, old_hashstr);
		}
		nsent++;
	}

	RB_FOREACH(pe, got_pathlist_head, refs) {
		const char *refname = pe->path;
		struct got_object_id *id = pe->data;
		struct got_object_id *their_id = NULL;
		struct got_pathlist_entry *their_pe;

		RB_FOREACH(their_pe, got_pathlist_head, &their_refs) {
			const char *their_refname = their_pe->path;
			if (got_path_cmp(refname, their_refname,
			    strlen(refname), strlen(their_refname)) == 0) {
				their_id = their_pe->data;
				break;
			}
		}
		if (their_id) {
			if (got_object_id_cmp(id, their_id) == 0) {
				if (chattygot) {
					fprintf(stderr,
					    "%s: no change for %s\n",
					    getprogname(), refname);
				}
				continue;
			}
			got_object_id_hex(their_id, old_hashstr,
			    sizeof(old_hashstr));
		} else {
			got_sha1_digest_to_str(zero_id, old_hashstr,
			    sizeof(old_hashstr));
		}
		got_object_id_hex(id, new_hashstr, sizeof(new_hashstr));
		err = describe_refchange(&n, &sent_my_capabilites,
		    my_capabilities, buf, sizeof(buf), refname,
		    old_hashstr, new_hashstr);
		if (err)
			goto done;
		err = got_pkt_writepkt(fd, buf, n, chattygot);
		if (err)
			goto done;
		if (chattygot) {
			if (their_id) {
				fprintf(stderr, "%s: updating %s %s -> %s\n",
				    getprogname(), refname, old_hashstr,
				    new_hashstr);
			} else {
				fprintf(stderr, "%s: creating %s %s\n",
				    getprogname(), refname, new_hashstr);
			}
		}
		nsent++;
	}
	err = got_pkt_flushpkt(fd, chattygot);
	if (err)
		goto done;

	err = send_pack_request(ibuf);
	if (err)
		goto done;

	err = recv_packfd(&packfd, ibuf);
	if (err)
		goto done;

	if (packfd != -1) {
		err = send_pack_file(fd, packfd, ibuf);
		if (err)
			goto done;
	}

	err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot, INFTIM);
	if (err)
		goto done;
	if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
		err = send_error(&buf[4], n - 4);
		goto done;
	} else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
		err = got_error_msg(GOT_ERR_BAD_PACKET,
		    "unexpected message from server");
		goto done;
	}

	while (nsent > 0) {
		err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot,
		    INFTIM);
		if (err)
			goto done;
		if (n < 3) {
			err = got_error_msg(GOT_ERR_BAD_PACKET,
			    "unexpected message from server");
			goto done;
		} else if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
			err = send_error(&buf[4], n - 4);
			goto done;
		} else if (strncmp(buf, "ok ", 3) == 0) {
			err = send_ref_status(ibuf, buf + 3, 1,
			    refs, delete_refs);
			if (err)
				goto done;
		} else if (strncmp(buf, "ng ", 3) == 0) {
			err = send_ref_status(ibuf, buf + 3, 0,
			    refs, delete_refs);
			if (err)
				goto done;
		} else {
			err = got_error_msg(GOT_ERR_BAD_PACKET,
			    "unexpected message from server");
			goto done;
		}
		nsent--;
	}

	err = send_done(ibuf);
done:
	got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
	free(id_str);
	free(id);
	free(refname);
	free(server_capabilities);
	free(my_capabilities);
	return err;
}

int
main(int argc, char **argv)
{
	const struct got_error *err = NULL;
	int sendfd = -1;
	struct imsgbuf ibuf;
	struct imsg imsg;
	struct got_pathlist_head refs;
	struct got_pathlist_head delete_refs;
	struct got_imsg_send_request send_req;
	struct got_imsg_send_ref href;
	size_t datalen, i;
#if 0
	static int attached;
	while (!attached)
		sleep (1);
#endif

	RB_INIT(&refs);
	RB_INIT(&delete_refs);

	if (imsgbuf_init(&ibuf, GOT_IMSG_FD_CHILD) == -1) {
		warn("imsgbuf_init");
		return 1;
	}
	imsgbuf_allow_fdpass(&ibuf);
#ifndef PROFILE
	/* revoke access to most system calls */
	if (pledge("stdio recvfd", NULL) == -1) {
		err = got_error_from_errno("pledge");
		got_privsep_send_error(&ibuf, err);
		imsgbuf_clear(&ibuf);
		return 1;
	}

	/* revoke fs access */
	if (landlock_no_fs() == -1) {
		err = got_error_from_errno("landlock_no_fs");
		got_privsep_send_error(&ibuf, err);
		return 1;
	}
	if (cap_enter() == -1) {
		err = got_error_from_errno("cap_enter");
		got_privsep_send_error(&ibuf, err);
		return 1;
	}
#endif
	if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
		if (err->code == GOT_ERR_PRIVSEP_PIPE)
			err = NULL;
		goto done;
	}
	if (imsg.hdr.type == GOT_IMSG_STOP)
		goto done;
	if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
		err = got_error(GOT_ERR_PRIVSEP_MSG);
		goto done;
	}
	datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
	if (datalen < sizeof(send_req)) {
		err = got_error(GOT_ERR_PRIVSEP_LEN);
		goto done;
	}
	memcpy(&send_req, imsg.data, sizeof(send_req));
	sendfd = imsg_get_fd(&imsg);
	imsg_free(&imsg);

	if (send_req.verbosity > 0)
		chattygot += send_req.verbosity;

	for (i = 0; i < send_req.nrefs; i++) {
		struct got_object_id *id;
		char *refname;
		struct got_pathlist_entry *new;

		if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
			if (err->code == GOT_ERR_PRIVSEP_PIPE)
				err = NULL;
			goto done;
		}
		if (imsg.hdr.type == GOT_IMSG_STOP)
			goto done;
		if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
			err = got_error(GOT_ERR_PRIVSEP_MSG);
			goto done;
		}
		datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
		if (datalen < sizeof(href)) {
			err = got_error(GOT_ERR_PRIVSEP_LEN);
			goto done;
		}
		memcpy(&href, imsg.data, sizeof(href));
		if (datalen - sizeof(href) < href.name_len) {
			err = got_error(GOT_ERR_PRIVSEP_LEN);
			goto done;
		}
		refname = malloc(href.name_len + 1);
		if (refname == NULL) {
			err = got_error_from_errno("malloc");
			goto done;
		}
		memcpy(refname, imsg.data + sizeof(href), href.name_len);
		refname[href.name_len] = '\0';

		/*
		 * Prevent sending of references that won't make any
		 * sense outside the local repository's context.
		 */
		if (strncmp(refname, "refs/got/", 9) == 0 ||
		    strncmp(refname, "refs/remotes/", 13) == 0) {
			err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
			    "%s", refname);
			goto done;
		}

		id = malloc(sizeof(*id));
		if (id == NULL) {
			free(refname);
			err = got_error_from_errno("malloc");
			goto done;
		}
		memcpy(id, &href.id, sizeof(*id));
		if (href.delete)
			err = got_pathlist_insert(&new, &delete_refs, refname, id);
		else
			err = got_pathlist_insert(&new, &refs, refname, id);
		if (err || new == NULL ) {
			free(refname);
			free(id);
			if (err)
				goto done;
		}

		imsg_free(&imsg);
	}

	err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
done:
	got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
	got_pathlist_free(&delete_refs, GOT_PATHLIST_FREE_ALL);
	if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
		err = got_error_from_errno("close");
	if (err != NULL && err->code != GOT_ERR_CANCELLED)  {
		fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
		got_privsep_send_error(&ibuf, err);
	}
	imsgbuf_clear(&ibuf);

	exit(0);
}