1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <stdio.h> 18#include <string.h> 19#include <stdlib.h> 20#include "fs_mgr_priv.h" 21 22#ifdef _LIBGEN_H 23#warning "libgen.h must not be included" 24#endif 25 26char *me = nullptr; 27 28static void usage(void) 29{ 30 LERROR << me << ": usage: " << me 31 << " <-a | -n mnt_point blk_dev | -u> <fstab_file>"; 32 exit(1); 33} 34 35/* Parse the command line. If an error is encountered, print an error message 36 * and exit the program, do not return to the caller. 37 * Return the number of argv[] entries consumed. 38 */ 39static void parse_options(int argc, char * const argv[], int *a_flag, int *u_flag, int *n_flag, 40 const char **n_name, const char **n_blk_dev) 41{ 42 me = basename(argv[0]); 43 44 if (argc <= 1) { 45 usage(); 46 } 47 48 if (!strcmp(argv[1], "-a")) { 49 if (argc != 3) { 50 usage(); 51 } 52 *a_flag = 1; 53 } 54 if (!strcmp(argv[1], "-n")) { 55 if (argc != 5) { 56 usage(); 57 } 58 *n_flag = 1; 59 *n_name = argv[2]; 60 *n_blk_dev = argv[3]; 61 } 62 if (!strcmp(argv[1], "-u")) { 63 if (argc != 3) { 64 usage(); 65 } 66 *u_flag = 1; 67 } 68 69 /* If no flag is specified, it's an error */ 70 if (!(*a_flag | *n_flag | *u_flag)) { 71 usage(); 72 } 73 74 /* If more than one flag is specified, it's an error */ 75 if ((*a_flag + *n_flag + *u_flag) > 1) { 76 usage(); 77 } 78 79 return; 80} 81 82int main(int argc, char * const argv[]) 83{ 84 int a_flag=0; 85 int u_flag=0; 86 int n_flag=0; 87 const char *n_name=NULL; 88 const char *n_blk_dev=NULL; 89 const char *fstab_file=NULL; 90 struct fstab *fstab=NULL; 91 92 setenv("ANDROID_LOG_TAGS", "*:i", 1); // Set log level to INFO 93 android::base::InitLogging( 94 const_cast<char **>(argv), &android::base::KernelLogger); 95 96 parse_options(argc, argv, &a_flag, &u_flag, &n_flag, &n_name, &n_blk_dev); 97 98 /* The name of the fstab file is last, after the option */ 99 fstab_file = argv[argc - 1]; 100 101 fstab = fs_mgr_read_fstab(fstab_file); 102 103 if (a_flag) { 104 return fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT); 105 } else if (n_flag) { 106 return fs_mgr_do_mount(fstab, n_name, (char *)n_blk_dev, 0); 107 } else if (u_flag) { 108 return fs_mgr_unmount_all(fstab); 109 } else { 110 LERROR << me << ": Internal error, unknown option"; 111 exit(1); 112 } 113 114 fs_mgr_free_fstab(fstab); 115 116 /* Should not get here */ 117 exit(1); 118} 119