Skip to content

Comments

Extract stats into their own file#129

Merged
ericcurtin merged 1 commit intoinotify-tools:masterfrom
TorunR:extraction
Jan 25, 2021
Merged

Extract stats into their own file#129
ericcurtin merged 1 commit intoinotify-tools:masterfrom
TorunR:extraction

Conversation

@TorunR
Copy link
Contributor

@TorunR TorunR commented Jan 25, 2021

Hi,

we've extracted some parts concerning the file access event stats into their own file during the course of a scientific study. We want to check with whether this extraction seems sensible for you.

@ericcurtin
Copy link
Member

I'll take a look later, only just very briefly looked at it. Is this only a refactoring? Any functional changes to point out?

@ericcurtin
Copy link
Member

ericcurtin commented Jan 25, 2021

What is the scientific study also? I'm trying to learn a little more about what the varying users of this are using the tools for 😄

@ericcurtin
Copy link
Member

Thanks for making it easy to review, new unique lines:

++ b/libinotifytools/src/Makefile.am
libinotifytools_la_SOURCES = inotifytools.c inotifytools_p.h redblack.c redblack.h stats.c stats.h
++ b/libinotifytools/src/inotifytools.c
#include "stats.h"
int collect_stats = 0;
int init = 0;
void _niceassert( long cond, int line, char const * file,
++ b/libinotifytools/src/inotifytools_p.h
										  /**
* @internal
* Assert that a condition evaluates to true, and optionally output a message
* if the assertion fails.
*
* You should use the niceassert() preprocessor macro instead.
*
* @param  cond  If 0, assertion fails, otherwise assertion succeeds.
*
* @param  line  Line number of source code where assertion is made.
*
* @param  file  Name of source file where assertion is made.
*
* @param  condstr  Stringified assertion expression.
*
* @param  mesg  A human-readable error message shown if assertion fails.
*/
void _niceassert( long cond, int line, char const * file,
                  char const * condstr, char const * mesg );
extern int init;
extern struct rbtree *tree_wd;
++ b/libinotifytools/src/stats.c
#include "stats.h"
++ b/libinotifytools/src/stats.h
#ifndef STATS_H
#define STATS_H
#include "inotifytools.h"
#include "inotifytools/inotify.h"
#include "inotifytools_p.h"
extern int collect_stats;
void record_stats( struct inotify_event const * event );
unsigned int *stat_ptr(watch *w, int event);
watch *watch_from_wd(int wd);
#endif // STATS_H

@ericcurtin ericcurtin merged commit dd59c53 into inotify-tools:master Jan 25, 2021
@TorunR
Copy link
Contributor Author

TorunR commented Jan 25, 2021

Hi,

thanks for accepting the Pullrequest. We don't use inotify directly, it rather is an evaluation target for an automated refactoring approach we are developing. We want to extract "modules" based on keyphrases, in this case "stats".
I'd be curious about your opinion, whether an approach like this can be interesting.

@ericcurtin
Copy link
Member

Could you show the code/script/designs? I guess it worked out ok here.

@NiLuJe
Copy link

NiLuJe commented Jan 27, 2021

This made libinotifytools' _niceassert symbol public (it was static before this), which breaks static builds because the tools also define a public _niceassert symbol.

/bin/sh ../libtool  --tag=CC   --mode=link gcc -Wall -Wextra -Wshadow -Wpointer-arith -Werror -std=c99 -I../libinotifytools/src -g -O2   -o inotifywait inotifywait.o common.o ../libinotifytools/src/libinotifytools.la 
libtool: link: gcc -Wall -Wextra -Wshadow -Wpointer-arith -Werror -std=c99 -I../libinotifytools/src -g -O2 -o inotifywait inotifywait.o common.o  ../libinotifytools/src/.libs/libinotifytools.a
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: ../libinotifytools/src/.libs/libinotifytools.a(inotifytools.o): in function `_niceassert':
inotifytools.c:(.text+0x330): multiple definition of `_niceassert'; common.o:common.c:(.text+0x520): first defined here
collect2: error: ld returned 1 exit status

@ericcurtin
Copy link
Member

ericcurtin commented Jan 27, 2021

Thanks so much for reporting @NiLuJe ! Two things needed here, I think. Add a statically linked build to our build script and remove the code duplication.

@ericcurtin
Copy link
Member

Would you mind adding the exact way you build to:

build_and_test.sh

@NiLuJe

and opening a PR?

@amir73il amir73il mentioned this pull request May 16, 2021
Qeole added a commit to Qeole/inotify-tools that referenced this pull request Oct 5, 2022
Running inotifywait 3.22.1.0 on Ubuntu 22.04, I observed a bug
apparently due to a bad file descriptor:

    $ inotifywait -e modify foo
    Setting up watches.
    Couldn't watch foo: Bad file descriptor

I could also reproduce with the master branch. Investigating further, it
appears that inotifytools_init() in inotifytools.c exits immediately
after entry, because the global variable "init" is non-null, even though
we just called it at the beginning of inotifywait.c's main(), and the
global variable is supposed to be initialised at 0.

Bisecting showed that this variable used to be static, but was changed
at some point to non-static to share it with stats.c, and this is where
the bug fist occurs. As far as I understand, the symbol conflicts with
the init() function from the program's entry point [0]. When entering
inotifytools_init(), it seems that the value for that function is picked
up and compared to 0, found non-null, and the initialization function
exits, leaving inotify_fd uninitialized (-1) and a bad file descriptor.

Renaming the variable to something other than "init" is enough to fix
the bug.

    $ ./src/inotifywait -e modify foo
    Setting up watches.
    Watches established
    <waits>

[0] https://www.geeksforgeeks.org/executing-main-in-c-behind-the-scene/

Fixes: dd59c53 ("Extract stats into their own file (inotify-tools#129)")
ericcurtin pushed a commit that referenced this pull request Oct 6, 2022
Running inotifywait 3.22.1.0 on Ubuntu 22.04, I observed a bug
apparently due to a bad file descriptor:

    $ inotifywait -e modify foo
    Setting up watches.
    Couldn't watch foo: Bad file descriptor

I could also reproduce with the master branch. Investigating further, it
appears that inotifytools_init() in inotifytools.c exits immediately
after entry, because the global variable "init" is non-null, even though
we just called it at the beginning of inotifywait.c's main(), and the
global variable is supposed to be initialised at 0.

Bisecting showed that this variable used to be static, but was changed
at some point to non-static to share it with stats.c, and this is where
the bug fist occurs. As far as I understand, the symbol conflicts with
the init() function from the program's entry point [0]. When entering
inotifytools_init(), it seems that the value for that function is picked
up and compared to 0, found non-null, and the initialization function
exits, leaving inotify_fd uninitialized (-1) and a bad file descriptor.

Renaming the variable to something other than "init" is enough to fix
the bug.

    $ ./src/inotifywait -e modify foo
    Setting up watches.
    Watches established
    <waits>

[0] https://www.geeksforgeeks.org/executing-main-in-c-behind-the-scene/

Fixes: dd59c53 ("Extract stats into their own file (#129)")
Qeole added a commit to Qeole/inotify-tools that referenced this pull request Oct 6, 2022
Running inotifywait 3.22.1.0 on Ubuntu 22.04, I observed a bug
apparently due to a bad file descriptor:

    $ inotifywait -e modify foo
    Setting up watches.
    Couldn't watch foo: Bad file descriptor

I could also reproduce with the master branch. Investigating further, it
appears that inotifytools_init() in inotifytools.c exits immediately
after entry, because the global variable "init" is non-null, even though
we just called it at the beginning of inotifywait.c's main(), and the
global variable is supposed to be initialised at 0.

Bisecting showed that this variable used to be static, but was changed
at some point to non-static to share it with stats.c, and this is where
the bug fist occurs. As far as I understand, the symbol conflicts with
the init() function from the program's entry point [0]. When entering
inotifytools_init(), it seems that the value for that function is picked
up and compared to 0, found non-null, and the initialization function
exits, leaving inotify_fd uninitialized (-1) and a bad file descriptor.

Renaming the variable to something other than "init" is enough to fix
the bug.

    $ ./src/inotifywait -e modify foo
    Setting up watches.
    Watches established
    <waits>

[0] https://www.geeksforgeeks.org/executing-main-in-c-behind-the-scene/

Fixes: dd59c53 ("Extract stats into their own file (inotify-tools#129)")
ericcurtin pushed a commit that referenced this pull request Oct 6, 2022
#170)

Running inotifywait 3.22.1.0 on Ubuntu 22.04, I observed a bug
apparently due to a bad file descriptor:

    $ inotifywait -e modify foo
    Setting up watches.
    Couldn't watch foo: Bad file descriptor

I could also reproduce with the master branch. Investigating further, it
appears that inotifytools_init() in inotifytools.c exits immediately
after entry, because the global variable "init" is non-null, even though
we just called it at the beginning of inotifywait.c's main(), and the
global variable is supposed to be initialised at 0.

Bisecting showed that this variable used to be static, but was changed
at some point to non-static to share it with stats.c, and this is where
the bug fist occurs. As far as I understand, the symbol conflicts with
the init() function from the program's entry point [0]. When entering
inotifytools_init(), it seems that the value for that function is picked
up and compared to 0, found non-null, and the initialization function
exits, leaving inotify_fd uninitialized (-1) and a bad file descriptor.

Renaming the variable to something other than "init" is enough to fix
the bug.

    $ ./src/inotifywait -e modify foo
    Setting up watches.
    Watches established
    <waits>

[0] https://www.geeksforgeeks.org/executing-main-in-c-behind-the-scene/

Fixes: dd59c53 ("Extract stats into their own file (#129)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants