d26a70aaca4a2fcc3031e81c6ecca7e1bf0e535d
[rsync/rsync.git] / connection.c
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
7  * it under the terms of the GNU General Public License version 3 as
8  * published by the Free Software Foundation.
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  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, visit the http://fsf.org website.
17  */
18
19 #include "rsync.h"
20
21 /****************************************************************************
22 simple routine to do connection counting
23 ****************************************************************************/
24 int claim_connection(char *fname,int max_connections)
25 {
26         int fd, i;
27
28         if (max_connections <= 0)
29                 return 1;
30
31         fd = open(fname,O_RDWR|O_CREAT, 0600);
32
33         if (fd == -1) {
34                 return 0;
35         }
36
37         /* find a free spot */
38         for (i=0;i<max_connections;i++) {
39                 if (lock_range(fd, i*4, 4)) return 1;
40         }
41
42         /* only interested in open failures */
43         errno = 0;
44
45         close(fd);
46         return 0;
47 }