shell_wdog.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * shell_startup.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include "tinyrl/tinyrl.h"
  7. #include "lub/string.h"
  8. /*----------------------------------------------------------------------- */
  9. int clish_shell_timeout_fn(tinyrl_t *tinyrl)
  10. {
  11. clish_context_t *context = tinyrl__get_context(tinyrl);
  12. clish_shell_t *this = context->shell;
  13. /* Idle timeout */
  14. if (!this->wdog_active) {
  15. tinyrl_crlf(tinyrl);
  16. fprintf(stderr, "Warning: Idle timeout. The session will be closed.\n");
  17. /* Return -1 to close session on timeout */
  18. return -1;
  19. }
  20. /* Watchdog timeout */
  21. clish_shell_wdog(this);
  22. this->wdog_active = BOOL_FALSE;
  23. tinyrl__set_timeout(tinyrl, this->idle_timeout);
  24. return 0;
  25. }
  26. /*----------------------------------------------------------------------- */
  27. int clish_shell_keypress_fn(tinyrl_t *tinyrl, int key)
  28. {
  29. clish_context_t *context = tinyrl__get_context(tinyrl);
  30. clish_shell_t *this = context->shell;
  31. if (this->wdog_active) {
  32. this->wdog_active = BOOL_FALSE;
  33. tinyrl__set_timeout(tinyrl, this->idle_timeout);
  34. }
  35. return 0;
  36. }
  37. /*----------------------------------------------------------- */
  38. int clish_shell_wdog(clish_shell_t *this)
  39. {
  40. clish_context_t context;
  41. assert(this->wdog);
  42. /* Prepare context */
  43. context.shell = this;
  44. context.cmd = this->wdog;
  45. context.action = clish_command__get_action(this->wdog);
  46. context.pargv = NULL;
  47. /* Call watchdog script */
  48. return clish_shell_execute(&context, NULL);
  49. }
  50. /*----------------------------------------------------------- */
  51. void clish_shell__set_wdog_timeout(clish_shell_t *this, unsigned int timeout)
  52. {
  53. assert(this);
  54. this->wdog_timeout = timeout;
  55. }
  56. /*----------------------------------------------------------- */
  57. unsigned int clish_shell__get_wdog_timeout(const clish_shell_t *this)
  58. {
  59. assert(this);
  60. return this->wdog_timeout;
  61. }