actualitzat Director::shutdownSystem()
This commit is contained in:
@@ -3,12 +3,14 @@
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_LogCategory, SDL_LogInfo, SDL_SetLogPriority, SDL_LogPriority, SDL_Quit
|
||||
#include <sys/stat.h> // Para mkdir, stat, S_IRWXU
|
||||
#include <unistd.h> // Para getuid
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h> // Para getuid
|
||||
|
||||
#include <cerrno> // Para errno, EEXIST, EACCES, ENAMETOOLONG
|
||||
#include <cstdio> // Para printf, perror
|
||||
#include <cstdlib> // Para exit, EXIT_FAILURE, srand, rand, system
|
||||
#include <ctime> // Para time
|
||||
#include <iostream> // Para std::cerr
|
||||
#include <memory> // Para make_unique, unique_ptr
|
||||
#include <span> // Para span
|
||||
#include <stdexcept> // Para runtime_error
|
||||
@@ -40,6 +42,11 @@
|
||||
#include <pwd.h> // Para getpwuid, passwd
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winbase.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
// Constructor
|
||||
Director::Director(int argc, std::span<char *> argv) {
|
||||
#ifdef RECORDING
|
||||
@@ -368,21 +375,77 @@ auto Director::run() -> int {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Apaga el sistema
|
||||
// Apaga el sistema de forma segura
|
||||
void Director::shutdownSystem(bool should_shutdown) {
|
||||
if (should_shutdown) {
|
||||
if (!should_shutdown) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
// Apaga el sistema en Windows
|
||||
system("shutdown /s /t 5");
|
||||
// Windows: Usar API nativa de Windows
|
||||
if (!InitiateSystemShutdownA(
|
||||
NULL, // lpMachineName (NULL = local machine)
|
||||
"Sistema apagándose...", // lpMessage
|
||||
5, // dwTimeout (5 segundos)
|
||||
TRUE, // bForceAppsClosed
|
||||
FALSE // bRebootAfterShutdown (FALSE = shutdown)
|
||||
)) {
|
||||
std::cerr << "Error: No se pudo iniciar el apagado en Windows. Código: "
|
||||
<< GetLastError() << std::endl;
|
||||
}
|
||||
|
||||
#elif __APPLE__
|
||||
// Apaga el sistema en macOS
|
||||
system("sudo shutdown -h +0.1");
|
||||
// macOS: Usar execvp con fork
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
// Proceso hijo - ejecutar shutdown
|
||||
char *args[] = {
|
||||
const_cast<char *>("shutdown"),
|
||||
const_cast<char *>("-h"),
|
||||
const_cast<char *>("+0.1"),
|
||||
NULL};
|
||||
execvp("shutdown", args);
|
||||
// Si execvp falla, salir del proceso hijo
|
||||
std::cerr << "Error: execvp falló en macOS" << std::endl;
|
||||
_exit(1);
|
||||
} else if (pid > 0) {
|
||||
// Proceso padre - esperar al hijo
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
if (WEXITSTATUS(status) != 0) {
|
||||
std::cerr << "Error: Comando shutdown falló en macOS" << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Error: No se pudo crear proceso fork en macOS" << std::endl;
|
||||
}
|
||||
|
||||
#elif __linux__
|
||||
// Apaga el sistema en Linux
|
||||
system("sleep 5; shutdown -h now");
|
||||
// Linux: Usar execvp con fork
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
// Proceso hijo - primero sleep, luego shutdown
|
||||
sleep(5);
|
||||
char *args[] = {
|
||||
const_cast<char *>("shutdown"),
|
||||
const_cast<char *>("-h"),
|
||||
const_cast<char *>("now"),
|
||||
NULL};
|
||||
execvp("shutdown", args);
|
||||
// Si execvp falla, salir del proceso hijo
|
||||
std::cerr << "Error: execvp falló en Linux" << std::endl;
|
||||
_exit(1);
|
||||
} else if (pid > 0) {
|
||||
// Proceso padre - esperar al hijo
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
if (WEXITSTATUS(status) != 0) {
|
||||
std::cerr << "Error: Comando shutdown falló en Linux" << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Error: No se pudo crear proceso fork en Linux" << std::endl;
|
||||
}
|
||||
|
||||
#else
|
||||
// Sistema operativo no compatible
|
||||
#error "Sistema operativo no soportado"
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user