Skip to content

Commit 4099ee2

Browse files
committed
rtapi: gate setrlimit(MEMLOCK) warning on CAP_IPC_LOCK
The previous DBG misled on the rootless path where the failure is expected and harmless: CAP_IPC_LOCK lets mlockall ignore RLIMIT_MEMLOCK regardless. Promote to WARN, fire only when the raise fails and CAP_IPC_LOCK is not held, i.e. when mlockall is actually going to fail.
1 parent 875e5f7 commit 4099ee2

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

src/rtapi/uspace_rtapi_main.cc

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -846,16 +846,27 @@ static void signal_handler(int sig, siginfo_t * /*si*/, void * /*uctx*/) {
846846
const static size_t PRE_ALLOC_SIZE = 1024 * 1024 * 32;
847847
const static struct rlimit unlimited = {RLIM_INFINITY, RLIM_INFINITY};
848848
static void configure_memory() {
849-
// Best-effort raise of the soft cap to the hard cap. Fails on
850-
// unprivileged processes without CAP_SYS_RESOURCE or without a
851-
// matching setrlimit; CAP_IPC_LOCK alone lets mlockall succeed
852-
// regardless of the rlimit, so ignoring the failure is safe.
849+
// Best-effort raise of the soft cap to the hard cap. Needs
850+
// CAP_SYS_RESOURCE; absent that, CAP_IPC_LOCK lets mlockall succeed
851+
// regardless of the rlimit. Only warn when neither path is open,
852+
// i.e. mlockall is actually going to fail.
853+
bool have_ipc_lock = false;
854+
#ifdef __linux__
855+
cap_t caps = cap_get_proc();
856+
if (caps) {
857+
cap_flag_value_t v;
858+
if (cap_get_flag(caps, CAP_IPC_LOCK, CAP_EFFECTIVE, &v) == 0)
859+
have_ipc_lock = (v == CAP_SET);
860+
cap_free(caps);
861+
}
862+
#endif
853863
struct rlimit limit;
854864
if (getrlimit(RLIMIT_MEMLOCK, &limit) == 0) {
855865
limit.rlim_cur = limit.rlim_max;
856-
if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0)
857-
rtapi_print_msg(RTAPI_MSG_DBG,
858-
"setrlimit(RLIMIT_MEMLOCK) failed: %s\n", strerror(errno));
866+
if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0 && !have_ipc_lock)
867+
rtapi_print_msg(RTAPI_MSG_WARN,
868+
"setrlimit(RLIMIT_MEMLOCK) failed and CAP_IPC_LOCK not held: %s. "
869+
"mlockall will likely fail.\n", strerror(errno));
859870
}
860871

861872
int res = mlockall(MCL_CURRENT | MCL_FUTURE);

0 commit comments

Comments
 (0)