8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libdscfg / common / cfg_lockdlck.c
blob14f7cb4b1df53c7252493c4f7ba8e276dd6ec171
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <signal.h>
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <strings.h>
34 #include <errno.h>
35 #include <assert.h>
37 #include "cfg_impl.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;
46 void
47 cfg_lfinit()
49 local_lockfd = open(CFG_RDEV_LOCKFILE, O_RDWR|O_CREAT, 0644);
50 local_lockfda = open(CFG_RDEV_LOCKFILE, O_RDWR|O_APPEND, 0644);
53 int
54 cfg_filelock(int segment, int flag)
56 struct flock lk;
57 struct stat sb;
58 pid_t pid = 0;
59 off_t off = segment_off(segment);
60 int rc;
62 while (fstat(local_lockfd, &sb) == -1 && errno == EINTR)
64 if (sb.st_size < off + sizeof (pid_t)) {
65 if ((flag&O_CREAT) == 0)
66 return (CFG_LF_EOF);
67 write(local_lockfda, &pid, sizeof (pid_t));
69 bzero(&lk, sizeof (lk));
70 lk.l_type = F_WRLCK;
71 lk.l_whence = SEEK_SET;
72 lk.l_start = off;
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);
80 return (CFG_LF_OKAY);
84 int
85 cfg_fileunlock(int segment)
87 struct flock lk;
88 off_t off = segment_off(segment);
90 bzero(&lk, sizeof (lk));
91 lk.l_type = F_UNLCK;
92 lk.l_whence = SEEK_SET;
93 lk.l_start = off;
94 lk.l_len = (off_t)sizeof (pid_t);
96 while (fcntl(local_lockfd, F_SETLK, &lk) < 0 && errno == EINTR)
98 return (1);
101 void
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));
109 void
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));
117 void
118 cfg_enterpid()
120 int i;
121 pid_t pid;
123 for (i = 0; ; i++) {
124 if (cfg_filelock(i, O_CREAT) == CFG_LF_OKAY) {
125 cfg_readpid(i, &pid);
126 if (pid != (pid_t)0) {
127 cfg_fileunlock(i);
128 continue;
130 pid = getpid();
131 cfg_writepid(i, pid);
132 break;