Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 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> |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 20 | #include "fs_mgr_priv.h" |
| 21 | |
Jin Qian | 8b7eb7b | 2016-11-03 13:36:35 -0700 | [diff] [blame] | 22 | #ifdef _LIBGEN_H |
| 23 | #warning "libgen.h must not be included" |
| 24 | #endif |
| 25 | |
bowgotsai | cea7ea7 | 2017-01-16 21:49:49 +0800 | [diff] [blame] | 26 | char *me = nullptr; |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 27 | |
| 28 | static void usage(void) |
| 29 | { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 30 | LERROR << me << ": usage: " << me |
| 31 | << " <-a | -n mnt_point blk_dev | -u> <fstab_file>"; |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 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 | */ |
Jin Qian | 8b7eb7b | 2016-11-03 13:36:35 -0700 | [diff] [blame] | 39 | static 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 Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 41 | { |
Jin Qian | 8b7eb7b | 2016-11-03 13:36:35 -0700 | [diff] [blame] | 42 | me = basename(argv[0]); |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 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 | |
Jin Qian | 8b7eb7b | 2016-11-03 13:36:35 -0700 | [diff] [blame] | 82 | int main(int argc, char * const argv[]) |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 83 | { |
| 84 | int a_flag=0; |
| 85 | int u_flag=0; |
| 86 | int n_flag=0; |
Jin Qian | 8b7eb7b | 2016-11-03 13:36:35 -0700 | [diff] [blame] | 87 | const char *n_name=NULL; |
| 88 | const char *n_blk_dev=NULL; |
| 89 | const char *fstab_file=NULL; |
Sasha Levitskiy | cdc1cfb | 2014-04-10 17:10:21 -0700 | [diff] [blame] | 90 | struct fstab *fstab=NULL; |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 91 | |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 92 | setenv("ANDROID_LOG_TAGS", "*:i", 1); // Set log level to INFO |
| 93 | android::base::InitLogging( |
| 94 | const_cast<char **>(argv), &android::base::KernelLogger); |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 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 */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 99 | fstab_file = argv[argc - 1]; |
| 100 | |
| 101 | fstab = fs_mgr_read_fstab(fstab_file); |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 102 | |
| 103 | if (a_flag) { |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 104 | return fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT); |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 105 | } else if (n_flag) { |
Jin Qian | 8b7eb7b | 2016-11-03 13:36:35 -0700 | [diff] [blame] | 106 | return fs_mgr_do_mount(fstab, n_name, (char *)n_blk_dev, 0); |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 107 | } else if (u_flag) { |
| 108 | return fs_mgr_unmount_all(fstab); |
| 109 | } else { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 110 | LERROR << me << ": Internal error, unknown option"; |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 111 | exit(1); |
| 112 | } |
| 113 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 114 | fs_mgr_free_fstab(fstab); |
| 115 | |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 116 | /* Should not get here */ |
| 117 | exit(1); |
| 118 | } |