监控安装通过/ proc /坐骑点的变化 [英] Monitoring mount point changes via /proc/mounts

查看:230
本文介绍了监控安装通过/ proc /坐骑点的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据PROC手册,可以监视通过开放的/ proc /坐骑在linux系统的挂载点的变化,并增加文件描述符在<$读 FD_SET C $ C>选择()电话。

According proc manual, one can monitor for mount point changes in linux system by opening "/proc/mounts", and adding the file descriptor to read fd_set in select() call.

code的下面这段工作在Ubuntu 9.04,而不是在Ubuntu 10.04(与2.6.32 Linux内核):

Following piece of code works on Ubuntu 9.04, and not in Ubuntu 10.04 (with 2.6.32 linux kernel):

int mfd = open("/proc/mounts", O_RDONLY, 0);

fd_set rfds;
struct timeval tv;
int rv;

FD_ZERO(&rfds);
FD_SET(mfd, &rfds);
tv.tv_sec = 5;
tv.tv_usec = 0;

int changes = 0;
while ((rv = select(mfd+1, &rfds, NULL, NULL, &tv)) >= 0) {
    if (FD_ISSET(mfd, &rfds)) {
        fprintf(stdout, "Mount points changed. %d.\n", changes++);
    }

    FD_ZERO(&rfds);
    FD_SET(mfd, &rfds);
    tv.tv_sec = 5;
    tv.tv_usec = 0;

    if (changes > 10) {
        exit(EXIT_FAILURE);
    }
}

可编译代码段。

文件描述符始终是在一个机器可读的,因此它保持在选择调用弹出。即使有在挂载没有任何变化。

The file descriptor is always readable in one machine, and hence it keeps popping up in the select call. Even there are no changes in mounts.

我失去了一些东西在这里?

Am I missing something here?

在此先感谢您的帮助!

人5 PROC:

的/ proc / [PID] /坐骑(因为Linux 2.4.19)

/proc/[pid]/mounts (since Linux 2.4.19)

这是当前安装在过程中的坐骑命名空间中的所有文件系统的列表。该文件的格式中的fstab是记录(5)。由于内核版本2.6.15,该文件是可轮询:在这个文件打开文件进行读取,改变(即文件系统装载或卸载)导致选择(2)来标记文件描述符为可读和民意调查(后2)和epoll_wait(2)将文件标记为具有一个错误条件

This is a list of all the file systems currently mounted in the process's mount namespace. 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 file system mount or unmount) causes select(2) to mark the file descriptor as readable, and poll(2) and epoll_wait(2) mark the file as having an error condition.

推荐答案

有一个<一个href=\"http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=31b07093c44a7a442394d44423e21d783f5523b8\">bugfix在Linux内核中描述的行为:

There was a bugfix in linux kernel describing that behavior:

SUSv3说:常规文件将永远轮询TRUE进行读取和
  写见
  <一href=\"http://www.opengroup.org/onlinepubs/009695399/functions/poll.html\">http://www.opengroup.org/onlinepubs/009695399/functions/poll.html

SUSv3 says "Regular files shall always poll TRUE for reading and writing". see http://www.opengroup.org/onlinepubs/009695399/functions/poll.html

所以,你必须使用轮询与POLLPRI | POLLERR标志。事情是这样的:

So, you have to use poll with POLLPRI | POLLERR flags. Something like this:



     int mfd = open("/proc/mounts", O_RDONLY, 0);
     struct pollfd pfd;
     int rv;

     int changes = 0;
     pfd.fd = mfd;
     pfd.events = POLLERR | POLLPRI;
     pfd.revents = 0;
     while ((rv = poll(&pfd, 1, 5)) >= 0) {
          if (pfd.revents & POLLERR) {
               fprintf(stdout, "Mount points changed. %d.\n", changes++);
          }

          pfd.revents = 0;
          if (changes > 10) {
               exit(EXIT_FAILURE);
          }
     }

这篇关于监控安装通过/ proc /坐骑点的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆