|
| 1 | +// +build linux |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright The containerd Authors. |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package command |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "os" |
| 24 | + |
| 25 | + sd "github.com/coreos/go-systemd/daemon" |
| 26 | + |
| 27 | + "github.com/containerd/containerd/log" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + // SdNotifyReady tells the service manager that service startup is finished |
| 32 | + // or the service finished loading its configuration. |
| 33 | + SdNotifyReady = "READY=1" |
| 34 | + |
| 35 | + // SdNotifyStopping tells the service manager that the service is beginning |
| 36 | + // its shutdown. |
| 37 | + SdNotifyStopping = "STOPPING=1" |
| 38 | +) |
| 39 | + |
| 40 | +// notifyReady notifies systemd that the daemon is ready to serve requests |
| 41 | +func notifyReady(ctx context.Context) error { |
| 42 | + return sdNotify(ctx, SdNotifyReady) |
| 43 | +} |
| 44 | + |
| 45 | +// notifyStopping notifies systemd that the daemon is about to be stopped |
| 46 | +func notifyStopping(ctx context.Context) error { |
| 47 | + return sdNotify(ctx, SdNotifyStopping) |
| 48 | +} |
| 49 | + |
| 50 | +func sdNotify(ctx context.Context, state string) error { |
| 51 | + if os.Getenv("NOTIFY_SOCKET") != "" { |
| 52 | + notified, err := sd.SdNotify(false, state) |
| 53 | + log.G(ctx). |
| 54 | + WithError(err). |
| 55 | + WithField("notified", notified). |
| 56 | + WithField("state", state). |
| 57 | + Debug("sd notification") |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
0 commit comments