xref: /onnv-gate/usr/src/cmd/sdpadm/sdpadm.c (revision 4996:1fec74abebba)
13422Snh145002 /*
23422Snh145002  * CDDL HEADER START
33422Snh145002  *
43422Snh145002  * The contents of this file are subject to the terms of the
53422Snh145002  * Common Development and Distribution License (the "License").
63422Snh145002  * You may not use this file except in compliance with the License.
73422Snh145002  *
83422Snh145002  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93422Snh145002  * or http://www.opensolaris.org/os/licensing.
103422Snh145002  * See the License for the specific language governing permissions
113422Snh145002  * and limitations under the License.
123422Snh145002  *
133422Snh145002  * When distributing Covered Code, include this CDDL HEADER in each
143422Snh145002  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153422Snh145002  * If applicable, add the following below this CDDL HEADER, with the
163422Snh145002  * fields enclosed by brackets "[]" replaced with your own identifying
173422Snh145002  * information: Portions Copyright [yyyy] [name of copyright owner]
183422Snh145002  *
193422Snh145002  * CDDL HEADER END
203422Snh145002  */
213422Snh145002 /*
223422Snh145002  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
233422Snh145002  * Use is subject to license terms.
243422Snh145002  */
253422Snh145002 
263422Snh145002 #pragma ident	"%Z%%M%	%I%	%E% SMI"
273422Snh145002 
283422Snh145002 #include <stdio.h>
293422Snh145002 #include <libintl.h>
303422Snh145002 #include <stdlib.h>
313422Snh145002 #include <errno.h>
323422Snh145002 #include <locale.h>
333422Snh145002 #include <unistd.h>
343422Snh145002 #include <strings.h>
353422Snh145002 #include <fcntl.h>
363422Snh145002 #include <stropts.h>
373422Snh145002 
383422Snh145002 #include <sys/types.h>
393422Snh145002 #include <sys/socket.h>
403422Snh145002 #include <sys/sockio.h>
413422Snh145002 
423422Snh145002 #define	E_SUCCESS	0		/* Exit status for success */
433422Snh145002 #define	E_ERROR		1		/* Exit status for error */
443422Snh145002 #define	E_USAGE		2		/* Exit status for usage error */
453422Snh145002 
463422Snh145002 static const char USAGE[] = "Usage:\tsdpadm\tstatus\n\t\tenable\n\t\tdisable\n";
473422Snh145002 static const char OPTS[] = "?";
483422Snh145002 
493422Snh145002 static const char FILEENTRY[] = "sysenable=%d\n";
503422Snh145002 static const char FILENAME[] = "/etc/sdp.conf";
513422Snh145002 static const char dcname[] = "/dev/sdp";
523422Snh145002 
533422Snh145002 static char conf_header[] =
543422Snh145002 "#\n"
553422Snh145002 "# CDDL HEADER START\n"
563422Snh145002 "#\n"
573422Snh145002 "# The contents of this file are subject to the terms of the\n"
583422Snh145002 "# Common Development and Distribution License (the \"License\").\n"
593422Snh145002 "# You may not use this file except in compliance with the License.\n"
603422Snh145002 "#\n"
613422Snh145002 "# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE\n"
623422Snh145002 "# or http://www.opensolaris.org/os/licensing.\n"
633422Snh145002 "# See the License for the specific language governing permissions\n"
643422Snh145002 "# and limitations under the License.\n"
653422Snh145002 "#\n"
663422Snh145002 "# When distributing Covered Code, include this CDDL HEADER in each\n"
673422Snh145002 "# file and include the License file at usr/src/OPENSOLARIS.LICENSE.\n"
683422Snh145002 "# If applicable, add the following below this CDDL HEADER, with the\n"
693422Snh145002 "# fields enclosed by brackets \"[]\" replaced with your own identifying\n"
703422Snh145002 "# information: Portions Copyright [yyyy] [name of copyright owner]\n"
713422Snh145002 "#\n"
723422Snh145002 "# CDDL HEADER END\n"
733422Snh145002 "#\n"
743422Snh145002 "#\n"
753422Snh145002 "# ident \"@(#)sdp.conf   1.1     07/01/03 SMI\"\n"
763422Snh145002 "#\n"
773422Snh145002 "# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.\n"
783422Snh145002 "# Use is subject to license terms.\n"
793422Snh145002 "#\n\n";
803422Snh145002 
813422Snh145002 static void
usage()823422Snh145002 usage()
833422Snh145002 {
843422Snh145002 	(void) fprintf(stderr, gettext(USAGE));
853422Snh145002 	exit(E_USAGE);
863422Snh145002 }
873422Snh145002 
883422Snh145002 int
main(int argc,char * argv[])893422Snh145002 main(int argc, char *argv[])
903422Snh145002 {
913422Snh145002 	int c, enable, ret = E_SUCCESS;
923422Snh145002 	int fd;
933422Snh145002 	FILE *fConf;
943422Snh145002 	struct strioctl stri;
953422Snh145002 
963422Snh145002 	(void) setlocale(LC_ALL, "");
973422Snh145002 	(void) textdomain(TEXT_DOMAIN);
983422Snh145002 
993422Snh145002 	while ((c = getopt(argc, argv, OPTS)) != EOF) {
1003422Snh145002 		switch (c) {
1013422Snh145002 			case '?':
1023422Snh145002 				usage();
1033422Snh145002 		}
1043422Snh145002 	}
1053422Snh145002 
1063422Snh145002 	if (argc != 2) {
1073422Snh145002 		usage();
1083422Snh145002 	}
1093422Snh145002 
1103422Snh145002 	fd = open(dcname, O_RDONLY);
1113422Snh145002 	if (fd  < 0) {
1123422Snh145002 		(void) fprintf(stderr, gettext("opening %s failed errno %d\n"),
1133422Snh145002 		    dcname, errno);
1143422Snh145002 		exit(0);
1153422Snh145002 	}
1163422Snh145002 
1173422Snh145002 	if (argc == 2) {
1183422Snh145002 		/* Parse the on|off from the user */
119*4996Sse146197 		if (strcasecmp(argv[1], "enable") == 0) {
1203422Snh145002 			enable = 1;
121*4996Sse146197 		} else if (strcasecmp(argv[1], "disable") == 0) {
1223422Snh145002 			enable = 0;
123*4996Sse146197 		} else if (strcasecmp(argv[1], "status") == 0)
1243422Snh145002 			enable = -1;
1253422Snh145002 		else {
1263422Snh145002 			usage();
1273422Snh145002 		}
1283422Snh145002 	}
1293422Snh145002 
130*4996Sse146197 	stri.ic_cmd = SIOCSENABLESDP;
1313422Snh145002 	stri.ic_timout = 0;
1323422Snh145002 	stri.ic_len = sizeof (int);
1333422Snh145002 	stri.ic_dp = (char *)&enable;
1343422Snh145002 
1353422Snh145002 	if (ioctl(fd, I_STR, &stri) >= 0) {
1363422Snh145002 		(void) fprintf(stdout, gettext("SDP is %s\n"),
1373422Snh145002 		    enable ? "Enabled" : "Disabled");
1383422Snh145002 		fConf = fopen(FILENAME, "w");
1393422Snh145002 		if (NULL != fConf) {
1403422Snh145002 			(void) fprintf(fConf, conf_header);
1413422Snh145002 			if (enable == 0) {
1423422Snh145002 				(void) fprintf(fConf, FILEENTRY, 0);
1433422Snh145002 			} else {
1443422Snh145002 				(void) fprintf(fConf, FILEENTRY, 1);
1453422Snh145002 			}
1463422Snh145002 			(void) fclose(fConf);
1473422Snh145002 		}
1483422Snh145002 	} else {
1493422Snh145002 		perror("ioctl failed");
1503422Snh145002 		ret = E_ERROR;
1513422Snh145002 	}
1523422Snh145002 	return (ret);
1533422Snh145002 }
154