"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/test/test-inotify.c" (15 Mar 2019, 2724 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-inotify.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 <unistd.h>
   25 
   26 #if !defined(__linux__)
   27 #error requires linux to build
   28 #endif
   29 
   30 #include <sys/select.h>
   31 #include <sys/inotify.h>
   32 
   33 #if !defined(IN_DELETE_SELF)
   34 #error missing inotify IN_DELETE_SELF flag
   35 #endif
   36 
   37 #if !defined(IN_MOVE_SELF)
   38 #error missing inotify IN_MOVE_SELF flag
   39 #endif
   40 
   41 #if !defined(IN_MOVED_TO)
   42 #error missing inotify IN_MOVED_TO flag
   43 #endif
   44 
   45 #if !defined(IN_MOVED_FROM)
   46 #error missing inotify IN_MOVED_FROM flag
   47 #endif
   48 
   49 #if !defined(IN_ATTRIB)
   50 #error missing inotify IN_ATTRIB flag
   51 #endif
   52 
   53 #if !defined(IN_ACCESS)
   54 #error missing inotify IN_ACCESS flag
   55 #endif
   56 
   57 #if !defined(IN_MODIFY)
   58 #error missing inotify IN_MODIFY flag
   59 #endif
   60 
   61 #if !defined(IN_CREATE)
   62 #error missing inotify IN_CREATE flag
   63 #endif
   64 
   65 #if !defined(IN_OPEN)
   66 #error missing inotify IN_OPEN flag
   67 #endif
   68 
   69 #if !defined(IN_CLOSE_WRITE)
   70 #error missing inotify IN_CLOSE_WRITE flag
   71 #endif
   72 
   73 #if !defined(IN_CLOSE_NOWRITE)
   74 #error missing inotify IN_CLOSE_NOWRITE flag
   75 #endif
   76 
   77 #if !defined(IN_DELETE)
   78 #error missing inotify IN_DELETE flag
   79 #endif
   80 
   81 #if !defined(IN_DELETE_SELF)
   82 #error missing inotify IN_DELETE_SELF flag
   83 #endif
   84 
   85 #define BUFFER_SIZE (4096)
   86 
   87 int main(void)
   88 {
   89     int fd, ret, wd;
   90     ssize_t len, i = 0;
   91     char buffer[1024];
   92 
   93     fd = inotify_init();
   94     if (fd < 0)
   95         return -1;
   96 
   97     wd = inotify_add_watch(fd, "/", IN_ACCESS);
   98 
   99     len = read(fd, buffer, sizeof(buffer));
  100     if (len < 0)
  101         return -1;
  102 
  103     while ((i >= 0) && (i <= len - (ssize_t)sizeof(struct inotify_event))) {
  104         struct inotify_event *event = (struct inotify_event *)&buffer[i];
  105 
  106         if (event->len > sizeof(buffer))
  107             break;
  108         i += sizeof(struct inotify_event) + event->len;
  109     }
  110 
  111     (void)inotify_rm_watch(fd, wd);
  112     (void)close(fd);
  113     return 0;
  114 }