resume.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2017 Denys Vlasenko <vda.linux@googlemail.com>
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  5. */
  6. //config:config RESUME
  7. //config: bool "resume (3.3 kb)"
  8. //config: default y
  9. //config: help
  10. //config: Resume from saved "suspend-to-disk" image
  11. //applet:IF_RESUME(APPLET_NOEXEC(resume, resume, BB_DIR_BIN, BB_SUID_DROP, resume))
  12. //kbuild:lib-$(CONFIG_RESUME) += resume.o
  13. #include "libbb.h"
  14. /* This is a NOEXEC applet. Be very careful! */
  15. /* name_to_dev_t() in klibc-utils supports extended device name formats,
  16. * apart from the usual case where /dev/NAME already exists.
  17. *
  18. * - device number in hexadecimal represents itself (in dev_t layout).
  19. * - device number in major:minor decimal represents itself.
  20. * - if block device (or partition) with this name is found in sysfs.
  21. * - if /dev/ prefix is not given, it is assumed.
  22. *
  23. * klibc-utils also recognizes these, but they don't work
  24. * for "resume" tool purposes (thus we don't support them (yet?)):
  25. * - /dev/nfs
  26. * - /dev/ram (alias to /dev/ram0)
  27. * - /dev/mtd
  28. */
  29. static dev_t name_to_dev_t(const char *devname)
  30. {
  31. char devfile[sizeof(int)*3 * 2 + 4];
  32. char *sysname;
  33. unsigned major_num, minor_num;
  34. struct stat st;
  35. int r;
  36. if (strncmp(devname, "/dev/", 5) != 0) {
  37. char *cptr;
  38. cptr = strchr(devname, ':');
  39. if (cptr) {
  40. /* Colon-separated decimal device number? */
  41. *cptr = '\0';
  42. major_num = bb_strtou(devname, NULL, 10);
  43. if (!errno)
  44. minor_num = bb_strtou(cptr + 1, NULL, 10);
  45. *cptr = ':';
  46. if (!errno)
  47. return makedev(major_num, minor_num);
  48. } else {
  49. /* Hexadecimal device number? */
  50. dev_t res = (dev_t) bb_strtoul(devname, NULL, 16);
  51. if (!errno)
  52. return res;
  53. }
  54. devname = xasprintf("/dev/%s", devname);
  55. }
  56. /* Now devname is always "/dev/FOO" */
  57. if (stat(devname, &st) == 0 && S_ISBLK(st.st_mode))
  58. return st.st_rdev;
  59. /* Full blockdevs as well as partitions may be visible
  60. * in /sys/class/block/ even if /dev is not populated.
  61. */
  62. sysname = xasprintf("/sys/class/block/%s/dev", devname + 5);
  63. r = open_read_close(sysname, devfile, sizeof(devfile) - 1);
  64. //free(sysname);
  65. if (r > 0) {
  66. devfile[r] = '\0';
  67. if (sscanf(devfile, "%u:%u", &major_num, &minor_num) == 2) {
  68. return makedev(major_num, minor_num);
  69. }
  70. }
  71. return (dev_t) 0;
  72. }
  73. //usage:#define resume_trivial_usage
  74. //usage: "BLOCKDEV [OFFSET]"
  75. //usage:#define resume_full_usage "\n"
  76. //usage: "\n""Restore system state from 'suspend-to-disk' data in BLOCKDEV"
  77. int resume_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  78. int resume_main(int argc UNUSED_PARAM, char **argv)
  79. {
  80. unsigned long long ofs;
  81. dev_t resume_device;
  82. char *s;
  83. int fd;
  84. argv++;
  85. if (!argv[0])
  86. bb_show_usage();
  87. resume_device = name_to_dev_t(argv[0]);
  88. if (major(resume_device) == 0) {
  89. bb_error_msg_and_die("invalid resume device: %s", argv[0]);
  90. }
  91. ofs = (argv[1] ? xstrtoull(argv[1], 0) : 0);
  92. fd = xopen("/sys/power/resume", O_WRONLY);
  93. s = xasprintf("%u:%u:%llu", major(resume_device), minor(resume_device), ofs);
  94. xwrite_str(fd, s);
  95. /* if write() returns, resume did not succeed */
  96. return EXIT_FAILURE; /* klibc-utils exits -1 aka 255 */
  97. }