Compare commits

...

3 Commits

Author SHA1 Message Date
6011365330 actualitzat Director::shutdownSystem() 2025-08-10 22:56:42 +02:00
03db5b4b0a actualitzat makefile per a linux_release 2025-08-10 22:56:19 +02:00
b1d4c8af07 actualitzat gitignore 2025-08-10 22:56:04 +02:00
3 changed files with 77 additions and 13 deletions

1
.gitignore vendored
View File

@@ -16,3 +16,4 @@ coffee_crisis*
debug.txt
cppcheck-result*
desktop.ini
ccae_release/

View File

@@ -242,7 +242,7 @@ linux_release:
# Empaqueta ficheros
$(RMFILE) "$(LINUX_RELEASE)"
cd "$(RELEASE_FOLDER)" && tar -czvf "../$(LINUX_RELEASE)" *
tar -czvf "$(LINUX_RELEASE)" -C "$(RELEASE_FOLDER)" .
# Elimina la carpeta temporal
$(RMDIR) "$(RELEASE_FOLDER)"
@@ -272,7 +272,7 @@ raspi_release:
# Empaqueta ficheros
$(RMFILE) "$(RASPI_RELEASE)"
cd "$(RELEASE_FOLDER)" && tar -czvf "../$(RASPI_RELEASE)" *
tar -czvf "$(RASPI_RELEASE)" -C "$(RELEASE_FOLDER)" .
# Elimina la carpeta temporal
$(RMDIR) "$(RELEASE_FOLDER)"

View File

@@ -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
}
}