shell_wdog.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. context.shell = this;
  43. context.cmd = this->wdog;
  44. context.pargv = NULL;
  45. /* Call watchdog script */
  46. return clish_shell_execute(&context, NULL);
  47. }
  48. /*----------------------------------------------------------- */
  49. void clish_shell__set_wdog_timeout(clish_shell_t *this, unsigned int timeout)
  50. {
  51. assert(this);
  52. this->wdog_timeout = timeout;
  53. }
  54. /*----------------------------------------------------------- */
  55. unsigned int clish_shell__get_wdog_timeout(const clish_shell_t *this)
  56. {
  57. assert(this);
  58. return this->wdog_timeout;
  59. }