From 5ba2dc259f3cdd8fddef68cfd28380a32534e49a Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Tue, 17 Apr 2012 22:25:24 +0200 Subject: udev: unify /dev static symlink setup --- src/shared/dev-setup.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/shared/dev-setup.c (limited to 'src/shared/dev-setup.c') diff --git a/src/shared/dev-setup.c b/src/shared/dev-setup.c new file mode 100644 index 0000000000..0b3d648acb --- /dev/null +++ b/src/shared/dev-setup.c @@ -0,0 +1,65 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010-2012 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include +#include +#include + +#include "dev-setup.h" +#include "log.h" +#include "macro.h" +#include "util.h" +#include "label.h" + +static int symlink_and_label(const char *old_path, const char *new_path) { + int r; + + assert(old_path); + assert(new_path); + + r = label_context_set(new_path, S_IFLNK); + if (r < 0) + return r; + + if (symlink(old_path, new_path) < 0) + r = -errno; + + label_context_clear(); + + return r; +} + +void dev_setup(void) { + 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) + symlink_and_label(j, k); +} -- cgit v1.2.3-54-g00ecf From 8f0e73f250f4a397ea07d29a339bd7e64d077612 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Tue, 14 Aug 2012 20:00:30 -0400 Subject: dev-setup: allow a path prefix for use in chroots With this adjustment, we can reuse this code elsewhere, such as in nspawn. --- src/core/mount-setup.c | 2 +- src/shared/dev-setup.c | 16 +++++++++++++--- src/shared/dev-setup.h | 7 ++----- src/udev/udevd.c | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) (limited to 'src/shared/dev-setup.c') diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c index e86a89321e..be11bb8f36 100644 --- a/src/core/mount-setup.c +++ b/src/core/mount-setup.c @@ -398,7 +398,7 @@ int mount_setup(bool loaded_policy) { /* Create a few default symlinks, which are normally created * by udevd, but some scripts might need them before we start * udevd. */ - dev_setup(); + dev_setup(""); /* Mark the root directory as shared in regards to mount * propagation. The kernel defaults to "private", but we think diff --git a/src/shared/dev-setup.c b/src/shared/dev-setup.c index 0b3d648acb..759ecd799f 100644 --- a/src/shared/dev-setup.c +++ b/src/shared/dev-setup.c @@ -50,7 +50,7 @@ static int symlink_and_label(const char *old_path, const char *new_path) { return r; } -void dev_setup(void) { +void dev_setup(const char *pathprefix) { const char *j, *k; static const char symlinks[] = @@ -60,6 +60,16 @@ void dev_setup(void) { "/proc/self/fd/1\0" "/dev/stdout\0" "/proc/self/fd/2\0" "/dev/stderr\0"; - NULSTR_FOREACH_PAIR(j, k, symlinks) - symlink_and_label(j, k); + 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); + } } diff --git a/src/shared/dev-setup.h b/src/shared/dev-setup.h index 58507587d4..320c0b30ba 100644 --- a/src/shared/dev-setup.h +++ b/src/shared/dev-setup.h @@ -1,7 +1,6 @@ /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ -#ifndef foodevsetuphfoo -#define foodevsetuphfoo +#pragma once /*** This file is part of systemd. @@ -22,6 +21,4 @@ along with systemd; If not, see . ***/ -void dev_setup(void); - -#endif +void dev_setup(const char *pathprefix); diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 23351aebd5..1bb15d8c9e 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -1155,7 +1155,7 @@ int main(int argc, char *argv[]) mkdir("/run/udev", 0755); - dev_setup(); + dev_setup(""); static_dev_create_from_modules(udev); /* before opening new files, make sure std{in,out,err} fds are in a sane state */ -- cgit v1.2.3-54-g00ecf From 01ed0e2307f3b889b64165fd503d79b4568c47e1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 21 Aug 2012 17:23:03 +0200 Subject: dev-setup: make NULL as parameter for dev_setup() equivalent to "" --- src/core/mount-setup.c | 2 +- src/shared/dev-setup.c | 19 +++++++++++-------- src/udev/udevd.c | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) (limited to 'src/shared/dev-setup.c') diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c index be11bb8f36..1a82a46fef 100644 --- a/src/core/mount-setup.c +++ b/src/core/mount-setup.c @@ -398,7 +398,7 @@ int mount_setup(bool loaded_policy) { /* Create a few default symlinks, which are normally created * by udevd, but some scripts might need them before we start * udevd. */ - dev_setup(""); + dev_setup(NULL); /* Mark the root directory as shared in regards to mount * propagation. The kernel defaults to "private", but we think diff --git a/src/shared/dev-setup.c b/src/shared/dev-setup.c index 759ecd799f..b0ac02d461 100644 --- a/src/shared/dev-setup.c +++ b/src/shared/dev-setup.c @@ -50,7 +50,7 @@ static int symlink_and_label(const char *old_path, const char *new_path) { return r; } -void dev_setup(const char *pathprefix) { +void dev_setup(const char *prefix) { const char *j, *k; static const char symlinks[] = @@ -61,15 +61,18 @@ void dev_setup(const char *pathprefix) { "/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; - } + if (prefix) { + char *linkname; - symlink_and_label(j, linkname); + if (asprintf(&linkname, "%s/%s", prefix, k) < 0) { + log_oom(); + break; + } - free(linkname); + symlink_and_label(j, linkname); + free(linkname); + } else + symlink_and_label(j, k); } } diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 1bb15d8c9e..b4fc624db3 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -1155,7 +1155,7 @@ int main(int argc, char *argv[]) mkdir("/run/udev", 0755); - dev_setup(""); + dev_setup(NULL); static_dev_create_from_modules(udev); /* before opening new files, make sure std{in,out,err} fds are in a sane state */ -- cgit v1.2.3-54-g00ecf