suspend-dummy.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2012 Nordic Semiconductor Inc.
  7. * Copyright (C) 2012 Instituto Nokia de Tecnologia - INdT
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <errno.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <glib.h>
  23. #include "src/log.h"
  24. #include "suspend.h"
  25. #define HOG_SUSPEND_FIFO "/tmp/hogsuspend"
  26. static suspend_event suspend_cb = NULL;
  27. static resume_event resume_cb = NULL;
  28. static guint watch = 0;
  29. static int fifo_open(void);
  30. static gboolean read_fifo(GIOChannel *io, GIOCondition cond, gpointer user_data)
  31. {
  32. char buffer[12];
  33. gsize offset, left, bread;
  34. GIOStatus iostatus;
  35. if (cond & (G_IO_ERR | G_IO_HUP)) {
  36. /*
  37. * Both ends needs to be open simultaneously before proceeding
  38. * any input or output operation. When the remote closes the
  39. * channel, hup signal is received on this end.
  40. */
  41. fifo_open();
  42. return FALSE;
  43. }
  44. offset = 0;
  45. left = sizeof(buffer) - 1;
  46. memset(buffer, 0, sizeof(buffer));
  47. do {
  48. iostatus = g_io_channel_read_chars(io, &buffer[offset], left,
  49. &bread, NULL);
  50. offset += bread;
  51. left -= bread;
  52. if (left == 0)
  53. break;
  54. } while (iostatus == G_IO_STATUS_NORMAL);
  55. if (g_ascii_strncasecmp("suspend", buffer, 7) == 0)
  56. suspend_cb();
  57. else if (g_ascii_strncasecmp("resume", buffer, 6) == 0)
  58. resume_cb();
  59. return TRUE;
  60. }
  61. static int fifo_open(void)
  62. {
  63. GIOCondition condition = G_IO_IN | G_IO_ERR | G_IO_HUP;
  64. GIOChannel *fifoio;
  65. int fd;
  66. fd = open(HOG_SUSPEND_FIFO, O_RDONLY | O_NONBLOCK);
  67. if (fd < 0) {
  68. int err = -errno;
  69. error("Can't open FIFO (%s): %s(%d)", HOG_SUSPEND_FIFO,
  70. strerror(-err), -err);
  71. return err;
  72. }
  73. fifoio = g_io_channel_unix_new(fd);
  74. g_io_channel_set_close_on_unref(fifoio, TRUE);
  75. watch = g_io_add_watch(fifoio, condition, read_fifo, NULL);
  76. g_io_channel_unref(fifoio);
  77. return 0;
  78. }
  79. int suspend_init(suspend_event suspend, resume_event resume)
  80. {
  81. struct stat st;
  82. int ret;
  83. DBG("");
  84. suspend_cb = suspend;
  85. resume_cb = resume;
  86. if (stat(HOG_SUSPEND_FIFO, &st) == 0) {
  87. if (!S_ISFIFO(st.st_mode)) {
  88. error("Unexpected non-FIFO %s file", HOG_SUSPEND_FIFO);
  89. return -EIO;
  90. }
  91. if (unlink(HOG_SUSPEND_FIFO) < 0) {
  92. int err = -errno;
  93. error("Failed to remove FIFO (%s): %s (%d)",
  94. HOG_SUSPEND_FIFO, strerror(-err), -err);
  95. return err;
  96. }
  97. }
  98. if (mkfifo(HOG_SUSPEND_FIFO, 0600) < 0) {
  99. int err = -errno;
  100. error("Can't create FIFO (%s): %s (%d)", HOG_SUSPEND_FIFO,
  101. strerror(-err), -err);
  102. return err;
  103. }
  104. DBG("Created suspend-dummy FIFO on %s", HOG_SUSPEND_FIFO);
  105. ret = fifo_open();
  106. if (ret < 0)
  107. unlink(HOG_SUSPEND_FIFO);
  108. return ret;
  109. }
  110. void suspend_exit(void)
  111. {
  112. if (watch > 0) {
  113. g_source_remove(watch);
  114. watch = 0;
  115. }
  116. unlink(HOG_SUSPEND_FIFO);
  117. }