shell_wdog.c 1.7 KB

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