]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sleep/sleep.c
util: rename write_one_line_file() to write_string_file()
[thirdparty/systemd.git] / src / sleep / sleep.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2012 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include "log.h"
27 #include "util.h"
28 #include "systemd/sd-id128.h"
29 #include "systemd/sd-messages.h"
30 #include "fileio.h"
31
32 int main(int argc, char *argv[]) {
33 const char *verb;
34 char* arguments[4];
35 int r;
36 FILE *f;
37
38 log_set_target(LOG_TARGET_AUTO);
39 log_parse_environment();
40 log_open();
41
42 if (argc != 2) {
43 log_error("Invalid number of arguments.");
44 r = -EINVAL;
45 goto finish;
46 }
47
48 if (streq(argv[1], "suspend"))
49 verb = "mem";
50 else if (streq(argv[1], "hibernate") || streq(argv[1], "hybrid-sleep"))
51 verb = "disk";
52 else {
53 log_error("Unknown action '%s'.", argv[1]);
54 r = -EINVAL;
55 goto finish;
56 }
57
58 /* Configure the hibernation mode */
59 if (streq(argv[1], "hibernate")) {
60 if (write_string_file("/sys/power/disk", "platform") < 0)
61 write_string_file("/sys/power/disk", "shutdown");
62 } else if (streq(argv[1], "hybrid-sleep")) {
63 if (write_string_file("/sys/power/disk", "suspend") < 0)
64 if (write_string_file("/sys/power/disk", "platform") < 0)
65 write_string_file("/sys/power/disk", "shutdown");
66 }
67
68 f = fopen("/sys/power/state", "we");
69 if (!f) {
70 log_error("Failed to open /sys/power/state: %m");
71 r = -errno;
72 goto finish;
73 }
74
75 arguments[0] = NULL;
76 arguments[1] = (char*) "pre";
77 arguments[2] = argv[1];
78 arguments[3] = NULL;
79 execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
80
81 if (streq(argv[1], "suspend"))
82 log_struct(LOG_INFO,
83 MESSAGE_ID(SD_MESSAGE_SLEEP_START),
84 "MESSAGE=Suspending system...",
85 "SLEEP=suspend",
86 NULL);
87 else if (streq(argv[1], "hibernate"))
88 log_struct(LOG_INFO,
89 MESSAGE_ID(SD_MESSAGE_SLEEP_START),
90 "MESSAGE=Hibernating system...",
91 "SLEEP=hibernate",
92 NULL);
93 else
94 log_struct(LOG_INFO,
95 MESSAGE_ID(SD_MESSAGE_SLEEP_START),
96 "MESSAGE=Hibernating and suspending system...",
97 "SLEEP=hybrid-sleep",
98 NULL);
99
100 fputs(verb, f);
101 fputc('\n', f);
102 fflush(f);
103
104 r = ferror(f) ? -errno : 0;
105
106 if (streq(argv[1], "suspend"))
107 log_struct(LOG_INFO,
108 MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
109 "MESSAGE=System resumed.",
110 "SLEEP=suspend",
111 NULL);
112 else
113 log_struct(LOG_INFO,
114 MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
115 "MESSAGE=System thawed.",
116 "SLEEP=hibernate",
117 NULL);
118
119 arguments[1] = (char*) "post";
120 execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
121
122 fclose(f);
123
124 finish:
125
126 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
127
128 }