chiark / gitweb /
treewide: another round of simplifications
[elogind.git] / src / hibernate-resume / hibernate-resume-generator.c
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
31 static const char *arg_dest = "/tmp";
32 static char *arg_resume_dev = NULL;
33
34 static 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
45 static int process_resume(void) {
46         _cleanup_free_ char *name = NULL, *lnk = NULL;
47
48         if (!arg_resume_dev)
49                 return 0;
50
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)
61                 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
62
63         return 0;
64 }
65
66 int main(int argc, char *argv[]) {
67         int r = 0;
68
69         if (argc > 1 && argc != 4) {
70                 log_error("This program takes three or no arguments.");
71                 return EXIT_FAILURE;
72         }
73
74         if (argc > 1)
75                 arg_dest = argv[1];
76
77         log_set_target(LOG_TARGET_SAFE);
78         log_parse_environment();
79         log_open();
80
81         umask(0022);
82
83         /* Don't even consider resuming outside of initramfs. */
84         if (!in_initrd())
85                 return EXIT_SUCCESS;
86
87         r = parse_proc_cmdline(parse_proc_cmdline_item);
88         if (r < 0)
89                 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
90
91         r = process_resume();
92         free(arg_resume_dev);
93
94         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
95 }