mirror of
https://github.com/gravitational/teleport.git
synced 2026-09-19 11:00:37 +08:00
* Move the reexec parts of lib/srv to session/reexec * Update references to the moved bits of lib/srv * Avoid testutils in session/reexec * Shuffle some constants around to avoid imports in session/reexec * Vendor in the relevant parts of logutils in session/reexec * Remove unnecessary symlink checks when opening files * Inline the last two things from lib/utils in session/reexec * Inline moved constants * Deprecate group name consts and fix missed inlines * Add missing session/reexec.TestMain with reexec check * Reuse existing constants from the log constants package in session * Fix broken godoc link
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
/*
|
|
* Teleport
|
|
* Copyright (C) 2025 Gravitational, Inc.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package sshutils
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"syscall"
|
|
|
|
"github.com/gravitational/teleport/session/reexec/reexecconstants"
|
|
)
|
|
|
|
// errorWithExitStatus defines an interface that provides an ExitStatus
|
|
// function to get the exit code of the process execution.
|
|
//
|
|
// This interface is introduced so ssh.ExitError can be mocked in unit test.
|
|
type errorWithExitStatus interface {
|
|
ExitStatus() int
|
|
}
|
|
|
|
// execExitError defines an interface that provides a Sys function to get exit
|
|
// status from the process execution.
|
|
//
|
|
// This interface is introduced so exec.ExitError can be mocked in unit test.
|
|
type execExitError interface {
|
|
Sys() any
|
|
}
|
|
|
|
// ExitCodeFromExecError extracts and returns the exit code from the
|
|
// error.
|
|
func ExitCodeFromExecError(err error) int {
|
|
// If no error occurred, return 0 (success).
|
|
if err == nil {
|
|
return reexecconstants.RemoteCommandSuccess
|
|
}
|
|
|
|
var execExitErr execExitError
|
|
var exitErr errorWithExitStatus
|
|
switch {
|
|
case errors.As(err, &execExitErr):
|
|
waitStatus, ok := execExitErr.Sys().(syscall.WaitStatus)
|
|
if !ok {
|
|
return reexecconstants.RemoteCommandFailure
|
|
}
|
|
return waitStatus.ExitStatus()
|
|
case errors.As(err, &exitErr):
|
|
return exitErr.ExitStatus()
|
|
// An error occurred, but the type is unknown, return a generic 255 code.
|
|
default:
|
|
slog.DebugContext(context.Background(), "Unknown error returned when executing command", "error", err)
|
|
return reexecconstants.RemoteCommandFailure
|
|
}
|
|
}
|