dev-setup.c revision 8f0e73f250f4a397ea07d29a339bd7e64d077612
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit/***
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit This file is part of systemd.
b34025e1e963e60c0f81c01af0f25f1984b9ca54James Phillpotts
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit Copyright 2010-2012 Lennart Poettering
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit systemd is free software; you can redistribute it and/or modify it
685810e390056c123842842f5104daa3179cf2c9Phill Cunnington under the terms of the GNU Lesser General Public License as published by
685810e390056c123842842f5104daa3179cf2c9Phill Cunnington the Free Software Foundation; either version 2.1 of the License, or
685810e390056c123842842f5104daa3179cf2c9Phill Cunnington (at your option) any later version.
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
685810e390056c123842842f5104daa3179cf2c9Phill Cunnington systemd is distributed in the hope that it will be useful, but
685810e390056c123842842f5104daa3179cf2c9Phill Cunnington WITHOUT ANY WARRANTY; without even the implied warranty of
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
685810e390056c123842842f5104daa3179cf2c9Phill Cunnington Lesser General Public License for more details.
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
685810e390056c123842842f5104daa3179cf2c9Phill Cunnington You should have received a copy of the GNU Lesser General Public License
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit along with systemd; If not, see <http://www.gnu.org/licenses/>.
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit***/
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include <errno.h>
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include <sys/stat.h>
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include <stdlib.h>
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include <string.h>
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include <assert.h>
b34025e1e963e60c0f81c01af0f25f1984b9ca54James Phillpotts#include <unistd.h>
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include "dev-setup.h"
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include "log.h"
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include "macro.h"
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include "util.h"
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit#include "label.h"
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavitstatic int symlink_and_label(const char *old_path, const char *new_path) {
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit int r;
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit assert(old_path);
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit assert(new_path);
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit r = label_context_set(new_path, S_IFLNK);
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit if (r < 0)
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit return r;
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit if (symlink(old_path, new_path) < 0)
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit r = -errno;
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
685810e390056c123842842f5104daa3179cf2c9Phill Cunnington label_context_clear();
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit return r;
80802511792d4e59a4ac67ad19677009d332b37dBruno Lavit}
void dev_setup(const char *pathprefix) {
const char *j, *k;
static const char symlinks[] =
"/proc/kcore\0" "/dev/core\0"
"/proc/self/fd\0" "/dev/fd\0"
"/proc/self/fd/0\0" "/dev/stdin\0"
"/proc/self/fd/1\0" "/dev/stdout\0"
"/proc/self/fd/2\0" "/dev/stderr\0";
NULSTR_FOREACH_PAIR(j, k, symlinks) {
char *linkname;
if (asprintf(&linkname, "%s/%s", pathprefix, k) < 0) {
log_oom();
break;
}
symlink_and_label(j, linkname);
free(linkname);
}
}