power.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2011 The Chromium OS Authors
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * power.c: power management routines
  20. */
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #include "flash.h" /* for msg_* */
  28. #include "locks.h" /* for SYSTEM_LOCKFILE_DIR */
  29. #include "power.h"
  30. /*
  31. * Path to a file containing flashrom's PID. While present, powerd avoids
  32. * suspending or shutting down the system.
  33. */
  34. static const char powerd_lock_file_name[] = "flashrom_powerd.lock";
  35. int disable_power_management()
  36. {
  37. FILE *lock_file = NULL;
  38. int rc = 0;
  39. char lock_file_path[PATH_MAX];
  40. msg_pdbg("%s: Disabling power management.\n", __func__);
  41. if (snprintf(lock_file_path, sizeof(lock_file_path), "%s/%s",
  42. SYSTEM_LOCKFILE_DIR, powerd_lock_file_name) < 0)
  43. return 1;
  44. if (!(lock_file = fopen(lock_file_path, "w"))) {
  45. msg_perr("%s: Failed to open %s for writing: %s\n",
  46. __func__, lock_file_path, strerror(errno));
  47. return 1;
  48. }
  49. if (fprintf(lock_file, "%ld", (long)getpid()) < 0) {
  50. msg_perr("%s: Failed to write PID to %s: %s\n",
  51. __func__, lock_file_path, strerror(errno));
  52. rc = 1;
  53. }
  54. if (fclose(lock_file) != 0) {
  55. msg_perr("%s: Failed to close %s: %s\n",
  56. __func__, lock_file_path, strerror(errno));
  57. }
  58. return rc;
  59. }
  60. int restore_power_management()
  61. {
  62. int result = 0;
  63. char lock_file_path[PATH_MAX];
  64. msg_pdbg("%s: Re-enabling power management.\n", __func__);
  65. if (snprintf(lock_file_path, sizeof(lock_file_path), "%s/%s",
  66. SYSTEM_LOCKFILE_DIR, powerd_lock_file_name) < 0)
  67. return 1;
  68. result = unlink(lock_file_path);
  69. if (result != 0 && errno != ENOENT) {
  70. msg_perr("%s: Failed to unlink %s: %s\n",
  71. __func__, lock_file_path, strerror(errno));
  72. return 1;
  73. }
  74. return 0;
  75. }