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