]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sleep/sleep.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / sleep / sleep.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
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 <errno.h>
23 #include <getopt.h>
24 #include <stdio.h>
25
26 #include "sd-messages.h"
27
28 #include "def.h"
29 #include "exec-util.h"
30 #include "fd-util.h"
31 #include "fileio.h"
32 #include "log.h"
33 #include "sleep-config.h"
34 #include "string-util.h"
35 #include "strv.h"
36 #include "util.h"
37
38 static char* arg_verb = NULL;
39
40 static int write_mode(char **modes) {
41 int r = 0;
42 char **mode;
43
44 STRV_FOREACH(mode, modes) {
45 int k;
46
47 k = write_string_file("/sys/power/disk", *mode, 0);
48 if (k == 0)
49 return 0;
50
51 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
52 *mode);
53 if (r == 0)
54 r = k;
55 }
56
57 if (r < 0)
58 log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
59
60 return r;
61 }
62
63 static int write_state(FILE **f, char **states) {
64 char **state;
65 int r = 0;
66
67 STRV_FOREACH(state, states) {
68 int k;
69
70 k = write_string_stream(*f, *state, 0);
71 if (k == 0)
72 return 0;
73 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
74 *state);
75 if (r == 0)
76 r = k;
77
78 fclose(*f);
79 *f = fopen("/sys/power/state", "we");
80 if (!*f)
81 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
82 }
83
84 return r;
85 }
86
87 static int execute(char **modes, char **states) {
88
89 char *arguments[] = {
90 NULL,
91 (char*) "pre",
92 arg_verb,
93 NULL
94 };
95 static const char* const dirs[] = {
96 SYSTEM_SLEEP_PATH,
97 NULL
98 };
99
100 int r;
101 _cleanup_fclose_ FILE *f = NULL;
102
103 /* This file is opened first, so that if we hit an error,
104 * we can abort before modifying any state. */
105 f = fopen("/sys/power/state", "we");
106 if (!f)
107 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
108
109 /* Configure the hibernation mode */
110 r = write_mode(modes);
111 if (r < 0)
112 return r;
113
114 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
115
116 log_struct(LOG_INFO,
117 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
118 LOG_MESSAGE("Suspending system..."),
119 "SLEEP=%s", arg_verb,
120 NULL);
121
122 r = write_state(&f, states);
123 if (r < 0)
124 return r;
125
126 log_struct(LOG_INFO,
127 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
128 LOG_MESSAGE("System resumed."),
129 "SLEEP=%s", arg_verb,
130 NULL);
131
132 arguments[1] = (char*) "post";
133 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
134
135 return r;
136 }
137
138 static void help(void) {
139 printf("%s COMMAND\n\n"
140 "Suspend the system, hibernate the system, or both.\n\n"
141 "Commands:\n"
142 " -h --help Show this help and exit\n"
143 " --version Print version string and exit\n"
144 " suspend Suspend the system\n"
145 " hibernate Hibernate the system\n"
146 " hybrid-sleep Both hibernate and suspend the system\n"
147 , program_invocation_short_name);
148 }
149
150 static int parse_argv(int argc, char *argv[]) {
151 enum {
152 ARG_VERSION = 0x100,
153 };
154
155 static const struct option options[] = {
156 { "help", no_argument, NULL, 'h' },
157 { "version", no_argument, NULL, ARG_VERSION },
158 {}
159 };
160
161 int c;
162
163 assert(argc >= 0);
164 assert(argv);
165
166 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
167 switch(c) {
168 case 'h':
169 help();
170 return 0; /* done */
171
172 case ARG_VERSION:
173 return version();
174
175 case '?':
176 return -EINVAL;
177
178 default:
179 assert_not_reached("Unhandled option");
180 }
181
182 if (argc - optind != 1) {
183 log_error("Usage: %s COMMAND",
184 program_invocation_short_name);
185 return -EINVAL;
186 }
187
188 arg_verb = argv[optind];
189
190 if (!streq(arg_verb, "suspend") &&
191 !streq(arg_verb, "hibernate") &&
192 !streq(arg_verb, "hybrid-sleep")) {
193 log_error("Unknown command '%s'.", arg_verb);
194 return -EINVAL;
195 }
196
197 return 1 /* work to do */;
198 }
199
200 int main(int argc, char *argv[]) {
201 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
202 int r;
203
204 log_set_target(LOG_TARGET_AUTO);
205 log_parse_environment();
206 log_open();
207
208 r = parse_argv(argc, argv);
209 if (r <= 0)
210 goto finish;
211
212 r = parse_sleep_config(arg_verb, &modes, &states);
213 if (r < 0)
214 goto finish;
215
216 r = execute(modes, states);
217
218 finish:
219 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
220 }