Browse code

daemon: translateContainerdStartErr(): use const/enum for exit-statuses

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2022/02/17 04:07:34
Showing 2 changed files
... ...
@@ -132,7 +132,20 @@ func (e startInvalidConfigError) Error() string {
132 132
 
133 133
 func (e startInvalidConfigError) InvalidParameter() {} // Is this right???
134 134
 
135
-func translateContainerdStartErr(setExitCode func(int), err error) error {
135
+// exitStatus is the exit-code as set by translateContainerdStartErr
136
+type exitStatus = int
137
+
138
+const (
139
+	exitEaccess     exitStatus = 126 // container cmd can't be invoked (permission denied)
140
+	exitCmdNotFound exitStatus = 127 // container cmd not found/does not exist or invalid bind-mount
141
+	exitUnknown     exitStatus = 128 // unknown error
142
+)
143
+
144
+// translateContainerdStartErr converts the error returned by containerd
145
+// when starting a container, and applies the corresponding exitStatus to the
146
+// container. It returns an errdefs error (either errdefs.ErrInvalidParameter
147
+// or errdefs.ErrUnknown).
148
+func translateContainerdStartErr(setExitCode func(exitStatus), err error) error {
136 149
 	errDesc := status.Convert(err).Message()
137 150
 	contains := func(s1, s2 string) bool {
138 151
 		return strings.Contains(strings.ToLower(s1), s2)
... ...
@@ -140,24 +153,24 @@ func translateContainerdStartErr(setExitCode func(int), err error) error {
140 140
 	var retErr = errdefs.Unknown(errors.New(errDesc))
141 141
 	// if we receive an internal error from the initial start of a container then lets
142 142
 	// return it instead of entering the restart loop
143
-	// set to 127 for container cmd not found/does not exist)
143
+	// set to 127 for container cmd not found/does not exist.
144 144
 	if contains(errDesc, "executable file not found") ||
145 145
 		contains(errDesc, "no such file or directory") ||
146 146
 		contains(errDesc, "system cannot find the file specified") ||
147 147
 		contains(errDesc, "failed to run runc create/exec call") {
148
-		setExitCode(127)
148
+		setExitCode(exitCmdNotFound)
149 149
 		retErr = startInvalidConfigError(errDesc)
150 150
 	}
151 151
 	// set to 126 for container cmd can't be invoked errors
152 152
 	if contains(errDesc, syscall.EACCES.Error()) {
153
-		setExitCode(126)
153
+		setExitCode(exitEaccess)
154 154
 		retErr = startInvalidConfigError(errDesc)
155 155
 	}
156 156
 
157 157
 	// attempted to mount a file onto a directory, or a directory onto a file, maybe from user specified bind mounts
158 158
 	if contains(errDesc, syscall.ENOTDIR.Error()) {
159 159
 		errDesc += ": Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type"
160
-		setExitCode(127)
160
+		setExitCode(exitCmdNotFound)
161 161
 		retErr = startInvalidConfigError(errDesc)
162 162
 	}
163 163
 
... ...
@@ -124,7 +124,7 @@ func (daemon *Daemon) containerStart(ctx context.Context, container *container.C
124 124
 			container.SetError(retErr)
125 125
 			// if no one else has set it, make sure we don't leave it at zero
126 126
 			if container.ExitCode() == 0 {
127
-				container.SetExitCode(128)
127
+				container.SetExitCode(exitUnknown)
128 128
 			}
129 129
 			if err := container.CheckpointTo(daemon.containersReplica); err != nil {
130 130
 				logrus.Errorf("%s: failed saving state on start failure: %v", container.ID, err)