Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FreeBSD build failures. Update tests for FreeBSD #92

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libpsl-native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,relro,-z,now")
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl")
elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems strange to pass -Wl with no arg. Of course the oddity was just duplicated from the Darwin case (where it exists in the first public version of this file).

It seems to me we ought to just mimic the Linux behaviour here, e.g. make the existing case above if (${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it unusual too, but opted to include it as I assumed if it was important enough for "Darwin" to have it then so should "FreeBSD". I will go ahead and add "FreeBSD" to the "Linux" case as suggested. Thanks!

endif()

set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/../powershell-unix")
Expand Down
7 changes: 7 additions & 0 deletions src/libpsl-native/src/getcurrentthreadid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <sys/syscall.h>
#include <pthread.h>

#if __FreeBSD__
#include <pthread_np.h>
#endif

pid_t GetCurrentThreadId()
{
pid_t tid = 0;
Expand All @@ -17,6 +21,9 @@ pid_t GetCurrentThreadId()
uint64_t tid64;
pthread_threadid_np(NULL, &tid64);
tid = (pid_t)tid64;
#elif defined(__FreeBSD__)
tid = pthread_getthreadid_np();
return (int)tid;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast is superfluous - this function returns pid_t, which is the type of tid. Probably want in the line above tid = (pid_t)pthread_getthreadid_np();, or just avoid explicit casts altogether.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one...I tried my best here to follow what is done under dotNET for pthread_getthreadid_np() but found it inconsistent. I can make changes both to this PR and open PRs in runtime to correct the usage it in order to avoid any information loss due to casting.

#endif
return tid;
}
17 changes: 11 additions & 6 deletions src/libpsl-native/src/getppid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#include <sys/sysctl.h>
#endif

#if __FreeBSD__
#include <sys/types.h>
#include <sys/sysctl.h>
#endif

//! @brief GetPPid returns the parent process id for a process
//!
//! GetPPid
Expand All @@ -26,20 +31,20 @@
//!
pid_t GetPPid(pid_t pid)
{

#if defined(__APPLE__) && defined(__MACH__)

#if defined (__APPLE__) && defined(__MACH__) || defined(__FreeBSD__)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roll this commit into previous?

const pid_t PIDUnknown = UINT_MAX;
struct kinfo_proc info;
size_t length = sizeof(struct kinfo_proc);
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
return PIDUnknown;
if (length == 0)
return PIDUnknown;

#if defined (__APPLE__) && defined(__MACH__)
return info.kp_eproc.e_ppid;

#elif defined(__FreeBSD__)
return info.ki_ppid;
#endif
#else

return UINT_MAX;
Expand Down
1 change: 1 addition & 0 deletions src/libpsl-native/src/getuserfrompid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#if __FreeBSD__
#include <sys/user.h>
#include <sys/sysctl.h>
#endif

char* GetUserFromPid(pid_t pid)
Expand Down
22 changes: 11 additions & 11 deletions src/libpsl-native/test/test-getcommonlstat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TEST(GetCommonLStat, GetOwnerIdOfRoot)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %u /", "r");
#else
p = popen("/usr/bin/stat -c %u /", "r");
Expand All @@ -56,7 +56,7 @@ TEST(GetCommonLStat, GetGroupId)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %g /", "r");
#else
p = popen("/usr/bin/stat -c %g /", "r");
Expand All @@ -73,7 +73,7 @@ TEST(GetCommonLStat, GetInodeNumber)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %i /", "r");
#else
p = popen("/usr/bin/stat -c %i /", "r");
Expand All @@ -90,7 +90,7 @@ TEST(GetCommonLStat, GetSize)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %z /", "r");
#else
p = popen("/usr/bin/stat -c %s /", "r");
Expand All @@ -107,7 +107,7 @@ TEST(GetCommonLStat, GetBlockSize)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %k /", "r");
#else
p = popen("/usr/bin/stat -c %o /", "r");
Expand All @@ -124,7 +124,7 @@ TEST(GetCommonLStat, GetBlockCount)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %b /", "r");
#else
p = popen("/usr/bin/stat -c %b /", "r");
Expand All @@ -141,7 +141,7 @@ TEST(GetCommonLStat, GetLinkCount)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %l /", "r");
#else
p = popen("/usr/bin/stat -c %h /", "r");
Expand All @@ -158,7 +158,7 @@ TEST(GetCommonLStat, GetDeviceId)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %d /", "r");
#else
p = popen("/usr/bin/stat -c %d /", "r");
Expand All @@ -175,7 +175,7 @@ TEST(GetCommonLStat, GetATime)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %a /", "r");
#else
p = popen("/usr/bin/stat -c %X /", "r");
Expand All @@ -192,7 +192,7 @@ TEST(GetCommonLStat, GetMTime)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %m /", "r");
#else
p = popen("/usr/bin/stat -c %Y /", "r");
Expand All @@ -209,7 +209,7 @@ TEST(GetCommonLStat, GetCTime)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %c /", "r");
#else
p = popen("/usr/bin/stat -c %Z /", "r");
Expand Down
24 changes: 12 additions & 12 deletions src/libpsl-native/test/test-getcommonstat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TEST(GetCommonStat, GetOwnerIdOfRoot)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %u /", "r");
#else
p = popen("/usr/bin/stat -c %u /", "r");
Expand All @@ -58,7 +58,7 @@ TEST(GetCommonStat, GetGroupId)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %g /", "r");
#else
p = popen("/usr/bin/stat -c %g /", "r");
Expand All @@ -75,7 +75,7 @@ TEST(GetCommonStat, GetInodeNumber)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %i /", "r");
#else
p = popen("/usr/bin/stat -c %i /", "r");
Expand All @@ -93,7 +93,7 @@ TEST(GetCommonStat, GetMode)
FILE *p;
CommonStat cs;
unsigned int mode = -1;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %p /", "r");
int result = fscanf(p, "%o", &mode);
#else
Expand All @@ -110,7 +110,7 @@ TEST(GetCommonStat, GetSize)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %z /", "r");
#else
p = popen("/usr/bin/stat -c %s /", "r");
Expand All @@ -127,7 +127,7 @@ TEST(GetCommonStat, GetBlockSize)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %k /", "r");
#else
p = popen("/usr/bin/stat -c %o /", "r");
Expand All @@ -144,7 +144,7 @@ TEST(GetCommonStat, GetBlockCount)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %b /", "r");
#else
p = popen("/usr/bin/stat -c %b /", "r");
Expand All @@ -161,7 +161,7 @@ TEST(GetCommonStat, GetLinkCount)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %l /", "r");
#else
p = popen("/usr/bin/stat -c %h /", "r");
Expand All @@ -178,7 +178,7 @@ TEST(GetCommonStat, GetDeviceId)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %d /", "r");
#else
p = popen("/usr/bin/stat -c %d /", "r");
Expand All @@ -195,7 +195,7 @@ TEST(GetCommonStat, GetATime)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %a /", "r");
#else
p = popen("/usr/bin/stat -c %X /", "r");
Expand All @@ -212,7 +212,7 @@ TEST(GetCommonStat, GetMTime)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %m /", "r");
#else
p = popen("/usr/bin/stat -c %Y /", "r");
Expand All @@ -229,7 +229,7 @@ TEST(GetCommonStat, GetCTime)
{
FILE *p;
CommonStat cs;
#if defined (__APPLE__)
#if defined (__APPLE__) || defined(__FreeBSD__)
p = popen("/usr/bin/stat -f %c /", "r");
#else
p = popen("/usr/bin/stat -c %Z /", "r");
Expand Down