xref: /titanic_41/usr/src/cmd/srptsvc/srptsvc.c (revision cab25e88571b90083876d2876f790fdf153773a2)
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 #include <libscf.h>	/* for SMF error exit codes */
36 
37 #include <srpt_ioctl.h>
38 
39 
40 /* Globals */
41 char	cmdName[] = "svc-srpt";
42 
43 int
44 main(int argc, char *argv[])
45 {
46 	int		ret;
47 	boolean_t	do_enable = B_FALSE;
48 	int		srpt_ioctl;
49 	int		fd = -1;
50 	int		saverr;
51 	char		*txt;
52 	struct stat	statbuf;
53 
54 	(void) setlocale(LC_ALL, "");
55 
56 	if (argc < 2 || argc > 2) {
57 		goto usage;
58 	}
59 
60 	if (strcasecmp(argv[1], "start") == 0) {
61 		do_enable = B_TRUE;
62 		srpt_ioctl = SRPT_IOC_ENABLE_SVC;
63 	} else if (strcasecmp(argv[1], "stop") == 0)  {
64 		srpt_ioctl = SRPT_IOC_DISABLE_SVC;
65 	} else {
66 		goto usage;
67 	}
68 
69 	fd = open(SRPT_NODE, O_RDONLY);
70 	if (fd < 0) {
71 		saverr = errno;
72 
73 		(void) fprintf(stderr, "%s: %s (%s): %s\n", cmdName,
74 		    gettext("Could not open SRP Target pseudodevice"),
75 		    SRPT_NODE, strerror(saverr));
76 
77 		if (saverr == ENOENT) {
78 			/* avoid having the service go into maintenance */
79 			if ((stat("/devices/ib", &statbuf)) != 0) {
80 				(void) fprintf(stderr, "%s: %s\n", cmdName,
81 				    gettext(
82 				    "No InfiniBand devices on this system."));
83 			}
84 			ret = 0;
85 
86 		} else {
87 			ret = SMF_EXIT_ERR_FATAL; /* avoid retries */
88 		}
89 
90 		(void) fprintf(stderr, "%s: %s\n", cmdName,
91 		    gettext("SRPT Driver may not be loaded."));
92 
93 		return (ret);
94 	}
95 
96 	ret = ioctl(fd, srpt_ioctl, NULL);
97 	if (ret != 0) {
98 		if (do_enable) {
99 			txt = gettext("Could not enable SRP Target");
100 		} else {
101 			txt = gettext("Could not disable SRP Target");
102 		}
103 
104 		(void) fprintf(stderr, "%s: %d", txt, ret);
105 		ret = SMF_EXIT_ERR_FATAL;
106 	}
107 
108 	(void) close(fd);
109 
110 	return (ret);
111 
112 usage:
113 	(void) fprintf(stderr, gettext("Usage: %s start|stop"), cmdName);
114 	(void) fprintf(stderr, "\n");
115 
116 	return (1);
117 }
118