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

Remove the check for root when no pids are specified #7

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading