]>
Commit | Line | Data |
---|---|---|
42483a74 IS |
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 | ||
42483a74 | 22 | #include <errno.h> |
cf0fbc49 | 23 | #include <stdio.h> |
42483a74 | 24 | #include <sys/stat.h> |
42483a74 | 25 | |
b5efdb8a LP |
26 | #include "alloc-util.h" |
27 | #include "fileio.h" | |
42483a74 IS |
28 | #include "log.h" |
29 | #include "util.h" | |
42483a74 IS |
30 | |
31 | int main(int argc, char *argv[]) { | |
32 | struct stat st; | |
33 | const char *device; | |
34 | _cleanup_free_ char *major_minor = NULL; | |
35 | int r; | |
36 | ||
37 | if (argc != 2) { | |
38 | log_error("This program expects one argument."); | |
39 | return EXIT_FAILURE; | |
40 | } | |
41 | ||
42 | log_set_target(LOG_TARGET_AUTO); | |
43 | log_parse_environment(); | |
44 | log_open(); | |
45 | ||
46 | umask(0022); | |
47 | ||
ac528e3e LP |
48 | /* Refuse to run unless we are in an initrd() */ |
49 | if (!in_initrd()) | |
50 | return EXIT_SUCCESS; | |
51 | ||
42483a74 IS |
52 | device = argv[1]; |
53 | ||
54 | if (stat(device, &st) < 0) { | |
56f64d95 | 55 | log_error_errno(errno, "Failed to stat '%s': %m", device); |
42483a74 IS |
56 | return EXIT_FAILURE; |
57 | } | |
58 | ||
59 | if (!S_ISBLK(st.st_mode)) { | |
60 | log_error("Resume device '%s' is not a block device.", device); | |
61 | return EXIT_FAILURE; | |
62 | } | |
63 | ||
64 | if (asprintf(&major_minor, "%d:%d", major(st.st_rdev), minor(st.st_rdev)) < 0) { | |
65 | log_oom(); | |
66 | return EXIT_FAILURE; | |
67 | } | |
68 | ||
4c1fc3e4 | 69 | r = write_string_file("/sys/power/resume", major_minor, WRITE_STRING_FILE_CREATE); |
42483a74 | 70 | if (r < 0) { |
da927ba9 | 71 | log_error_errno(r, "Failed to write '%s' to /sys/power/resume: %m", major_minor); |
42483a74 IS |
72 | return EXIT_FAILURE; |
73 | } | |
74 | ||
75 | /* | |
76 | * The write above shall not return. | |
77 | * | |
78 | * However, failed resume is a normal condition (may mean that there is | |
79 | * no hibernation image). | |
80 | */ | |
81 | ||
82 | log_info("Could not resume from '%s' (%s).", device, major_minor); | |
83 | return EXIT_SUCCESS; | |
84 | } |