Notify of changes on a file under /proc

I have written a small ‘daemon’ in bash that will switch to the headphones if they are detected, and if not, switch to an external USB speaker with PulseAudio.

What I’m looking for is some way to get notification of changes on the file /proc/asound/card0/codec#0, just like inotifywait does on real files (considering files under /proc to be as “pseudo-files”).

I find my code a bit insane, because it runs sleep 1 with awk for the whole day, that is 86400 times a day 🙂

while sleep 1; do
    _1=${_2:-}
    _2=$(awk '/Pin-ctls/{n++;if(n==4)print}' '/proc/asound/card0/codec#0')

    [[ ${_1:-} = $_2 ]] ||
        if [[ $_2 =~ OUT ]]; then
            use_speakers
        else
            use_internal
        fi
done

What I’m looking for is something like (this example doesn’t work):

codec=/proc/asound/card0/codec#0
while inotifywait $codec; do
    if [[ $(awk '/Pin-ctls/{n++;if(n==4)print}' $codec) =~ OUT ]]; then
        use_speakers
    else
        use_internal
    fi
done

This way the commands inside the loop would be run only when there are real changes on the $codec file.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

What I’m looking for is some way to get notification of changes on the file [in proc]

You can’t, because they aren’t files. This is not quite a duplicate question, but the answer here explains why.

/proc is a kernel interface. There are no real files there, hence they can’t change. Reading from the handles is a request and the data in the file when you read it is a reply to that.

The only way you could simulate something like this would be to read the file at intervals and compare the content to see if the reply from the kernel has changed — looks like you’ve already done that.

If you stat procfs files, the atime and the mtime will be the same: for some files it is whenever the stat call was, for others a time from during system boot. In the first case, it will always seem to have changed, in the second, it will never seem to have changed.

Method 2

If you are using PulseAudio, pactl subscribe does this.

Method 3

Also take in mind that some files under /proc/ permit to be monitored for changes via polling, for example if you do man proc you can read the following about /proc/self/mounts file:

/proc/[pid]/mounts (since Linux 2.4.19)
This file lists all the filesystems currently mounted in the process’s mount namespace (see mount_namespaces(7)). The
format of this file is documented in fstab(5).

Since kernel version 2.6.15, this file is pollable: after opening the file for reading, a change in this file (i.e., a
filesystem mount or unmount) causes select(2) to mark the file descriptor as having an exceptional condition, and poll(2)
and epoll_wait(2) mark the file as having a priority event (POLLPRI). (Before Linux 2.6.30, a change in this file was
indicated by the file descriptor being marked as readable for select(2), and being marked as having an error condition for
poll(2) and epoll_wait(2).)

And that is exactly what is being implemented in the following question:

https://stackoverflow.com/questions/5070801/monitoring-mount-point-changes-via-proc-mounts

Method 4

I had a similar use-case and found some success monitoring the directory rather than the “file” itself.

Try

inotifywait -r -m /proc/asound/card0/

and you should see events something like:

/proc/asound/card0/ OPEN codec#0
/proc/asound/card0/ ACCESS codec#0
/proc/asound/card0/ CLOSE_NOWRITE,CLOSE codec#0

A simple grep against the codec#0 and a bit of awk will tell you if the file was written to.

Method 5

Try to use netlink to monitor /proc files changed.

https://mdlayher.com/blog/linux-netlink-and-go-part-1-netlink/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x