From 900b46d28139ff8c4fcc889c648b78a63cc16f3f Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 12 Jul 2016 20:02:10 -0700 Subject: [PATCH] Add GetUserFromPid() to libpsl And clean up some tests. --- src/libpsl-native/src/CMakeLists.txt | 1 + src/libpsl-native/src/getuserfrompid.cpp | 47 +++++++++++++++++++ src/libpsl-native/src/getuserfrompid.h | 9 ++++ src/libpsl-native/test/CMakeLists.txt | 1 + src/libpsl-native/test/test-getfileowner.cpp | 15 ++---- .../test/test-getuserfrompid.cpp | 13 +++++ src/libpsl-native/test/test-getusername.cpp | 6 +-- 7 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 src/libpsl-native/src/getuserfrompid.cpp create mode 100644 src/libpsl-native/src/getuserfrompid.h create mode 100644 src/libpsl-native/test/test-getuserfrompid.cpp diff --git a/src/libpsl-native/src/CMakeLists.txt b/src/libpsl-native/src/CMakeLists.txt index 8b5279f8e4..8b3d0f19a9 100644 --- a/src/libpsl-native/src/CMakeLists.txt +++ b/src/libpsl-native/src/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(psl-native SHARED getstat.cpp getpwuid.cpp + getuserfrompid.cpp getfileowner.cpp getcurrentprocessorid.cpp getusername.cpp diff --git a/src/libpsl-native/src/getuserfrompid.cpp b/src/libpsl-native/src/getuserfrompid.cpp new file mode 100644 index 0000000000..fd148a9ff2 --- /dev/null +++ b/src/libpsl-native/src/getuserfrompid.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include "pal.h" +#include "getfileowner.h" +#include "getpwuid.h" +#include "getuserfrompid.h" + +char* GetUserFromPid(pid_t pid) +{ + +#if defined(__linux__) + + // Get effective owner of pid from procfs + std::stringstream ss; + ss << "/proc/" << pid; + std::string path; + ss >> path; + + return GetFileOwner(path.c_str()); + +#elif defined(__APPLE__) && defined(__MACH__) + + // Get effective owner of pid from sysctl + struct kinfo_proc oldp; + size_t oldlenp = sizeof(oldp); + int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; + u_int namelen = sizeof(name)/sizeof(int); + + // Read-only query + int ret = sysctl(name, namelen, &oldp, &oldlenp, NULL, 0); + if (ret != 0 || oldlenp == 0) + { + errno = ERROR_GEN_FAILURE; + return NULL; + } + + return GetPwUid(oldp.kp_eproc.e_ucred.cr_uid); + +#else + + return NULL; + +#endif + +} diff --git a/src/libpsl-native/src/getuserfrompid.h b/src/libpsl-native/src/getuserfrompid.h new file mode 100644 index 0000000000..c019f475fc --- /dev/null +++ b/src/libpsl-native/src/getuserfrompid.h @@ -0,0 +1,9 @@ +#pragma once + +#include "pal.h" + +PAL_BEGIN_EXTERNC + +char* GetUserFromPid(pid_t pid); + +PAL_END_EXTERNC diff --git a/src/libpsl-native/test/CMakeLists.txt b/src/libpsl-native/test/CMakeLists.txt index 1ab2bfd3e4..ed4440d2a3 100644 --- a/src/libpsl-native/test/CMakeLists.txt +++ b/src/libpsl-native/test/CMakeLists.txt @@ -3,6 +3,7 @@ add_subdirectory(googletest) add_executable(psl-native-test test-getfileowner.cpp test-locale.cpp + test-getuserfrompid.cpp test-getcurrentprocessid.cpp test-getusername.cpp test-getcomputername.cpp diff --git a/src/libpsl-native/test/test-getfileowner.cpp b/src/libpsl-native/test/test-getfileowner.cpp index 77f1e2fc8c..275c63ed11 100644 --- a/src/libpsl-native/test/test-getfileowner.cpp +++ b/src/libpsl-native/test/test-getfileowner.cpp @@ -7,25 +7,18 @@ #include #include "getfileowner.h" -using namespace std; - -//! Test fixture for GetFileOwner -class GetFileOwnerTest : public ::testing::Test +TEST(GetFileOwnerTest, CanGetOwnerOfRoot) { -}; - -TEST_F(GetFileOwnerTest, CanGetOwnerOfRoot) -{ - ASSERT_STREQ(GetFileOwner("/"), "root"); + EXPECT_STREQ(GetFileOwner("/"), "root"); } -TEST_F(GetFileOwnerTest, CannotGetOwnerOfFakeFile) +TEST(GetFileOwnerTest, CannotGetOwnerOfFakeFile) { EXPECT_STREQ(GetFileOwner("SomeMadeUpFileNameThatDoesNotExist"), NULL); EXPECT_EQ(errno, ERROR_FILE_NOT_FOUND); } -TEST_F(GetFileOwnerTest, ReturnsNullForNullInput) +TEST(GetFileOwnerTest, ReturnsNullForNullInput) { EXPECT_STREQ(GetFileOwner(NULL), NULL); EXPECT_EQ(errno, ERROR_INVALID_PARAMETER); diff --git a/src/libpsl-native/test/test-getuserfrompid.cpp b/src/libpsl-native/test/test-getuserfrompid.cpp new file mode 100644 index 0000000000..ca6d866c63 --- /dev/null +++ b/src/libpsl-native/test/test-getuserfrompid.cpp @@ -0,0 +1,13 @@ +//! @file test-getuserfrompid.cpp +//! @author Andrew Schwartzmeyer +//! @brief Unit tests for GetUserFromPid + +#include +#include +#include "getuserfrompid.h" + +TEST(GetUserFromPid, Success) +{ + char* expected = getpwuid(geteuid())->pw_name; + EXPECT_STREQ(GetUserFromPid(getpid()), expected); +} diff --git a/src/libpsl-native/test/test-getusername.cpp b/src/libpsl-native/test/test-getusername.cpp index 2ccb29f68a..3f4751276f 100644 --- a/src/libpsl-native/test/test-getusername.cpp +++ b/src/libpsl-native/test/test-getusername.cpp @@ -2,9 +2,6 @@ //! @author Andrew Schwartzmeyer //! @brief Unit tests for GetUserName -#include -#include -#include #include #include #include "getusername.h" @@ -12,6 +9,5 @@ TEST(GetUserName, Success) { char* expected = getpwuid(geteuid())->pw_name; - ASSERT_TRUE(expected != NULL); - ASSERT_EQ(GetUserName(), std::string(expected)); + EXPECT_STREQ(GetUserName(), expected); }