]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hibernate-resume/hibernate-resume-generator.c
01222db5167efca4e1d118e7dcab4a86eebb3bdc
[thirdparty/systemd.git] / src / hibernate-resume / hibernate-resume-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Ivan Shapovalov
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <stdio.h>
23
24 #include "alloc-util.h"
25 #include "fstab-util.h"
26 #include "log.h"
27 #include "mkdir.h"
28 #include "proc-cmdline.h"
29 #include "special.h"
30 #include "string-util.h"
31 #include "unit-name.h"
32 #include "util.h"
33
34 static const char *arg_dest = "/tmp";
35 static char *arg_resume_device = NULL;
36
37 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
38
39 if (streq(key, "resume")) {
40 char *s;
41
42 if (proc_cmdline_value_missing(key, value))
43 return 0;
44
45 s = fstab_node_to_udev_node(value);
46 if (!s)
47 return log_oom();
48
49 free(arg_resume_device);
50 arg_resume_device = s;
51 }
52
53 return 0;
54 }
55
56 static int process_resume(void) {
57 _cleanup_free_ char *name = NULL, *lnk = NULL;
58 int r;
59
60 if (!arg_resume_device)
61 return 0;
62
63 r = unit_name_from_path_instance("systemd-hibernate-resume", arg_resume_device, ".service", &name);
64 if (r < 0)
65 return log_error_errno(r, "Failed to generate unit name: %m");
66
67 lnk = strjoin(arg_dest, "/" SPECIAL_SYSINIT_TARGET ".wants/", name);
68 if (!lnk)
69 return log_oom();
70
71 mkdir_parents_label(lnk, 0755);
72 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-hibernate-resume@.service", lnk) < 0)
73 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
74
75 return 0;
76 }
77
78 int main(int argc, char *argv[]) {
79 int r = 0;
80
81 if (argc > 1 && argc != 4) {
82 log_error("This program takes three or no arguments.");
83 return EXIT_FAILURE;
84 }
85
86 if (argc > 1)
87 arg_dest = argv[1];
88
89 log_set_target(LOG_TARGET_SAFE);
90 log_parse_environment();
91 log_open();
92
93 umask(0022);
94
95 /* Don't even consider resuming outside of initramfs. */
96 if (!in_initrd())
97 return EXIT_SUCCESS;
98
99 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
100 if (r < 0)
101 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
102
103 r = process_resume();
104 free(arg_resume_device);
105
106 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
107 }