blob: f3919d9d985c9890b6fcc940fb14e6e71c16b2f2 [file] [log] [blame]
Ken Sumrall7574c032012-01-06 19:09:42 -08001/*
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>
Ken Sumrall7574c032012-01-06 19:09:42 -080020#include "fs_mgr_priv.h"
21
Jin Qian8b7eb7b2016-11-03 13:36:35 -070022#ifdef _LIBGEN_H
23#warning "libgen.h must not be included"
24#endif
25
bowgotsaicea7ea72017-01-16 21:49:49 +080026char *me = nullptr;
Ken Sumrall7574c032012-01-06 19:09:42 -080027
28static void usage(void)
29{
bowgotsai47878de2017-01-23 14:04:34 +080030 LERROR << me << ": usage: " << me
31 << " <-a | -n mnt_point blk_dev | -u> <fstab_file>";
Ken Sumrall7574c032012-01-06 19:09:42 -080032 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 */
Jin Qian8b7eb7b2016-11-03 13:36:35 -070039static 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)
Ken Sumrall7574c032012-01-06 19:09:42 -080041{
Jin Qian8b7eb7b2016-11-03 13:36:35 -070042 me = basename(argv[0]);
Ken Sumrall7574c032012-01-06 19:09:42 -080043
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
Jin Qian8b7eb7b2016-11-03 13:36:35 -070082int main(int argc, char * const argv[])
Ken Sumrall7574c032012-01-06 19:09:42 -080083{
84 int a_flag=0;
85 int u_flag=0;
86 int n_flag=0;
Jin Qian8b7eb7b2016-11-03 13:36:35 -070087 const char *n_name=NULL;
88 const char *n_blk_dev=NULL;
89 const char *fstab_file=NULL;
Sasha Levitskiycdc1cfb2014-04-10 17:10:21 -070090 struct fstab *fstab=NULL;
Ken Sumrall7574c032012-01-06 19:09:42 -080091
bowgotsai47878de2017-01-23 14:04:34 +080092 setenv("ANDROID_LOG_TAGS", "*:i", 1); // Set log level to INFO
93 android::base::InitLogging(
94 const_cast<char **>(argv), &android::base::KernelLogger);
Ken Sumrall7574c032012-01-06 19:09:42 -080095
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 */
Ken Sumrallab6b8522013-02-13 12:58:40 -080099 fstab_file = argv[argc - 1];
100
101 fstab = fs_mgr_read_fstab(fstab_file);
Ken Sumrall7574c032012-01-06 19:09:42 -0800102
103 if (a_flag) {
Wei Wangd61a7e22016-08-23 11:58:09 -0700104 return fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT);
Ken Sumrall7574c032012-01-06 19:09:42 -0800105 } else if (n_flag) {
Jin Qian8b7eb7b2016-11-03 13:36:35 -0700106 return fs_mgr_do_mount(fstab, n_name, (char *)n_blk_dev, 0);
Ken Sumrall7574c032012-01-06 19:09:42 -0800107 } else if (u_flag) {
108 return fs_mgr_unmount_all(fstab);
109 } else {
bowgotsai47878de2017-01-23 14:04:34 +0800110 LERROR << me << ": Internal error, unknown option";
Ken Sumrall7574c032012-01-06 19:09:42 -0800111 exit(1);
112 }
113
Ken Sumrallab6b8522013-02-13 12:58:40 -0800114 fs_mgr_free_fstab(fstab);
115
Ken Sumrall7574c032012-01-06 19:09:42 -0800116 /* Should not get here */
117 exit(1);
118}