GithubHelp home page GithubHelp logo

isabella232 / nsfilelock Goto Github PK

View Code? Open in Web Editor NEW

This project forked from longhorn/nsfilelock

0.0 0.0 0.0 35 KB

File lock (flock) in another namespace

License: Apache License 2.0

Makefile 5.06% Go 76.02% Shell 18.93%

nsfilelock's Introduction

NSFileLock

File lock (flock) in another namespace.

Motivation:

I am working on various projects rely on a privileged container(Docker), in which case the process running inside the container will have the ability to switch namespace to the host. Sometimes, we want to make sure certain thing won't happen simultaneously on the host in the case of multiple instances of the container is running. But bind-mount an additional directory just for this purpose seems unnecessarily cumbersome, and especially we already have the ability to switch to the host namespace (especially mount namespace) for certain operations.

But as a Go program, it's hard to hold a file descriptor in another namespace to perform flock operation, since nsenter, which is the easiest way to switch namespace, is a bash program. Of course, you can call setns syscall used by nsenter to get the process into the new namespace, though that means you got to rewrite the nsenter just for grabbing a lock.

This library provides a reliable way to do so without rewrite nsenter.

Target

The target is simply: simulate the behavior of flock in another namespace.

The most import behavior of flock is:

The lock will expire immediately when the process holding file descriptor exits.

It's the key reason why we don't want to create a file in the other namespace to simulate the lock. In that case, the crash of process holding the lock will result in infinitely blocking all the following processes.

For flock, since it's only related to opened file descriptor, as soon as the file descriptor is closed (e.g. unlock, or process crashed so OS release all its resources), the lock will expire.

nsfilelock's People

Contributors

yasker avatar ttpcodes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.