resume-generator.c revision d2c68822c47e37b582820f45b496b2e7d1f9e642
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2d0611ffc9f91c5fc2ddccb93f9a3d17791ae650takashi
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend/***
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend This file is part of systemd.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
7ec4d5cc4aa574e3191bc5a612e68fd8f25ab7earpluem Copyright 2014 Ivan Shapovalov
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend systemd is free software; you can redistribute it and/or modify it
b4a00883f358625923365ca1560c96edec172a52sf under the terms of the GNU Lesser General Public License as published by
50c04f297d76a57ead2fa6b73845f7563b1fc788sf the Free Software Foundation; either version 2.1 of the License, or
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend (at your option) any later version.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
d9b843d090f14405079b4a61a493316cd3f1e5b9minfrin systemd is distributed in the hope that it will be useful, but
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend WITHOUT ANY WARRANTY; without even the implied warranty of
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
909ce17e2bd0faef7b1c294f2307f009793fd493nd Lesser General Public License for more details.
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
0ce5630695a7aa568811a9dd0aceedd685f040dend You should have received a copy of the GNU Lesser General Public License
a29610af88e278144045bfa1bc63b4a1a4b5ff14trawick along with systemd; If not, see <http://www.gnu.org/licenses/>.
42af92a661a06b3cebc88d585aad75064a309d51nd***/
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
6fe26506780e73be2a412d758af77fafdf03291and#include <stdio.h>
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include <errno.h>
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "log.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "util.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "special.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend#include "mkdir.h"
43f6cc6aa3312619633538b8978619a48c0ce52eminfrin#include "unit-name.h"
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
78f97ce162b66a0dbfd7af4dcd9984f162569b04minfrinstatic const char *arg_dest = "/tmp";
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fendstatic char *arg_resume_dev = NULL;
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fendstatic int parse_proc_cmdline_item(const char *key, const char *value) {
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend if (streq(key, "resume") && value) {
7906201913b68fe78b9d6a22ab33bf21d82c490eminfrin free(arg_resume_dev);
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend arg_resume_dev = fstab_node_to_udev_node(value);
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend if (!arg_resume_dev)
05ede5110427cb9dc071cc671d5aaba5d3b88c79nd return log_oom();
e8b603fa9ccf7b17b11b42df6d8916fd97c2331dnd }
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
2f39535ac2206b346dcfbcaf271e3addc901f520rbowen return 0;
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend}
d6ce05b6521a82cc93da69f7c2116c4a5bc54f8cjim
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fendstatic int process_resume(void) {
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend _cleanup_free_ char *name = NULL, *lnk = NULL;
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend name = unit_name_from_path_instance("systemd-hibernate-resume", arg_resume_dev, ".service");
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend if (!name)
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend return log_oom();
03a4ff9ac4c9b8009249010e7c53bb86ff05915and
23f1535d6a60817d2846bac0aea230ea475d7dccminfrin lnk = strjoin(arg_dest, "/" SPECIAL_SYSINIT_TARGET ".wants/", name, NULL);
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend if (!lnk)
1e367a83311e5243e04d68857d007d04765a48f9chrisd return log_oom();
1e367a83311e5243e04d68857d007d04765a48f9chrisd
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend mkdir_parents_label(lnk, 0755);
7d15331eeb5429d7148d13d6fd914a641bf1c000pquerna if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-hibernate-resume@.service", lnk) < 0) {
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend log_error("Failed to create symlink %s: %m", lnk);
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend return -errno;
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend }
2704de98885368683621b01c8f8f4e4b01557611takashi
2704de98885368683621b01c8f8f4e4b01557611takashi return 0;
2704de98885368683621b01c8f8f4e4b01557611takashi}
1e367a83311e5243e04d68857d007d04765a48f9chrisd
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fendint main(int argc, char *argv[]) {
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend int r = 0;
c819c19c2f1ffbf3a3f12a4070cc6c3f4ea2a6f2sf
d2b809e5d72658bff23819d8b77f20e4939af541nd if (argc > 1 && argc != 4) {
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend log_error("This program takes three or no arguments.");
fb51a6b789d85113d0976148685b0063c294220drbowen return EXIT_FAILURE;
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend }
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend if (argc > 1)
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend arg_dest = argv[1];
e81d1e4f661023c964899abcbf2a60c2f8278f4aminfrin
27d778df0b517e1578f907d2e51eb961cd8ee5fbjim log_set_target(LOG_TARGET_SAFE);
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend log_parse_environment();
6b0fe1f447ba35827cd5cf1d2a703bd8517f33ffmturk log_open();
6b0fe1f447ba35827cd5cf1d2a703bd8517f33ffmturk
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend umask(0022);
438b4817913a5ff55d9cad4c7ddf133330b4466ejim
e5ce3ac0e9b720c0fa23782e29168a0810697fdetakashi /* Don't even consider resuming outside of initramfs. */
f73390048b37c973a8016ffd22108d7a73c6cc22pquerna if (!in_initrd())
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend return EXIT_SUCCESS;
4126704c4950bfd46d32ad54e3b106ac6d868a73sf
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
0a69d9bb491d9810892a9949c01403a1de3c7ac2nd return EXIT_FAILURE;
ebdfe3e677aaa92fe2df702295eac81a97a4610frbowen
68c4447ba8e057cf38cbbec918e0549b817f20b4minfrin if (arg_resume_dev != NULL)
5f4e50966b2b9b58436a1651cbe588d1b595657ewrowe r = process_resume();
5332c9934b1c0136e9cd4815fe7d70b79d83fddcsf
623eebe956d9c2d6d073ed3eae855b56030b40e9noodl free(arg_resume_dev);
f73f2c2fae0ded6c8273c28d025ba8aa8136a0fend
79b024b81f6bb3c44dce77a7552191daf8b522d2jim return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
4277c95b5a9454a5c25f8d8762634fcff37aa840minfrin}
4277c95b5a9454a5c25f8d8762634fcff37aa840minfrin