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