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