1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "systemd/sd-id128.h"
29 #include "systemd/sd-messages.h"
32 int main(int argc, char *argv[]) {
38 log_set_target(LOG_TARGET_AUTO);
39 log_parse_environment();
43 log_error("Invalid number of arguments.");
48 if (streq(argv[1], "suspend"))
50 else if (streq(argv[1], "hibernate") || streq(argv[1], "hybrid-sleep"))
53 log_error("Unknown action '%s'.", argv[1]);
58 /* Configure the hibernation mode */
59 if (streq(argv[1], "hibernate")) {
60 if (write_one_line_file("/sys/power/disk", "platform") < 0)
61 write_one_line_file("/sys/power/disk", "shutdown");
62 } else if (streq(argv[1], "hybrid-sleep")) {
63 if (write_one_line_file("/sys/power/disk", "suspend") < 0)
64 if (write_one_line_file("/sys/power/disk", "platform") < 0)
65 write_one_line_file("/sys/power/disk", "shutdown");
68 f = fopen("/sys/power/state", "we");
70 log_error("Failed to open /sys/power/state: %m");
76 arguments[1] = (char*) "pre";
77 arguments[2] = argv[1];
79 execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
81 if (streq(argv[1], "suspend"))
83 MESSAGE_ID(SD_MESSAGE_SLEEP_START),
84 "MESSAGE=Suspending system...",
87 else if (streq(argv[1], "hibernate"))
89 MESSAGE_ID(SD_MESSAGE_SLEEP_START),
90 "MESSAGE=Hibernating system...",
95 MESSAGE_ID(SD_MESSAGE_SLEEP_START),
96 "MESSAGE=Hibernating and suspending system...",
104 r = ferror(f) ? -errno : 0;
106 if (streq(argv[1], "suspend"))
108 MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
109 "MESSAGE=System resumed.",
114 MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
115 "MESSAGE=System thawed.",
119 arguments[1] = (char*) "post";
120 execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
126 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;