import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / gen / plock.c
blob6d74af0e2437767100e86d8adbe28d61463e12b7
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
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 * plock - lock "segments" in physical memory.
31 * Supports SVID-compatible plock, taking into account dynamically linked
32 * objects (such as shared libraries).
35 #include "lint.h"
36 #include <mtlib.h>
37 #include <sys/types.h>
38 #include <sys/mman.h>
39 #include <sys/lock.h>
40 #include <errno.h>
41 #include <stddef.h>
42 #include <unistd.h>
43 #include <thread.h>
44 #include <synch.h>
47 * Module-scope variables.
49 static int lock_state = 0; /* lock state */
50 static pid_t state_pid = -1; /* pid to which state belongs */
51 static mutex_t plock_lock = DEFAULTMUTEX;
54 * plock
56 int
57 plock(int op) /* desired operation */
59 int e; /* return value */
60 pid_t pid; /* current pid */
62 lmutex_lock(&plock_lock);
65 * Validate state of lock's. If parent has forked, then
66 * the lock state needs to be reset (children do not inherit
67 * memory locks, and thus do not inherit their state).
69 if ((pid = getpid()) != state_pid) {
70 lock_state = 0;
71 state_pid = pid;
75 * Dispatch on operation. Note: plock and its relatives depend
76 * upon "op" being bit encoded.
78 switch (op) {
81 * UNLOCK: remove all memory locks. Requires that some be set!
83 case UNLOCK:
84 if (lock_state == 0) {
85 errno = EINVAL;
86 lmutex_unlock(&plock_lock);
87 return (-1);
89 e = munlockall();
90 if (e) {
91 lmutex_unlock(&plock_lock);
92 return (-1);
93 } else {
94 lock_state = 0;
95 lmutex_unlock(&plock_lock);
96 return (0);
98 /*NOTREACHED*/
101 * TXTLOCK: locks text segments.
103 case TXTLOCK:
106 * If a text or process lock is already set, then fail.
108 if ((lock_state & TXTLOCK) || (lock_state & PROCLOCK)) {
109 errno = EINVAL;
110 lmutex_unlock(&plock_lock);
111 return (-1);
115 * Try to apply the lock(s). If a failure occurs,
116 * memcntl backs them out automatically.
118 e = memcntl(NULL, 0, MC_LOCKAS, (caddr_t)MCL_CURRENT,
119 PROC_TEXT|PRIVATE, 0);
120 if (!e)
121 lock_state |= TXTLOCK;
122 lmutex_unlock(&plock_lock);
123 return (e);
124 /*NOTREACHED*/
127 * DATLOCK: locks data segment(s), including the stack and all
128 * future growth in the address space.
130 case DATLOCK:
133 * If a data or process lock is already set, then fail.
135 if ((lock_state & DATLOCK) || (lock_state & PROCLOCK)) {
136 errno = EINVAL;
137 lmutex_unlock(&plock_lock);
138 return (-1);
142 * Try to lock the data and stack segments. On failure
143 * memcntl undoes the locks internally.
145 e = memcntl(NULL, 0, MC_LOCKAS, (caddr_t)MCL_CURRENT,
146 PROC_DATA|PRIVATE, 0);
147 if (e) {
148 lmutex_unlock(&plock_lock);
149 return (-1);
152 /* try to set a lock for all future mappings. */
153 e = mlockall(MCL_FUTURE);
156 * If failures have occurred, back out the locks
157 * and return failure.
159 if (e) {
160 e = errno;
161 (void) memcntl(NULL, 0, MC_UNLOCKAS,
162 (caddr_t)MCL_CURRENT, PROC_DATA|PRIVATE, 0);
163 errno = e;
164 lmutex_unlock(&plock_lock);
165 return (-1);
169 * Data, stack, and growth have been locked. Set state
170 * and return success.
172 lock_state |= DATLOCK;
173 lmutex_unlock(&plock_lock);
174 return (0);
175 /*NOTREACHED*/
178 * PROCLOCK: lock everything, and all future things as well.
179 * There should be nothing locked when this is called.
181 case PROCLOCK:
182 if (lock_state) {
183 errno = EINVAL;
184 lmutex_unlock(&plock_lock);
185 return (-1);
187 if (mlockall(MCL_CURRENT | MCL_FUTURE) == 0) {
188 lock_state |= PROCLOCK;
189 lmutex_unlock(&plock_lock);
190 return (0);
191 } else {
192 lmutex_unlock(&plock_lock);
193 return (-1);
195 /*NOTREACHED*/
198 * Invalid operation.
200 default:
201 errno = EINVAL;
202 lmutex_unlock(&plock_lock);
203 return (-1);
204 /*NOTREACHED*/