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