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

Variable naming error #2

Open
wants to merge 3 commits into
base: fix-top
Choose a base branch
from

Conversation

lifubang
Copy link

errorNoPID -> errorNoPid

kolyshkin and others added 3 commits September 12, 2018 14:01
In case a user specifies arguments such as -C or f, optimization
introduced by commit a076bad can't be used, so the code falls back
to listing all the processing and filtering those with PIDs of the
container specified.

In case a user also specifies -o with some fields that may contain
spaces, and those fields are before the PID field (such as -o comm,pid),
PID filtering is broken. In the worst case scenario, command can
contain numeric arguments which are then parsed as PIDs, leading to
showing some processes not belonging to the container specified.

To fix, we need to
1. Check if ps arguments contain -o. If true:
2.1. Add pid, to the beginning of -o argument (so the first field is PID).
2.2. Use the first field to obtain PID for filtering purposes.
2.3. Remove the first field from the output.

This fixes two bugs:
 - inability to use -o if it does not contain pid;
 - wrongly listing processes not belonging to a container.

In addition, now, since we are adding our own PID column, any
tricks that a malicious user might do with column naming (such
as adding `-ouid=PID`) won't work anymore, so we can drop
the option validation entirely, as well as its test case!

Big thanks to https://github.com/lifubang for analysis and
preliminary patches (see moby#37748).

To test:

```
ID=$(docker run -d busybox top)
docker top -ocmd $ID
docker top -o cmd $ID
docker top -C top -o cmd $ID
docker rm -f $ID
```

[v2: don't add pid if it's the first field already,
     add pid field even for the first ps (with q) run]

[v3: add o and --format handling, remove validatePSArgs]
[v4: make sure PID is the first column if we added it]
[v5: improve error return]

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: Lifubang <lifubang@acmcoder.com>
@kolyshkin kolyshkin force-pushed the fix-top branch 11 times, most recently from 182834e to 6d43175 Compare September 19, 2018 05:03
kolyshkin added a commit that referenced this pull request Nov 28, 2018
When go-1.11beta1 is used for building, the following error is
reported:

> 14:56:20 daemon\graphdriver\lcow\lcow.go:236: Debugf format %s reads
> arg #2, but call has 1 arg

While fixing this, let's also fix a few other things in this
very function (startServiceVMIfNotRunning):

1. Do not use fmt.Printf when not required.
2. Use `title` whenever possible.
3. Don't add `id` to messages as `title` already has it.
4. Remove duplicated colons.
5. Try to unify style of messages.
6. s/startservicevmifnotrunning/startServiceVMIfNotRunning/
...

In general, logging/debugging here is a mess and requires much more
love than I can give it at the moment. Areas for improvement:

1. Add a global var logger = logrus.WithField("storage-driver", "lcow")
and use it everywhere else in the code.
2. Use logger.WithField("id", id) whenever possible (same for "context"
and other similar fields).
3. Revise all the errors returned to be uniform.
4. Make use of errors.Wrap[f] whenever possible.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants