5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers/***
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers This file is part of systemd.
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers Copyright 2010-2012 Lennart Poettering
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers systemd is free software; you can redistribute it and/or modify it
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers under the terms of the GNU Lesser General Public License as published by
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers the Free Software Foundation; either version 2.1 of the License, or
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers (at your option) any later version.
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers systemd is distributed in the hope that it will be useful, but
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers WITHOUT ANY WARRANTY; without even the implied warranty of
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers Lesser General Public License for more details.
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers You should have received a copy of the GNU Lesser General Public License
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers along with systemd; If not, see <http://www.gnu.org/licenses/>.
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers***/
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers#include <errno.h>
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers#include <stdlib.h>
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers#include <unistd.h>
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
ee104e11e303499a637c5cd8157bd12ad5cc116dLennart Poettering#include "dev-setup.h"
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers#include "label.h"
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include "log.h"
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering#include "path-util.h"
ee104e11e303499a637c5cd8157bd12ad5cc116dLennart Poettering#include "user-util.h"
ee104e11e303499a637c5cd8157bd12ad5cc116dLennart Poettering#include "util.h"
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poetteringint dev_setup(const char *prefix, uid_t uid, gid_t gid) {
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers static const char symlinks[] =
696fee7d95194948f7f6a17eab93213b925a846dZbigniew Jędrzejewski-Szmek "-/proc/kcore\0" "/dev/core\0"
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers "/proc/self/fd\0" "/dev/fd\0"
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers "/proc/self/fd/0\0" "/dev/stdin\0"
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers "/proc/self/fd/1\0" "/dev/stdout\0"
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers "/proc/self/fd/2\0" "/dev/stderr\0";
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering const char *j, *k;
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering int r;
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner NULSTR_FOREACH_PAIR(j, k, symlinks) {
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering _cleanup_free_ char *link_name = NULL;
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering const char *n;
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering
696fee7d95194948f7f6a17eab93213b925a846dZbigniew Jędrzejewski-Szmek if (j[0] == '-') {
696fee7d95194948f7f6a17eab93213b925a846dZbigniew Jędrzejewski-Szmek j++;
696fee7d95194948f7f6a17eab93213b925a846dZbigniew Jędrzejewski-Szmek
2b85f4e19cee6a8533208f9fd618a7da6d32ad51Lennart Poettering if (access(j, F_OK) < 0)
696fee7d95194948f7f6a17eab93213b925a846dZbigniew Jędrzejewski-Szmek continue;
696fee7d95194948f7f6a17eab93213b925a846dZbigniew Jędrzejewski-Szmek }
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner
01ed0e2307f3b889b64165fd503d79b4568c47e1Lennart Poettering if (prefix) {
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering link_name = prefix_root(prefix, k);
7f112f50fea585411ea2d493b3582bea77eb4d6eLennart Poettering if (!link_name)
7f112f50fea585411ea2d493b3582bea77eb4d6eLennart Poettering return -ENOMEM;
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering n = link_name;
01ed0e2307f3b889b64165fd503d79b4568c47e1Lennart Poettering } else
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering n = k;
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering r = symlink_label(j, n);
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering if (r < 0)
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering if (uid != UID_INVALID || gid != GID_INVALID)
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering if (lchown(n, uid, gid) < 0)
03cfe0d51499e86b1573d121337594719d9f2012Lennart Poettering log_debug_errno(errno, "Failed to chown %s: %m", n);
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner }
7f112f50fea585411ea2d493b3582bea77eb4d6eLennart Poettering
7f112f50fea585411ea2d493b3582bea77eb4d6eLennart Poettering return 0;
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers}