summaryrefslogtreecommitdiffstats
path: root/sys_posix.c
blob: 356e86a71f07eba259201eceb4b11204a4a5ffc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Richard P. Curnow  1997-2003
 * Copyright (C) John G. Hasler  2009
 * Copyright (C) Miroslav Lichvar  2009-2012, 2014-2018
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 **********************************************************************

  =======================================================================

  This module is for POSIX compliant operating systems.

  */

#include "config.h"

#include "sysincl.h"

#include <sys/utsname.h>

#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
#include <pthread.h>
#include <sched.h>
#endif

#if defined(HAVE_MLOCKALL)
#include <sys/mman.h>
#endif
#if defined(HAVE_SETRLIMIT_MEMLOCK)
#include <sys/resource.h>
#endif

#include "sys_posix.h"
#include "conf.h"
#include "local.h"
#include "logging.h"
#include "util.h"

/* ================================================== */

#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
/* Install SCHED_FIFO real-time scheduler with specified priority */
void
SYS_Posix_SetScheduler(int priority)
{
  struct sched_param sched;
  int pmax, pmin;

  if (priority < 1 || priority > 99)
    LOG_FATAL("Bad scheduler priority: %d", priority);

  sched.sched_priority = priority;
  pmax = sched_get_priority_max(SCHED_FIFO);
  pmin = sched_get_priority_min(SCHED_FIFO);
  if (priority > pmax) {
    sched.sched_priority = pmax;
  } else if (priority < pmin) {
    sched.sched_priority = pmin;
  }

  if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched) < 0) {
    LOG(LOGS_ERR, "pthread_setschedparam() failed");
  } else {
    DEBUG_LOG("Enabled SCHED_FIFO with priority %d", sched.sched_priority);
  }
}
#endif /* HAVE_PTHREAD_SETSCHEDPARAM  */

/* ================================================== */

#if defined(HAVE_MLOCKALL)
/* Lock the process into RAM so that it will never be swapped out */
void
SYS_Posix_MemLockAll(void)
{
#if defined(HAVE_SETRLIMIT_MEMLOCK)
  struct rlimit rlim;

  /* Ensure we can reserve as much as we need */
  rlim.rlim_max = RLIM_INFINITY;
  rlim.rlim_cur = RLIM_INFINITY;
  if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
    LOG(LOGS_ERR, "setrlimit() failed");
    return;
  }
#endif

  if (mlockall(MCL_CURRENT|MCL_FUTURE) < 0) {
    LOG(LOGS_ERR, "mlockall() failed");
  } else {
    DEBUG_LOG("Successfully locked into RAM");
  }
}
#endif /* HAVE_MLOCKALL */