]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/hibernate-resume/hibernate-resume-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
9 #include "fstab-util.h"
10 #include "generator.h"
12 #include "main-func.h"
14 #include "proc-cmdline.h"
16 #include "string-util.h"
17 #include "unit-name.h"
19 static const char *arg_dest
= "/tmp";
20 static char *arg_resume_device
= NULL
;
21 static char *arg_resume_options
= NULL
;
22 static char *arg_root_options
= NULL
;
23 static bool arg_noresume
= false;
25 STATIC_DESTRUCTOR_REGISTER(arg_resume_device
, freep
);
26 STATIC_DESTRUCTOR_REGISTER(arg_resume_options
, freep
);
27 STATIC_DESTRUCTOR_REGISTER(arg_root_options
, freep
);
29 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
31 if (streq(key
, "resume")) {
34 if (proc_cmdline_value_missing(key
, value
))
37 s
= fstab_node_to_udev_node(value
);
41 free_and_replace(arg_resume_device
, s
);
43 } else if (streq(key
, "resumeflags")) {
45 if (proc_cmdline_value_missing(key
, value
))
48 if (!strextend_with_separator(&arg_resume_options
, ",", value
, NULL
))
51 } else if (streq(key
, "rootflags")) {
53 if (proc_cmdline_value_missing(key
, value
))
56 if (!strextend_with_separator(&arg_root_options
, ",", value
, NULL
))
59 } else if (streq(key
, "noresume")) {
61 log_warning("\"noresume\" kernel command line switch specified with an argument, ignoring.");
71 static int process_resume(void) {
72 _cleanup_free_
char *service_unit
= NULL
, *device_unit
= NULL
, *lnk
= NULL
;
75 if (!arg_resume_device
)
78 r
= unit_name_from_path_instance("systemd-hibernate-resume", arg_resume_device
, ".service",
81 return log_error_errno(r
, "Failed to generate unit name: %m");
83 lnk
= strjoin(arg_dest
, "/" SPECIAL_SYSINIT_TARGET
".wants/", service_unit
);
87 mkdir_parents_label(lnk
, 0755);
88 if (symlink(SYSTEM_DATA_UNIT_PATH
"/systemd-hibernate-resume@.service", lnk
) < 0)
89 return log_error_errno(errno
, "Failed to create symlink %s: %m", lnk
);
91 r
= unit_name_from_path(arg_resume_device
, ".device", &device_unit
);
93 return log_error_errno(r
, "Failed to generate unit name: %m");
95 r
= write_drop_in(arg_dest
, device_unit
, 40, "device-timeout",
96 "# Automatically generated by systemd-hibernate-resume-generator\n\n"
97 "[Unit]\nJobTimeoutSec=0");
99 log_warning_errno(r
, "Failed to write device timeout drop-in: %m");
101 r
= generator_write_timeouts(arg_dest
,
104 arg_resume_options
?: arg_root_options
,
112 static int run(int argc
, char *argv
[]) {
115 log_setup_generator();
117 if (argc
> 1 && argc
!= 4)
118 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
119 "This program takes three or no arguments.");
124 /* Don't even consider resuming outside of initramfs. */
126 log_debug("Not running in an initrd, quitting.");
130 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
132 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
135 log_notice("Found \"noresume\" on the kernel command line, quitting.");
139 return process_resume();
142 DEFINE_MAIN_FUNCTION(run
);