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