2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-04-17 22:25:24 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-27 00:42:07 +01:00
|
|
|
#include "dev-setup.h"
|
2012-04-17 22:25:24 +02:00
|
|
|
#include "label.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "log.h"
|
2015-05-21 16:30:58 +02:00
|
|
|
#include "path-util.h"
|
2015-10-27 00:42:07 +01:00
|
|
|
#include "user-util.h"
|
|
|
|
#include "util.h"
|
2012-04-17 22:25:24 +02:00
|
|
|
|
2015-05-21 16:30:58 +02:00
|
|
|
int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
|
2012-04-17 22:25:24 +02:00
|
|
|
static const char symlinks[] =
|
2013-06-10 14:50:59 +02:00
|
|
|
"-/proc/kcore\0" "/dev/core\0"
|
2012-04-17 22:25:24 +02:00
|
|
|
"/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";
|
|
|
|
|
2015-05-21 16:30:58 +02:00
|
|
|
const char *j, *k;
|
|
|
|
int r;
|
|
|
|
|
2012-08-15 02:00:30 +02:00
|
|
|
NULSTR_FOREACH_PAIR(j, k, symlinks) {
|
2015-05-21 16:30:58 +02:00
|
|
|
_cleanup_free_ char *link_name = NULL;
|
|
|
|
const char *n;
|
|
|
|
|
2013-06-10 14:50:59 +02:00
|
|
|
if (j[0] == '-') {
|
|
|
|
j++;
|
|
|
|
|
2014-03-19 16:23:32 +01:00
|
|
|
if (access(j, F_OK) < 0)
|
2013-06-10 14:50:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-08-15 02:00:30 +02:00
|
|
|
|
2012-08-21 17:23:03 +02:00
|
|
|
if (prefix) {
|
2015-05-21 16:30:58 +02:00
|
|
|
link_name = prefix_root(prefix, k);
|
2014-01-20 19:54:51 +01:00
|
|
|
if (!link_name)
|
|
|
|
return -ENOMEM;
|
2012-08-15 02:00:30 +02:00
|
|
|
|
2015-05-21 16:30:58 +02:00
|
|
|
n = link_name;
|
2012-08-21 17:23:03 +02:00
|
|
|
} else
|
2015-05-21 16:30:58 +02:00
|
|
|
n = k;
|
|
|
|
|
|
|
|
r = symlink_label(j, n);
|
|
|
|
if (r < 0)
|
|
|
|
log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
|
|
|
|
|
|
|
|
if (uid != UID_INVALID || gid != GID_INVALID)
|
|
|
|
if (lchown(n, uid, gid) < 0)
|
|
|
|
log_debug_errno(errno, "Failed to chown %s: %m", n);
|
2012-08-15 02:00:30 +02:00
|
|
|
}
|
2014-01-20 19:54:51 +01:00
|
|
|
|
|
|
|
return 0;
|
2012-04-17 22:25:24 +02:00
|
|
|
}
|