Fix: avoid silent fatalities

demo: https://tcp.ac/i/JMSUc.gif
This commit is contained in:
kayos@tcp.direct 2024-06-17 00:30:24 -07:00
parent 3337efcb8f
commit 395f5ab6bc
No known key found for this signature in database
GPG key ID: 4B841471B4BEE979
13 changed files with 115 additions and 78 deletions

View file

@ -8,9 +8,9 @@ import (
"regexp"
"strings"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/klauspost/cpuid/v2"
"github.com/HikariKnight/quickpassthrough/internal/common"
"github.com/HikariKnight/quickpassthrough/internal/logger"
"github.com/HikariKnight/quickpassthrough/pkg/command"
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
@ -82,7 +82,7 @@ func Set_KernelStub(isRoot bool) {
kernel_args := fileio.ReadFile(config.Path.CMDLINE)
// Run and log, check for errors
errorcheck.ErrorCheck(command.ExecAndLogSudo(isRoot, true,
common.ErrorCheck(command.ExecAndLogSudo(isRoot, true,
"kernelstub -a "+kernel_args,
),
"Error, kernelstub command returned exit code 1",
@ -99,7 +99,7 @@ func Set_Grubby(isRoot bool) string {
// Run and log, check for errors
err := command.ExecAndLogSudo(isRoot, true, "grubby --update-kernel=ALL "+fmt.Sprintf("--args=%s", kernel_args))
errorcheck.ErrorCheck(err, "Error, grubby command returned exit code 1")
common.ErrorCheck(err, "Error, grubby command returned exit code 1")
// Return what we did
return fmt.Sprintf("Executed: sudo grubby --update-kernel=ALL --args=\"%s\"", kernel_args)
@ -233,11 +233,11 @@ func Set_Grub2(isRoot bool) error {
}
if lpErr == nil {
// we know mkconfig is empty despite no error;
// so set an error for [errorcheck.ErrorCheck].
// so set an error for [common.ErrorCheck].
lpErr = errors.New("neither grub-mkconfig or grub2-mkconfig found")
}
errorcheck.ErrorCheck(lpErr, lpErr.Error()+"\n")
return lpErr // note: unreachable as [errorcheck.ErrorCheck] calls fatal
common.ErrorCheck(lpErr, lpErr.Error()+"\n")
return lpErr // note: unreachable as [common.ErrorCheck] calls fatal
default:
}
@ -245,9 +245,9 @@ func Set_Grub2(isRoot bool) error {
// tabulate the output, [command.RunErrSudo] logged the execution.
logger.Printf("\t" + strings.Join(mklog, "\n\t"))
errorcheck.ErrorCheck(err, "Failed to update /boot/grub/grub.cfg")
common.ErrorCheck(err, "Failed to update /boot/grub/grub.cfg")
// always returns nil as [errorcheck.ErrorCheck] calls fatal
// always returns nil as [common.ErrorCheck] calls fatal
// keeping the ret signature, as we should consider passing down errors
// but that's a massive rabbit hole to go down for this codebase as a whole
return err

View file

@ -7,7 +7,7 @@ import (
"regexp"
"strings"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/HikariKnight/quickpassthrough/internal/common"
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
)
@ -15,7 +15,7 @@ import (
func initramfs_readHeader(lines int, fileName string) string {
// Open the file
f, err := os.Open(fileName)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening %s", fileName))
common.ErrorCheck(err, fmt.Sprintf("Error opening %s", fileName))
defer f.Close()
header_re := regexp.MustCompile(`^#`)
@ -50,7 +50,7 @@ func initramfs_addModules(conffile string) {
// Open the system file for reading
sysfile, err := os.Open(syspath)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening file for reading %s", syspath))
common.ErrorCheck(err, fmt.Sprintf("Error opening file for reading %s", syspath))
defer sysfile.Close()
// Check if user has vendor-reset installed/enabled and make sure that is first

View file

@ -6,7 +6,7 @@ import (
"path/filepath"
"strings"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/HikariKnight/quickpassthrough/internal/common"
"github.com/HikariKnight/quickpassthrough/internal/logger"
)
@ -55,12 +55,12 @@ func GenerateVBIOSDumper(vbios_path string) {
// Make the script file
scriptfile, err := os.Create("utils/dump_vbios.sh")
errorcheck.ErrorCheck(err, "Cannot create file \"utils/dump_vbios.sh\"")
common.ErrorCheck(err, "Cannot create file \"utils/dump_vbios.sh\"")
defer scriptfile.Close()
// Make the script executable
scriptfile.Chmod(0775)
errorcheck.ErrorCheck(err, "Could not change permissions of \"utils/dump_vbios.sh\"")
common.ErrorCheck(err, "Could not change permissions of \"utils/dump_vbios.sh\"")
// Write to logger
logger.Printf("Writing utils/dump_vbios.sh\n")

View file

@ -5,7 +5,7 @@ import (
"os"
"strings"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/HikariKnight/quickpassthrough/internal/common"
"github.com/HikariKnight/quickpassthrough/internal/logger"
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
)
@ -26,7 +26,7 @@ func DisableVFIOVideo(i int) {
if strings.Contains(kernel_args, "vfio_pci.disable_vga") {
// Remove the old file
err := os.Remove(config.Path.CMDLINE)
errorcheck.ErrorCheck(err, fmt.Sprintf("Could not rewrite %s", config.Path.CMDLINE))
common.ErrorCheck(err, fmt.Sprintf("Could not rewrite %s", config.Path.CMDLINE))
// Enable or disable the VGA based on our given value
if i == 0 {

View file

@ -6,7 +6,6 @@ import (
"os"
"regexp"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/klauspost/cpuid/v2"
"github.com/HikariKnight/quickpassthrough/internal/common"
@ -83,21 +82,21 @@ func InitConfigs() {
// Remove old config
if err := os.RemoveAll("config"); err != nil && !errors.Is(err, os.ErrNotExist) {
if errors.Is(err, os.ErrPermission) {
errorcheck.ErrorCheck(err, common.PermissionNotice)
common.ErrorCheck(err, common.PermissionNotice)
return // note: unreachable due to ErrorCheck calling fatal
}
// won't be called if the error is ErrNotExist
errorcheck.ErrorCheck(err, "\nError removing old config")
common.ErrorCheck(err, "\nError removing old config")
}
// Make the config folder
if err := os.Mkdir("config", os.ModePerm); err != nil && !errors.Is(err, os.ErrExist) {
if errors.Is(err, os.ErrPermission) {
errorcheck.ErrorCheck(err, common.PermissionNotice)
common.ErrorCheck(err, common.PermissionNotice)
return // note: unreachable due to ErrorCheck calling fatal
}
// won't be called if the error is ErrExist
errorcheck.ErrorCheck(err, "\nError making config folder")
common.ErrorCheck(err, "\nError making config folder")
}
// Make a regex to get the system path instead of the config path
@ -113,10 +112,10 @@ func InitConfigs() {
// If we received an error that is not ErrNotExist
if err != nil {
if errors.Is(err, os.ErrPermission) {
errorcheck.ErrorCheck(err, common.PermissionNotice)
common.ErrorCheck(err, common.PermissionNotice)
return // note: unreachable due to ErrorCheck calling fatal
}
errorcheck.ErrorCheck(err, "\nError checking for directory: "+syspath)
common.ErrorCheck(err, "\nError checking for directory: "+syspath)
continue // note: also unreachable
}
@ -136,10 +135,10 @@ func InitConfigs() {
// Create the directories for our configs
if err = os.MkdirAll(confpath, os.ModePerm); err != nil && !errors.Is(err, os.ErrExist) {
if errors.Is(err, os.ErrPermission) {
errorcheck.ErrorCheck(err, common.PermissionNotice)
common.ErrorCheck(err, common.PermissionNotice)
return // note: unreachable due to ErrorCheck calling fatal
}
errorcheck.ErrorCheck(err, "\nError making directory: "+confpath)
common.ErrorCheck(err, "\nError making directory: "+confpath)
}
}
}
@ -168,10 +167,10 @@ func InitConfigs() {
// If we received an error that is not ErrNotExist
if err != nil {
if errors.Is(err, os.ErrPermission) {
errorcheck.ErrorCheck(err, common.PermissionNotice)
common.ErrorCheck(err, common.PermissionNotice)
return // note: unreachable due to ErrorCheck calling fatal
}
errorcheck.ErrorCheck(err, "\nError checking for file: "+sysfile)
common.ErrorCheck(err, "\nError checking for file: "+sysfile)
continue // note: also unreachable
}
@ -186,7 +185,7 @@ func InitConfigs() {
// Create the directories for our configs
file, err := os.Create(conffile)
errorcheck.ErrorCheck(err)
common.ErrorCheck(err)
// Close the file so we can edit it
_ = file.Close()
@ -197,10 +196,10 @@ func InitConfigs() {
exists, err = fileio.FileExist(conffile)
if err != nil {
if errors.Is(err, os.ErrPermission) {
errorcheck.ErrorCheck(err, common.PermissionNotice)
common.ErrorCheck(err, common.PermissionNotice)
return // note: unreachable due to ErrorCheck calling fatal
}
errorcheck.ErrorCheck(err, "\nError checking for file: "+conffile)
common.ErrorCheck(err, "\nError checking for file: "+conffile)
continue // note: also unreachable
}
@ -269,10 +268,10 @@ func backupFile(source string) {
for _, err := range []error{configFileError, sysFileError, destFileError} {
if err != nil {
if errors.Is(configFileError, os.ErrPermission) {
errorcheck.ErrorCheck(configFileError, common.PermissionNotice)
common.ErrorCheck(configFileError, common.PermissionNotice)
return // note: unreachable due to ErrorCheck calling fatal
}
errorcheck.ErrorCheck(configFileError, "\nError checking for file: "+source)
common.ErrorCheck(configFileError, "\nError checking for file: "+source)
return // note: also unreachable
}
}
@ -282,7 +281,7 @@ func backupFile(source string) {
case configExists && !sysExists:
// Create the blank file so that a copy of the backup folder to /etc
file, err := os.Create(dest)
errorcheck.ErrorCheck(err, "Error creating file %s\n", dest)
common.ErrorCheck(err, "Error creating file %s\n", dest)
_ = file.Close()
// If a backup of the file does not exist
@ -302,10 +301,10 @@ func makeBackupDir(dest string) {
if err != nil {
// If we received an error that is not ErrNotExist
if errors.Is(err, os.ErrPermission) {
errorcheck.ErrorCheck(err, common.PermissionNotice)
common.ErrorCheck(err, common.PermissionNotice)
return // note: unreachable due to ErrorCheck calling fatal
}
errorcheck.ErrorCheck(err, "Error checking for backup/ folder")
common.ErrorCheck(err, "Error checking for backup/ folder")
return // note: also unreachable
}
@ -319,10 +318,10 @@ func makeBackupDir(dest string) {
err = nil
}
if errors.Is(err, os.ErrPermission) {
errorcheck.ErrorCheck(err, common.PermissionNotice)
common.ErrorCheck(err, common.PermissionNotice)
return // note: unreachable due to ErrorCheck calling fatal
}
errorcheck.ErrorCheck(err, "Error making backup/ folder")
common.ErrorCheck(err, "Error making backup/ folder")
}
// CopyToSystem copies a file to the system.
@ -334,7 +333,7 @@ func CopyToSystem(isRoot bool, conffile, sysfile string) {
fmt.Printf("Copying: %s to %s\n", conffile, sysfile)
// [command.ExecAndLogSudo] will log the command's output
errorcheck.ErrorCheck(command.ExecAndLogSudo(isRoot, false,
common.ErrorCheck(command.ExecAndLogSudo(isRoot, false,
fmt.Sprintf("cp -v \"%s\" %s", conffile, sysfile),
), // if error, log and exit
fmt.Sprintf("Failed to copy %s to %s", conffile, sysfile),
@ -342,7 +341,7 @@ func CopyToSystem(isRoot bool, conffile, sysfile string) {
// ---------------------------------------------------------------------------------
// note that if we failed the error check, the following will not appear in the log!
// this is because the [errorcheck.ErrorCheck] function will call [log.Fatalf] and exit
// this is because the [common.ErrorCheck] function will call [log.Fatalf] and exit
// ---------------------------------------------------------------------------------
logger.Printf("Copied %s to %s\n", conffile, sysfile)