]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/dev-setup.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / shared / dev-setup.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
5ba2dc25
KS
2/***
3 This file is part of systemd.
4
5 Copyright 2010-2012 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
21#include <errno.h>
5ba2dc25 22#include <stdlib.h>
5ba2dc25
KS
23#include <unistd.h>
24
b5efdb8a 25#include "alloc-util.h"
ee104e11 26#include "dev-setup.h"
5ba2dc25 27#include "label.h"
a8fbdf54 28#include "log.h"
03cfe0d5 29#include "path-util.h"
ee104e11
LP
30#include "user-util.h"
31#include "util.h"
5ba2dc25 32
03cfe0d5 33int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
5ba2dc25 34 static const char symlinks[] =
696fee7d 35 "-/proc/kcore\0" "/dev/core\0"
5ba2dc25
KS
36 "/proc/self/fd\0" "/dev/fd\0"
37 "/proc/self/fd/0\0" "/dev/stdin\0"
38 "/proc/self/fd/1\0" "/dev/stdout\0"
39 "/proc/self/fd/2\0" "/dev/stderr\0";
40
03cfe0d5
LP
41 const char *j, *k;
42 int r;
43
8f0e73f2 44 NULSTR_FOREACH_PAIR(j, k, symlinks) {
03cfe0d5
LP
45 _cleanup_free_ char *link_name = NULL;
46 const char *n;
47
696fee7d
ZJS
48 if (j[0] == '-') {
49 j++;
50
2b85f4e1 51 if (access(j, F_OK) < 0)
696fee7d
ZJS
52 continue;
53 }
8f0e73f2 54
01ed0e23 55 if (prefix) {
03cfe0d5 56 link_name = prefix_root(prefix, k);
7f112f50
LP
57 if (!link_name)
58 return -ENOMEM;
8f0e73f2 59
03cfe0d5 60 n = link_name;
01ed0e23 61 } else
03cfe0d5
LP
62 n = k;
63
64 r = symlink_label(j, n);
65 if (r < 0)
66 log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
67
68 if (uid != UID_INVALID || gid != GID_INVALID)
69 if (lchown(n, uid, gid) < 0)
70 log_debug_errno(errno, "Failed to chown %s: %m", n);
8f0e73f2 71 }
7f112f50
LP
72
73 return 0;
5ba2dc25 74}