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