2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "sd-messages.h"
28 #include "exec-util.h"
32 //#include "sleep-config.h"
33 //#include "string-util.h"
37 /// Additional includes needed by elogind
40 static char* arg_verb = NULL;
42 static int write_mode(char **modes) {
46 STRV_FOREACH(mode, modes) {
49 k = write_string_file("/sys/power/disk", *mode, 0);
53 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
60 log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
65 static int write_state(FILE **f, char **states) {
69 STRV_FOREACH(state, states) {
72 k = write_string_stream(*f, *state, 0);
75 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
81 *f = fopen("/sys/power/state", "we");
83 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
89 static int execute(char **modes, char **states) {
97 static const char* const dirs[] = {
103 _cleanup_fclose_ FILE *f = NULL;
105 /* This file is opened first, so that if we hit an error,
106 * we can abort before modifying any state. */
107 f = fopen("/sys/power/state", "we");
109 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
111 /* Configure the hibernation mode */
112 r = write_mode(modes);
116 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
119 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
120 LOG_MESSAGE("Suspending system..."),
121 "SLEEP=%s", arg_verb,
124 r = write_state(&f, states);
129 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
130 LOG_MESSAGE("System resumed."),
131 "SLEEP=%s", arg_verb,
134 arguments[1] = (char*) "post";
135 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
140 #if 0 /// elogind calls execute() by itself and does not need another binary
141 static void help(void) {
142 printf("%s COMMAND\n\n"
143 "Suspend the system, hibernate the system, or both.\n\n"
145 " -h --help Show this help and exit\n"
146 " --version Print version string and exit\n"
147 " suspend Suspend the system\n"
148 " hibernate Hibernate the system\n"
149 " hybrid-sleep Both hibernate and suspend the system\n"
150 , program_invocation_short_name);
153 static int parse_argv(int argc, char *argv[]) {
158 static const struct option options[] = {
159 { "help", no_argument, NULL, 'h' },
160 { "version", no_argument, NULL, ARG_VERSION },
169 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
182 assert_not_reached("Unhandled option");
185 if (argc - optind != 1) {
186 log_error("Usage: %s COMMAND",
187 program_invocation_short_name);
191 arg_verb = argv[optind];
193 if (!streq(arg_verb, "suspend") &&
194 !streq(arg_verb, "hibernate") &&
195 !streq(arg_verb, "hybrid-sleep")) {
196 log_error("Unknown command '%s'.", arg_verb);
200 return 1 /* work to do */;
203 int main(int argc, char *argv[]) {
204 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
207 log_set_target(LOG_TARGET_AUTO);
208 log_parse_environment();
211 r = parse_argv(argc, argv);
215 r = parse_sleep_config(arg_verb, &modes, &states);
219 r = execute(modes, states);
222 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
225 int do_sleep(const char *verb, char **modes, char **states) {
227 arg_verb = (char*)verb;
228 return execute(modes, states);