Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flockit: init at 2012-08-11 #27902

Merged
merged 1 commit into from
Aug 5, 2017
Merged

flockit: init at 2012-08-11 #27902

merged 1 commit into from
Aug 5, 2017

Conversation

basvandijk
Copy link
Member

Motivation for this change

flockit is a LD_PRELOAD shim to add file locking to programs that don't do it (I'm looking at you, rsync!)

I use it to backup graphite databases using rsync.

See this blog post for the motivation and an example.

Things done

Please check what applies. Note that these are not hard requirements but merely serve as information for reviewers.

  • Tested using sandboxing
    (nix.useSandbox on NixOS,
    or option build-use-sandbox in nix.conf
    on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • Linux
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

@bjornfor
Copy link
Contributor

bjornfor commented Aug 3, 2017

Off topic. I was hoping for a flockit executable. Having to setup LD_PRELOAD yourself seems more work.

@basvandijk
Copy link
Member Author

@bjornfor how about this?

Now the following:

$ env LD_PRELOAD=$(nix-build -A pkgs.flockit)/lib/libflockit.so FLOCKIT_FILE_PREFIX=test rsync SRC DEST

can be simplified to:

$ $(nix-build -A pkgs.flockit)/bin/flockit test rsync SRC DEST

@bjornfor
Copy link
Contributor

bjornfor commented Aug 3, 2017

Yes, that's an interface I like. Upstream it?

cp ./libflockit.so $out/lib

(cat <<EOI
#!${bash}/bin/bash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sh won't do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably would. I will change it to #!/bin/sh.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it.

Do note that #!/bin/sh is automatically rewritten to #!${pkgs.bash}/bin/sh during the post-installation fixups:

post-installation fixup
...
/nix/store/fd7sj17pyja2wpm2yh5jzmim8nhkra15-flockit-2012-08-11/bin/flockit: interpreter directive changed from "/bin/sh" to "/nix/store/jjg5zsfjl35b239f6znp0nfa9775qy2a-bash-4.4-p12/bin/sh"

chmod +x $out/bin/flockit
'';

meta = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to build on Darwin (see TravisCI), so should probably have a platform attribute to limit its exposure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will add it.

The flockit library and tool exists solely because rsync doesn't have file
locking.

It's not used like a normal library; you don't link against it, and you don't
have to patch your source code to use it. It's inserted between your program and
its libraries by use of LD_PRELOAD.

For example:

  $ env LD_PRELOAD=$(nix-build -A pkgs.flockit)/lib/libflockit.so FLOCKIT_FILE_PREFIX=test rsync SRC DEST

Besides the library a handy executable is provided which can simplify the above to:

  $ $(nix-build -A pkgs.flockit)/bin/flockit test rsync SRC DEST

Also see the following blog post:
https://www.swiftstack.com/blog/2012/08/15/old-school-monkeypatching/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants