2021-11-26 06:41:56 +03:00
|
|
|
/**
|
|
|
|
* @brief getty - Manage a TTY.
|
2018-08-14 11:13:38 +03:00
|
|
|
*
|
|
|
|
* Wraps a serial port (or other dumb connection) with a pty
|
|
|
|
* and manages a login for it.
|
2021-11-26 06:41:56 +03:00
|
|
|
*
|
|
|
|
* This is not really what 'getty' should do - see @ref login-loop.c
|
|
|
|
* for something more akin to the "getty" on other platforms.
|
|
|
|
*
|
|
|
|
* @copyright
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2018 K. Lange
|
2018-07-06 04:40:49 +03:00
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-10-08 08:52:34 +03:00
|
|
|
#include <string.h>
|
2018-07-06 04:40:49 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2018-08-02 07:13:27 +03:00
|
|
|
#include <pty.h>
|
2018-08-02 12:48:45 +03:00
|
|
|
#include <sys/wait.h>
|
2018-08-02 07:13:27 +03:00
|
|
|
#include <sys/fswait.h>
|
2018-07-06 04:40:49 +03:00
|
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
2018-10-29 13:55:55 +03:00
|
|
|
int fd_serial;
|
2018-07-06 04:40:49 +03:00
|
|
|
char * file = "/dev/ttyS0";
|
2018-08-13 07:30:12 +03:00
|
|
|
char * user = NULL;
|
2018-07-06 04:40:49 +03:00
|
|
|
|
|
|
|
if (getuid() != 0) {
|
|
|
|
fprintf(stderr, "%s: only root can do that\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-13 07:30:12 +03:00
|
|
|
int opt;
|
|
|
|
while ((opt = getopt(argc, argv, "a:")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'a':
|
|
|
|
user = optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
file = argv[optind];
|
2023-10-08 08:52:34 +03:00
|
|
|
optind++;
|
2018-08-02 12:48:45 +03:00
|
|
|
}
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-08-02 12:48:45 +03:00
|
|
|
fd_serial = open(file, O_RDWR);
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
if (fd_serial < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
setsid();
|
|
|
|
dup2(fd_serial, 0);
|
|
|
|
dup2(fd_serial, 1);
|
|
|
|
dup2(fd_serial, 2);
|
2024-01-19 14:14:18 +03:00
|
|
|
ioctl(STDIN_FILENO, TIOCSCTTY, &(int){1});
|
|
|
|
tcsetpgrp(STDIN_FILENO, getpid());
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-10-29 14:56:41 +03:00
|
|
|
system("stty sane");
|
2023-10-08 08:52:34 +03:00
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
if (*argv[optind] >= '0' && *argv[optind] <= '9' && strlen(argv[optind]) < 30) {
|
|
|
|
char tmp[100];
|
|
|
|
snprintf(tmp, 100, "stty %s", argv[optind]);
|
|
|
|
system(tmp);
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
/* If there's still arguments, assume TERM value */
|
|
|
|
setenv("TERM", argv[optind], 1);
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
system("ttysize -q");
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
char * tokens[] = {"/bin/login",NULL,NULL,NULL};
|
2018-07-06 04:40:49 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
if (user) {
|
|
|
|
tokens[1] = "-f";
|
|
|
|
tokens[2] = user;
|
2018-07-06 04:40:49 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
execvp(tokens[0], tokens);
|
|
|
|
exit(1);
|
2018-07-06 04:40:49 +03:00
|
|
|
}
|