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