]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hibernate-resume/hibernate-resume.c
treewide: use log_*_errno whenever %m is in the format string
[thirdparty/systemd.git] / src / hibernate-resume / hibernate-resume.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Ivan Shapovalov
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include "log.h"
29 #include "util.h"
30 #include "fileio.h"
31
32 int main(int argc, char *argv[]) {
33 struct stat st;
34 const char *device;
35 _cleanup_free_ char *major_minor = NULL;
36 int r;
37
38 if (argc != 2) {
39 log_error("This program expects one argument.");
40 return EXIT_FAILURE;
41 }
42
43 log_set_target(LOG_TARGET_AUTO);
44 log_parse_environment();
45 log_open();
46
47 umask(0022);
48
49 /* Refuse to run unless we are in an initrd() */
50 if (!in_initrd())
51 return EXIT_SUCCESS;
52
53 device = argv[1];
54
55 if (stat(device, &st) < 0) {
56 log_error_errno(errno, "Failed to stat '%s': %m", device);
57 return EXIT_FAILURE;
58 }
59
60 if (!S_ISBLK(st.st_mode)) {
61 log_error("Resume device '%s' is not a block device.", device);
62 return EXIT_FAILURE;
63 }
64
65 if (asprintf(&major_minor, "%d:%d", major(st.st_rdev), minor(st.st_rdev)) < 0) {
66 log_oom();
67 return EXIT_FAILURE;
68 }
69
70 r = write_string_file("/sys/power/resume", major_minor);
71 if (r < 0) {
72 log_error_errno(r, "Failed to write '%s' to /sys/power/resume: %m", major_minor);
73 return EXIT_FAILURE;
74 }
75
76 /*
77 * The write above shall not return.
78 *
79 * However, failed resume is a normal condition (may mean that there is
80 * no hibernation image).
81 */
82
83 log_info("Could not resume from '%s' (%s).", device, major_minor);
84 return EXIT_SUCCESS;
85 }