File: Resource.hs

package info (click to toggle)
mighttpd2 4.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 192 kB
  • sloc: haskell: 1,382; makefile: 4; sh: 3
file content (49 lines) | stat: -rw-r--r-- 1,291 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE CPP #-}

module Program.Mighty.Resource (
    amIrootUser,
    setGroupUser,
    unlimit,
) where

import Control.Exception
import System.Posix

----------------------------------------------------------------

-- | Checking if this process has the root privilege.
amIrootUser :: IO Bool
amIrootUser = (== 0) <$> getRealUserID

----------------------------------------------------------------

-- | Setting user and group.
setGroupUser
    :: String
    -- ^ User
    -> String
    -- ^ Group
    -> IO Bool
setGroupUser user group = do
    root <- amIrootUser
    if root
        then do
            getGroupEntryForName group >>= setGroupID . groupID
            getUserEntryForName user >>= setUserID . userID
            return True
        else
            return False

----------------------------------------------------------------

-- | Set the limit of open files.
unlimit :: Integer -> IO ()
unlimit limit = handle (\(SomeException _) -> return ()) $ do
    hard <- hardLimit <$> getResourceLimit ResourceOpenFiles
    let lim =
            if hard == ResourceLimitInfinity
                then
                    ResourceLimits (ResourceLimit limit) hard
                else
                    ResourceLimits hard hard
    setResourceLimit ResourceOpenFiles lim