17574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall/*
27574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * Copyright (C) 2012 The Android Open Source Project
37574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall *
47574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * Licensed under the Apache License, Version 2.0 (the "License");
57574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * you may not use this file except in compliance with the License.
67574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * You may obtain a copy of the License at
77574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall *
87574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall *      http://www.apache.org/licenses/LICENSE-2.0
97574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall *
107574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * Unless required by applicable law or agreed to in writing, software
117574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * distributed under the License is distributed on an "AS IS" BASIS,
127574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * See the License for the specific language governing permissions and
147574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * limitations under the License.
157574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall */
167574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
177574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall#include <stdio.h>
187574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall#include <string.h>
197574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall#include <stdlib.h>
207574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall#include <libgen.h>
217574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall#include "fs_mgr_priv.h"
227574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
237574c035b2dcf8316c562c648113be6be65cb061Ken Sumrallchar *me = "";
247574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
257574c035b2dcf8316c562c648113be6be65cb061Ken Sumrallstatic void usage(void)
267574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall{
277574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    ERROR("%s: usage: %s <-a | -n mnt_point blk_dev | -u> <fstab_file>\n", me, me);
287574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    exit(1);
297574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall}
307574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
317574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall/* Parse the command line.  If an error is encountered, print an error message
327574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * and exit the program, do not return to the caller.
337574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall * Return the number of argv[] entries consumed.
347574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall */
357574c035b2dcf8316c562c648113be6be65cb061Ken Sumrallstatic void parse_options(int argc, char *argv[], int *a_flag, int *u_flag, int *n_flag,
367574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall                     char **n_name, char **n_blk_dev)
377574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall{
387574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    me = basename(strdup(argv[0]));
397574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
407574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    if (argc <= 1) {
417574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        usage();
427574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    }
437574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
447574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    if (!strcmp(argv[1], "-a")) {
457574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        if (argc != 3) {
467574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall            usage();
477574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        }
487574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        *a_flag = 1;
497574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    }
507574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    if (!strcmp(argv[1], "-n")) {
517574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        if (argc != 5) {
527574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall            usage();
537574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        }
547574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        *n_flag = 1;
557574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        *n_name = argv[2];
567574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        *n_blk_dev = argv[3];
577574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    }
587574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    if (!strcmp(argv[1], "-u")) {
597574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        if (argc != 3) {
607574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall            usage();
617574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        }
627574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        *u_flag = 1;
637574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    }
647574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
657574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    /* If no flag is specified, it's an error */
667574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    if (!(*a_flag | *n_flag | *u_flag)) {
677574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        usage();
687574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    }
697574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
707574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    /* If more than one flag is specified, it's an error */
717574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    if ((*a_flag + *n_flag + *u_flag) > 1) {
727574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        usage();
737574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    }
747574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
757574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    return;
767574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall}
777574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
787574c035b2dcf8316c562c648113be6be65cb061Ken Sumrallint main(int argc, char *argv[])
797574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall{
807574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    int a_flag=0;
817574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    int u_flag=0;
827574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    int n_flag=0;
837574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    char *n_name;
847574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    char *n_blk_dev;
857574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    char *fstab;
867574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
877574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    klog_init();
887574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    klog_set_level(6);
897574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
907574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    parse_options(argc, argv, &a_flag, &u_flag, &n_flag, &n_name, &n_blk_dev);
917574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
927574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    /* The name of the fstab file is last, after the option */
937574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    fstab = argv[argc - 1];
947574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
957574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    if (a_flag) {
967574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        return fs_mgr_mount_all(fstab);
977574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    } else if (n_flag) {
987574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        return fs_mgr_do_mount(fstab, n_name, n_blk_dev, 0);
997574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    } else if (u_flag) {
1007574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        return fs_mgr_unmount_all(fstab);
1017574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    } else {
1027574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        ERROR("%s: Internal error, unknown option\n", me);
1037574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall        exit(1);
1047574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    }
1057574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
1067574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    /* Should not get here */
1077574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall    exit(1);
1087574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall}
1097574c035b2dcf8316c562c648113be6be65cb061Ken Sumrall
110