fanotify用例

文章展示了使用C++和Golang通过fanotifyAPI来监控文件系统事件的代码示例,包括文件打开权限和可写文件关闭事件的处理。在C++示例中,程序读取fanotify事件并响应,而在Golang版本中,程序同样监听事件并获取进程信息。

目录

c++ 

golang


c++ 

下列程序能够监控单文件和目录

NegativeMjark/fanotify: A simple fanotify example for watching events on a filesystem. (github.com)

另外一个

#define _GNU_SOURCE /* Needed to get O_LARGEFILE definition */ #include <errno.h>

#include <fcntl.h>

#include <limits.h>

#include <poll.h>

#include <stdio.h>

#include <stdlib.h>

#include <sys/fanotify.h>

#include <unistd.h>

/* Read all available fanotify events from the file descriptor 'fd'. */

static void
handle_events(int fd) {
  const struct fanotify_event_metadata * metadata;
  struct fanotify_event_metadata buf[200];
  ssize_t len;
  char path[PATH_MAX];
  ssize_t path_len;
  char procfd_path[PATH_MAX];
  struct fanotify_response response;

  /* Loop while events can be read from fanotify file descriptor. */

  for (;;) {

    /* Read some events. */

    len = read(fd, buf, sizeof(buf));
    if (len == -1 && errno != EAGAIN) {
      perror("read");
      exit(EXIT_FAILURE);
    }

    /* Check if end of available data reached. */

    if (len <= 0)
      break;

    /* Point to the first event in the buffer. */

    metadata = buf;

    /* Loop over all events in the buffer. */

    while (FAN_EVENT_OK(metadata, len)) {

      /* Check that run-time and compile-time structures match. */

      if 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信安成长日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值