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