]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hibernate-resume/hibernate-resume.c
Merge pull request #22759 from msekletar/issue-18077-long-sysfs-paths-hashing
[thirdparty/systemd.git] / src / hibernate-resume / hibernate-resume.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <sys/stat.h>
6
7 #include "alloc-util.h"
8 #include "devnum-util.h"
9 #include "fileio.h"
10 #include "log.h"
11 #include "util.h"
12
13 int main(int argc, char *argv[]) {
14 struct stat st;
15 const char *device;
16 int r;
17
18 if (argc != 2) {
19 log_error("This program expects one argument.");
20 return EXIT_FAILURE;
21 }
22
23 log_setup();
24
25 umask(0022);
26
27 /* Refuse to run unless we are in an initrd() */
28 if (!in_initrd())
29 return EXIT_SUCCESS;
30
31 device = argv[1];
32
33 if (stat(device, &st) < 0) {
34 log_error_errno(errno, "Failed to stat '%s': %m", device);
35 return EXIT_FAILURE;
36 }
37
38 if (!S_ISBLK(st.st_mode)) {
39 log_error("Resume device '%s' is not a block device.", device);
40 return EXIT_FAILURE;
41 }
42
43 r = write_string_file("/sys/power/resume", FORMAT_DEVNUM(st.st_rdev), WRITE_STRING_FILE_DISABLE_BUFFER);
44 if (r < 0) {
45 log_error_errno(r, "Failed to write '" DEVNUM_FORMAT_STR "' to /sys/power/resume: %m", DEVNUM_FORMAT_VAL(st.st_rdev));
46 return EXIT_FAILURE;
47 }
48
49 /*
50 * The write above shall not return.
51 *
52 * However, failed resume is a normal condition (may mean that there is
53 * no hibernation image).
54 */
55
56 log_info("Could not resume from '%s' (" DEVNUM_FORMAT_STR ").", device, DEVNUM_FORMAT_VAL(st.st_rdev));
57 return EXIT_SUCCESS;
58 }