tests: Don't assume that unprivileged uids are bound by RLIMIT_NPROC

Some CI platforms invoke tests as euid != 0, but with capabilities that
include CAP_SYS_RESOURCE and/or CAP_SYS_ADMIN. If we detect this,
we can't test what happens if our RLIMIT_NPROC is too low to create a
thread, because RLIMIT_NPROC is bypassed in these cases.

Signed-off-by: Simon McVittie <smcv@collabora.com>
Fixes: https://gitlab.gnome.org/GNOME/glib/issues/2029
This commit is contained in:
Simon McVittie 2020-02-13 12:54:46 +00:00
parent d6886d898f
commit 6e28ce2860

View File

@ -138,12 +138,6 @@ test_thread4 (void)
GError *error;
gint ret;
/* Linux CAP_SYS_RESOURCE overrides RLIMIT_NPROC, and probably similar
* things are true on other systems.
*/
if (getuid () == 0 || geteuid () == 0)
return;
getrlimit (RLIMIT_NPROC, &nl);
nl.rlim_cur = 1;
@ -152,9 +146,26 @@ test_thread4 (void)
error = NULL;
thread = g_thread_try_new ("a", thread1_func, NULL, &error);
if (thread != NULL)
{
gpointer result;
/* Privileged processes might be able to create new threads even
* though the rlimit is too low. There isn't much we can do about
* this; we just can't test this failure mode in this situation. */
g_test_skip ("Unable to test g_thread_try_new() failing with EAGAIN "
"while privileged (CAP_SYS_RESOURCE, CAP_SYS_ADMIN or "
"euid 0?)");
result = g_thread_join (thread);
g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
}
else
{
g_assert (thread == NULL);
g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
g_error_free (error);
}
if ((ret = prlimit (getpid (), RLIMIT_NPROC, &ol, NULL)) != 0)
g_error ("resetting RLIMIT_NPROC failed: %s", g_strerror (errno));