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/>.
28 #include "systemd/sd-id128.h"
29 #include "systemd/sd-messages.h"
35 #include "sleep-config.h"
37 static char* arg_verb = NULL;
39 static int write_mode(char **modes) {
43 STRV_FOREACH(mode, modes) {
44 int k = write_string_file("/sys/power/disk", *mode);
47 log_debug("Failed to write '%s' to /sys/power/disk: %s",
54 log_error("Failed to write mode to /sys/power/disk: %s",
60 static int write_state(FILE *f0, char **states) {
61 FILE _cleanup_fclose_ *f = f0;
65 STRV_FOREACH(state, states) {
68 k = write_string_to_file(f, *state);
71 log_debug("Failed to write '%s' to /sys/power/state: %s",
72 *state, strerror(-k));
77 f = fopen("/sys/power/state", "we");
79 log_error("Failed to open /sys/power/state: %m");
87 static int execute(char **modes, char **states) {
91 const char* note = strappenda("SLEEP=", arg_verb);
93 /* This file is opened first, so that if we hit an error,
94 * we can abort before modyfing any state. */
95 f = fopen("/sys/power/state", "we");
97 log_error("Failed to open /sys/power/state: %m");
101 /* Configure the hibernation mode */
102 r = write_mode(modes);
107 arguments[1] = (char*) "pre";
108 arguments[2] = arg_verb;
110 execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
113 MESSAGE_ID(SD_MESSAGE_SLEEP_START),
114 "MESSAGE=Suspending system...",
118 r = write_state(f, states);
123 MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
124 "MESSAGE=System resumed.",
128 arguments[1] = (char*) "post";
129 execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
134 static int help(void) {
135 printf("%s COMMAND\n\n"
136 "Suspend the system, hibernate the system, or both.\n\n"
138 " -h --help Show this help and exit\n"
139 " --version Print version string and exit\n"
140 " suspend Suspend the system\n"
141 " hibernate Hibernate the system\n"
142 " hybrid-sleep Both hibernate and suspend the system\n"
143 , 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)
171 puts(PACKAGE_STRING);
172 puts(SYSTEMD_FEATURES);
179 assert_not_reached("Unhandled option");
182 if (argc - optind != 1) {
183 log_error("Usage: %s COMMAND",
184 program_invocation_short_name);
188 arg_verb = argv[optind];
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);
197 return 1 /* work to do */;
200 int main(int argc, char *argv[]) {
201 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
204 log_set_target(LOG_TARGET_AUTO);
205 log_parse_environment();
208 r = parse_argv(argc, argv);
212 r = parse_sleep_config(arg_verb, &modes, &states);
216 r = execute(modes, states);
219 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;