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