xref: /titanic_53/usr/src/cmd/sdpadm/sdpadm.c (revision 74e20cfe817b82802b16fac8690dadcda76f54f5)
1*74e20cfeSnh145002 /*
2*74e20cfeSnh145002  * CDDL HEADER START
3*74e20cfeSnh145002  *
4*74e20cfeSnh145002  * The contents of this file are subject to the terms of the
5*74e20cfeSnh145002  * Common Development and Distribution License (the "License").
6*74e20cfeSnh145002  * You may not use this file except in compliance with the License.
7*74e20cfeSnh145002  *
8*74e20cfeSnh145002  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*74e20cfeSnh145002  * or http://www.opensolaris.org/os/licensing.
10*74e20cfeSnh145002  * See the License for the specific language governing permissions
11*74e20cfeSnh145002  * and limitations under the License.
12*74e20cfeSnh145002  *
13*74e20cfeSnh145002  * When distributing Covered Code, include this CDDL HEADER in each
14*74e20cfeSnh145002  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*74e20cfeSnh145002  * If applicable, add the following below this CDDL HEADER, with the
16*74e20cfeSnh145002  * fields enclosed by brackets "[]" replaced with your own identifying
17*74e20cfeSnh145002  * information: Portions Copyright [yyyy] [name of copyright owner]
18*74e20cfeSnh145002  *
19*74e20cfeSnh145002  * CDDL HEADER END
20*74e20cfeSnh145002  */
21*74e20cfeSnh145002 /*
22*74e20cfeSnh145002  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*74e20cfeSnh145002  * Use is subject to license terms.
24*74e20cfeSnh145002  */
25*74e20cfeSnh145002 
26*74e20cfeSnh145002 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*74e20cfeSnh145002 
28*74e20cfeSnh145002 #include <stdio.h>
29*74e20cfeSnh145002 #include <libintl.h>
30*74e20cfeSnh145002 #include <stdlib.h>
31*74e20cfeSnh145002 #include <errno.h>
32*74e20cfeSnh145002 #include <locale.h>
33*74e20cfeSnh145002 #include <unistd.h>
34*74e20cfeSnh145002 #include <strings.h>
35*74e20cfeSnh145002 #include <fcntl.h>
36*74e20cfeSnh145002 #include <stropts.h>
37*74e20cfeSnh145002 
38*74e20cfeSnh145002 #include <sys/types.h>
39*74e20cfeSnh145002 #include <sys/socket.h>
40*74e20cfeSnh145002 #include <sys/sockio.h>
41*74e20cfeSnh145002 
42*74e20cfeSnh145002 #define	E_SUCCESS	0		/* Exit status for success */
43*74e20cfeSnh145002 #define	E_ERROR		1		/* Exit status for error */
44*74e20cfeSnh145002 #define	E_USAGE		2		/* Exit status for usage error */
45*74e20cfeSnh145002 
46*74e20cfeSnh145002 static const char USAGE[] = "Usage:\tsdpadm\tstatus\n\t\tenable\n\t\tdisable\n";
47*74e20cfeSnh145002 static const char OPTS[] = "?";
48*74e20cfeSnh145002 
49*74e20cfeSnh145002 static const char FILEENTRY[] = "sysenable=%d\n";
50*74e20cfeSnh145002 static const char FILENAME[] = "/etc/sdp.conf";
51*74e20cfeSnh145002 static const char dcname[] = "/dev/sdp";
52*74e20cfeSnh145002 
53*74e20cfeSnh145002 static char conf_header[] =
54*74e20cfeSnh145002 "#\n"
55*74e20cfeSnh145002 "# CDDL HEADER START\n"
56*74e20cfeSnh145002 "#\n"
57*74e20cfeSnh145002 "# The contents of this file are subject to the terms of the\n"
58*74e20cfeSnh145002 "# Common Development and Distribution License (the \"License\").\n"
59*74e20cfeSnh145002 "# You may not use this file except in compliance with the License.\n"
60*74e20cfeSnh145002 "#\n"
61*74e20cfeSnh145002 "# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE\n"
62*74e20cfeSnh145002 "# or http://www.opensolaris.org/os/licensing.\n"
63*74e20cfeSnh145002 "# See the License for the specific language governing permissions\n"
64*74e20cfeSnh145002 "# and limitations under the License.\n"
65*74e20cfeSnh145002 "#\n"
66*74e20cfeSnh145002 "# When distributing Covered Code, include this CDDL HEADER in each\n"
67*74e20cfeSnh145002 "# file and include the License file at usr/src/OPENSOLARIS.LICENSE.\n"
68*74e20cfeSnh145002 "# If applicable, add the following below this CDDL HEADER, with the\n"
69*74e20cfeSnh145002 "# fields enclosed by brackets \"[]\" replaced with your own identifying\n"
70*74e20cfeSnh145002 "# information: Portions Copyright [yyyy] [name of copyright owner]\n"
71*74e20cfeSnh145002 "#\n"
72*74e20cfeSnh145002 "# CDDL HEADER END\n"
73*74e20cfeSnh145002 "#\n"
74*74e20cfeSnh145002 "#\n"
75*74e20cfeSnh145002 "# ident \"@(#)sdp.conf   1.1     07/01/03 SMI\"\n"
76*74e20cfeSnh145002 "#\n"
77*74e20cfeSnh145002 "# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.\n"
78*74e20cfeSnh145002 "# Use is subject to license terms.\n"
79*74e20cfeSnh145002 "#\n\n";
80*74e20cfeSnh145002 
81*74e20cfeSnh145002 static void
82*74e20cfeSnh145002 usage()
83*74e20cfeSnh145002 {
84*74e20cfeSnh145002 	(void) fprintf(stderr, gettext(USAGE));
85*74e20cfeSnh145002 	exit(E_USAGE);
86*74e20cfeSnh145002 }
87*74e20cfeSnh145002 
88*74e20cfeSnh145002 int
89*74e20cfeSnh145002 main(int argc, char *argv[])
90*74e20cfeSnh145002 {
91*74e20cfeSnh145002 	int c, enable, ret = E_SUCCESS;
92*74e20cfeSnh145002 	int fd;
93*74e20cfeSnh145002 	FILE *fConf;
94*74e20cfeSnh145002 	struct strioctl stri;
95*74e20cfeSnh145002 
96*74e20cfeSnh145002 	(void) setlocale(LC_ALL, "");
97*74e20cfeSnh145002 	(void) textdomain(TEXT_DOMAIN);
98*74e20cfeSnh145002 
99*74e20cfeSnh145002 	while ((c = getopt(argc, argv, OPTS)) != EOF) {
100*74e20cfeSnh145002 		switch (c) {
101*74e20cfeSnh145002 			case '?':
102*74e20cfeSnh145002 				usage();
103*74e20cfeSnh145002 		}
104*74e20cfeSnh145002 	}
105*74e20cfeSnh145002 
106*74e20cfeSnh145002 	if (argc != 2) {
107*74e20cfeSnh145002 		usage();
108*74e20cfeSnh145002 	}
109*74e20cfeSnh145002 
110*74e20cfeSnh145002 	fd = open(dcname, O_RDONLY);
111*74e20cfeSnh145002 	if (fd  < 0) {
112*74e20cfeSnh145002 		(void) fprintf(stderr, gettext("opening %s failed errno %d\n"),
113*74e20cfeSnh145002 		    dcname, errno);
114*74e20cfeSnh145002 		exit(0);
115*74e20cfeSnh145002 	}
116*74e20cfeSnh145002 
117*74e20cfeSnh145002 	if (argc == 2) {
118*74e20cfeSnh145002 		/* Parse the on|off from the user */
119*74e20cfeSnh145002 		if (strncasecmp(argv[1], "enable", 7) == 0) {
120*74e20cfeSnh145002 			enable = 1;
121*74e20cfeSnh145002 		} else if (strncasecmp(argv[1], "disable", 8) == 0) {
122*74e20cfeSnh145002 			enable = 0;
123*74e20cfeSnh145002 		} else if (strncasecmp(argv[1], "status", 7) == 0)
124*74e20cfeSnh145002 			enable = -1;
125*74e20cfeSnh145002 		else {
126*74e20cfeSnh145002 			usage();
127*74e20cfeSnh145002 		}
128*74e20cfeSnh145002 	}
129*74e20cfeSnh145002 
130*74e20cfeSnh145002 	stri.ic_cmd = SIOSYSENABLESDP;
131*74e20cfeSnh145002 	stri.ic_timout = 0;
132*74e20cfeSnh145002 	stri.ic_len = sizeof (int);
133*74e20cfeSnh145002 	stri.ic_dp = (char *)&enable;
134*74e20cfeSnh145002 
135*74e20cfeSnh145002 	if (ioctl(fd, I_STR, &stri) >= 0) {
136*74e20cfeSnh145002 		(void) fprintf(stdout, gettext("SDP is %s\n"),
137*74e20cfeSnh145002 		    enable ? "Enabled" : "Disabled");
138*74e20cfeSnh145002 		fConf = fopen(FILENAME, "w");
139*74e20cfeSnh145002 		if (NULL != fConf) {
140*74e20cfeSnh145002 			(void) fprintf(fConf, conf_header);
141*74e20cfeSnh145002 			if (enable == 0) {
142*74e20cfeSnh145002 				(void) fprintf(fConf, FILEENTRY, 0);
143*74e20cfeSnh145002 			} else {
144*74e20cfeSnh145002 				(void) fprintf(fConf, FILEENTRY, 1);
145*74e20cfeSnh145002 			}
146*74e20cfeSnh145002 			(void) fclose(fConf);
147*74e20cfeSnh145002 		}
148*74e20cfeSnh145002 	} else {
149*74e20cfeSnh145002 		perror("ioctl failed");
150*74e20cfeSnh145002 		ret = E_ERROR;
151*74e20cfeSnh145002 	}
152*74e20cfeSnh145002 	return (ret);
153*74e20cfeSnh145002 }
154