"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/test/test-fanotify.c" (15 Mar 2019, 2685 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test-fanotify.c": 0.09.49_vs_0.09.50.

    1 /*
    2  * Copyright (C) 2012-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  */
   24 #include "stress-ng.h"
   25 
   26 #if !defined(__linux__)
   27 #error requires linux to build
   28 #endif
   29 
   30 #include <mntent.h>
   31 #include <sys/select.h>
   32 #include <sys/fanotify.h>
   33 
   34 #if !defined(FAN_MARK_ADD)
   35 #error missing fnotify FAN_MARK_ADD flag
   36 #endif
   37 
   38 #if !defined(FAN_MARK_MOUNT)
   39 #error missing fnotify FAN_MARK_MOUNT flag
   40 #endif
   41 
   42 #if !defined(FAN_ACCESS)
   43 #error missing fnotify FAN_ACCESS flag
   44 #endif
   45 
   46 #if !defined(FAN_MODIFY)
   47 #error missing fnotify FAN_MODIFY flag
   48 #endif
   49 
   50 #if !defined(FAN_OPEN)
   51 #error missing fnotify FAN_OPEN flag
   52 #endif
   53 
   54 #if !defined(FAN_CLOSE)
   55 #error missing fnotify FAN_CLOSE flag
   56 #endif
   57 
   58 #if !defined(FAN_ONDIR)
   59 #error missing fnotify FAN_ONDIR flag
   60 #endif
   61 
   62 #if !defined(FAN_EVENT_ON_CHILD)
   63 #error missing fnotify FAN_EVENT_ON_CHILD flag
   64 #endif
   65 
   66 #if !defined(FAN_EVENT_OK)
   67 #error missing fnotify FAN_EVENT_OK macro
   68 #endif
   69 
   70 #if !defined(FAN_NOFD)
   71 #error missing fnotify FAN_NOFD macro
   72 #endif
   73 
   74 #define BUFFER_SIZE (4096)
   75 
   76 int main(void)
   77 {
   78     int fan_fd, ret;
   79     size_t len;
   80     struct fanotify_event_metadata *metadata;
   81     void *buffer;
   82 
   83     ret = posix_memalign(&buffer, BUFFER_SIZE, BUFFER_SIZE);
   84     if (ret != 0 || buffer == NULL)
   85         return -1;
   86 
   87     fan_fd = fanotify_init(0, 0);
   88     if (fan_fd < 0) {
   89         free(buffer);
   90         return -1;
   91     }
   92 
   93     ret = fanotify_mark(fan_fd, FAN_MARK_ADD | FAN_MARK_MOUNT,
   94         FAN_ACCESS| FAN_MODIFY | FAN_OPEN | FAN_CLOSE |
   95         FAN_ONDIR | FAN_EVENT_ON_CHILD, AT_FDCWD, "/");
   96     (void)ret;
   97 
   98     len = read(fan_fd, (void *)buffer, BUFFER_SIZE);
   99     metadata = (struct fanotify_event_metadata *)buffer;
  100 
  101     while (FAN_EVENT_OK(metadata, len)) {
  102         metadata = FAN_EVENT_NEXT(metadata, len);
  103     }
  104 
  105     free(buffer);
  106     (void)close(fan_fd);
  107     return 0;
  108 }