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/>.
27 #include "sd-messages.h"
33 #include "sleep-config.h"
36 static char* arg_verb = NULL;
38 static int write_mode(char **modes) {
42 STRV_FOREACH(mode, modes) {
45 k = write_string_file("/sys/power/disk", *mode);
49 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
56 log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
61 static int write_state(FILE **f, char **states) {
65 STRV_FOREACH(state, states) {
68 k = write_string_stream(*f, *state);
71 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
77 *f = fopen("/sys/power/state", "we");
79 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
85 static int execute(char **modes, char **states) {
93 static const char* const dirs[] = {SYSTEM_SLEEP_PATH, NULL};
96 _cleanup_fclose_ FILE *f = NULL;
98 /* This file is opened first, so that if we hit an error,
99 * we can abort before modifying any state. */
100 f = fopen("/sys/power/state", "we");
102 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
104 /* Configure the hibernation mode */
105 r = write_mode(modes);
109 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
112 LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
113 LOG_MESSAGE("Suspending system..."),
114 "SLEEP=%s", arg_verb,
117 r = write_state(&f, states);
122 LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
123 LOG_MESSAGE("System resumed."),
124 "SLEEP=%s", arg_verb,
127 arguments[1] = (char*) "post";
128 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
133 static void help(void) {
134 printf("%s COMMAND\n\n"
135 "Suspend the system, hibernate the system, or both.\n\n"
137 " -h --help Show this help and exit\n"
138 " --version Print version string and exit\n"
139 " suspend Suspend the system\n"
140 " hibernate Hibernate the system\n"
141 " hybrid-sleep Both hibernate and suspend the system\n"
142 , program_invocation_short_name);
145 static int parse_argv(int argc, char *argv[]) {
150 static const struct option options[] = {
151 { "help", no_argument, NULL, 'h' },
152 { "version", no_argument, NULL, ARG_VERSION },
161 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
168 puts(PACKAGE_STRING);
169 puts(SYSTEMD_FEATURES);
176 assert_not_reached("Unhandled option");
179 if (argc - optind != 1) {
180 log_error("Usage: %s COMMAND",
181 program_invocation_short_name);
185 arg_verb = argv[optind];
187 if (!streq(arg_verb, "suspend") &&
188 !streq(arg_verb, "hibernate") &&
189 !streq(arg_verb, "hybrid-sleep")) {
190 log_error("Unknown command '%s'.", arg_verb);
194 return 1 /* work to do */;
197 int main(int argc, char *argv[]) {
198 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
201 log_set_target(LOG_TARGET_AUTO);
202 log_parse_environment();
205 r = parse_argv(argc, argv);
209 r = parse_sleep_config(arg_verb, &modes, &states);
213 r = execute(modes, states);
216 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;