From 3ddbe4d26b15a09bbec11f031aa522b0c9a9eebd Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 9 Jun 2009 10:37:45 +0200 Subject: switch_root: fix coding style Signed-off-by: Peter Jones Acked-by: Peter Jones Signed-off-by: Karel Zak --- sys-utils/switch_root.c | 150 ++++++++++++++++++++++-------------------------- 1 file changed, 70 insertions(+), 80 deletions(-) (limited to 'sys-utils/switch_root.c') 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 * Jeremy Katz */ - -#define _GNU_SOURCE 1 - #include #include #include @@ -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 - */ -- cgit v1.2.3-55-g7522