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