Confused NewBe

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.

dlopen is defined in dlfcn.h which is part of libroot.
This is documented here: https://grok.nikisoft.one/opengrok/xref/haiku/docs/user/posix/dlfcn.dox?r=b57c6ae0f4941c054392a76a37262fae66f78626
Unfortunately the web version of the documentation page doesn’t mention libroot anywhere: The Haiku Book: dlfcn.h File Reference

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.

2 Likes

You actually have to do that.

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.

1 Like

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. :wink:

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).

OK, I see. I’ll keep the code as is for now. Under Linux I used this when filtering contents of folders (using dirent→d_type).

At haikuports there should be plenty of patches around dealing with dirent→d_type, maybe those could shed some light here?

1 Like

It actually does, in a way: the “C, POSIX, GNU and BSD functions” at the top is the “group libroot.so”.

It appears to be a Linux extension. It’s not specified in POSIX, and FreeBSD doesn’t have it either.

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.

You don’t need to link to anything explicitly to use those functions; libroot is linked automatically by default with our compiler setup.

1 Like

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.

Uhm, I’m using my build-files ported from Linux, so it’s not already set up for me, but this is good to know. :slight_smile:

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.)

2 Likes

Thank you. I’ve now looked through all of them, and they all solve this by either just ignoring d_type or using stat / lstat.

I wish there was a more robust way, as I’m writing a generic tool, not a specific application.

If the inode provides a reference to the attributes directly or indirectly, that would probably be a good way.

So CLOCK_MONOTONIC is actually CLOCK_MONOTONIC_RAW on Haiku ?

-Then I can definitely use it.

OK, then I’ll use system_time_nsecs - thank you so much! :slight_smile:

1 Like

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.

2 Likes

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. :slight_smile:

2 Likes

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.

1 Like

No need to throw in find and xargs; just use grep -R (or -r, depending on whether or not you want to follow symlinks).

1 Like

I’ll let others figure that one out :slight_smile: so far things are working for “me” :slight_smile:

1 Like