platform_system_core/toolbox/wipe.c
Ken Sumrall e3aeeb4de3 Try to unmount writable filesystems when rebooting
Ext4 filesystems like to be unmounted before rebooting.  The Android system
doesn't have a traditional Linux init setup, and shutting down the system
was not much more than calling sync(2) and reboot(2).  This adds a new
function to libcutils called android_reboot().  By default, it calls sync()
and then remounts all writable filesystems as read-only and marks them clean.
There is a flag parameter in which the caller can ask for sync() not to be
called, or to not remount the filesystems as read-only.  Then it will call
reboot(2) as directed by the other parameters.  This change also updates
adb, init and toolbox to call the new android_reboot() function.
Fixes bugs 3350709 and 3495575.

Change-Id: I16d71ffce3134310d7a260f61ec6f4dd204124a7
2011-03-10 18:11:46 -08:00

176 lines
4 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <cutils/android_reboot.h>
#include <sys/stat.h>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
/* Directories created by init defined in system/rootdir/init.rc */
static char *INIT_DIRS[] = {
"/system/etc/ppp",
"/data/misc",
"/data/local",
"/data/local/tmp",
"/data/data",
"/data/app_private",
"/data/app",
NULL
};
static void wipe (const char *path);
static int usage()
{
fprintf(stderr, "wipe <system|data|all>\n\n"
"system means '/system'\n"
"data means '/data'\n");
return -1;
}
int wipe_main (int argc, char *argv[])
{
char *whatToWipe;
if (argc != 2) return usage();
whatToWipe = argv[1];
if (0 == strcmp (whatToWipe, "system")) {
fprintf(stdout, "Wiping /system\n");
wipe ("/system");
fprintf(stdout, "Done wiping /android\n");
} else if (0 == strcmp (whatToWipe, "data")) {
fprintf(stdout, "Wiping /data\n");
wipe ("/data");
fprintf(stdout, "Done wiping /data\n");
} else if (0 == strcmp (whatToWipe, "all")) {
fprintf(stdout, "Wiping /system and /data\n");
wipe ("/system");
wipe ("/data");
fprintf(stdout, "Done wiping /system and /data\n");
} else if (0 == strcmp(whatToWipe, "nuke")) {
int ret;
fprintf(stdout, "Nuking the device...\n");
wipe ("/system");
wipe ("/data");
fprintf(stdout, "Device nuked! Rebooting...\n");
ret = android_reboot(ANDROID_RB_RESTART, 0, 0);
if (ret < 0) {
fprintf(stderr, "Reboot failed, %s\n", strerror(errno));
return 1;
}
} else {
return usage();
}
return 0;
}
static char nameBuffer[PATH_MAX];
static struct stat statBuffer;
static void wipe (const char *path)
{
DIR *dir;
struct dirent *de;
int ret;
dir = opendir(path);
if (dir == NULL) {
fprintf (stderr, "Error opendir'ing %s '%s'\n",
path, strerror(errno));
return;
}
char *filenameOffset;
strcpy(nameBuffer, path);
strcat(nameBuffer, "/");
filenameOffset = nameBuffer + strlen(nameBuffer);
for (;;) {
de = readdir(dir);
if (de == NULL) {
break;
}
if (0 == strcmp(de->d_name, ".")
|| 0 == strcmp(de->d_name, "..")
|| 0 == strcmp(de->d_name, "lost+found")
) {
continue;
}
strcpy(filenameOffset, de->d_name);
ret = lstat (nameBuffer, &statBuffer);
if (ret != 0) {
fprintf(stderr, "stat() error on '%s' '%s'\n",
nameBuffer, strerror(errno));
}
if(S_ISDIR(statBuffer.st_mode)) {
int i;
char *newpath;
#if 0
closedir(dir);
#endif
newpath = strdup(nameBuffer);
wipe(newpath);
/* Leave directories created by init, they have special permissions. */
for (i = 0; INIT_DIRS[i]; i++) {
if (strcmp(INIT_DIRS[i], newpath) == 0) {
break;
}
}
if (INIT_DIRS[i] == NULL) {
ret = rmdir(newpath);
if (ret != 0) {
fprintf(stderr, "rmdir() error on '%s' '%s'\n",
newpath, strerror(errno));
}
}
free(newpath);
#if 0
dir = opendir(path);
if (dir == NULL) {
fprintf (stderr, "Error opendir'ing %s '%s'\n",
path, strerror(errno));
return;
}
#endif
strcpy(nameBuffer, path);
strcat(nameBuffer, "/");
} else {
ret = unlink(nameBuffer);
if (ret != 0) {
fprintf(stderr, "unlink() error on '%s' '%s'\n",
nameBuffer, strerror(errno));
}
}
}
closedir(dir);
}