]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hibernate-resume/hibernate-resume.c
1cfd55c2cf2f0638f6089821781b4141f4f9affe
[thirdparty/systemd.git] / src / hibernate-resume / hibernate-resume.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2014 Ivan Shapovalov
4 ***/
5
6 #include <errno.h>
7 #include <stdio.h>
8 #include <sys/stat.h>
9
10 #include "alloc-util.h"
11 #include "fileio.h"
12 #include "log.h"
13 #include "util.h"
14
15 int main(int argc, char *argv[]) {
16 struct stat st;
17 const char *device;
18 _cleanup_free_ char *major_minor = NULL;
19 int r;
20
21 if (argc != 2) {
22 log_error("This program expects one argument.");
23 return EXIT_FAILURE;
24 }
25
26 log_set_target(LOG_TARGET_AUTO);
27 log_parse_environment();
28 log_open();
29
30 umask(0022);
31
32 /* Refuse to run unless we are in an initrd() */
33 if (!in_initrd())
34 return EXIT_SUCCESS;
35
36 device = argv[1];
37
38 if (stat(device, &st) < 0) {
39 log_error_errno(errno, "Failed to stat '%s': %m", device);
40 return EXIT_FAILURE;
41 }
42
43 if (!S_ISBLK(st.st_mode)) {
44 log_error("Resume device '%s' is not a block device.", device);
45 return EXIT_FAILURE;
46 }
47
48 if (asprintf(&major_minor, "%d:%d", major(st.st_rdev), minor(st.st_rdev)) < 0) {
49 log_oom();
50 return EXIT_FAILURE;
51 }
52
53 r = write_string_file("/sys/power/resume", major_minor, WRITE_STRING_FILE_CREATE);
54 if (r < 0) {
55 log_error_errno(r, "Failed to write '%s' to /sys/power/resume: %m", major_minor);
56 return EXIT_FAILURE;
57 }
58
59 /*
60 * The write above shall not return.
61 *
62 * However, failed resume is a normal condition (may mean that there is
63 * no hibernation image).
64 */
65
66 log_info("Could not resume from '%s' (%s).", device, major_minor);
67 return EXIT_SUCCESS;
68 }