]>
Commit | Line | Data |
---|---|---|
c5c80ef4 SF |
1 | /* |
2 | chronyd/chronyc - Programs for keeping computer clocks accurate. | |
3 | ||
4 | ********************************************************************** | |
5 | * Copyright (C) Richard P. Curnow 1997-2003 | |
6 | * Copyright (C) John G. Hasler 2009 | |
7 | * Copyright (C) Miroslav Lichvar 2009-2012, 2014-2018 | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of version 2 of the GNU General Public License as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
21 | * | |
22 | ********************************************************************** | |
23 | ||
24 | ======================================================================= | |
25 | ||
26 | This module is for POSIX compliant operating systems. | |
27 | ||
28 | */ | |
29 | ||
30 | #include "config.h" | |
31 | ||
32 | #include "sysincl.h" | |
33 | ||
34 | #include <sys/utsname.h> | |
35 | ||
36 | #if defined(HAVE_PTHREAD_SETSCHEDPARAM) | |
37 | #include <pthread.h> | |
38 | #include <sched.h> | |
39 | #endif | |
40 | ||
41 | #if defined(HAVE_MLOCKALL) | |
42 | #include <sys/mman.h> | |
43 | #endif | |
44 | #if defined(HAVE_SETRLIMIT_MEMLOCK) | |
45 | #include <sys/resource.h> | |
46 | #endif | |
47 | ||
48 | #include "sys_posix.h" | |
49 | #include "conf.h" | |
50 | #include "local.h" | |
51 | #include "logging.h" | |
52 | #include "util.h" | |
53 | ||
54 | /* ================================================== */ | |
55 | ||
56 | #if defined(HAVE_PTHREAD_SETSCHEDPARAM) | |
57 | /* Install SCHED_FIFO real-time scheduler with specified priority */ | |
58 | void | |
59 | SYS_Posix_SetScheduler(int priority) | |
60 | { | |
61 | struct sched_param sched; | |
62 | int pmax, pmin; | |
63 | ||
64 | if (priority < 1 || priority > 99) | |
65 | LOG_FATAL("Bad scheduler priority: %d", priority); | |
66 | ||
67 | sched.sched_priority = priority; | |
68 | pmax = sched_get_priority_max(SCHED_FIFO); | |
69 | pmin = sched_get_priority_min(SCHED_FIFO); | |
70 | if (priority > pmax) { | |
71 | sched.sched_priority = pmax; | |
72 | } else if (priority < pmin) { | |
73 | sched.sched_priority = pmin; | |
74 | } | |
75 | ||
76 | if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched) < 0) { | |
77 | LOG(LOGS_ERR, "pthread_setschedparam() failed"); | |
78 | } else { | |
79 | DEBUG_LOG("Enabled SCHED_FIFO with priority %d", sched.sched_priority); | |
80 | } | |
81 | } | |
82 | #endif /* HAVE_PTHREAD_SETSCHEDPARAM */ | |
83 | ||
84 | /* ================================================== */ | |
85 | ||
86 | #if defined(HAVE_MLOCKALL) | |
87 | /* Lock the process into RAM so that it will never be swapped out */ | |
88 | void | |
89 | SYS_Posix_MemLockAll(void) | |
90 | { | |
91 | #if defined(HAVE_SETRLIMIT_MEMLOCK) | |
92 | struct rlimit rlim; | |
93 | ||
94 | /* Ensure we can reserve as much as we need */ | |
95 | rlim.rlim_max = RLIM_INFINITY; | |
96 | rlim.rlim_cur = RLIM_INFINITY; | |
97 | if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) { | |
98 | LOG(LOGS_ERR, "setrlimit() failed"); | |
99 | return; | |
100 | } | |
101 | #endif | |
102 | ||
103 | if (mlockall(MCL_CURRENT|MCL_FUTURE) < 0) { | |
104 | LOG(LOGS_ERR, "mlockall() failed"); | |
105 | } else { | |
106 | DEBUG_LOG("Successfully locked into RAM"); | |
107 | } | |
108 | } | |
109 | #endif /* HAVE_MLOCKALL */ |