diff --git a/include/omicron/pty.h b/include/omicron/pty.h index ab73032..63e254d 100644 --- a/include/omicron/pty.h +++ b/include/omicron/pty.h @@ -1,13 +1,9 @@ /* PTY: crea el par master/slave, lanza el hijo ya aislado y bombea E/S. */ -#ifndef OMICRON_PTY_H -#define OMICRON_PTY_H +#pragma once #include -#ifndef OM_POLICY_FWD -#define OM_POLICY_FWD typedef struct om_policy om_policy; -#endif /* Códigos de salida reservados del hijo: distinguen "no se ejecutó" de "falló". */ #define OM_EXIT_SANDBOX 127 /* el sandbox no se pudo aplicar; nunca hubo exec */ @@ -26,6 +22,4 @@ int om_pty_pump(om_pty *t); int om_pty_resize(om_pty *t, unsigned rows, unsigned cols); int om_pty_wait(om_pty *t, int *status); -void om_pty_kill(om_pty *t); - -#endif /* OMICRON_PTY_H */ +void om_pty_kill(om_pty *t); \ No newline at end of file diff --git a/src/pty.c b/src/pty.c index 0c7b50d..a872093 100644 --- a/src/pty.c +++ b/src/pty.c @@ -1,45 +1,81 @@ /* Dueño: LevtCode. Portable: usar posix_openpt, no openpty (evita #ifdef). */ -#include "omicron/pty.h" +/* Macro del compilador para poder usar posix_openpt. (Debe declararse casi al principio del código) */ +#define _XOPEN_SOURCE 600 + +#include "omicron/pty.h" #include "omicron/sandbox.h" +#include +#include +#include +#include #include -int om_pty_spawn(char *const argv[], const om_policy *p, om_pty *t) +int om_pty_spawn(char *const argv[], const om_policy *pol, om_pty *pty) { (void)argv; - (void)p; - (void)t; + (void)pol; + (void)pty; /* Orden obligatorio en el hijo: cerrar fds ANTES de om_sandbox_apply. */ errno = ENOSYS; return -1; + + // Hay que corregir muchas cosas, de momento, hacemos que la ejecución llegue hasta aquí + // y termine de forma segura. + char *slave_name; + + pty->master_fd = posix_openpt(O_RDWR | O_NOCTTY); + if (pty->master_fd == -1) { + perror("Error in posix_openpt"); + return EXIT_FAILURE; + } + + if (grantpt(pty->master_fd) == -1) { + perror("Error in grantpt"); + close(pty->master_fd); + return EXIT_FAILURE; + } + + if (unlockpt(pty->master_fd) == -1) { + perror("Error in unlockpt"); + close(pty->master_fd); + return EXIT_FAILURE; + } + + slave_name = ptsname(pty->master_fd); + if (slave_name == NULL) { + perror("Error in ptsname"); + close(pty->master_fd); + return EXIT_FAILURE; + } } -int om_pty_pump(om_pty *t) +int om_pty_pump(om_pty *pty) { - (void)t; + (void)pty; errno = ENOSYS; return -1; } -int om_pty_resize(om_pty *t, unsigned rows, unsigned cols) +int om_pty_resize(om_pty *pty, unsigned rows, unsigned cols) { - (void)t; + (void)pty; (void)rows; (void)cols; errno = ENOSYS; return -1; } -int om_pty_wait(om_pty *t, int *status) +int om_pty_wait(om_pty *pty, int *status) { - (void)t; + (void)pty; (void)status; errno = ENOSYS; return -1; } -void om_pty_kill(om_pty *t) +void om_pty_kill(om_pty *pty) { - (void)t; + (void)pty; }