186 Commits

Author SHA1 Message Date
8754687b55 libpod: move linux-specific code to _linux.go
This fixes a few "unused" linter warnings on freebsd.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-02 13:35:14 -07:00
5aa035c69c libpod: fix a few minor staticcheck warnings
These:

> libpod/container_copy_common.go:34:16: QF1011: could omit type bool from declaration; it will be inferred from the right-hand side (staticcheck)
> 		locked       bool = true
> 		             ^
> libpod/container_internal_common.go:793:3: QF1006: could lift into loop condition (staticcheck)
> 		if maxSymLinks > 40 {
> 		^
> libpod/oci_conmon_linux.go:170:2: QF1007: could merge conditional assignment into variable declaration (staticcheck)
> 	mustCreateCgroup := true
> 	^

Should not result in any change of logic.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-03-31 12:27:55 -07:00
999a11c8b1 replace deprecated selinux/label calls
These functions were removed in github.com/opencontainers/selinux
v1.12.0.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-03-24 17:44:43 +01:00
88b62d2c27 vendor: update c/common
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2025-03-20 13:37:19 +01:00
a2953dad10 libpod: improve createRootlessContainer
do not run the expensive pmount.GetMounts() function if it is not
needed.

As a follow-up for commit c9c44d400c2870e9a6c966647be1c414dc773b66, do
not restore the propagation flag for the parent mount to shared unless
it was changed to slave first.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2025-03-13 16:55:11 +01:00
c9c44d400c libpod: do not cover idmapped mountpoint
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2025-03-11 11:03:41 +01:00
49eb5af301 libpod: intermediate mount if UID not mapped into the userns
if the current user is not mapped into the new user namespace, use an
intermediate mount to allow the mount point to be accessible instead
of opening up all the parent directories for the mountpoint.

Closes: https://github.com/containers/podman/issues/23028

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-06-21 18:01:26 +02:00
094bc673ef libpod: unlock the thread if possible
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-06-21 18:01:26 +02:00
72f1617fac Bump Go module to v5
Moving from Go module v4 to v5 prepares us for public releases.

Move done using gomove [1] as with the v3 and v4 moves.

[1] https://github.com/KSubedi/gomove

Signed-off-by: Matt Heon <mheon@redhat.com>
2024-02-08 09:35:39 -05:00
2a2d0b0e18 chore: delete obsolete // +build lines
Signed-off-by: Oleksandr Redko <Oleksandr_Redko@epam.com>
2024-01-04 11:53:38 +02:00
03d411abc0 libpod: split out cgroups call into linux specific file
So that we do not cause compile error on freebsd.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-12-07 11:24:47 +01:00
a687c38860 use rootless netns from c/common
Use the new rootlessnetns logic from c/common, drop the podman code
here and make use of the new much simpler API.

ref: https://github.com/containers/common/pull/1761

[NO NEW TESTS NEEDED]

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-12-07 11:24:46 +01:00
18d6bb40d5 Support passing of Ulimits as -1 to mean max
Docker allows the passing of -1 to indicate the maximum limit
allowed for the current process.

Fixes: https://github.com/containers/podman/issues/19319

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2023-11-01 08:46:55 -04:00
bad25da92e libpod: add !remote tag
This should never be pulled into the remote client.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-10-24 12:11:34 +02:00
c726cf8106 libpod: improve conmon error handling
When conmon is started it blocks and waits for us to signal it to start
via pipe. This works but when conmon exits before it waits for the start
message it causes podman to fail with `write child: broken pipe`. This
error is meaningless to podman users.

The real error is that conmon failed so we should not return early if we
fail to send the start message to conmon. Instead ignore the EPIPE error
case as it is safe to assume to the conmon died and for other errors we
make sure to kill conmon so that the following wait() call does not hang
forever. This also fixes problems with having conmon zombie processes
leaked as wait() was never called.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-08-17 15:32:59 +02:00
a3a62275c8 libpod: use new libcontainer BlockIO constructors
[NO NEW TESTS NEEDED]

Signed-off-by: Peter Hunt <pehunt@redhat.com>
2023-07-03 15:11:35 -04:00
55191ecc20 Add and use Container.LinuxResource() helper
This gets c.config.Spec.Linux.Resources, with some nil checks.
Using this means less open coding of the nil-checks, but also the
existing user of this field in moveConmonToCgroupAndSignal() was
using ctr.Spec().Linux.Resources instead, and the Spec() call
is very expensive.

[NO NEW TESTS NEEDED] Just minor performance effects

Signed-off-by: Alexander Larsson <alexl@redhat.com>
2022-10-12 13:45:49 +02:00
d43fac20f3 libpod: Move moveConmonToCgroupAndSignal and GetLimits to oci_conmon_linux.go
[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-08-18 08:05:42 +01:00
93bad90486 libpod: Move socket label handling from oci_conmon_common.go to oci_conmon_linux.go
[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-08-18 08:05:42 +01:00
6791cdbdf1 libpod: Move rootless handling from oci_conmon_common.go to oci_conmon_linux.go
[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-08-18 08:05:42 +01:00
bebf55c0f2 libpod: Move oci_conmon_linux.go to oci_conmon_common.go
[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-08-18 08:05:42 +01:00
3fc126e152 libpod: allow the notify socket to be passed programatically
The notify socket can now either be specified via an environment
variable or programatically (where the env is ignored).  The
notify mode and the socket are now also displayed in `container inspect`
which comes in handy for debugging and allows for propper testing.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-08-10 21:10:17 +02:00
ffbc2adf70 fix wrong log message on Trace level
[NO NEW TESTS NEEDED]

Empty path to runtime binary was printed instead of a real path.

Before fix:
TRAC[0000] found runtime ""
TRAC[0000] found runtime ""

After:
TRAC[0000] found runtime "/usr/bin/crun"
TRAC[0000] found runtime "/usr/bin/runc"

Signed-off-by: Mikhail Khachayants <khachayants@arrival.com>
2022-07-12 10:37:56 +03:00
377057b400 [CI:DOCS] Improve language. Fix spelling and typos.
* Correct spelling and typos.

* Improve language.

Co-authored-by: Ed Santiago <santiago@redhat.com>
Signed-off-by: Erik Sjölund <erik.sjolund@gmail.com>
2022-07-11 21:59:32 +02:00
62cdc387de podman wait: return 0 if container never ran
Make sure to return/exit with 0 when waiting for a container that never
ran.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-07-11 13:06:40 +02:00
cc6faddfaa use c/common code for resize and CopyDetachable
Since conmon-rs also uses this code we moved it to c/common. Now podman
should has this also to prevent duplication.

[NO NEW TESTS NEEDED]

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2022-07-06 16:57:07 +02:00
251d91699d libpod: switch to golang native error wrapping
We now use the golang error wrapping format specifier `%w` instead of
the deprecated github.com/pkg/errors package.

[NO NEW TESTS NEEDED]

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
2022-07-05 16:06:32 +02:00
088665d2cf Merge pull request #14654 from cdoern/cgroup
podman cgroup enhancement
2022-06-27 15:14:20 +00:00
2792e598c7 podman cgroup enhancement
currently, setting any sort of resource limit in a pod does nothing. With the newly refactored creation process in c/common, podman ca now set resources at a pod level
meaning that resource related flags can now be exposed to podman pod create.

cgroupfs and systemd are both supported with varying completion. cgroupfs is a much simpler process and one that is virtually complete for all resource types, the flags now just need to be added. systemd on the other hand
has to be handeled via the dbus api meaning that the limits need to be passed as recognized properties to systemd. The properties added so far are the ones that podman pod create supports as well as `cpuset-mems` as this will
be the next flag I work on.

Signed-off-by: Charlie Doern <cdoern@redhat.com>
2022-06-24 15:39:15 -04:00
30e7cbccc1 libpod: fix wait and exit-code logic
This commit addresses three intertwined bugs to fix an issue when using
Gitlab runner on Podman.  The three bug fixes are not split into
separate commits as tests won't pass otherwise; avoidable noise when
bisecting future issues.

1) Podman conflated states: even when asking to wait for the `exited`
   state, Podman returned as soon as a container transitioned to
   `stopped`.  The issues surfaced in Gitlab tests to fail [1] as
   `conmon`'s buffers have not (yet) been emptied when attaching to a
   container right after a wait.  The race window was extremely narrow,
   and I only managed to reproduce with the Gitlab runner [1] unit
   tests.

2) The clearer separation between `exited` and `stopped` revealed a race
   condition predating the changes.  If a container is configured for
   autoremoval (e.g., via `run --rm`), the "run" process competes with
   the "cleanup" process running in the background.  The window of the
   race condition was sufficiently large that the "cleanup" process has
   already removed the container and storage before the "run" process
   could read the exit code and hence waited indefinitely.

   Address the exit-code race condition by recording exit codes in the
   main libpod database.  Exit codes can now be read from a database.
   When waiting for a container to exit, Podman first waits for the
   container to transition to `exited` and will then query the database
   for its exit code. Outdated exit codes are pruned during cleanup
   (i.e., non-performance critical) and when refreshing the database
   after a reboot.  An exit code is considered outdated when it is older
   than 5 minutes.

   While the race condition predates this change, the waiting process
   has apparently always been fast enough in catching the exit code due
   to issue 1): `exited` and `stopped` were conflated.  The waiting
   process hence caught the exit code after the container transitioned
   to `stopped` but before it `exited` and got removed.

3) With 1) and 2), Podman is now waiting for a container to properly
   transition to the `exited` state.  Some tests did not pass after 1)
   and 2) which revealed the third bug: `conmon` was executed with its
   working directory pointing to the OCI runtime bundle of the
   container.  The changed working directory broke resolving relative
   paths in the "cleanup" process.  The "cleanup" process error'ed
   before actually cleaning up the container and waiting "main" process
   ran indefinitely - or until hitting a timeout.  Fix the issue by
   executing `conmon` with the same working directory as Podman.

Note that fixing 3) *may* address a number of issues we have seen in the
past where for *some* reason cleanup processes did not fire.

[1] https://gitlab.com/gitlab-org/gitlab-runner/-/issues/27119#note_970712864

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>

[MH: Minor reword of commit message]

Signed-off-by: Matthew Heon <mheon@redhat.com>
2022-06-23 09:11:57 -04:00
15188dce05 conmon: silence json-file error
We should just silently fall through.  The log was flooding the
system-service logs when running Gitlab runner.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-06-23 09:11:57 -04:00
ed755cb14e Merge pull request #14596 from giuseppe/move-conmon-different-cgroup-system-service
libpod: improve check to create conmon cgroup
2022-06-15 16:18:18 +00:00
751ba07a76 libpod: improve check to create conmon cgroup
commit 1951ff168a63157fa2f4711fde283edfc4981ed3 introduced a check so
that conmon is not moved to a new cgroup when podman is running inside
of a systemd service.  This is helpful to integrate podman in systemd
so that the spawned conmon lives in the same cgroup as the service
that created it.

Unfortunately this breaks when podman daemon is running in a systemd
service since the same check is in place thus all the conmon processes
end up in the same cgroup as the podman daemon.  When the podman
daemon systemd service stops the conmon processes are also terminated
as well as the containers they monitor.

Improve the check to exclude podman running as a daemon.

Closes: https://bugzilla.redhat.com/show_bug.cgi?id=2052697

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2022-06-15 15:43:12 +02:00
41528739ce golangci-lint: enable nolintlint
The nolintlint linter does not deny the use of `//nolint`
Instead it allows us to enforce a common nolint style:
- force that a linter name must be specified
- do not add a space between `//` and `nolint`
- make sure nolint is only used when there is actually a problem

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2022-06-14 16:29:42 +02:00
c77691f06f Do not error on signalling a just-stopped container
Previous PR #12394 tried to address this, but made a mistake:
containers that have just exited do not move to the Exited state
but rather the Stopped state - as such, the code would never have
run (there is no way we start `podman kill`, and the container
transitions to Exited while we are doing it - that requires
holding the container lock, which Kill already does).

Fix the code to check Stopped as well (we omit Exited entirely
but it's a cheap check and our state logic could change in the
future). Also, return an error, instead of exiting cleanly - the
Kill failed, after all. ErrCtrStateInvalid is already handled by
the sig-proxy logic so there won't be issues.

[NO NEW TESTS NEEDED] This fixes a race that I cannot reproduce
myself, and I have no idea how we'd repro in CI.

Signed-off-by: Matthew Heon <mheon@redhat.com>
2022-06-09 09:11:18 -04:00
03af8213ce sdnotify: send MAINPID only once
Send the main PID only once.  Previously, `(*Container).start()` and
the conmon handler sent them ~simultaneously and went into a race.

I noticed the issue while debugging a WIP PR.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-05-12 11:11:37 +02:00
51fbf3da9e enable gocritic linter
The linter ensures a common code style.
- use switch/case instead of else if
- use if instead of switch/case for single case statement
- add space between comment and text
- detect the use of defer with os.Exit()
- use short form var += "..." instead of var = var + "..."
- detect problems with append()
```
newSlice := append(orgSlice, val)
```
  This could lead to nasty bugs because the orgSlice will be changed in
  place if it has enough capacity too hold the new elements. Thus we
  newSlice might not be a copy.

Of course most of the changes are just cosmetic and do not cause any
logic errors but I think it is a good idea to enforce a common style.
This should help maintainability.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2022-04-26 18:12:22 +02:00
c7b16645af enable unparam linter
The unparam linter is useful to detect unused function parameters and
return values.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2022-04-25 13:23:20 +02:00
e5d86aa35b Merge pull request #13093 from 0xC0ncord/selinux-conmon-agnostic
selinux: remove explicit range transition when starting conmon
2022-04-12 03:38:09 -04:00
c185d8c0d6 Add option for pod logs to display different colors per container.
Signed-off-by: Krzysztof Baran <krysbaran@gmail.com>
Signed-off-by: gcalin <caling@protonmail.com>
2022-03-29 17:29:13 +02:00
7ae52e86ff readConmonPipeData: try to improve error
Issue #10927 reports `container create failed (no logs from conmon): EOF`
errors. Since we do not know the root cause it would be helpful to try
to get as much info as possible out of the error.
(buffer).ReadBytes() will return the bytes read even when an error
occurs. So when we get an EOF we could still have some valuable
information in the buffer. Lets try to unmarshal them and if this fails
we add the bytes to the error message.

This does not fix the issue but it might help us getting a better error.

[NO NEW TESTS NEEDED]

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2022-03-24 12:06:48 +01:00
0f12b6fe55 linter: enable nilerr
A number of cases looked suspicious, so I marked them with `FIXME`s to
leave some breadcrumbs.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-03-22 13:04:35 +01:00
bb6b69b4ab linter: enable wastedassign
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-03-22 13:04:34 +01:00
026bd9b205 bump golangci-lint to v1.45.0
* supports Go 1.18
* disable a number of new linters
* fix minor stylecheck issues

[NO NEW TESTS NEEDED]

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-03-21 10:42:53 +01:00
f8b04bf56c Merge pull request #13552 from vrothberg/go1.18
go fmt: use go 1.18 conditional-build syntax
2022-03-18 11:24:58 +01:00
ea08765f40 go fmt: use go 1.18 conditional-build syntax
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-03-18 09:11:53 +01:00
1fd76c901f podman machine: remove hostip from port
Inside the podman machine vm we always remove the hostip from the port
mapping because this should only be used on the actual host. Otherwise
you run into issues when we would bind 127.0.0.1 or try to bind a
host address that is not available in the VM.

This was already done for cni/netavark ports and slirp4netns but not for
the port bindings inside libpod which are only used as root.

[NO NEW TESTS NEEDED] We still do not have machine tests!

Fixes #13543

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2022-03-17 18:59:55 +01:00
bd0766e966 selinux: remove explicit range transition when starting conmon
Do not explicitly transition to s0 when starting conmon. Instead, the
policy should implement this behavior.

[NO NEW TESTS NEEDED]
This is dependent on the SELinux policy to implement the desired
behavior. Additionally, entirely custom SELinux policies may choose to
implement the behavior differently.

Signed-off-by: Kenton Groombridge <me@concord.sh>
2022-03-08 10:57:26 -05:00
db4d15e852 Propagate $CONTAINERS_CONF to conmon
The CONTAINERS_CONF environment variable can be used to override the
configuration file, which is useful for testing.  However, at the moment
this variable is not propagated to conmon.  That means in particular, that
conmon can't propagate it back to podman when invoking its --exit-command.
The mismatch in configuration between the starting and cleaning up podman
instances can cause a variety of errors.

This patch also adds two related test cases.  One checks explicitly that
the correct CONTAINERS_CONF value appears in conmon's environment.  The
other checks for a possible specific impact of this bug: if we use a
nonstandard name for the runtime (even if its path is just a regular crun),
then the podman container cleanup invoked at container exit will fail.
That has the effect of meaning that a container started with -d --rm won't
be correctly removed once complete.

Fixes #12917

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2022-02-18 12:35:25 +11:00
7e30531f20 compat attach: fix write on closed channel
Waiting on an initialized sync.WaitGroup returns immediately.
Hence, move the goroutine to wait and close *after* reading
the logs.

Fixes: #12904
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2022-01-18 16:32:38 +01:00