/src/monitor/notify/FdNotifyObject.cpp

https://gitlab.com/admin-github-cloud/cynara · C++ · 66 lines · 35 code · 10 blank · 21 comment · 8 complexity · 42792ce42786988ed001cbf022bd9b14 MD5 · raw file

  1. /*
  2. * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  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. * @file src/monitor/notify/FdNotifyObject.cpp
  18. * @author Zofia Abramowska <z.abramowska@samsung.com>
  19. * @version 1.0
  20. * @brief This file contains implementation of cynara's fd notification object
  21. */
  22. #include <cerrno>
  23. #include <cstring>
  24. #include <unistd.h>
  25. #include <log/log.h>
  26. #include "FdNotifyObject.h"
  27. namespace Cynara {
  28. FdNotifyObject::FdNotifyObject() : m_pipeFd{-1, -1} {
  29. }
  30. bool FdNotifyObject::init(void) {
  31. int ret = pipe(m_pipeFd);
  32. if (ret != 0) {
  33. int err = errno;
  34. LOGE("Couldn't initialize pipes: " << strerror(err));
  35. return false;
  36. }
  37. return true;
  38. }
  39. int FdNotifyObject::getNotifyFd(void) {
  40. return m_pipeFd[0];
  41. }
  42. bool FdNotifyObject::notify(void) {
  43. const char wakeup[] = "w";
  44. int ret = TEMP_FAILURE_RETRY(write(m_pipeFd[1], wakeup, sizeof(wakeup)));
  45. if (ret == -1) {
  46. return false;
  47. }
  48. return true;
  49. }
  50. FdNotifyObject::~FdNotifyObject() {
  51. if (m_pipeFd[0] != -1)
  52. close(m_pipeFd[0]);
  53. if (m_pipeFd[1] != -1)
  54. close(m_pipeFd[1]);
  55. }
  56. } /* namespace Cynara */