]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hibernate-resume/hibernate-resume-generator.c
tree-wide: make parse_proc_cmdline() strip "rd." prefix automatically
[thirdparty/systemd.git] / src / hibernate-resume / hibernate-resume-generator.c
CommitLineData
d2c68822
IS
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
d2c68822 20#include <errno.h>
07630cea 21#include <stdio.h>
d2c68822 22
b5efdb8a 23#include "alloc-util.h"
6550203e 24#include "fstab-util.h"
d2c68822 25#include "log.h"
d2c68822 26#include "mkdir.h"
4e731273 27#include "proc-cmdline.h"
07630cea
LP
28#include "special.h"
29#include "string-util.h"
d2c68822 30#include "unit-name.h"
07630cea 31#include "util.h"
d2c68822
IS
32
33static const char *arg_dest = "/tmp";
34static char *arg_resume_dev = NULL;
35
96287a49 36static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
7410616c 37
d2c68822
IS
38 if (streq(key, "resume") && value) {
39 free(arg_resume_dev);
40 arg_resume_dev = fstab_node_to_udev_node(value);
41 if (!arg_resume_dev)
42 return log_oom();
43 }
44
45 return 0;
46}
47
48static int process_resume(void) {
49 _cleanup_free_ char *name = NULL, *lnk = NULL;
7410616c 50 int r;
d2c68822 51
b5884878
LP
52 if (!arg_resume_dev)
53 return 0;
54
7410616c
LP
55 r = unit_name_from_path_instance("systemd-hibernate-resume", arg_resume_dev, ".service", &name);
56 if (r < 0)
57 return log_error_errno(r, "Failed to generate unit name: %m");
d2c68822
IS
58
59 lnk = strjoin(arg_dest, "/" SPECIAL_SYSINIT_TARGET ".wants/", name, NULL);
60 if (!lnk)
61 return log_oom();
62
63 mkdir_parents_label(lnk, 0755);
4a62c710
MS
64 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-hibernate-resume@.service", lnk) < 0)
65 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
d2c68822
IS
66
67 return 0;
68}
69
70int main(int argc, char *argv[]) {
71 int r = 0;
72
73 if (argc > 1 && argc != 4) {
74 log_error("This program takes three or no arguments.");
75 return EXIT_FAILURE;
76 }
77
78 if (argc > 1)
79 arg_dest = argv[1];
80
81 log_set_target(LOG_TARGET_SAFE);
82 log_parse_environment();
83 log_open();
84
85 umask(0022);
86
87 /* Don't even consider resuming outside of initramfs. */
88 if (!in_initrd())
89 return EXIT_SUCCESS;
90
d7f69e16 91 r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, false);
b5884878 92 if (r < 0)
da927ba9 93 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
d2c68822 94
b5884878 95 r = process_resume();
d2c68822
IS
96 free(arg_resume_dev);
97
98 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
99}