xref: /onnv-gate/usr/src/cmd/dfs.cmds/sharemgr/sharemgr_main.c (revision 3082:6bb20c63d0cc)
13034Sdougm /*
23034Sdougm  * CDDL HEADER START
33034Sdougm  *
43034Sdougm  * The contents of this file are subject to the terms of the
53034Sdougm  * Common Development and Distribution License (the "License").
63034Sdougm  * You may not use this file except in compliance with the License.
73034Sdougm  *
83034Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93034Sdougm  * or http://www.opensolaris.org/os/licensing.
103034Sdougm  * See the License for the specific language governing permissions
113034Sdougm  * and limitations under the License.
123034Sdougm  *
133034Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
143034Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153034Sdougm  * If applicable, add the following below this CDDL HEADER, with the
163034Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
173034Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
183034Sdougm  *
193034Sdougm  * CDDL HEADER END
203034Sdougm  */
213034Sdougm 
223034Sdougm /*
233034Sdougm  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
243034Sdougm  * Use is subject to license terms.
253034Sdougm  */
263034Sdougm 
273034Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
283034Sdougm 
293034Sdougm #include <stdlib.h>
303034Sdougm #include <stdio.h>
313034Sdougm #include <string.h>
323034Sdougm #include <ctype.h>
333034Sdougm #include <unistd.h>
343034Sdougm #include <getopt.h>
353034Sdougm #include <libgen.h>
363034Sdougm 
373034Sdougm #include <libshare.h>
383034Sdougm #include "sharemgr.h"
393034Sdougm #include <libintl.h>
403034Sdougm #include <locale.h>
413034Sdougm 
423034Sdougm char *protocol = NULL;
433034Sdougm static int help = 0;
443034Sdougm 
453034Sdougm static int run_command(char *, int, char **, char *);
463034Sdougm extern sa_command_t *sa_lookup(char *, char *);
473034Sdougm extern void sub_command_help(char *proto);
483034Sdougm 
493034Sdougm static void
503034Sdougm global_help()
513034Sdougm {
523034Sdougm 	(void) printf(gettext("usage: sharemgr [-h | <command> [options]]\n"));
533034Sdougm 	sub_command_help(NULL);
543034Sdougm }
553034Sdougm 
563034Sdougm int
573034Sdougm main(int argc, char *argv[])
583034Sdougm {
593034Sdougm 	int c;
603034Sdougm 	int rval;
613034Sdougm 	char *command = NULL;
623034Sdougm 
633034Sdougm 	/*
643034Sdougm 	 * make sure locale and gettext domain is setup
653034Sdougm 	 */
663034Sdougm 	(void) setlocale(LC_ALL, "");
673034Sdougm 	(void) textdomain(TEXT_DOMAIN);
683034Sdougm 
693034Sdougm 	/*
703034Sdougm 	 * parse enough of command line to get protocol, if any.
713034Sdougm 	 * Note that options need to come "after" the subcommand.
723034Sdougm 	 */
733034Sdougm 	command = basename(argv[0]);
743034Sdougm 	if (strcmp(command, "share") != 0 && strcmp(command, "unshare") != 0) {
753034Sdougm 	    while ((c = getopt(argc, argv, "h?")) != EOF) {
763034Sdougm 		switch (c) {
773034Sdougm 		default:
783034Sdougm 		case 'h':
793034Sdougm 		case '?':
803034Sdougm 		    help = 1;
813034Sdougm 		    break;
823034Sdougm 		}
833034Sdougm 	    }
843034Sdougm 	    if (argc == 1)
853034Sdougm 		help = 1;
863034Sdougm 	}
873034Sdougm 
883034Sdougm 	if (strcmp(command, "sharemgr") == 0) {
893034Sdougm 	    command = argv[optind];
903034Sdougm 	    argv++;
913034Sdougm 	    argc--;
923034Sdougm 	}
933034Sdougm 
943034Sdougm 	if (help) {
953034Sdougm 		/* no subcommand */
963034Sdougm 		global_help();
973034Sdougm 		exit(SA_OK);
983034Sdougm 	}
993034Sdougm 
1003034Sdougm 	/*
1013034Sdougm 	 * now have enough to parse rest of command line
102*3082Sdougm 	 *
103*3082Sdougm 	 * First, initialize the plugin architecture.
104*3082Sdougm 	 * Plugins are needed in the event of a global help
105*3082Sdougm 	 * request.
106*3082Sdougm 	 *
107*3082Sdougm 	 * reset optind to 1 so the parsing that takes place in
108*3082Sdougm 	 * sa_init() will work correctly.
1093034Sdougm 	 */
110*3082Sdougm 
111*3082Sdougm 	optind = 1;
112*3082Sdougm 	sa_init(SA_INIT_SHARE_API);
113*3082Sdougm 
114*3082Sdougm 	/*
115*3082Sdougm 	 * reset optind again since we will start parsing all over in
116*3082Sdougm 	 * the sub-commands.
117*3082Sdougm 	 */
118*3082Sdougm 	optind = 1;
1193034Sdougm 	rval = run_command(command, argc, argv, protocol);
1203034Sdougm 
1213034Sdougm 	sa_fini();
1223034Sdougm 	return (rval);
1233034Sdougm }
1243034Sdougm 
1253034Sdougm static int
1263034Sdougm run_command(char *command, int argc, char *argv[], char *proto)
1273034Sdougm {
1283034Sdougm 	sa_command_t *cmdvec;
1293034Sdougm 	int ret;
1303034Sdougm 
1313034Sdougm 	/*
1323034Sdougm 	 * To get here, we know there should be a command due to the
1333034Sdougm 	 * preprocessing done earlier.  Need to find the protocol
1343034Sdougm 	 * that is being affected. If no protocol, then it is ALL
1353034Sdougm 	 * protocols.
1363034Sdougm 	 *
1373034Sdougm 	 * We don't currently use the protocol here at this point. It
1383034Sdougm 	 * is left in as a placeholder for the future addition of
1393034Sdougm 	 * protocol specific sub-commands.
1403034Sdougm 	 *
1413034Sdougm 	 * Known sub-commands are handled at this level. An unknown
1423034Sdougm 	 * command will be passed down to the shared object that
1433034Sdougm 	 * actually implements it. We can do this since the semantics
1443034Sdougm 	 * of the common sub-commands is well defined.
1453034Sdougm 	 */
1463034Sdougm 
1473034Sdougm 	cmdvec = sa_lookup(command, proto);
1483034Sdougm 	if (cmdvec == NULL) {
1493034Sdougm 		(void) printf(gettext("command %s not found\n"), command);
1503034Sdougm 		exit(1);
1513034Sdougm 	}
1523034Sdougm 	/*
1533034Sdougm 	 * need to check priviledges and restrict what can be done
1543034Sdougm 	 * based on least priviledge and sub-command so pass this in
1553034Sdougm 	 * as a flag.
1563034Sdougm 	 */
1573034Sdougm 	ret = cmdvec->cmdfunc(cmdvec->priv, argc, argv);
1583034Sdougm 	return (ret);
1593034Sdougm }
160