getty.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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("ttysize -q");
  47. char * tokens[] = {"/bin/login",NULL,NULL,NULL};
  48. if (user) {
  49. tokens[1] = "-f";
  50. tokens[2] = user;
  51. }
  52. execvp(tokens[0], tokens);
  53. exit(1);
  54. }