Skip to content

Commit

Permalink
Merge pull request #7 from adetokunbo/remove-root-check
Browse files Browse the repository at this point in the history
Remove the check for root when no pids are specified
  • Loading branch information
adetokunbo authored Jan 31, 2024
2 parents 2c88148 + e4b201a commit e30681b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

`mem-info` uses [PVP Versioning][1].

## Unreleased -- 2024-01-31

- Remove the check for __root__ when no pids are specified

- previously, an error occurred if no pids were specified without sudo
- after this, all processes of the current user are shown

## 0.2.0.0 -- 2024-01-28

- Simplify the output when the -d (--discriminate-by-pid) flag is used
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ re-implementation allows its behaviour to be used as library code in haskell
programs.

It provides an executable command `printmem`, that mimics `ps_mem` while adding
new features, and `mem-info`, a haskell library package.
[new features], and `mem-info`, a haskell library package.

## Rationale

Expand Down Expand Up @@ -44,7 +44,7 @@ common per-process breakdown of the RAM measurements.

You can download a pre-built binary from [releases]. It is a statically-linked executable that should run on most recent Linux distributions.

Download it, place it in a directory on your path and give it executable permissions
Download it, place it in a directory on your path and give it executable permissions

E.g, the following commands should suffice

Expand Down Expand Up @@ -84,19 +84,21 @@ Available options:
### Example output

You can run `printmem` *without* filtering; this will try to display data for
all running processes, so sudo is required
all running processes accessible by the current user

```sudo printmem```
```printmem```

Usually, you'll want to filter the results. which is supported by the `-p <pid>`
option. This can be specified multiple times to select multiple processes. [pgrep] is great companion tool for obtaining the specific sets of pids for filtering.
Often, you'll want to filter the results. which is supported by the `-p <pid>`
option. This can be specified multiple times to select multiple processes.
[pgrep] is great companion tool for obtaining the specific sets of pids for
filtering.

#### Example: breakdown the memory use of the current user
#### Example: breakdown the memory use of a different user

To restrict output to the current $USER, you can obtain the user's process IDs using pgrep comme ca
To restrict output to the specific user, you can obtain the user's process IDs using pgrep comme ca

```
sudo printmem -S -p $(pgrep -d' -p ' -u $USER)
sudo printmem -S -p $(pgrep -d' -p ' -u <other-username>)
```

giving output like this:
Expand Down Expand Up @@ -220,3 +222,4 @@ main = monitorRamOf $ 96334 :| [96335]-- replace with your own process IDs
[releases]: <https://github.com/adetokunbo/mem-info/releases>
[nix]: <https://nixos.org/manual/nix/stable/installation/installation>
[cabal]: <https://cabal.readthedocs.io/en/stable/index.html>
[new features]: <https://hackage.haskell.org/package/mem-info/changelog>
10 changes: 1 addition & 9 deletions src/System/MemInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ import System.MemInfo.SysInfo (
fmtSwapFlaws,
mkReportBud,
)
import System.Posix.User (getEffectiveUserID)


{- | Print a report to @stdout@ displaying the memory usage of the programs
Expand Down Expand Up @@ -324,7 +323,7 @@ verify' pidsMb = do
thenMkBud = either (pure . Left) mkBud'
case pidsMb of
Just pids -> checkAllExist pids >>= thenMkBud
Nothing -> whenRoot $ allKnownProcs >>= thenMkBud
Nothing -> allKnownProcs >>= thenMkBud


procRoot :: String
Expand All @@ -335,13 +334,6 @@ pidPath :: String -> ProcessID -> FilePath
pidPath base pid = "" +| procRoot |++| toInteger pid |+ "/" +| base |+ ""


whenRoot :: IO (Either NotRun a) -> IO (Either NotRun a)
whenRoot action = do
-- if choicePidsToShow is Nothing, must be running as root
isRoot' <- (== 0) <$> getEffectiveUserID
if isRoot' then action else pure $ Left NeedsRoot


{- | pidExists returns false for any ProcessID that does not exist or cannot
be accessed
-}
Expand Down

0 comments on commit e30681b

Please sign in to comment.