shell_wdog.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /*----------------------------------------------------------- */
  52. void clish_shell__set_wdog_timeout(clish_shell_t *this, unsigned int timeout)
  53. {
  54. assert(this);
  55. this->wdog_timeout = timeout;
  56. }
  57. /*----------------------------------------------------------- */
  58. unsigned int clish_shell__get_wdog_timeout(const clish_shell_t *this)
  59. {
  60. assert(this);
  61. return this->wdog_timeout;
  62. }