1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
7 Copyright 2013 Zbigniew Jędrzejewski-Szmek
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "sd-messages.h"
35 #include "sleep-config.h"
38 static char* arg_verb = NULL;
40 static int write_mode(char **modes) {
44 STRV_FOREACH(mode, modes) {
47 k = write_string_file("/sys/power/disk", *mode);
51 log_debug("Failed to write '%s' to /sys/power/disk: %s",
58 log_error("Failed to write mode to /sys/power/disk: %s",
64 static int write_state(FILE **f, char **states) {
68 STRV_FOREACH(state, states) {
71 k = write_string_stream(*f, *state);
74 log_debug("Failed to write '%s' to /sys/power/state: %s",
75 *state, strerror(-k));
80 *f = fopen("/sys/power/state", "we");
82 log_error("Failed to open /sys/power/state: %m");
90 static int execute(char **modes, char **states) {
93 _cleanup_fclose_ FILE *f = NULL;
94 const char* note = strappenda("SLEEP=", arg_verb);
96 /* This file is opened first, so that if we hit an error,
97 * we can abort before modifying any state. */
98 f = fopen("/sys/power/state", "we");
100 log_error("Failed to open /sys/power/state: %m");
104 /* Configure the hibernation mode */
105 r = write_mode(modes);
110 arguments[1] = (char*) "pre";
111 arguments[2] = arg_verb;
113 execute_directory(SYSTEM_SLEEP_PATH, NULL, DEFAULT_TIMEOUT_USEC, arguments);
116 MESSAGE_ID(SD_MESSAGE_SLEEP_START),
117 "MESSAGE=Suspending system...",
121 r = write_state(&f, states);
126 MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
127 "MESSAGE=System resumed.",
131 arguments[1] = (char*) "post";
132 execute_directory(SYSTEM_SLEEP_PATH, NULL, DEFAULT_TIMEOUT_USEC, arguments);
137 static void help(void) {
138 printf("%s COMMAND\n\n"
139 "Suspend the system, hibernate the system, or both.\n\n"
141 " -h --help Show this help and exit\n"
142 " --version Print version string and exit\n"
143 " suspend Suspend the system\n"
144 " hibernate Hibernate the system\n"
145 " hybrid-sleep Both hibernate and suspend the system\n"
146 , program_invocation_short_name);
149 static int parse_argv(int argc, char *argv[]) {
154 static const struct option options[] = {
155 { "help", no_argument, NULL, 'h' },
156 { "version", no_argument, NULL, ARG_VERSION },
165 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
172 puts(PACKAGE_STRING);
173 puts(SYSTEMD_FEATURES);
180 assert_not_reached("Unhandled option");
183 if (argc - optind != 1) {
184 log_error("Usage: %s COMMAND",
185 program_invocation_short_name);
189 arg_verb = argv[optind];
191 if (!streq(arg_verb, "suspend") &&
192 !streq(arg_verb, "hibernate") &&
193 !streq(arg_verb, "hybrid-sleep")) {
194 log_error("Unknown command '%s'.", arg_verb);
198 return 1 /* work to do */;
201 int main(int argc, char *argv[]) {
202 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
205 log_set_target(LOG_TARGET_AUTO);
206 log_parse_environment();
209 r = parse_argv(argc, argv);
213 r = parse_sleep_config(arg_verb, &modes, &states);
217 r = execute(modes, states);
220 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;