I’m currently attempting to port one of my tools to Haiku.
I’m having a few issues; the most important is that I don’t know the name of the library containing dlopen. How do I find out what the name is of a library containing a function I know the name of ?
Other issues I’ve run into:
CLOCK_MONOTONIC_RAW does not exist. Kwikfix is to #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC - but that will make it imprecise
I can’t seem to get the file type / file attributes when calling opendir / readdir - kwikfix is to use stat, but that’s a bad solution (it’s not atomic)
So far, I’ve figured I need to use #ifdef _HAIKU_ to isolate Haiku-specific code.
For BeFS file attributes,it’s best to use Haiku-specific functions from the Storage Kit: The Haiku Book: Storage Kit
Haikus file attributes are very feature-rich and the standard POSIX APIs don’t know about these specific features.
Even on systems where the dirent structure contains this information, its availability depends on where it is stored on disk. On some filesystems, the directory entry contains this information, and it can be provided in dirent (this is the case for ext4 and most other filesystems on Linux). But in other cases (such as bfs), this information is not available at that level, and it’s indeed required to stat() the node itself to get it.
Thank you. I’ve already looked into the Storage Kit (the first thing I did), but maybe I’ll be able to convince myself to use that instead of dlopen.
And thank you for the information about libroot. (I tried to find it first in the Haiku Book, then man dlopen, but was unsuccesful). I really like the way ‘man’ on Linux shows the libraries to use; the libraries are also mentioned on some of the Kit documentations (but not all).
Wouldn’t it be a good idea to explicitly add the name of the library so that developers know what they need to link to if they use that function?
It could look like that: “libroot: C, POSIX, GNU and BSD functions”
If I need to use a function, I usually look it up using ‘man’, then the man-page will show me which file(s) to include and which library to link to. I find this very convenient. I don’t know if there’s a better way to do it.
Sorry, I forgot to mention this is related to clock_gettime()
The CLOCK_MONOTONIC clock is …. affected by the incremental adjustments performed by adjtime(3) and NTP.
That is my main problem.
CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
Similar to CLOCK_MONOTONIC, but provides access to a raw hardware-based time that is not subject to NTP adjustments or the incremental adjustments performed by adjtime(3). This clock does not count time that the system is suspended.
This is exactly what I need, eg. a true number of seconds elapsed relative to a specific point in time (needs at least millisecond accuracy, though I’ve used nanosecond accuracy until now).
Is something like this available in Haiku ?
As far as I understand, CLOCK_MONOTONIC_RAW is much more “basic” than CLOCK_MONOTONIC, since no processing needs to be done for it.
This is a Linux-specific behavior. On Haiku, CLOCK_MONOTONIC never does that; it’s just the CPU’s time stamp counter converted to microseconds (the same as the Haiku-specific system_time() does.)
Haiku has system_time_nsecs for this.
Your build files don’t need to be set up for it. It’s part of the compiler toolchain itself. That’s how you can have a “Hello world” application get compiled with just gcc hello.c, without specifying -lroot explicitly. (The same is true on Linux; you don’t have to specify -lc, or whatever its name happens to be, there.)
When searching for the mystery header file which declares a function, I find TrackerGrep add-on invaluable. Right click on /system/development/headers (or similar, I’m posting from the phone), select the Tracker add-on “TrackerGrep”, and type in your desired function name, and in seconds you’ll find all references to the specific function.
Haiku also has world leading file location finding, just hit Alt-F (or Opt-F) or Find from Deskbar, and type in file name. In 1 sec it will report the location on BeFS volumes, thanks to database like facilities in the filesystem. You can create a permanent “live query” as well from the Query window so your live results are always available months from now.
To search for functions in headers I’ve gathered some alias’s (from @mmu_man and @BiPolar ) which can be very helpful to quickly find them:
# 'grep' the headers. From mmu_man.
function hgrepall() {
find /boot/system/develop/headers -type f -print0 | xargs -0 grep -n -E "$@"
}
# See what libraries define the given symbol.
function lgrep() {
libdir=$(/bin/ls /boot/system/lib/*.so.*)
if [ "$(uname -p)" = "x86" ]; then
libx86dir=$(/bin/ls /boot/system/lib/x86/*.so.*)
libdir=("${libx86dir[0]}")
fi
for lib in $libdir; do
if [ -L $lib ]; then
continue
fi
nm -A -C -g $lib | grep $1
done
return 0
}
Add these in your “profile” file in ~/config/settings, restart Terminal to make them available.
Yes, or rather CLOCK_MONOTONIC is badly implemented in Linux, and instead of fixing it, they introduced a new CLOCK_MONOTONIC_RAW that does what CLOCK_MONOTONIC should be doing. I guess they were afraid of breaking something that uses their existing behavior?
In Haiku, we don’t currently drift the clock to keep NTP synchronized. We just do one synchronization with a big clock jump (if needed) at system boot, and we don’t aim for super precise synchronization, being within one second of NTP time is considered good enough for a desktop machine.