4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <sys/types.h>
38 #include "cfg_lockd.h"
41 #define segment_off(s) ((off_t)(s) * sizeof (pid_t))
43 static int local_lockfd
;
44 static int local_lockfda
;
49 local_lockfd
= open(CFG_RDEV_LOCKFILE
, O_RDWR
|O_CREAT
, 0644);
50 local_lockfda
= open(CFG_RDEV_LOCKFILE
, O_RDWR
|O_APPEND
, 0644);
54 cfg_filelock(int segment
, int flag
)
59 off_t off
= segment_off(segment
);
62 while (fstat(local_lockfd
, &sb
) == -1 && errno
== EINTR
)
64 if (sb
.st_size
< off
+ sizeof (pid_t
)) {
65 if ((flag
&O_CREAT
) == 0)
67 write(local_lockfda
, &pid
, sizeof (pid_t
));
69 bzero(&lk
, sizeof (lk
));
71 lk
.l_whence
= SEEK_SET
;
73 lk
.l_len
= (off_t
)sizeof (pid_t
);
75 while ((rc
= fcntl(local_lockfd
, F_SETLK
, &lk
)) < 0 && errno
== EINTR
)
77 if (rc
== -1 && errno
== EAGAIN
)
78 return (CFG_LF_AGAIN
);
85 cfg_fileunlock(int segment
)
88 off_t off
= segment_off(segment
);
90 bzero(&lk
, sizeof (lk
));
92 lk
.l_whence
= SEEK_SET
;
94 lk
.l_len
= (off_t
)sizeof (pid_t
);
96 while (fcntl(local_lockfd
, F_SETLK
, &lk
) < 0 && errno
== EINTR
)
102 cfg_readpid(int segment
, pid_t
*pidp
)
104 off_t off
= segment_off(segment
);
105 lseek(local_lockfd
, off
, SEEK_SET
);
106 read(local_lockfd
, pidp
, sizeof (pid_t
));
110 cfg_writepid(int segment
, pid_t pid
)
112 off_t off
= segment_off(segment
);
113 lseek(local_lockfd
, off
, SEEK_SET
);
114 write(local_lockfd
, &pid
, sizeof (pid_t
));
124 if (cfg_filelock(i
, O_CREAT
) == CFG_LF_OKAY
) {
125 cfg_readpid(i
, &pid
);
126 if (pid
!= (pid_t
)0) {
131 cfg_writepid(i
, pid
);