|
| 1 | +// Copyright 2025 The gVisor Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package eventfd |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "golang.org/x/sys/unix" |
| 21 | + "gvisor.dev/gvisor/pkg/log" |
| 22 | +) |
| 23 | + |
| 24 | +func (efd *EventFileDescription) beforeSave() { |
| 25 | + if efd.hostfd < 0 { |
| 26 | + return |
| 27 | + } |
| 28 | + if !efd.sentryOwnedHostfd { |
| 29 | + panic("EventFileDescription.beforeSave: hostfd is not owned by the sentry") |
| 30 | + } |
| 31 | + var buf [8]byte |
| 32 | + if _, err := unix.Read(efd.hostfd, buf[:]); err != nil { |
| 33 | + log.Warningf("Failed to read host fd for eventfd: %v", err) |
| 34 | + return |
| 35 | + } |
| 36 | + copy(efd.hostfdState[:], buf[:]) |
| 37 | +} |
| 38 | + |
| 39 | +func (efd *EventFileDescription) afterLoad(ctx context.Context) { |
| 40 | + if efd.hostfd < 0 { |
| 41 | + return |
| 42 | + } |
| 43 | + efd.hostfd = -1 |
| 44 | + if _, err := efd.HostFD(); err != nil { |
| 45 | + log.Warningf("Failed to create host fd for eventfd: %v", err) |
| 46 | + return |
| 47 | + } |
| 48 | + if _, err := unix.Write(efd.hostfd, efd.hostfdState[:]); err != nil { |
| 49 | + log.Warningf("Failed to write host fd for eventfd: %v", err) |
| 50 | + } |
| 51 | +} |
0 commit comments