toaruos/apps/getty.c

67 lines
1.2 KiB
C
Raw Normal View History

2018-08-14 11:13:38 +03:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* 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
*
* getty - Manage a TTY.
*
* Wraps a serial port (or other dumb connection) with a pty
* and manages a login for it.
2018-07-06 04:40:49 +03:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <pty.h>
#include <sys/wait.h>
#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];
}
2018-07-06 04:40:49 +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);
2018-07-06 04:40:49 +03:00
2018-10-29 14:56:41 +03:00
system("stty sane");
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
}