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

netbsd does not have KERN_PROC_PATHNAME but /proc/self/exe #16

Merged
merged 1 commit into from
Jan 7, 2019
Merged

netbsd does not have KERN_PROC_PATHNAME but /proc/self/exe #16

merged 1 commit into from
Jan 7, 2019

Conversation

marcIhm
Copy link

@marcIhm marcIhm commented Dec 30, 2018

Hi, thanx for your library !
(I am using it for my own project yabasic)

Found it is not working out of the box on netbsd but a simple modification helps.

Tested for netbsd 8.0

Thanx for merging (if you do …) !

@gpakosz
Copy link
Owner

gpakosz commented Dec 30, 2018

Hello @marcIhm 👋

I don't really know NetBSD 😐

@jbeich Can you please chime in and tell us what you think about the proposed changes?

As far as I can tell, you made the judgment call to assume NetBSD has KERN_PROC_PATHNAME

Thanks in advance 🙏

@jbeich
Copy link
Contributor

jbeich commented Dec 30, 2018

lldb KERN_PROC_PATHNAME source highligts the difference between FreeBSD and NetBSD. The correct order in name (the 1st argument) is also documented in sysctl(3) on FreeBSD and sysctl(7) on NetBSD.

@marcIhm, can you try the following instead?

diff --git a/src/whereami.c b/src/whereami.c
index 7441823..a43e975 100644
--- a/src/whereami.c
+++ b/src/whereami.c
@@ -576,17 +576,21 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
   char buffer1[PATH_MAX];
   char buffer2[PATH_MAX];
   char* path = buffer1;
   char* resolved = NULL;
   int length = -1;
 
   for (;;)
   {
+#if defined(__NetBSD__)
+    int mib[4] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME };
+#else
     int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
+#endif
     size_t size = sizeof(buffer1);
 
     if (sysctl(mib, (u_int)(sizeof(mib) / sizeof(mib[0])), path, &size, NULL, 0) != 0)
         break;
 
     resolved = realpath(path, buffer2);
     if (!resolved)
       break;

@jbeich
Copy link
Contributor

jbeich commented Dec 30, 2018

NetBSD 6.x reached EOL a few months ago but NetBSD 7.x is still supported and lacks KERN_PROC_PATHNAME. Depending on the requirements NetBSD < 8.0 support can be left for downstream (e.g., PkgSrc) to deal with in order to avoid cruft when NetBSD 7.x reaches EOL (like Firefox, libuv, lldb do). Otherwise, you'd have to move <sys/sysctl.h> in order to check whether it defines KERN_PROC_PATHNAME e.g.,

diff --git a/src/whereami.c b/src/whereami.c
index 7441823..cda4e8d 100644
--- a/src/whereami.c
+++ b/src/whereami.c
@@ -7,16 +7,22 @@
 #if !defined(WHEREAMI_H)
 #include <whereami.h>
 #endif
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+#if defined(__DragonFly__) || defined(__FreeBSD__) || \
+      defined(__FreeBSD_kernel__) || defined(__NetBSD__)
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
+
 #if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC)
 #include <stdlib.h>
 #endif
 
 #if !defined(WAI_MALLOC)
 #define WAI_MALLOC(size) malloc(size)
 #endif
 
@@ -153,17 +159,17 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
 #endif
   {
     length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length);
   }
 
   return length;
 }
 
-#elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun)
+#elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun) || (defined(__NetBSD__) && !defined(KERN_PROC_PATHNAME))
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #if defined(__linux__)
 #include <linux/limits.h>
 #else
 #include <limits.h>
@@ -555,38 +561,39 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
     }
 
     break;
   }
 
   return length;
 }
 
-#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
-      defined(__FreeBSD_kernel__) || defined(__NetBSD__)
+#elif defined(KERN_PROC_PATHNAME)
 
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
-#include <sys/sysctl.h>
 #include <dlfcn.h>
 
 WAI_FUNCSPEC
 int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
 {
   char buffer1[PATH_MAX];
   char buffer2[PATH_MAX];
   char* path = buffer1;
   char* resolved = NULL;
   int length = -1;
 
   for (;;)
   {
+#if defined(__NetBSD__)
+    int mib[4] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME };
+#else
     int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
+#endif
     size_t size = sizeof(buffer1);
 
     if (sysctl(mib, (u_int)(sizeof(mib) / sizeof(mib[0])), path, &size, NULL, 0) != 0)
         break;
 
     resolved = realpath(path, buffer2);
     if (!resolved)
       break;

@krytarowski
Copy link

7.0 will get EOL probably soon as we are planning to release -9 earlier

@gpakosz
Copy link
Owner

gpakosz commented Dec 31, 2018

Does inspecting /proc/self/exe work for NetBSD versions 6.0, 7.0 and 8.0?

@marcIhm
Copy link
Author

marcIhm commented Dec 31, 2018

The patch provided by Jan works for my netBSD 8 box like a charm; updated my pull request accordingly.

@gpakosz
Copy link
Owner

gpakosz commented Dec 31, 2018

Well I vastly prefer your initial patch that inspects /proc/self/exe for all NetBSDs if it works for 6.0, 7.0 and 8.0.

This makes the code clearer than interspersing #ifdefs all over the place.

@marcIhm
Copy link
Author

marcIhm commented Dec 31, 2018

Reverted to the initial Version :-)

@krytarowski
Copy link

/proc is deprecated, please never use it except as a fallback for old versions of NetBSD (but at this point we can ignore them)

@marcIhm
Copy link
Author

marcIhm commented Jan 1, 2019

Okay, thanx to Kamils comment we are back at the Changes proposed by Jan. Seems like sometimes you just Need some ifdefs :-)
Hope this is Okay for you too Gregory !
Best regards
Marc

@gpakosz
Copy link
Owner

gpakosz commented Jan 5, 2019

Time to iron things out!

My understanding is that the current code in master doesn't work for NetBSD at all:

  • because there's no KERN_PROC_PATHNAME available for NetBSD < 8.0
  • and because the arguments to sysctl() call are in wrong order

To avoid scattering the platform detection #ifdef dance all over the place, if you all agree, I suggest following @krytarowski's recommendation and not bother with NetBSD < 8.0.

If we really want to support NetBSD 7.0 or help packagers, we could alter line 161 to force the inspection of /self/proc/exe:

#elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun) || defined(WAI_PROC_SELF_EXE)

What do you think?

If we all agree, for this PR to merge:

  • Should @jbeich be made the author of the commit and @marcIhm the committer? 🤔
  • Can you please make the PR a single commit and use a commit message that tells something like "fixed getExecutablePath() for NetBSD"? 🙏

Oh and happy new year 2019! 🎉

@marcIhm
Copy link
Author

marcIhm commented Jan 5, 2019

Yes, woule be great for me ! So it would be Jans first and simpler proposal without support for NetBSD 7 and earlier. (And Jan should be the author of Course !)

- assume NetBSD >= 8.0
- for NetBSD < 8.0, define WAI_USE_PROC_SELF_EXE
@gpakosz
Copy link
Owner

gpakosz commented Jan 5, 2019

@marcIhm I squashed and pushed to your master branch, can you please confirm it's still working for you?

@marcIhm
Copy link
Author

marcIhm commented Jan 6, 2019

Just compiled and started yabasic on NetBSD 8: No Problems !

@gpakosz gpakosz merged commit 4414eb5 into gpakosz:master Jan 7, 2019
gpakosz added a commit that referenced this pull request Jan 7, 2019
@gpakosz
Copy link
Owner

gpakosz commented Jan 7, 2019

Thanks for improving whereami guys, appreciated 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants