clang-tidy
This commit is contained in:
@@ -1,24 +1,28 @@
|
||||
#include "shutdown.h"
|
||||
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <string>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
#endif
|
||||
|
||||
namespace SystemShutdown {
|
||||
|
||||
#ifndef _WIN32
|
||||
// Función auxiliar para sistemas Unix-like
|
||||
auto executeUnixShutdown(const char* command, char* const args[]) -> ShutdownResult {
|
||||
// Función auxiliar para sistemas Unix-like
|
||||
auto executeUnixShutdown(const char* command, const std::vector<char*>& args) -> ShutdownResult {
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
// Proceso hijo
|
||||
execvp(command, args);
|
||||
execvp(command, args.data());
|
||||
// Si llegamos aquí, execvp falló
|
||||
std::cerr << "Error: No se pudo ejecutar " << command << '\n';
|
||||
_exit(1);
|
||||
@@ -33,7 +37,7 @@ auto executeUnixShutdown(const char* command, char* const args[]) -> ShutdownRes
|
||||
}
|
||||
#endif
|
||||
|
||||
// Implementación de las funciones públicas
|
||||
// Implementación de las funciones públicas
|
||||
auto shutdownSystem() -> ShutdownResult {
|
||||
ShutdownConfig config;
|
||||
return shutdownSystem(config);
|
||||
@@ -48,71 +52,70 @@ auto shutdownSystem(int delay_seconds, bool force_apps) -> ShutdownResult {
|
||||
|
||||
auto shutdownSystem(const ShutdownConfig& config) -> ShutdownResult {
|
||||
#ifdef _WIN32
|
||||
// Windows: Usar CreateProcess
|
||||
STARTUPINFOA si = {0};
|
||||
PROCESS_INFORMATION pi = {0};
|
||||
si.cb = sizeof(si);
|
||||
|
||||
// Crear comando con el delay especificado
|
||||
std::string command = "shutdown.exe /s /t " + std::to_string(config.delay_seconds);
|
||||
if (config.force_close_apps) {
|
||||
command += " /f";
|
||||
}
|
||||
|
||||
// CreateProcess necesita un array de char modificable
|
||||
char* cmd_buffer = new char[command.length() + 1];
|
||||
strcpy(cmd_buffer, command.c_str());
|
||||
|
||||
bool success = CreateProcessA(
|
||||
NULL, // lpApplicationName
|
||||
cmd_buffer, // lpCommandLine
|
||||
NULL, // lpProcessAttributes
|
||||
NULL, // lpThreadAttributes
|
||||
FALSE, // bInheritHandles
|
||||
0, // dwCreationFlags
|
||||
NULL, // lpEnvironment
|
||||
NULL, // lpCurrentDirectory
|
||||
&si, // lpStartupInfo
|
||||
&pi // lpProcessInformation
|
||||
);
|
||||
|
||||
delete[] cmd_buffer;
|
||||
|
||||
if (success) {
|
||||
CloseHandle(pi.hProcess);
|
||||
CloseHandle(pi.hThread);
|
||||
return ShutdownResult::SUCCESS;
|
||||
} else {
|
||||
DWORD error = GetLastError();
|
||||
if (error == ERROR_ACCESS_DENIED) {
|
||||
return ShutdownResult::ERROR_PERMISSION;
|
||||
}
|
||||
return ShutdownResult::ERROR_SYSTEM_CALL;
|
||||
// Windows: Usar CreateProcess
|
||||
STARTUPINFOA si = {0};
|
||||
PROCESS_INFORMATION pi = {0};
|
||||
si.cb = sizeof(si);
|
||||
|
||||
// Crear comando con el delay especificado
|
||||
std::string command = "shutdown.exe /s /t " + std::to_string(config.delay_seconds);
|
||||
if (config.force_close_apps) {
|
||||
command += " /f";
|
||||
}
|
||||
|
||||
// CreateProcess necesita un array de char modificable
|
||||
char* cmd_buffer = new char[command.length() + 1];
|
||||
strcpy(cmd_buffer, command.c_str());
|
||||
|
||||
bool success = CreateProcessA(
|
||||
NULL, // lpApplicationName
|
||||
cmd_buffer, // lpCommandLine
|
||||
NULL, // lpProcessAttributes
|
||||
NULL, // lpThreadAttributes
|
||||
FALSE, // bInheritHandles
|
||||
0, // dwCreationFlags
|
||||
NULL, // lpEnvironment
|
||||
NULL, // lpCurrentDirectory
|
||||
&si, // lpStartupInfo
|
||||
&pi // lpProcessInformation
|
||||
);
|
||||
|
||||
delete[] cmd_buffer;
|
||||
|
||||
if (success) {
|
||||
CloseHandle(pi.hProcess);
|
||||
CloseHandle(pi.hThread);
|
||||
return ShutdownResult::SUCCESS;
|
||||
} else {
|
||||
DWORD error = GetLastError();
|
||||
if (error == ERROR_ACCESS_DENIED) {
|
||||
return ShutdownResult::ERROR_PERMISSION;
|
||||
}
|
||||
return ShutdownResult::ERROR_SYSTEM_CALL;
|
||||
}
|
||||
|
||||
#elif __APPLE__
|
||||
// macOS - apagado inmediato
|
||||
char* args[] = {
|
||||
const_cast<char*>("shutdown"),
|
||||
const_cast<char*>("-h"),
|
||||
const_cast<char*>("now"),
|
||||
NULL
|
||||
};
|
||||
|
||||
return executeUnixShutdown("shutdown", args);
|
||||
// macOS - apagado inmediato
|
||||
std::vector<char*> args = {
|
||||
const_cast<char*>("shutdown"),
|
||||
const_cast<char*>("-h"),
|
||||
const_cast<char*>("now"),
|
||||
nullptr};
|
||||
|
||||
return executeUnixShutdown("shutdown", args);
|
||||
|
||||
#elif __linux__
|
||||
// Linux - apagado inmediato
|
||||
char* args[] = {
|
||||
const_cast<char*>("shutdown"),
|
||||
const_cast<char*>("-h"),
|
||||
const_cast<char*>("now"),
|
||||
nullptr};
|
||||
// Linux - apagado inmediato
|
||||
std::vector<char*> args = {
|
||||
const_cast<char*>("shutdown"),
|
||||
const_cast<char*>("-h"),
|
||||
const_cast<char*>("now"),
|
||||
nullptr};
|
||||
|
||||
return executeUnixShutdown("shutdown", args);
|
||||
return executeUnixShutdown("shutdown", args);
|
||||
|
||||
#else
|
||||
return ShutdownResult::ERROR_UNSUPPORTED;
|
||||
return ShutdownResult::ERROR_UNSUPPORTED;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -135,20 +138,20 @@ auto resultToString(ShutdownResult result) -> const char* {
|
||||
|
||||
auto isShutdownSupported() -> bool {
|
||||
#if defined(_WIN32) || defined(__APPLE__) || defined(__linux__)
|
||||
return true;
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
auto getRequiredPermissions() -> const char* {
|
||||
#ifdef _WIN32
|
||||
return "Requiere permisos de Administrador en Windows";
|
||||
return "Requiere permisos de Administrador en Windows";
|
||||
#elif defined(__APPLE__) || defined(__linux__)
|
||||
return "Requiere permisos de root/sudo en Unix";
|
||||
return "Requiere permisos de root/sudo en Unix";
|
||||
#else
|
||||
return "Sistema no soportado";
|
||||
return "Sistema no soportado";
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace SystemShutdown
|
||||
} // namespace SystemShutdown
|
||||
Reference in New Issue
Block a user