]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hibernate-resume/hibernate-resume.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / hibernate-resume / hibernate-resume.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
42483a74 2/***
96b2fb93 3 Copyright © 2014 Ivan Shapovalov
42483a74
IS
4***/
5
42483a74 6#include <errno.h>
cf0fbc49 7#include <stdio.h>
42483a74 8#include <sys/stat.h>
42483a74 9
b5efdb8a
LP
10#include "alloc-util.h"
11#include "fileio.h"
42483a74
IS
12#include "log.h"
13#include "util.h"
42483a74
IS
14
15int 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
ac528e3e
LP
32 /* Refuse to run unless we are in an initrd() */
33 if (!in_initrd())
34 return EXIT_SUCCESS;
35
42483a74
IS
36 device = argv[1];
37
38 if (stat(device, &st) < 0) {
56f64d95 39 log_error_errno(errno, "Failed to stat '%s': %m", device);
42483a74
IS
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
4c1fc3e4 53 r = write_string_file("/sys/power/resume", major_minor, WRITE_STRING_FILE_CREATE);
42483a74 54 if (r < 0) {
da927ba9 55 log_error_errno(r, "Failed to write '%s' to /sys/power/resume: %m", major_minor);
42483a74
IS
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}