2 Copyright (C) Andrew Tridgell 1998
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* support the max connections option */
22 int yield_connection(char *fname, int max_connections)
25 pid_t mypid=getpid(), pid=0;
27 if (max_connections <= 0)
30 fd = open(fname,O_RDWR);
32 rprintf(FERROR,"Couldn't open lock file %s (%s)\n",fname,strerror(errno));
37 rprintf(FERROR,"failed to lock %s\n", fname);
42 /* find the right spot */
43 for (i=0;i<max_connections;i++) {
44 if (read(fd, &pid, sizeof(pid)) != sizeof(pid)) {
49 if (pid == mypid) break;
52 if (i == max_connections) {
53 rprintf(FERROR,"Entry not found in lock file %s\n",fname);
62 if (lseek(fd,i*sizeof(pid),SEEK_SET) != i*sizeof(pid) ||
63 write(fd, &pid,sizeof(pid)) != sizeof(pid)) {
64 rprintf(FERROR,"Couldn't update lock file %s (%s)\n",fname,strerror(errno));
76 /****************************************************************************
77 simple routine to do connection counting
78 ****************************************************************************/
79 int claim_connection(char *fname,int max_connections)
84 if (max_connections <= 0)
87 fd = open(fname,O_RDWR|O_CREAT, 0600);
94 rprintf(FERROR,"failed to lock %s\n", fname);
99 /* find a free spot */
100 for (i=0;i<max_connections;i++) {
101 if (read(fd,&pid,sizeof(pid)) != sizeof(pid)) break;
102 if (pid == 0 || !process_exists(pid)) break;
105 if (i == max_connections) {
113 if (lseek(fd,i*sizeof(pid),SEEK_SET) != i*sizeof(pid) ||
114 write(fd, &pid,sizeof(pid)) != sizeof(pid)) {