unit.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #ifndef __H_WATCHDOG_UNIT_H__
  18. #define __H_WATCHDOG_UNIT_H__
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <map>
  22. class WatchDogUnit;
  23. class WatchDogObject
  24. {
  25. protected:
  26. friend class WatchDogUnit;
  27. WatchDogUnit *watchdog_object_owner_;
  28. int watchdog_object_pid_;
  29. char watchdog_object_name_[16];
  30. public:
  31. WatchDogObject(void) : watchdog_object_owner_(NULL), watchdog_object_pid_(0) { watchdog_object_name_[0] = '0'; }
  32. WatchDogObject(WatchDogUnit *u) : watchdog_object_owner_(u), watchdog_object_pid_(0) { watchdog_object_name_[0] = '0'; }
  33. WatchDogObject(WatchDogUnit *u, const char *n) : watchdog_object_owner_(u), watchdog_object_pid_(0)
  34. {
  35. strncpy(watchdog_object_name_, n, sizeof(watchdog_object_name_));
  36. }
  37. WatchDogObject(WatchDogUnit *u, const char *n, int i) : watchdog_object_owner_(u), watchdog_object_pid_(i)
  38. {
  39. strncpy(watchdog_object_name_, n, sizeof(watchdog_object_name_));
  40. }
  41. virtual ~WatchDogObject();
  42. virtual void exited_notify(int retval);
  43. virtual void killed_notify(int signo, int coredumped);
  44. const char *Name() const { return watchdog_object_name_; }
  45. int get_watchdog_pid() const { return watchdog_object_pid_; }
  46. int attach_watch_dog(WatchDogUnit *o = 0);
  47. void report_kill_alarm(int signo, int coredumped);
  48. };
  49. class WatchDogUnit
  50. {
  51. private:
  52. friend class WatchDogObject;
  53. typedef std::map<int, WatchDogObject *> pidmap_t;
  54. int pid_count_;
  55. pidmap_t pid_map_watchdog_object_;
  56. private:
  57. int attach_process(WatchDogObject *);
  58. public:
  59. WatchDogUnit();
  60. virtual ~WatchDogUnit();
  61. /* return #pid monitored */
  62. int check_watchdog();
  63. int kill_allwatchdog();
  64. int force_kill_allwatchdog();
  65. int get_process_count() const { return pid_count_; }
  66. };
  67. #endif