1
0

shell_wdog.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * shell_wdog.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 = clish_context__get_shell(context);
  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 = clish_context__get_shell(context);
  31. if (this->wdog_active) {
  32. this->wdog_active = BOOL_FALSE;
  33. tinyrl__set_timeout(tinyrl, this->idle_timeout);
  34. }
  35. key = key; /* Happy compiler */
  36. return 0;
  37. }
  38. /*----------------------------------------------------------- */
  39. int clish_shell_wdog(clish_shell_t *this)
  40. {
  41. clish_context_t context;
  42. assert(this->wdog);
  43. /* Prepare context */
  44. clish_context_init(&context, this);
  45. clish_context__set_cmd(&context, this->wdog);
  46. clish_context__set_action(&context,
  47. clish_command__get_action(this->wdog));
  48. /* Call watchdog script */
  49. return clish_shell_execute(&context, NULL);
  50. }
  51. CLISH_SET(shell, unsigned int, wdog_timeout);
  52. CLISH_GET(shell, unsigned int, wdog_timeout);