# Using inotify on 2.6.12-gentoo-r10

## penguinhostage

To all-

I have run into a block which I am unable to find good (or easy to find) google resources of additional information. I have downloaded the sample code from IBM for inotify and have enabled inotify in my kernel. I have double checked that it has been installed with 

% dmesg | grep ^inotify

inotify device minor=63

and have followed their code to the letter. Unfortunately every time I run their sample program I get the same error:

ioctl(fd, INOTIFY_WATCH, {/home/classb, -1}) = -1 : Bad file descriptor

/home/classb WD=-1

where /home/classb is the argument I pass to ./inotify_test. I am trying to find someone with experience in inotify to tell me what my problem is and what I can do for a solution.

Thanks to all,

Ben Classen

----------

## Simba

I have the same problem,

```

#./inotify_test /tmp

ioctl(fd, INOTIFY_WATCH, {/tmp, -1}) = -1 : Bad file descriptor

/tmp WD=-1

```

If I check it with strace I get this output:

```

....

munmap(0xb7fbc000, 56470)               = 0

open("/dev/urandom", O_RDONLY)          = 3

read(3, "\202\33M\321", 4)              = 4

close(3)                                = 0

open("/dev/inotify", O_RDONLY)          = 3

brk(0)                                  = 0x804c000

brk(0x806d000)                          = 0x806d000

ioctl(3, SNDCTL_SEQ_SYNC, 0xbfcdbad0)   = -1 EBADF (Bad file descriptor)

fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 6), ...}) = 0

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fc9000

write(1, "ioctl(fd, INOTIFY_WATCH, {/tmp, "..., 41ioctl(fd, INOTIFY_WATCH, {/tmp, -1}) = -1) = 41

dup(2)                                  = 4

fcntl64(4, F_GETFL)                     = 0x2 (flags O_RDWR)

fstat64(4, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 6), ...}) = 0

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fc8000

_llseek(4, 0, 0xbfcdb960, SEEK_CUR)     = -1 ESPIPE (Illegal seek)

write(4, " : Bad file descriptor\n", 23 : Bad file descriptor

) = 23

close(4)                                = 0

munmap(0xb7fc8000, 4096)                = 0

write(1, "/tmp WD=-1\n", 11/tmp WD=-1

)            = 11

select(4, [3], NULL, NULL, {4, 0}

```

I can see in the output that it tried to do something with SNDCTL_SEQ_SYNC:

 ioctl(3, SNDCTL_SEQ_SYNC, 0xbfcdbad0)   = -1 EBADF (Bad file descriptor)

I don't really understand why inotify has something todo with sound card.:

And this is my emerge --info:

```

Portage 2.0.51.22-r3 (hardened/x86/2.6, gcc-3.4.4-hardenednopiessp, glibc-2.3.5- r1, 2.6.12-gentoo-r10 i686)

=================================================================

System uname: 2.6.12-gentoo-r10 i686 AMD Athlon(tm) XP 2600+

Gentoo Base System version 1.6.13

dev-lang/python:     2.3.5-r2

sys-apps/sandbox:    1.2.12

sys-devel/autoconf:  2.13, 2.59-r6

sys-devel/automake:  1.4_p6, 1.5, 1.6.3, 1.7.9-r1, 1.8.5-r3, 1.9.6

sys-devel/binutils:  2.15.92.0.2-r10

sys-devel/libtool:   1.5.18-r1

virtual/os-headers:  2.6.11-r2

ACCEPT_KEYWORDS="x86"

AUTOCLEAN="yes"

CBUILD="i686-pc-linux-gnu"

CFLAGS="-O3 -march=athlon-xp -fomit-frame-pointer -mno-tls-direct-seg-refs -nopi e -fno-stack-protector"

CHOST="i686-pc-linux-gnu"

CONFIG_PROTECT="/etc /usr/kde/2/share/config /usr/kde/3.4/env /usr/kde/3.4/share /config /usr/kde/3.4/shutdown /usr/kde/3/share/config /usr/lib/X11/xkb /usr/lib/ mozilla/defaults/pref /usr/share/config /var/qmail/control"

CONFIG_PROTECT_MASK="/etc/gconf /etc/hotplug /etc/terminfo /etc/env.d"

CXXFLAGS="-O3 -march=athlon-xp -fomit-frame-pointer -mno-tls-direct-seg-refs -no pie -fno-stack-protector"

DISTDIR="/usr/portage/distfiles"

FEATURES="autoconfig buildpkg ccache distlocks sandbox sfperms strict"

GENTOO_MIRRORS="http://gentoo.inode.at http://gd.tuwien.ac.at/opsys/linux/00dist rib/gentoo"

MAKEOPTS="-j2"

PKGDIR="/usr/portage//packages/x86/"

PORTAGE_TMPDIR="/var/tmp"

PORTDIR="/usr/portage/"

PORTDIR_OVERLAY="/usr/local/portage"

SYNC="rsync://rsync.gentoo.org/gentoo-portage"

USE="X acl alsa apache2 arts berkdb cdr crypt cups curl dlloader eds esd fam gd gdbm gif gnome gpm gstreamer gtk hardened imlib innodb ipv6 kerberos ldap ldirec tord lids mad mbox mikmod milter mozilla ncurses nls nptl odbc ogg opengl pam pc ap perl pic png python quotas readline samba sasl sdl snmp ssl svga tcltk tcpd t iff type1 userlocales vorbis winbind x86 xml2 xv zlib userland_GNU kernel_linux elibc_glibc"

Unset:  ASFLAGS, CTARGET, LANG, LC_ALL, LDFLAGS, LINGUAS

```

Any help or hint?

thanks,

simba

----------

## Richie

Maybe you should try with this code from Robert Love (the guy who made inotify):

 :Arrow:  http://www.kernel.org/pub/linux/kernel/people/rml/inotify/utils/

Don't know how well it works with kernel 2.6.12.x sources, but inotify is not in official kernel tree from before 2.6.13.x anyway..

I think the ibm article is from when inotify was a lot more experimental than it is now, and possibly that code does not work well with a current inotify codebase.

At least for me, the code from the link above works like a charm, although I havn't played too much with it yet

----------

## Simba

many thanks, I was looking for this utility  :Smile: 

I tried this utillity, and it seem that inotify watch only the change in current directory, and not in its subdirectories .

That means if I want to watch the whole directory tree, I have to go recursively to all subdirectory and watch it,

is that true? 

Simba

----------

## Richie

I believe so. Actually I'm currently writing a program that logs file system activity. But unfortunately I'm also learning c++ at the same time, which means I will not have anything useful to share for some time.

----------

## penguinhostage

I have to say thank you for the excellent input and it seems that I will be emerging the latest gentoo-sources. Unfortunately I am still getting an error using the inotify-utils (your prediction about it's use in 2.6.12 was true in my case). Thank you very much for your help.

I will be interested in your project about recursively watching the file system- I feel I have a good use for it in the future and would be very willing to help in any way possible. Let me know.

Again, many thanks!

Ben Classen

----------

## Richie

I've set up a project page at:

 :Arrow:  http://coderworld.net/project/view.php?pid=32

It will take some time before I have anything to publish there, maybe months.

I think I saw an error too, but it still works, right? It  seems to return a WD of -1. Just if it was passed an empty dir or something..   :Rolling Eyes: 

----------

## penguinhostage

Actually- the error is

```
classb@classbt43p inotify-utils-0.25 % ./inotify_test /home/classb

inotify_init: Function not implemented

inotify device fd = -1

```

Which I am attributing to the fact I am in 2.6.12 instead of 2.6.13 even though inotify says it is running. I am using the inotify-utils-0.25. But I may be wrong in my problem and there could be something undermining the entire operation. Any additional thoughts?

The problem is I want to implement this on my server which houses all of my files and I want to be able to load them into a database so I can organize them by time modified, names, things like that. I figured instead of running an update script every hour that inotify would be nice since I could tell inotify to trigger an event to run a script that would add the file modified into the database with its characteristics.

Do you have any suggestions as to another way to do this since I cannot get stable hardened sources for 2.6.13. Perhaps another type of program that monitors the file system recursively?

----------

## Richie

 *Quote:*   

> Do you have any suggestions as to another way to do this since I cannot get stable hardened sources for 2.6.13. Perhaps another type of program that monitors the file system recursively?

 

There is dnotify, which inotify replaces, but I have no experience with that one.. I think there is some docs in the kernel source.

dnotify is used to monitor dirs for changes, whereas inotify is used to monitor the files in a dir.

----------

## penguinhostage

Excellent!

Thank you a bunch. I had read dnotify is inferior to inotify but until I can get inotify working this will do quite well. After you emerge dnotify the man page gives excellent examples. I think it will work out.

Many thanks for your suggestions.

Have a good day,

Ben Classen

----------

## Herodot

 *penguinhostage wrote:*   

> Which I am attributing to the fact I am in 2.6.12 instead of 2.6.13 even though inotify says it is running. I am using the inotify-utils-0.25. But I may be wrong in my problem and there could be something undermining the entire operation.

 

I've had similar problems, but I'm now happy to report that 0.25 apparently works on a 2.6.13-gentoo-r3 kernel.

Now I'm trying to find out whether Gamin picks up Inotify or not. Any thought on that?

----------

## Simba

Hi,

I wrote a perl script to monitor directory (can be recursively) in real time, and send an

email alert to difference contact person in difference directory. The configuration file

is an easy xml format contains all information about directories and users you want to

notify if there is any changes in directory. 

This script need a kernel >= 2.6.13 , and perl module Linux::Inotify2.

Usage:

```

iwatch iwatch.xml

```

Here is the link: http://s2.enemy.org/~cahya/Inotify

Please try it and send me a bug report just here  :Smile: 

Simba.

----------

