dev-setup.c revision 2b85f4e19cee6a8533208f9fd618a7da6d32ad51
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/***
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen This file is part of systemd.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Copyright 2010-2012 Lennart Poettering
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen systemd is free software; you can redistribute it and/or modify it
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen under the terms of the GNU Lesser General Public License as published by
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen (at your option) any later version.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen systemd is distributed in the hope that it will be useful, but
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Lesser General Public License for more details.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen You should have received a copy of the GNU Lesser General Public License
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen***/
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <errno.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <sys/stat.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <stdlib.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <string.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <assert.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <unistd.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include "dev-setup.h"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include "log.h"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include "macro.h"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include "util.h"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include "label.h"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic int symlink_and_label(const char *old_path, const char *new_path) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen int r;
920b52e4909d9dc812817fd8b82f83ca23a11c91Thomas Hindoe Paaboel Andersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert(old_path);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen assert(new_path);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = label_context_set(new_path, S_IFLNK);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (r < 0)
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering return r;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (symlink(old_path, new_path) < 0)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = -errno;
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen label_context_clear();
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return r;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen}
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenint dev_setup(const char *prefix) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const char *j, *k;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen static const char symlinks[] =
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen "-/proc/kcore\0" "/dev/core\0"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen "/proc/self/fd\0" "/dev/fd\0"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen "/proc/self/fd/0\0" "/dev/stdin\0"
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering "/proc/self/fd/1\0" "/dev/stdout\0"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen "/proc/self/fd/2\0" "/dev/stderr\0";
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen NULSTR_FOREACH_PAIR(j, k, symlinks) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (j[0] == '-') {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen j++;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (access(j, F_OK) < 0)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen continue;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen }
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (prefix) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen _cleanup_free_ char *link_name = NULL;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen link_name = strjoin(prefix, "/", k, NULL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (!link_name)
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen return -ENOMEM;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen symlink_and_label(j, link_name);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen } else
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen symlink_and_label(j, k);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen }
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen return 0;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen}
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering