linux-namespaces 0.1.3.1 → 0.2.0.1
raw patch · 3 files changed
Files
- ChangeLog.md +8/−0
- System/Linux/Namespaces.hsc +56/−2
- linux-namespaces.cabal +2/−2
ChangeLog.md view
@@ -1,3 +1,11 @@+#### 0.2.0.1 *2024-12-16*++ * Fix build with pre 5.6 linux-headers, where there is no support for time namespaces.++#### 0.2.0.0 *2024-11-29*++ * Add support for CGroup and time namespaces.+ #### 0.1.3.1 *2023-06-23* * Support building with `unix-2.8`.
System/Linux/Namespaces.hsc view
@@ -45,6 +45,10 @@ , writeUserMappings , writeGroupMappings + -- * Setting offsets for virtualized clocks+ , Clock(..)+ , setClockOffset+ -- * Example -- $example ) where@@ -54,7 +58,7 @@ import Foreign import Foreign.C-import System.Posix.Types (Fd(..), ProcessID, UserID, GroupID)+import System.Posix.Types (Fd(..), ProcessID, UserID, GroupID, EpochTime) import System.Posix.IO import System.Posix.Files (readSymbolicLink) import Control.Exception (bracket)@@ -70,7 +74,15 @@ -------------------------------------------------------------------------------- -- | Types of namespaces.-data Namespace = IPC | Network | Mount | PID | User | UTS+data Namespace+ = IPC+ | Network+ | Mount+ | PID+ | User+ | UTS+ | CGroup+ | Time deriving (Show, Read, Eq, Bounded, Enum) toCloneFlags :: Namespace -> CInt@@ -82,6 +94,12 @@ PID -> (#const CLONE_NEWPID) User -> (#const CLONE_NEWUSER) UTS -> (#const CLONE_NEWUTS)+ CGroup -> (#const CLONE_NEWCGROUP)+#ifdef CLONE_NEWTIME+ Time -> (#const CLONE_NEWTIME)+#else+ Time -> error "CLONE_NEWTIME is not defined"+#endif toProcName :: Namespace -> String toProcName ns =@@ -92,6 +110,8 @@ PID -> "pid" User -> "user" UTS -> "uts"+ CGroup -> "cgroup"+ Time -> "time" -- | Detach the process from one or more namespaces and move it to new -- ones. See the man page of @unshare(2)@ for more details.@@ -130,7 +150,11 @@ setNamespace fd (Just ns) where openFd' = ioeSetLoc "enterNamespace" $+#if MIN_VERSION_unix(2,8,0) openFd path ReadOnly defaultFileFlags {nonBlock = True}+#else+ openFd path ReadOnly Nothing defaultFileFlags {nonBlock = True}+#endif path = toProcPath (Just pid) ns -- | A unique namespace id.@@ -213,9 +237,39 @@ -------------------------------------------------------------------------------- +-- | The virtualized clock whose offset is set+-- @time_namespaces(7)@ for more details.+data Clock = Monotonic | Boottime+ deriving (Show, Read, Eq)++-- | Set the offset for a virtualized clock. This can only be called before any+-- process has been created in the time namespace. This function requires+-- @\/proc@ to be mounted. See @time_namespaces(7)@ for more details.+setClockOffset+ :: Clock -- ^ Specify the clock whose offset is set.+ -> EpochTime -- ^ The seconds component of the offset. This value+ -- can be negative.+ -> CLong -- ^ The nanoseconds component of the offset. This+ -- value must not be negative.+ -> IO ()+setClockOffset clock offsetSecs offsetNanosecs =+ ioeSetLoc "setClockOffset" $ do+ writeProcFile "/proc/self/timens_offsets" (C.pack s)+ where+ s = concat [clockId, " ", show offsetSecs, " ", show offsetNanosecs]+ clockId = case clock of+ Monotonic -> "monotonic"+ Boottime -> "boottime"++--------------------------------------------------------------------------------+ writeProcFile :: FilePath -> ByteString -> IO () writeProcFile path bs =+#if MIN_VERSION_unix(2,8,0) bracket (openFd path WriteOnly defaultFileFlags) closeFd $ \fd ->+#else+ bracket (openFd path WriteOnly Nothing defaultFileFlags) closeFd $ \fd ->+#endif S.useAsCStringLen bs $ \(ptr, nb) -> fdWriteBuf fd (castPtr ptr) (fromIntegral nb) >> return ()
linux-namespaces.cabal view
@@ -1,5 +1,5 @@ name: linux-namespaces-version: 0.1.3.1+version: 0.2.0.1 synopsis: Work with linux namespaces: create new or enter existing ones description: This library provides bindings to the @unshare(2)@ and @setns(2)@ linux@@ -23,7 +23,7 @@ library exposed-modules: System.Linux.Namespaces- build-depends: base >=4.6 && <5, unix >=2.8 && <3.0, bytestring >=0.10 && <1.0+ build-depends: base >=4.6 && <5, unix >=2.6 && <3.0, bytestring >=0.10 && <1.0 build-tools: hsc2hs default-language: Haskell2010 ghc-options: -Wall