summaryrefslogtreecommitdiff
path: root/sys-utils
diff options
context:
space:
mode:
authorLaMont Jones <lamont@debian.org>2009-07-21 15:45:04 -0600
committerLaMont Jones <lamont@debian.org>2009-07-21 15:45:04 -0600
commit7a22bb9abcb56d1010cefd204709e3888f95f735 (patch)
tree5c38b4485f79ee8f6a9d5f9537e973f1f663f027 /sys-utils
parent4e13a18f57a5b427c559f3b466b9655db14d28f8 (diff)
parent741064af10ec81635d708e9fb8373f96341246eb (diff)
downloadutil-linux-old-7a22bb9abcb56d1010cefd204709e3888f95f735.tar.gz
Merge commit 'origin/master'
Conflicts: AUTHORS NEWS config/include-Makefile.am configure.ac mount/lomount.c po/POTFILES.in po/ca.po po/cs.po po/da.po po/de.po po/es.po po/et.po po/eu.po po/fi.po po/fr.po po/hu.po po/id.po po/it.po po/ja.po po/nl.po po/pl.po po/pt_BR.po po/ru.po po/sl.po po/sv.po po/tr.po po/uk.po po/util-linux-ng.pot po/vi.po po/zh_CN.po
Diffstat (limited to 'sys-utils')
-rw-r--r--sys-utils/.gitignore1
-rw-r--r--sys-utils/Makefile.am23
-rw-r--r--sys-utils/ldattach.85
-rw-r--r--sys-utils/ldattach.c7
-rw-r--r--sys-utils/switch_root.849
-rw-r--r--sys-utils/switch_root.c280
6 files changed, 354 insertions, 11 deletions
diff --git a/sys-utils/.gitignore b/sys-utils/.gitignore
index c47cd077..6f0be710 100644
--- a/sys-utils/.gitignore
+++ b/sys-utils/.gitignore
@@ -36,6 +36,7 @@ sparc32.8
sparc32bash.8
sparc64.8
sparc.8
+switch_root
tunelp
vidmode.8
x86_64.8
diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am
index b4c6ac60..19ecccca 100644
--- a/sys-utils/Makefile.am
+++ b/sys-utils/Makefile.am
@@ -2,8 +2,8 @@ include $(top_srcdir)/config/include-Makefile.am
bin_PROGRAMS =
sbin_PROGRAMS =
-usrbinexec_PROGRAMS = flock ipcrm ipcs ipcmk renice setsid
-usrsbinexec_PROGRAMS = readprofile
+usrbin_exec_PROGRAMS = flock ipcrm ipcs ipcmk renice setsid
+usrsbin_exec_PROGRAMS = readprofile
dist_man_MANS = flock.1 ipcrm.1 ipcs.1 ipcmk.1 renice.1 setsid.1 \
readprofile.1
@@ -11,8 +11,8 @@ dist_man_MANS = flock.1 ipcrm.1 ipcs.1 ipcmk.1 renice.1 setsid.1 \
if LINUX
bin_PROGRAMS += dmesg
sbin_PROGRAMS += ctrlaltdel
-usrbinexec_PROGRAMS += cytune setarch lscpu
-usrsbinexec_PROGRAMS += ldattach tunelp rtcwake
+usrbin_exec_PROGRAMS += cytune setarch lscpu
+usrsbin_exec_PROGRAMS += ldattach tunelp rtcwake
dist_man_MANS += dmesg.1 ctrlaltdel.8 cytune.8 setarch.8 \
ldattach.8 lscpu.1 tunelp.8 rtcwake.8
@@ -28,6 +28,11 @@ sbin_PROGRAMS += pivot_root
dist_man_MANS += pivot_root.8
endif
+if BUILD_SWITCH_ROOT
+sbin_PROGRAMS += switch_root
+dist_man_MANS += switch_root.8
+endif
+
if BUILD_ARCH
bin_PROGRAMS += arch
dist_man_MANS += arch.1
@@ -35,7 +40,7 @@ endif
RDEV_LINKS =
if BUILD_RDEV
-usrsbinexec_PROGRAMS += rdev
+usrsbin_exec_PROGRAMS += rdev
RDEV_LINKS += ramsize vidmode rootflags
dist_man_MANS += rdev.8
endif
@@ -80,17 +85,17 @@ $(SETARCH_MAN_LINKS): setarch.8
install-exec-hook:
for I in $(RDEV_LINKS); do \
- cd $(DESTDIR)$(usrsbinexecdir) && ln -sf rdev $$I ; \
+ cd $(DESTDIR)$(usrsbin_execdir) && ln -sf rdev $$I ; \
done
for I in $(SETARCH_LINKS); do \
- cd $(DESTDIR)$(usrbinexecdir) && ln -sf setarch $$I ; \
+ cd $(DESTDIR)$(usrbin_execdir) && ln -sf setarch $$I ; \
done
uninstall-hook:
for I in $(RDEV_LINKS); do \
- rm -f $(DESTDIR)$(usrbinexecdir)/$$I ; \
+ rm -f $(DESTDIR)$(usrbin_execdir)/$$I ; \
done
for I in $(SETARCH_LINKS); do \
- rm -f $(DESTDIR)$(usrbinexecdir)/$$I ; \
+ rm -f $(DESTDIR)$(usrbin_execdir)/$$I ; \
done
diff --git a/sys-utils/ldattach.8 b/sys-utils/ldattach.8
index 4ee62293..7afe51dd 100644
--- a/sys-utils/ldattach.8
+++ b/sys-utils/ldattach.8
@@ -35,7 +35,7 @@ With no arguments,
.B ldattach
prints usage information.
.SH LINE DISCIPLINES
-As of kernel release 2.6.21, the following line disciplines are supported:
+Depending on the kernel release, the following line disciplines are supported:
.TP
.BR TTY ( 0 )
The default line discipline,
@@ -80,6 +80,9 @@ Bluetooth HCI UART driver.
.TP
.BR GIGASET_M101 ( 16 )
Driver for Siemens Gigaset M101 serial DECT adapter.
+.TP
+.BR PPS ( 18 )
+Driver for serial line Pulse Per Second (PPS) source.
.SH OPTIONS
.TP
\fB-d\fP | \fB--debug\fP
diff --git a/sys-utils/ldattach.c b/sys-utils/ldattach.c
index 801dd1cb..19f70fe6 100644
--- a/sys-utils/ldattach.c
+++ b/sys-utils/ldattach.c
@@ -34,6 +34,10 @@
# define N_GIGASET_M101 16
#endif
+#ifndef N_PPS
+# define N_PPS 18
+#endif
+
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif
@@ -65,7 +69,8 @@ ld_table[] = {
{ "HCI", N_HCI },
{ "GIGASET_M101", N_GIGASET_M101 },
{ "GIGASET", N_GIGASET_M101 },
- { "M101", N_GIGASET_M101 }
+ { "M101", N_GIGASET_M101 },
+ { "PPS", N_PPS },
};
/* look up line discipline code */
diff --git a/sys-utils/switch_root.8 b/sys-utils/switch_root.8
new file mode 100644
index 00000000..ea175b00
--- /dev/null
+++ b/sys-utils/switch_root.8
@@ -0,0 +1,49 @@
+.\" Karel Zak <kzak@redhat.com>
+.TH SWITCH_ROOT 8 "June 2009" "Linux"
+.SH NAME
+switch_root \- switch to another filesystem as the root of the mount tree.
+.SH SYNOPSIS
+.B switch_root
+.RB [\-hV]
+.LP
+.B switch_root
+.I newroot
+.I init
+.RB [ \fIarg\fP
+.RB ...]
+.SH DESCRIPTION
+.B switch_root
+moves already mounted /proc, /dev and /sys to
+.I newroot
+and makes
+.I newroot
+the new root filesystem and starts
+.I init
+process.
+
+.B WARNING: switch_root removes recursively all files and directories on the current root filesystem.
+
+.SH OPTIONS
+.IP "\fB\-h, \-\-help\fP"
+show help and exit
+.IP "\fB\-V, \-\-version\fP"
+show version number and exit
+
+.SH RETURN VALUE
+.B switch_root
+returns 0 on success and 1 on failure.
+
+.SH "SEE ALSO"
+.BR mount (8)
+.BR chroot (2)
+.BR init (8)
+.BR mkinitrd (8)
+.SH AUTHORS
+.nf
+Peter Jones <pjones@redhat.com>
+Jeremy Katz <katzj@redhat.com>
+Karel Zak <kzak@redhat.com>
+.fi
+.SH AVAILABILITY
+The switch_root command is part of the util-linux-ng package and is available from
+ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.
diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c
new file mode 100644
index 00000000..b192a08e
--- /dev/null
+++ b/sys-utils/switch_root.c
@@ -0,0 +1,280 @@
+/*
+ * switchroot.c - switch to new root directory and start init.
+ *
+ * Copyright 2002-2009 Red Hat, Inc. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Peter Jones <pjones@redhat.com>
+ * Jeremy Katz <katzj@redhat.com>
+ */
+#include <sys/mount.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <ctype.h>
+#include <dirent.h>
+#include <err.h>
+#include <libgen.h>
+
+#ifndef MS_MOVE
+#define MS_MOVE 8192
+#endif
+
+/* remove all files/directories below dirName -- don't cross mountpoints */
+static int recursiveRemove(int fd)
+{
+ struct stat rb;
+ DIR *dir;
+ int rc = -1;
+ int dfd;
+
+ if (!(dir = fdopendir(fd))) {
+ warn("failed to open directory");
+ goto done;
+ }
+
+ /* fdopendir() precludes us from continuing to use the input fd */
+ dfd = dirfd(dir);
+
+ if (fstat(dfd, &rb)) {
+ warn("failed to stat directory");
+ goto done;
+ }
+
+ while(1) {
+ struct dirent *d;
+
+ errno = 0;
+ if (!(d = readdir(dir))) {
+ if (errno) {
+ warn("failed to read directory");
+ goto done;
+ }
+ break; /* end of directory */
+ }
+
+ if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
+ continue;
+
+ if (d->d_type == DT_DIR) {
+ struct stat sb;
+
+ if (fstatat(dfd, d->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
+ warn("failed to stat %s", d->d_name);
+ continue;
+ }
+
+ /* remove subdirectories if device is same as dir */
+ if (sb.st_dev == rb.st_dev) {
+ int cfd;
+
+ cfd = openat(dfd, d->d_name, O_RDONLY);
+ if (cfd >= 0) {
+ recursiveRemove(cfd);
+ close(cfd);
+ }
+ } else
+ continue;
+ }
+
+ if (unlinkat(dfd, d->d_name,
+ d->d_type == DT_DIR ? AT_REMOVEDIR : 0))
+ warn("failed to unlink %s", d->d_name);
+ }
+
+ rc = 0; /* success */
+
+done:
+ if (dir)
+ closedir(dir);
+ return rc;
+}
+
+/* find the enclosing mount point of a path, by examining the backing device
+ * of parent directories until we reach / or find a parent with a differing
+ * device. Result must be freed.
+ */
+static char *get_parent_mount(const char *path)
+{
+ struct stat sb;
+ char *dir;
+ char tmp[PATH_MAX];
+ dev_t inner_dev;
+
+ if (stat(path, &sb) != 0) {
+ warn("failed to stat %s", path);
+ return NULL;
+ }
+
+ inner_dev = sb.st_dev;
+ dir = strdup(path);
+
+ while (dir) {
+ char *parent;
+
+ strncpy(tmp, dir, PATH_MAX);
+ tmp[PATH_MAX - 1] = '\0';
+ parent = dirname(tmp);
+
+ if (stat(parent, &sb) != 0) {
+ warn("failed to stat %s", parent);
+ return NULL;
+ }
+ if (sb.st_dev != inner_dev)
+ return dir;
+
+ strncpy(dir, parent, PATH_MAX);
+ dir[PATH_MAX - 1] = '\0';
+
+ /* maybe we've reached / */
+ if (*dir == '/' && !*(dir + 1))
+ return dir;
+ }
+ return NULL;
+}
+
+static int switchroot(const char *newroot)
+{
+ /* Don't try to unmount the old "/", there's no way to do it. */
+ const char *umounts[] = { "/dev", "/proc", "/sys", NULL };
+ int i;
+ int cfd, rc = -1;
+ pid_t pid;
+ const char *chroot_path = NULL;
+ char *newroot_mnt;
+
+ for (i = 0; umounts[i] != NULL; i++) {
+ char newmount[PATH_MAX];
+
+ snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);
+
+ if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
+ warn("failed to mount moving %s to %s",
+ umounts[i], newmount);
+ warnx("forcing unmount of %s", umounts[i]);
+ umount2(umounts[i], MNT_FORCE);
+ }
+ }
+
+ newroot_mnt = get_parent_mount(newroot);
+ if (newroot_mnt && strcmp(newroot, newroot_mnt)) {
+ /* newroot is not a mount point, so we have to MS_MOVE the
+ * parent mount point and then chroot in to the "subroot"
+ */
+ chroot_path = newroot + strlen(newroot_mnt);
+ newroot = newroot_mnt;
+ }
+
+ if (chdir(newroot)) {
+ warn("failed to change directory to %s", newroot);
+ goto done;
+ }
+
+ cfd = open("/", O_RDONLY);
+
+ if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
+ warn("failed to mount moving %s to /", newroot);
+ goto done;
+ }
+
+ /* move to the real root of the device */
+ if (chroot(".")) {
+ warn("failed to change root");
+ goto done;
+ }
+
+ /* move to the subdirectory on the root device (subroot) */
+ if (chroot_path) {
+ if (chdir(chroot_path)) {
+ warn("failed to chdir to subroot %s", chroot_path);
+ goto done;
+ }
+ if (chroot(".")) {
+ warn("failed to change root to subroot %s", chroot_path);
+ goto done;
+ }
+ }
+
+ if (cfd >= 0) {
+ pid = fork();
+ if (pid <= 0) {
+ recursiveRemove(cfd);
+ if (pid == 0)
+ exit(EXIT_SUCCESS);
+ }
+ close(cfd);
+ }
+ rc = 0;
+done:
+ free(newroot_mnt);
+ return rc;
+}
+
+static void usage(FILE *output)
+{
+ fprintf(output, "usage: %s <newrootdir> <init> <args to init>\n",
+ program_invocation_short_name);
+ exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+static void version(void)
+{
+ fprintf(stdout, "%s from %s\n", program_invocation_short_name,
+ PACKAGE_STRING);
+ exit(EXIT_SUCCESS);
+}
+
+int main(int argc, char *argv[])
+{
+ char *newroot, *init, **initargs;
+
+ if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
+ usage(stdout);
+ if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")))
+ version();
+ if (argc < 3)
+ usage(stderr);
+
+ newroot = argv[1];
+ init = argv[2];
+ initargs = &argv[2];
+
+ if (!*newroot || !*init)
+ usage(stderr);
+
+ if (switchroot(newroot))
+ errx(EXIT_FAILURE, "failed. Sorry.");
+
+ if (access(init, X_OK))
+ warn("cannot access %s", init);
+
+ /* get session leader */
+ setsid();
+
+ /* set controlling terminal */
+ if (ioctl (0, TIOCSCTTY, 1))
+ warn("failed to TIOCSCTTY");
+
+ execv(init, initargs);
+ err(EXIT_FAILURE, "failed to execute %s", init);
+}
+