]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hibernate-resume/hibernate-resume-generator.c
Merge pull request #10795 from poettering/generator-fixes
[thirdparty/systemd.git] / src / hibernate-resume / hibernate-resume-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdio.h>
5
6 #include "alloc-util.h"
7 #include "fstab-util.h"
8 #include "log.h"
9 #include "mkdir.h"
10 #include "proc-cmdline.h"
11 #include "special.h"
12 #include "string-util.h"
13 #include "unit-name.h"
14 #include "util.h"
15
16 static const char *arg_dest = "/tmp";
17 static char *arg_resume_device = NULL;
18 static bool arg_noresume = false;
19
20 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
21
22 if (streq(key, "resume")) {
23 char *s;
24
25 if (proc_cmdline_value_missing(key, value))
26 return 0;
27
28 s = fstab_node_to_udev_node(value);
29 if (!s)
30 return log_oom();
31
32 free_and_replace(arg_resume_device, s);
33
34 } else if (streq(key, "noresume")) {
35 if (value) {
36 log_warning("\"noresume\" kernel command line switch specified with an argument, ignoring.");
37 return 0;
38 }
39
40 arg_noresume = true;
41 }
42
43 return 0;
44 }
45
46 static int process_resume(void) {
47 _cleanup_free_ char *name = NULL, *lnk = NULL;
48 int r;
49
50 if (!arg_resume_device)
51 return 0;
52
53 r = unit_name_from_path_instance("systemd-hibernate-resume", arg_resume_device, ".service", &name);
54 if (r < 0)
55 return log_error_errno(r, "Failed to generate unit name: %m");
56
57 lnk = strjoin(arg_dest, "/" SPECIAL_SYSINIT_TARGET ".wants/", name);
58 if (!lnk)
59 return log_oom();
60
61 mkdir_parents_label(lnk, 0755);
62 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-hibernate-resume@.service", lnk) < 0)
63 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
64
65 return 0;
66 }
67
68 int main(int argc, char *argv[]) {
69 int r = 0;
70
71 log_set_prohibit_ipc(true);
72 log_set_target(LOG_TARGET_AUTO);
73 log_parse_environment();
74 log_open();
75
76 umask(0022);
77
78 if (argc > 1 && argc != 4) {
79 log_error("This program takes three or no arguments.");
80 return EXIT_FAILURE;
81 }
82
83 if (argc > 1)
84 arg_dest = argv[1];
85
86 /* Don't even consider resuming outside of initramfs. */
87 if (!in_initrd()) {
88 log_debug("Not running in an initrd, quitting.");
89 return EXIT_SUCCESS;
90 }
91
92 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
93 if (r < 0)
94 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
95
96 if (arg_noresume) {
97 log_notice("Found \"noresume\" on the kernel command line, quitting.");
98 r = 0;
99 goto finish;
100 }
101
102 r = process_resume();
103
104 finish:
105 free(arg_resume_device);
106
107 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
108 }