← Back to team overview

kernel-packages team mailing list archive

[Bug 454722] Re: inotify does not watch /proc

 

#24 > Using inotifywait on /proc works fine too.

False, it does not notify you of new entries (like new processes
launched).  Try the program below for proof; launch it and run "sleep
999" and you will not see the PID of your new sleep appear printed out.

Using inotify with /proc/ will probably never work, because the /proc/
entries are not files.  They don't get updated until you look at them.
It's not a bug, it's a design "feature".  See

    http://unix.stackexchange.com/questions/74713/how-frequently-is-the-
proc-file-system-updated-on-linux?rq=1


If you want to monitor new procs under Linux, use a socket with NETLINK_CONNECTOR instead.


#include <stdlib.h>
#include <stdio.h>
#include <sys/inotify.h>

int main(int argc, char* argv[]){
    int inotify_fd = inotify_init();
    if (inotify_fd == -1) {
        perror("inotify_init(): ");
    }
 
//    int watch_descriptor = inotify_add_watch(inotify_fd, "/proc", IN_CREATE);
    int watch_descriptor = inotify_add_watch(inotify_fd, "/proc", IN_ALL_EVENTS);
    if (watch_descriptor == -1) {
        perror("inotify_add_watch(): ");
    }
 
    struct inotify_event *event;
    char buf[1024];
    ssize_t result;
    
    // Watch forever, until signal (CTRL-C, etc.)
    while(1) 
    {
		result = read(inotify_fd, buf, sizeof(buf));
		if (result == -1){
		    perror("read(): ");
		}

		// Print all events read from inotify_fd:
		int index = 0;
		while (index < result) {
			event = (void *)(&buf[index]);
			printf("index %d: %s\n", (int)index, event->name);
			index += sizeof(struct inotify_event) + event->len;
		}
		printf("\n");

    }
}

-- 
You received this bug notification because you are a member of Kernel
Packages, which is subscribed to linux in Ubuntu.
https://bugs.launchpad.net/bugs/454722

Title:
  inotify does not watch /proc

Status in “linux” package in Ubuntu:
  Won't Fix

Bug description:
  Compile the following program (inotifyerr.c)

  #include <stdlib.h>
  #include <stdio.h>
  #include <sys/inotify.h>

  int main(int argc, char* argv[]){
      int fd = inotify_init();
      if (fd == -1){
          perror("inotify_init");
      }
      char path[256];
      sprintf(path,"/proc/%s",argv[1]);
      printf("watching %s\n",path);
      int wd = inotify_add_watch(fd,path,IN_ALL_EVENTS);
      if (wd == -1){
          perror("inotify_add_watch");
      }
      char buf[1024];
      ssize_t siz = read(fd,buf,1024);
      if (siz == -1){
          perror("inotify read");
      }
      printf("read done, bytes: %d\n",siz);
  }

  gcc inotifyerr.c

  The program is supposed to watch events occurring on file /proc/nnn, where nnn is passed as
  argument to program invokation.
  Then start a process in background, and watch it with the program above. E.g.

  $ sleep 20 &
  [1] 8246
  $ ls /proc/8246
  attr             cpuset   io        mountinfo   pagemap      smaps    wchan
  auxv             cwd      latency   mounts      personality  stat
  cgroup           environ  limits    mountstats  root         statm
  clear_refs       exe      loginuid  net         sched        status
  cmdline          fd       maps      oom_adj     schedstat    syscall
  coredump_filter  fdinfo   mem       oom_score   sessionid    task
  $ ./a.out 8246
  watching /proc/8246
  ^C
  [1]+  Done                    sleep 20

  Note that the background process and the inotifyerr one are run from
  within the same terminal, same user, and that the latter has read
  access to the file being watched.
  No events are reported.

  The expected behaviour of inotify is to report events on /proc files
  and directories instead.

  Additional information:
  I have run the same test also with superuser privileges, but the result
  is the same.
  I have modified the test program so as to make the process to whatch
  a child of the inotifyerr one. In this case inotify catches all the events.

  Ubuntu 9.04. 2.6.28-15-generic #52-Ubuntu SMP Wed Sep 9 10:49:34 UTC 2009 i686 GNU/Linux
  --- 
  AlsaVersion: Advanced Linux Sound Architecture Driver Version 1.0.21.
  Architecture: i386
  AudioDevicesInUse:
   USER        PID ACCESS COMMAND
   /dev/snd/controlC0:  angelo     1376 F.... pulseaudio
  CRDA: Error: [Errno 2] No such file or directory
  Card0.Amixer.info:
   Card hw:0 'NVidia'/'HDA NVidia at 0xfe028000 irq 23'
     Mixer name	: 'Realtek ALC888'
     Components	: 'HDA:10ec0888,1631e601,00100001'
     Controls      : 37
     Simple ctrls  : 21
  DistroRelease: Ubuntu 10.04
  HibernationDevice: RESUME=UUID=335bd2d5-504f-4410-a53a-96814ee336e8
  InstallationMedia: Ubuntu 10.04 LTS "Lucid Lynx" - Release i386 (20100429)
  Lsusb:
   Bus 002 Device 002: ID 046d:c315 Logitech, Inc. Classic New Touch Keyboard
   Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
   Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  MachineType: Packard Bell BV IMEDIA 8425
  NonfreeKernelModules: nvidia
  Package: linux (not installed)
  ProcCmdLine: BOOT_IMAGE=/boot/vmlinuz-2.6.32-24-generic root=UUID=2258e664-9423-4566-ae33-52c12a4645ec ro quiet splash
  ProcEnviron:
   LANG=en_US.utf8
   SHELL=/bin/bash
  ProcVersionSignature: Ubuntu 2.6.32-24.41-generic 2.6.32.15+drm33.5
  Regression: No
  RelatedPackageVersions: linux-firmware 1.34.1
  Reproducible: Yes
  RfKill:
   0: phy0: Wireless LAN
   	Soft blocked: no
   	Hard blocked: no
  Tags: lucid filesystem needs-upstream-testing
  Uname: Linux 2.6.32-24-generic i686
  UserGroups: adm admin cdrom dialout lpadmin plugdev sambashare
  dmi.bios.date: 11/01/2006
  dmi.bios.vendor: Phoenix Technologies, LTD
  dmi.bios.version: M2N-NM 0401
  dmi.board.name: M2N-NM
  dmi.board.vendor: Packard Bell BV
  dmi.board.version: 1.XX
  dmi.chassis.asset.tag: 123456789000
  dmi.chassis.type: 3
  dmi.chassis.vendor: Packard Bell BV
  dmi.chassis.version: Chassis Version
  dmi.modalias: dmi:bvnPhoenixTechnologies,LTD:bvrM2N-NM0401:bd11/01/2006:svnPackardBellBV:pnIMEDIA8425:pvrPB80107506:rvnPackardBellBV:rnM2N-NM:rvr1.XX:cvnPackardBellBV:ct3:cvrChassisVersion:
  dmi.product.name: IMEDIA 8425
  dmi.product.version: PB80107506
  dmi.sys.vendor: Packard Bell BV

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/linux/+bug/454722/+subscriptions