summaryrefslogtreecommitdiffstats
path: root/sys-utils/switch_root.c
diff options
context:
space:
mode:
authorKarel Zak2009-06-09 10:37:45 +0200
committerKarel Zak2009-06-22 21:30:47 +0200
commit3ddbe4d26b15a09bbec11f031aa522b0c9a9eebd (patch)
treee048d6a9a75ed90bee46beab33293d606ec74b0e /sys-utils/switch_root.c
parentbuild-sys: add --disable-switch_root (diff)
downloadkernel-qcow2-util-linux-3ddbe4d26b15a09bbec11f031aa522b0c9a9eebd.tar.gz
kernel-qcow2-util-linux-3ddbe4d26b15a09bbec11f031aa522b0c9a9eebd.tar.xz
kernel-qcow2-util-linux-3ddbe4d26b15a09bbec11f031aa522b0c9a9eebd.zip
switch_root: fix coding style
Signed-off-by: Peter Jones <pjones@redhat.com> Acked-by: Peter Jones <pjones@redhat.com> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils/switch_root.c')
-rw-r--r--sys-utils/switch_root.c150
1 files changed, 70 insertions, 80 deletions
diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c
index 9188006eb..152038723 100644
--- a/sys-utils/switch_root.c
+++ b/sys-utils/switch_root.c
@@ -20,9 +20,6 @@
* Peter Jones <pjones@redhat.com>
* Jeremy Katz <katzj@redhat.com>
*/
-
-#define _GNU_SOURCE 1
-
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -40,10 +37,6 @@
#define MS_MOVE 8192
#endif
-#ifndef MNT_DETACH
-#define MNT_DETACH 0x2
-#endif
-
enum {
ok,
err_no_directory,
@@ -53,69 +46,69 @@ enum {
/* remove all files/directories below dirName -- don't cross mountpoints */
static int
recursiveRemove(char * dirName)
- {
- struct stat sb,rb;
- DIR * dir;
- struct dirent * d;
- char * strBuf = alloca(strlen(dirName) + 1024);
-
- if (!(dir = opendir(dirName))) {
- printf("error opening %s: %m\n", dirName);
- return 0;
- }
-
- if (fstat(dirfd(dir),&rb)) {
- printf("unable to stat %s: %m\n", dirName);
- closedir(dir);
- return 0;
- }
-
- errno = 0;
- while ((d = readdir(dir))) {
- errno = 0;
-
- if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) {
- errno = 0;
- continue;
- }
-
- strcpy(strBuf, dirName);
- strcat(strBuf, "/");
- strcat(strBuf, d->d_name);
-
- if (lstat(strBuf, &sb)) {
- printf("failed to stat %s: %m\n", strBuf);
- errno = 0;
- continue;
- }
-
- /* only descend into subdirectories if device is same as dir */
- if (S_ISDIR(sb.st_mode)) {
- if (sb.st_dev == rb.st_dev) {
- recursiveRemove(strBuf);
- if (rmdir(strBuf))
- printf("failed to rmdir %s: %m\n", strBuf);
- }
- errno = 0;
- continue;
- }
- if (unlink(strBuf)) {
- printf("failed to remove %s: %m\n", strBuf);
- errno = 0;
- continue;
- }
- }
-
- if (errno) {
- closedir(dir);
- printf("error reading from %s: %m\n", dirName);
- return 1;
- }
-
- closedir(dir);
-
- return 0;
- }
+{
+ struct stat sb,rb;
+ DIR * dir;
+ struct dirent * d;
+ char * strBuf = alloca(strlen(dirName) + 1024);
+
+ if (!(dir = opendir(dirName))) {
+ printf("error opening %s: %m\n", dirName);
+ return 0;
+ }
+
+ if (fstat(dirfd(dir),&rb)) {
+ printf("unable to stat %s: %m\n", dirName);
+ closedir(dir);
+ return 0;
+ }
+
+ errno = 0;
+
+ while ((d = readdir(dir))) {
+ errno = 0;
+
+ if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) {
+ errno = 0;
+ continue;
+ }
+
+ strcpy(strBuf, dirName);
+ strcat(strBuf, "/");
+ strcat(strBuf, d->d_name);
+
+ if (lstat(strBuf, &sb)) {
+ printf("failed to stat %s: %m\n", strBuf);
+ errno = 0;
+ continue;
+ }
+
+ /* only descend into subdirectories if device is same as dir */
+ if (S_ISDIR(sb.st_mode)) {
+ if (sb.st_dev == rb.st_dev) {
+ recursiveRemove(strBuf);
+ if (rmdir(strBuf))
+ printf("failed to rmdir %s: %m\n", strBuf);
+ }
+ errno = 0;
+ continue;
+ }
+ if (unlink(strBuf)) {
+ printf("failed to remove %s: %m\n", strBuf);
+ errno = 0;
+ continue;
+ }
+ }
+
+ if (errno) {
+ closedir(dir);
+ printf("error reading from %s: %m\n", dirName);
+ return 1;
+ }
+
+ closedir(dir);
+ return 0;
+}
static int switchroot(const char *newroot)
{
@@ -137,10 +130,10 @@ static int switchroot(const char *newroot)
}
if (chdir(newroot) < 0) {
- errnum=errno;
- fprintf(stderr, "switchroot: chdir failed: %m\n");
- errno=errnum;
- return -1;
+ errnum=errno;
+ fprintf(stderr, "switchroot: chdir failed: %m\n");
+ errno=errnum;
+ return -1;
}
recursiveRemove("/");
if (mount(newroot, "/", NULL, MS_MOVE, NULL) < 0) {
@@ -179,11 +172,11 @@ int main(int argc, char *argv[])
}
if (switchroot(newroot) < 0) {
- fprintf(stderr, "switchroot has failed. Sorry.\n");
- return 1;
+ fprintf(stderr, "switchroot has failed. Sorry.\n");
+ return 1;
}
if (access(initargs[0], X_OK))
- fprintf(stderr, "WARNING: can't access %s\n", initargs[0]);
+ fprintf(stderr, "WARNING: can't access %s\n", initargs[0]);
/* get session leader */
setsid();
@@ -193,6 +186,3 @@ int main(int argc, char *argv[])
execv(initargs[0], initargs);
}
-/*
- * vim:noet:ts=8:sw=8:sts=8
- */