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 static char* arg_verb
= NULL
;
39 static int write_mode(char **modes
) {
43 STRV_FOREACH(mode
, modes
) {
46 k
= write_string_file("/sys/power/disk", *mode
, 0);
50 log_debug_errno(k
, "Failed to write '%s' to /sys/power/disk: %m",
57 log_error_errno(r
, "Failed to write mode to /sys/power/disk: %m");
62 static int write_state(FILE **f
, char **states
) {
66 STRV_FOREACH(state
, states
) {
69 k
= write_string_stream(*f
, *state
, true);
72 log_debug_errno(k
, "Failed to write '%s' to /sys/power/state: %m",
78 *f
= fopen("/sys/power/state", "we");
80 return log_error_errno(errno
, "Failed to open /sys/power/state: %m");
86 static int execute(char **modes
, char **states
) {
94 static const char* const dirs
[] = {SYSTEM_SLEEP_PATH
, NULL
};
97 _cleanup_fclose_
FILE *f
= NULL
;
99 /* This file is opened first, so that if we hit an error,
100 * we can abort before modifying any state. */
101 f
= fopen("/sys/power/state", "we");
103 return log_error_errno(errno
, "Failed to open /sys/power/state: %m");
105 /* Configure the hibernation mode */
106 r
= write_mode(modes
);
110 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
);
113 LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START
),
114 LOG_MESSAGE("Suspending system..."),
115 "SLEEP=%s", arg_verb
,
118 r
= write_state(&f
, states
);
123 LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_STOP
),
124 LOG_MESSAGE("System resumed."),
125 "SLEEP=%s", arg_verb
,
128 arguments
[1] = (char*) "post";
129 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
);
134 static void 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
);
146 static int parse_argv(int argc
, char *argv
[]) {
151 static const struct option options
[] = {
152 { "help", no_argument
, NULL
, 'h' },
153 { "version", no_argument
, NULL
, ARG_VERSION
},
162 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
175 assert_not_reached("Unhandled option");
178 if (argc
- optind
!= 1) {
179 log_error("Usage: %s COMMAND",
180 program_invocation_short_name
);
184 arg_verb
= argv
[optind
];
186 if (!streq(arg_verb
, "suspend") &&
187 !streq(arg_verb
, "hibernate") &&
188 !streq(arg_verb
, "hybrid-sleep")) {
189 log_error("Unknown command '%s'.", arg_verb
);
193 return 1 /* work to do */;
196 int main(int argc
, char *argv
[]) {
197 _cleanup_strv_free_
char **modes
= NULL
, **states
= NULL
;
200 log_set_target(LOG_TARGET_AUTO
);
201 log_parse_environment();
204 r
= parse_argv(argc
, argv
);
208 r
= parse_sleep_config(arg_verb
, &modes
, &states
);
212 r
= execute(modes
, states
);
215 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;