xsecurelock  1.7.0
XSecureLock is an X11 screen lock utility.
mlock_page.h
Go to the documentation of this file.
1 /*
2 Copyright 2014 Google Inc. All rights reserved.
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16 
17 // This is a helper to support mlock on non page aligned data. It will simply
18 // lock the whole page covering the data.
19 
20 #ifndef MLOCK_PAGE_H
21 #define MLOCK_PAGE_H
22 
23 #include <limits.h>
24 #include <stdint.h>
25 #include <sys/mman.h>
26 #include <unistd.h>
27 
28 #if defined(_POSIX_MEMLOCK_RANGE)
29 #ifndef PAGESIZE
30 #define PAGESIZE sysconf(_SC_PAGESIZE)
31 #endif
32 
36 #define MLOCK_PAGE(ptr, size) \
37  mlock((void *)((((uintptr_t)(ptr)) / (uintptr_t)PAGESIZE) * \
38  (uintptr_t)PAGESIZE), \
39  (((uintptr_t)(ptr) + (size)-1) / (uintptr_t)PAGESIZE - \
40  ((uintptr_t)(ptr)) / (uintptr_t)PAGESIZE + 1) * \
41  (uintptr_t)PAGESIZE)
42 #elif _POSIX_MEMLOCK > 0
43 #warning mlock() is unavailable. More memory will be locked than necessary.
44 
48 #define MLOCK_PAGE(ptr, size) mlockall(MCL_CURRENT)
49 #else
50 #warning mlock() and mlockall() are unavailable. Passwords might leak to swap.
51 
55 #define MLOCK_PAGE(ptr, size) 0
56 #endif
57 
58 #endif