mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add GetUserFromPid() to libpsl
And clean up some tests.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
add_library(psl-native SHARED
|
||||
getstat.cpp
|
||||
getpwuid.cpp
|
||||
getuserfrompid.cpp
|
||||
getfileowner.cpp
|
||||
getcurrentprocessorid.cpp
|
||||
getusername.cpp
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <errno.h>
|
||||
#include <sys/sysctl.h>
|
||||
#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
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "pal.h"
|
||||
|
||||
PAL_BEGIN_EXTERNC
|
||||
|
||||
char* GetUserFromPid(pid_t pid);
|
||||
|
||||
PAL_END_EXTERNC
|
||||
@@ -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
|
||||
|
||||
@@ -7,25 +7,18 @@
|
||||
#include <unistd.h>
|
||||
#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);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//! @file test-getuserfrompid.cpp
|
||||
//! @author Andrew Schwartzmeyer <andschwa@microsoft.com>
|
||||
//! @brief Unit tests for GetUserFromPid
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <pwd.h>
|
||||
#include "getuserfrompid.h"
|
||||
|
||||
TEST(GetUserFromPid, Success)
|
||||
{
|
||||
char* expected = getpwuid(geteuid())->pw_name;
|
||||
EXPECT_STREQ(GetUserFromPid(getpid()), expected);
|
||||
}
|
||||
@@ -2,9 +2,6 @@
|
||||
//! @author Andrew Schwartzmeyer <andschwa@microsoft.com>
|
||||
//! @brief Unit tests for GetUserName
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unistd.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <pwd.h>
|
||||
#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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user