Skip to content

Commit

Permalink
Add support for other systems
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin Halliday <[email protected]>
  • Loading branch information
ghalliday committed Oct 8, 2024
1 parent 308ea36 commit f0c3a45
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
16 changes: 10 additions & 6 deletions system/jlib/jdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,24 +835,28 @@ static const char * queryCGroup()
{
auto processLine = [](size_t len, const char * ln)
{
//Note ln points at the start of the line, but the line is not null terminated
switch (*ln)
{
case '0':
if (strncmp(ln, "0::/", 4) == 0)
{
//Format is 0::/<cgroup>. Note ln points at the start of the line, but the line is not terminated
cgroup.set(ln+4, len-4);
//Format is 0::/<cgroup>
//If not running in a container the "cgroup" may be something like user.slice/user-1000.slice/[email protected]/....
//If so ignore because it is not a real cgroup
if (!memchr(ln+4, '/', len-4))
cgroup.set(ln+4, len-4);
}
break;
}
//Some systems with version 1 cgroups have <n>:cpu,cpuacct:/<cgroup>
const char * match = (const char *)jmemmem(len, ln, 14, ":cpu,cpuacct:/");
if (match)
cgroup.set(match+14, (ln + len) - (match + 14));
};

processLines(contents, processLine);
}
//If not running in a container the "cgroup" may be something like user.slice/user-1000.slice/[email protected]/....
//If so clear the cached value because it is not a real cgroup
if (cgroup && strchr(cgroup, '/'))
cgroup.clear();
gatheredGroup = true;
}
}
Expand Down
28 changes: 28 additions & 0 deletions system/jlib/jstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2955,3 +2955,31 @@ const char * stristr (const char *haystack, const char *needle)
}
return nullptr;
}


const void * jmemmem(size_t lenHaystack, const void * haystack, size_t lenNeedle, const void *needle)
{
if (lenNeedle == 0)
return haystack;

if (lenHaystack < lenNeedle)
return nullptr;

const char * search = (const char *)needle;
char first = *search;
if (lenNeedle == 1)
return memchr(haystack, first, lenHaystack);

const char * buffer = (const char *)haystack;
for (size_t i = 0; i <= lenHaystack - lenNeedle; i++)
{
//Special case the first character to avoid a function call each iteration.
if (buffer[i] == first)
{
if (memcmp(buffer + i + 1, search + 1, lenNeedle-1) == 0)
return buffer + i;
}
}

return nullptr;
}
3 changes: 3 additions & 0 deletions system/jlib/jstring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,7 @@ extern jlib_decl void getSnakeCase(StringBuffer & out, const char * camelValue);
//If the string has any characters, ensure the last character matches the separator
extern jlib_decl void ensureSeparator(StringBuffer & out, char separator);

//Search for one block of bytes within another block of bytes - memmem is not standard, so we provide our own
extern jlib_decl const void * jmemmem(size_t lenHaystack, const void * haystack, size_t lenNeedle, const void *needle);

#endif
16 changes: 16 additions & 0 deletions testing/unittests/jlibtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4631,6 +4631,7 @@ class JLibStringTest : public CppUnit::TestFixture
public:
CPPUNIT_TEST_SUITE(JLibStringTest);
CPPUNIT_TEST(testStristr);
CPPUNIT_TEST(testMemMem);
CPPUNIT_TEST_SUITE_END();

void testStristr()
Expand All @@ -4648,6 +4649,21 @@ class JLibStringTest : public CppUnit::TestFixture
CPPUNIT_ASSERT_EQUAL_STR(stristr("", "ABC"), "");
CPPUNIT_ASSERT_EQUAL_STR(stristr("ABC", ""), "");
}

void testMemMem()
{
constexpr const char * haystack = "abcdefghijklmnopqrstuvwxyz";
CPPUNIT_ASSERT_EQUAL((const void*)(haystack), jmemmem(10, haystack, 0, nullptr));
CPPUNIT_ASSERT_EQUAL((const void*)(haystack), jmemmem(10, haystack, 3, "abc"));
CPPUNIT_ASSERT_EQUAL((const void*)(haystack), jmemmem(3, haystack, 3, "abc"));
CPPUNIT_ASSERT_EQUAL((const void*)nullptr, jmemmem(2, haystack, 3, "abc"));
CPPUNIT_ASSERT_EQUAL((const void*)(haystack+7), jmemmem(10, haystack, 3, "hij"));
CPPUNIT_ASSERT_EQUAL((const void*)nullptr, jmemmem(10, haystack, 3, "ijk"));
CPPUNIT_ASSERT_EQUAL((const void*)(haystack+8), jmemmem(10, haystack, 1, "i"));
CPPUNIT_ASSERT_EQUAL((const void*)(nullptr), jmemmem(8, haystack, 1, "i"));
CPPUNIT_ASSERT_EQUAL((const void*)(nullptr), jmemmem(9, haystack, 2, "ij"));
CPPUNIT_ASSERT_EQUAL((const void*)(haystack+8), jmemmem(10, haystack, 2, "ij"));
}
};

CPPUNIT_TEST_SUITE_REGISTRATION( JLibStringTest );
Expand Down

0 comments on commit f0c3a45

Please sign in to comment.