Martin gave his approval to use GPLv3 with this code.
[rsync/rsync.git] / connection.c
CommitLineData
0f78b815
WD
1/*
2 * Support the max connections option.
3 *
4 * Copyright (C) 1998 Andrew Tridgell
5 *
6 * This program is free software; you can redistribute it and/or modify
4fd842f9 7 * it under the terms of the GNU General Public License version 3 as
ba2133d6 8 * published by the Free Software Foundation.
0f78b815
WD
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
e7c67065 15 * You should have received a copy of the GNU General Public License along
4fd842f9 16 * with this program; if not, visit the http://fsf.org website.
0f78b815 17 */
0c515f17 18
0f78b815 19#include "rsync.h"
0c515f17
AT
20
21/****************************************************************************
22simple routine to do connection counting
23****************************************************************************/
24int claim_connection(char *fname,int max_connections)
25{
26 int fd, i;
0c515f17
AT
27
28 if (max_connections <= 0)
29 return 1;
8d72ef6e 30
0c515f17
AT
31 fd = open(fname,O_RDWR|O_CREAT, 0600);
32
33 if (fd == -1) {
34 return 0;
35 }
36
0c515f17
AT
37 /* find a free spot */
38 for (i=0;i<max_connections;i++) {
31593dd6 39 if (lock_range(fd, i*4, 4)) return 1;
0f78b815 40 }
0c515f17 41
8d72ef6e
AT
42 /* only interested in open failures */
43 errno = 0;
44
0c515f17 45 close(fd);
31593dd6 46 return 0;
0c515f17 47}