]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sleep/sleep.c
journal: generate structured journal messages for a number of events
[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
31 int main(int argc, char *argv[]) {
32 const char *verb;
33 char* arguments[4];
34 int r;
35 FILE *f;
36
37 log_set_target(LOG_TARGET_AUTO);
38 log_parse_environment();
39 log_open();
40
41 if (argc != 2) {
42 log_error("Invalid number of arguments.");
43 r = -EINVAL;
44 goto finish;
45 }
46
47 if (streq(argv[1], "suspend"))
48 verb = "mem";
49 else if (streq(argv[1], "hibernate"))
50 verb = "disk";
51 else {
52 log_error("Unknown action '%s'.", argv[1]);
53 r = -EINVAL;
54 goto finish;
55 }
56
57 f = fopen("/sys/power/state", "we");
58 if (!f) {
59 log_error("Failed to open /sys/power/state: %m");
60 r = -errno;
61 goto finish;
62 }
63
64 arguments[0] = NULL;
65 arguments[1] = (char*) "pre";
66 arguments[2] = argv[1];
67 arguments[3] = NULL;
68 execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
69
70 if (streq(argv[1], "suspend"))
71 log_struct(LOG_INFO,
72 "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_SLEEP_START),
73 "MESSAGE=Suspending system...",
74 "SLEEP=suspend",
75 NULL);
76 else
77 log_struct(LOG_INFO,
78 "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_SLEEP_START),
79 "MESSAGE=Hibernating system...",
80 "SLEEP=hibernate",
81 NULL);
82
83 fputs(verb, f);
84 fputc('\n', f);
85 fflush(f);
86
87 r = ferror(f) ? -errno : 0;
88
89 if (streq(argv[1], "suspend"))
90 log_struct(LOG_INFO,
91 "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_SLEEP_STOP),
92 "MESSAGE=System resumed.",
93 "SLEEP=suspend",
94 NULL);
95 else
96 log_struct(LOG_INFO,
97 "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_SLEEP_STOP),
98 "MESSAGE=System thawed.",
99 "SLEEP=hibernate",
100 NULL);
101
102 arguments[1] = (char*) "post";
103 execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
104
105 fclose(f);
106
107 finish:
108
109 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
110
111 }