2

I'm using FreeBSD-12.0-RELEASE-amd64, when I list all the files in /dev, I don't see /dev/dsp (note there's no output after the first command), but when I list the file by explicitly specifying its name, it is found. Even more weird, when I then ls -al /dev again (and grep for dsp, like in the initial command), the /dev/dsp file disappears again, but now /dev/dsp0.0 exists:

# ls -al /dev | grep dsp
# ls -al /dev/dsp
crw-rw-rw-  1 root  wheel  0x56 May 24 07:46 /dev/dsp
# ls -al /dev | grep dsp
crw-rw-rw-   1 root  wheel     0x56 May 24 07:46 dsp0.0

The above has been done by running the ISO image in QEMU, but I was able to reproduce this on real hardware, too. Just boot with the default parameters, then select "Shell" in the menu that appears, then enter the commands from above:

qemu-system-x86_64 -soundhw hda -cdrom FreeBSD-12.0-RELEASE-amd64-disc1.iso

And for the record:

# cat /dev/sndstat
Installed devices:
pcm0: <Generic (0x1af40022) (Analog)> (play/rec) default
No devices installed from userspace.

Listing /dev to me sounds like a good way to find out which devices / disks FreeBSD "sees", coming from a Linux background. There's probably some good reason (creating nodes on demand?) why it doesn't appear, but is there some other way to ask the system "what devices did you actually find and are available?" without having to "touch" the files for them to magically appear?

  • What is it that you're actually wanting to do? Get a list of the devices in general? Does dmesg give you any usable information? Does the sysctl utility help? – Kusalananda May 24 at 8:22
  • /dev/ is not a normal directory any more. It's the mount point of the devfs(5) file system. – Richard Smith May 24 at 8:27
  • I checked the dmesg output, I only found pcm-related messages. Didn't yet try sysctl. I read online that the sound device is /dev/dsp, and my initial hunch was "let's use ls to check if that file exists in /dev". – Thomas Perl May 24 at 8:29
  • 1
    dmesg | grep pcm to find sound devices. Status check cat /dev/sndstat. Test cat file > /dev/dsp. /dev/dsp created as needed. See 7.2.2. Testing Sound – Claus Andersen May 24 at 11:36
  • 1
    Yes - it is documented in the FILES section of man pcm which lists the various /dev/dsp variants – Claus Andersen May 25 at 15:19
1

Thanks to all the comments. The main takeaways for me were:

  1. Do not use ls /dev for checking which devices are on the system, but use dmesg instead (and in the particular case of sound cards, 7.2.2 Testing Sound is really useful)
  2. devfs has the ability to create device nodes on demand (when they are first accessed)

The mentioned sound(4) manpage says:

The above device nodes are only created on demand through the dynamic devfs(5) clone handler.

The "clone handler" there is - as far as I understood it - dev_clone(9), and indeed dsp.c in the FreeBSD sources calls it in dsp_sysinit(), the code that creates the /dev/dsp0.0 entry above:

        *dev = make_dev(&dsp_cdevsw, PCMMINOR(udcmask),
            UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
            devname, unit, devsep, cunit);

While researching, I also stumbled upon Rethinking /dev and devices in the UNIX kernel by Poul-Henning Kamp in BSDCon 2002, which in "On-demand device creation" says:

An interesting mixed use of this mechanism is with the sound device drivers. Modern sound devices have multiple channels, presumably to allow the user to listen to CNN, Napstered MP3 files and Quake sound effects at the same time. The only problem is that all applications attempt to open /dev/dsp since they have no concept of multiple sound devices. The sound device drivers use the cloning facility to direct /dev/dsp to the first available sound channel completely transparently to the process.

There are very few drawbacks to this mechanism, the major one being that ls /dev now errs on the sparse side instead of the rich when used as a system device inventory, a practice which has always been of dubious precision at best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.