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