xref: /onnv-gate/usr/src/cmd/dfs.cmds/sharemgr/sharemgr_main.c (revision 3034:3199b356d00f)
1*3034Sdougm /*
2*3034Sdougm  * CDDL HEADER START
3*3034Sdougm  *
4*3034Sdougm  * The contents of this file are subject to the terms of the
5*3034Sdougm  * Common Development and Distribution License (the "License").
6*3034Sdougm  * You may not use this file except in compliance with the License.
7*3034Sdougm  *
8*3034Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3034Sdougm  * or http://www.opensolaris.org/os/licensing.
10*3034Sdougm  * See the License for the specific language governing permissions
11*3034Sdougm  * and limitations under the License.
12*3034Sdougm  *
13*3034Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
14*3034Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3034Sdougm  * If applicable, add the following below this CDDL HEADER, with the
16*3034Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
17*3034Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
18*3034Sdougm  *
19*3034Sdougm  * CDDL HEADER END
20*3034Sdougm  */
21*3034Sdougm 
22*3034Sdougm /*
23*3034Sdougm  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*3034Sdougm  * Use is subject to license terms.
25*3034Sdougm  */
26*3034Sdougm 
27*3034Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*3034Sdougm 
29*3034Sdougm #include <stdlib.h>
30*3034Sdougm #include <stdio.h>
31*3034Sdougm #include <string.h>
32*3034Sdougm #include <ctype.h>
33*3034Sdougm #include <unistd.h>
34*3034Sdougm #include <getopt.h>
35*3034Sdougm #include <libgen.h>
36*3034Sdougm 
37*3034Sdougm #include <libshare.h>
38*3034Sdougm #include "sharemgr.h"
39*3034Sdougm #include <libintl.h>
40*3034Sdougm #include <locale.h>
41*3034Sdougm 
42*3034Sdougm char *protocol = NULL;
43*3034Sdougm static int help = 0;
44*3034Sdougm 
45*3034Sdougm static int run_command(char *, int, char **, char *);
46*3034Sdougm extern sa_command_t *sa_lookup(char *, char *);
47*3034Sdougm extern void sub_command_help(char *proto);
48*3034Sdougm 
49*3034Sdougm static void
50*3034Sdougm global_help()
51*3034Sdougm {
52*3034Sdougm 	(void) printf(gettext("usage: sharemgr [-h | <command> [options]]\n"));
53*3034Sdougm 	sub_command_help(NULL);
54*3034Sdougm }
55*3034Sdougm 
56*3034Sdougm int
57*3034Sdougm main(int argc, char *argv[])
58*3034Sdougm {
59*3034Sdougm 	int c;
60*3034Sdougm 	int rval;
61*3034Sdougm 	char *command = NULL;
62*3034Sdougm 
63*3034Sdougm 	/*
64*3034Sdougm 	 * make sure locale and gettext domain is setup
65*3034Sdougm 	 */
66*3034Sdougm 	(void) setlocale(LC_ALL, "");
67*3034Sdougm 	(void) textdomain(TEXT_DOMAIN);
68*3034Sdougm 
69*3034Sdougm 	/*
70*3034Sdougm 	 * initialize the plugin architecture.
71*3034Sdougm 	 * Plugins are needed in the event of a global help
72*3034Sdougm 	 * request.
73*3034Sdougm 	 */
74*3034Sdougm 
75*3034Sdougm 	sa_init(SA_INIT_SHARE_API);
76*3034Sdougm 	optind = 1;	/* reset to beginning */
77*3034Sdougm 
78*3034Sdougm 	/*
79*3034Sdougm 	 * parse enough of command line to get protocol, if any.
80*3034Sdougm 	 * Note that options need to come "after" the subcommand.
81*3034Sdougm 	 */
82*3034Sdougm 	command = basename(argv[0]);
83*3034Sdougm 	if (strcmp(command, "share") != 0 && strcmp(command, "unshare") != 0) {
84*3034Sdougm 	    while ((c = getopt(argc, argv, "h?")) != EOF) {
85*3034Sdougm 		switch (c) {
86*3034Sdougm 		default:
87*3034Sdougm 		case 'h':
88*3034Sdougm 		case '?':
89*3034Sdougm 		    help = 1;
90*3034Sdougm 		    break;
91*3034Sdougm 		}
92*3034Sdougm 	    }
93*3034Sdougm 	    if (argc == 1)
94*3034Sdougm 		help = 1;
95*3034Sdougm 	}
96*3034Sdougm 
97*3034Sdougm 	if (strcmp(command, "sharemgr") == 0) {
98*3034Sdougm 	    command = argv[optind];
99*3034Sdougm 	    argv++;
100*3034Sdougm 	    argc--;
101*3034Sdougm 	}
102*3034Sdougm 
103*3034Sdougm 	if (help) {
104*3034Sdougm 		/* no subcommand */
105*3034Sdougm 		global_help();
106*3034Sdougm 		exit(SA_OK);
107*3034Sdougm 	}
108*3034Sdougm 
109*3034Sdougm 	/*
110*3034Sdougm 	 * now have enough to parse rest of command line
111*3034Sdougm 	 */
112*3034Sdougm 	rval = run_command(command, argc, argv, protocol);
113*3034Sdougm 
114*3034Sdougm 	sa_fini();
115*3034Sdougm 	return (rval);
116*3034Sdougm }
117*3034Sdougm 
118*3034Sdougm static int
119*3034Sdougm run_command(char *command, int argc, char *argv[], char *proto)
120*3034Sdougm {
121*3034Sdougm 	sa_command_t *cmdvec;
122*3034Sdougm 	int ret;
123*3034Sdougm 
124*3034Sdougm 	/*
125*3034Sdougm 	 * To get here, we know there should be a command due to the
126*3034Sdougm 	 * preprocessing done earlier.  Need to find the protocol
127*3034Sdougm 	 * that is being affected. If no protocol, then it is ALL
128*3034Sdougm 	 * protocols.
129*3034Sdougm 	 *
130*3034Sdougm 	 * We don't currently use the protocol here at this point. It
131*3034Sdougm 	 * is left in as a placeholder for the future addition of
132*3034Sdougm 	 * protocol specific sub-commands.
133*3034Sdougm 	 *
134*3034Sdougm 	 * Known sub-commands are handled at this level. An unknown
135*3034Sdougm 	 * command will be passed down to the shared object that
136*3034Sdougm 	 * actually implements it. We can do this since the semantics
137*3034Sdougm 	 * of the common sub-commands is well defined.
138*3034Sdougm 	 */
139*3034Sdougm 
140*3034Sdougm 	cmdvec = sa_lookup(command, proto);
141*3034Sdougm 	if (cmdvec == NULL) {
142*3034Sdougm 		(void) printf(gettext("command %s not found\n"), command);
143*3034Sdougm 		exit(1);
144*3034Sdougm 	}
145*3034Sdougm 	/*
146*3034Sdougm 	 * need to check priviledges and restrict what can be done
147*3034Sdougm 	 * based on least priviledge and sub-command so pass this in
148*3034Sdougm 	 * as a flag.
149*3034Sdougm 	 */
150*3034Sdougm 	ret = cmdvec->cmdfunc(cmdvec->priv, argc, argv);
151*3034Sdougm 	return (ret);
152*3034Sdougm }
153