xref: /illumos-gate/usr/src/cmd/srptsvc/srptsvc.c (revision 129b3e6c5b0ac55b5021a4c38db6387b6acdaaf1)
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 2009 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 <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <locale.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <strings.h>
35 
36 #include <srpt_ioctl.h>
37 
38 
39 /* Globals */
40 char	cmdName[] = "svc-srpt";
41 
42 int
43 main(int argc, char *argv[])
44 {
45 	int		ret;
46 	boolean_t	do_enable = B_FALSE;
47 	int		srpt_ioctl;
48 	int		fd = -1;
49 	int		saverr;
50 	char		*txt;
51 
52 	(void) setlocale(LC_ALL, "");
53 
54 	if (argc < 2 || argc > 2) {
55 		goto usage;
56 	}
57 
58 	if (strcasecmp(argv[1], "start") == 0) {
59 		do_enable = B_TRUE;
60 		srpt_ioctl = SRPT_IOC_ENABLE_SVC;
61 	} else if (strcasecmp(argv[1], "stop") == 0)  {
62 		srpt_ioctl = SRPT_IOC_DISABLE_SVC;
63 	} else {
64 		goto usage;
65 	}
66 
67 	fd = open(SRPT_NODE, O_RDONLY);
68 	if (fd < 0) {
69 		saverr = errno;
70 
71 		(void) fprintf(stderr, "%s: %s", cmdName,
72 		    gettext("Could not open SRP Target pseudodevice."));
73 
74 		if (saverr == ENOENT) {
75 			(void) fprintf(stderr,
76 			    gettext("  Driver may not be loaded."));
77 		} else {
78 			(void) fprintf(stderr, gettext("  error = %d"), saverr);
79 		}
80 		(void) fprintf(stderr, "\n");
81 		return (1);
82 	}
83 
84 	ret = ioctl(fd, srpt_ioctl, NULL);
85 	if (ret != 0) {
86 		if (do_enable) {
87 			txt = gettext("Could not enable SRP Target");
88 		} else {
89 			txt = gettext("Could not disable SRP Target");
90 		}
91 
92 		(void) fprintf(stderr, "%s: %d", txt, ret);
93 		ret = 1;
94 	}
95 
96 	(void) close(fd);
97 
98 	return (ret);
99 
100 usage:
101 	(void) fprintf(stderr, gettext("Usage: %s start|stop"), cmdName);
102 	(void) fprintf(stderr, "\n");
103 
104 	return (1);
105 }
106