getty.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * getty - Manage a TTY.
  7. *
  8. * Wraps a serial port (or other dumb connection) with a pty
  9. * and manages a login for it.
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <pty.h>
  16. #include <sys/wait.h>
  17. #include <sys/fswait.h>
  18. int main(int argc, char * argv[]) {
  19. int fd_serial;
  20. char * file = "/dev/ttyS0";
  21. char * user = NULL;
  22. if (getuid() != 0) {
  23. fprintf(stderr, "%s: only root can do that\n", argv[0]);
  24. return 1;
  25. }
  26. int opt;
  27. while ((opt = getopt(argc, argv, "a:")) != -1) {
  28. switch (opt) {
  29. case 'a':
  30. user = optarg;
  31. break;
  32. }
  33. }
  34. if (optind < argc) {
  35. file = argv[optind];
  36. }
  37. fd_serial = open(file, O_RDWR);
  38. if (fd_serial < 0) {
  39. perror("open");
  40. return 1;
  41. }
  42. setsid();
  43. dup2(fd_serial, 0);
  44. dup2(fd_serial, 1);
  45. dup2(fd_serial, 2);
  46. system("stty sane");
  47. system("ttysize -q");
  48. char * tokens[] = {"/bin/login",NULL,NULL,NULL};
  49. if (user) {
  50. tokens[1] = "-f";
  51. tokens[2] = user;
  52. }
  53. execvp(tokens[0], tokens);
  54. exit(1);
  55. }