]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hibernate-resume/hibernate-resume-generator.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / hibernate-resume / hibernate-resume-generator.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d2c68822
IS
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
d2c68822 21#include <errno.h>
07630cea 22#include <stdio.h>
d2c68822 23
b5efdb8a 24#include "alloc-util.h"
6550203e 25#include "fstab-util.h"
d2c68822 26#include "log.h"
d2c68822 27#include "mkdir.h"
4e731273 28#include "proc-cmdline.h"
07630cea
LP
29#include "special.h"
30#include "string-util.h"
d2c68822 31#include "unit-name.h"
07630cea 32#include "util.h"
d2c68822
IS
33
34static const char *arg_dest = "/tmp";
1d84ad94 35static char *arg_resume_device = NULL;
d2c68822 36
96287a49 37static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
7410616c 38
1d84ad94
LP
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)
d2c68822 47 return log_oom();
1d84ad94
LP
48
49 free(arg_resume_device);
50 arg_resume_device = s;
d2c68822
IS
51 }
52
53 return 0;
54}
55
56static int process_resume(void) {
57 _cleanup_free_ char *name = NULL, *lnk = NULL;
7410616c 58 int r;
d2c68822 59
1d84ad94 60 if (!arg_resume_device)
b5884878
LP
61 return 0;
62
1d84ad94 63 r = unit_name_from_path_instance("systemd-hibernate-resume", arg_resume_device, ".service", &name);
7410616c
LP
64 if (r < 0)
65 return log_error_errno(r, "Failed to generate unit name: %m");
d2c68822 66
605405c6 67 lnk = strjoin(arg_dest, "/" SPECIAL_SYSINIT_TARGET ".wants/", name);
d2c68822
IS
68 if (!lnk)
69 return log_oom();
70
71 mkdir_parents_label(lnk, 0755);
4a62c710
MS
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);
d2c68822
IS
74
75 return 0;
76}
77
78int 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
1d84ad94 99 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
b5884878 100 if (r < 0)
da927ba9 101 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
d2c68822 102
b5884878 103 r = process_resume();
1d84ad94 104 free(arg_resume_device);
d2c68822
IS
105
106 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
107}