xref: /titanic_52/usr/src/cmd/srptsvc/srptsvc.c (revision 168665f7ddaca2a65705c3b127078ffc6b24adc3)
11bdd6c0eSSue Gleeson /*
21bdd6c0eSSue Gleeson  * CDDL HEADER START
31bdd6c0eSSue Gleeson  *
41bdd6c0eSSue Gleeson  * The contents of this file are subject to the terms of the
51bdd6c0eSSue Gleeson  * Common Development and Distribution License (the "License").
61bdd6c0eSSue Gleeson  * You may not use this file except in compliance with the License.
71bdd6c0eSSue Gleeson  *
81bdd6c0eSSue Gleeson  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91bdd6c0eSSue Gleeson  * or http://www.opensolaris.org/os/licensing.
101bdd6c0eSSue Gleeson  * See the License for the specific language governing permissions
111bdd6c0eSSue Gleeson  * and limitations under the License.
121bdd6c0eSSue Gleeson  *
131bdd6c0eSSue Gleeson  * When distributing Covered Code, include this CDDL HEADER in each
141bdd6c0eSSue Gleeson  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151bdd6c0eSSue Gleeson  * If applicable, add the following below this CDDL HEADER, with the
161bdd6c0eSSue Gleeson  * fields enclosed by brackets "[]" replaced with your own identifying
171bdd6c0eSSue Gleeson  * information: Portions Copyright [yyyy] [name of copyright owner]
181bdd6c0eSSue Gleeson  *
191bdd6c0eSSue Gleeson  * CDDL HEADER END
201bdd6c0eSSue Gleeson  */
211bdd6c0eSSue Gleeson /*
221bdd6c0eSSue Gleeson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
231bdd6c0eSSue Gleeson  * Use is subject to license terms.
241bdd6c0eSSue Gleeson  */
251bdd6c0eSSue Gleeson 
261bdd6c0eSSue Gleeson #include <stdio.h>
271bdd6c0eSSue Gleeson #include <libintl.h>
281bdd6c0eSSue Gleeson #include <sys/types.h>
291bdd6c0eSSue Gleeson #include <sys/stat.h>
301bdd6c0eSSue Gleeson #include <fcntl.h>
311bdd6c0eSSue Gleeson #include <locale.h>
321bdd6c0eSSue Gleeson #include <errno.h>
331bdd6c0eSSue Gleeson #include <unistd.h>
341bdd6c0eSSue Gleeson #include <strings.h>
35*168665f7SSue Gleeson #include <libscf.h>	/* for SMF error exit codes */
361bdd6c0eSSue Gleeson 
371bdd6c0eSSue Gleeson #include <srpt_ioctl.h>
381bdd6c0eSSue Gleeson 
391bdd6c0eSSue Gleeson 
401bdd6c0eSSue Gleeson /* Globals */
411bdd6c0eSSue Gleeson char	cmdName[] = "svc-srpt";
421bdd6c0eSSue Gleeson 
431bdd6c0eSSue Gleeson int
441bdd6c0eSSue Gleeson main(int argc, char *argv[])
451bdd6c0eSSue Gleeson {
461bdd6c0eSSue Gleeson 	int		ret;
471bdd6c0eSSue Gleeson 	boolean_t	do_enable = B_FALSE;
481bdd6c0eSSue Gleeson 	int		srpt_ioctl;
491bdd6c0eSSue Gleeson 	int		fd = -1;
501bdd6c0eSSue Gleeson 	int		saverr;
511bdd6c0eSSue Gleeson 	char		*txt;
52*168665f7SSue Gleeson 	struct stat	statbuf;
531bdd6c0eSSue Gleeson 
541bdd6c0eSSue Gleeson 	(void) setlocale(LC_ALL, "");
551bdd6c0eSSue Gleeson 
561bdd6c0eSSue Gleeson 	if (argc < 2 || argc > 2) {
571bdd6c0eSSue Gleeson 		goto usage;
581bdd6c0eSSue Gleeson 	}
591bdd6c0eSSue Gleeson 
601bdd6c0eSSue Gleeson 	if (strcasecmp(argv[1], "start") == 0) {
611bdd6c0eSSue Gleeson 		do_enable = B_TRUE;
621bdd6c0eSSue Gleeson 		srpt_ioctl = SRPT_IOC_ENABLE_SVC;
631bdd6c0eSSue Gleeson 	} else if (strcasecmp(argv[1], "stop") == 0)  {
641bdd6c0eSSue Gleeson 		srpt_ioctl = SRPT_IOC_DISABLE_SVC;
651bdd6c0eSSue Gleeson 	} else {
661bdd6c0eSSue Gleeson 		goto usage;
671bdd6c0eSSue Gleeson 	}
681bdd6c0eSSue Gleeson 
691bdd6c0eSSue Gleeson 	fd = open(SRPT_NODE, O_RDONLY);
701bdd6c0eSSue Gleeson 	if (fd < 0) {
711bdd6c0eSSue Gleeson 		saverr = errno;
721bdd6c0eSSue Gleeson 
73*168665f7SSue Gleeson 		(void) fprintf(stderr, "%s: %s (%s): %s\n", cmdName,
74*168665f7SSue Gleeson 		    gettext("Could not open SRP Target pseudodevice"),
75*168665f7SSue Gleeson 		    SRPT_NODE, strerror(saverr));
761bdd6c0eSSue Gleeson 
771bdd6c0eSSue Gleeson 		if (saverr == ENOENT) {
78*168665f7SSue Gleeson 			/* avoid having the service go into maintenance */
79*168665f7SSue Gleeson 			if ((stat("/devices/ib", &statbuf)) != 0) {
80*168665f7SSue Gleeson 				(void) fprintf(stderr, "%s: %s\n", cmdName,
81*168665f7SSue Gleeson 				    gettext(
82*168665f7SSue Gleeson 				    "No InfiniBand devices on this system."));
831bdd6c0eSSue Gleeson 			}
84*168665f7SSue Gleeson 			ret = 0;
85*168665f7SSue Gleeson 
86*168665f7SSue Gleeson 		} else {
87*168665f7SSue Gleeson 			ret = SMF_EXIT_ERR_FATAL; /* avoid retries */
88*168665f7SSue Gleeson 		}
89*168665f7SSue Gleeson 
90*168665f7SSue Gleeson 		(void) fprintf(stderr, "%s: %s\n", cmdName,
91*168665f7SSue Gleeson 		    gettext("SRPT Driver may not be loaded."));
92*168665f7SSue Gleeson 
93*168665f7SSue Gleeson 		return (ret);
941bdd6c0eSSue Gleeson 	}
951bdd6c0eSSue Gleeson 
961bdd6c0eSSue Gleeson 	ret = ioctl(fd, srpt_ioctl, NULL);
971bdd6c0eSSue Gleeson 	if (ret != 0) {
981bdd6c0eSSue Gleeson 		if (do_enable) {
991bdd6c0eSSue Gleeson 			txt = gettext("Could not enable SRP Target");
1001bdd6c0eSSue Gleeson 		} else {
1011bdd6c0eSSue Gleeson 			txt = gettext("Could not disable SRP Target");
1021bdd6c0eSSue Gleeson 		}
1031bdd6c0eSSue Gleeson 
1041bdd6c0eSSue Gleeson 		(void) fprintf(stderr, "%s: %d", txt, ret);
105*168665f7SSue Gleeson 		ret = SMF_EXIT_ERR_FATAL;
1061bdd6c0eSSue Gleeson 	}
1071bdd6c0eSSue Gleeson 
1081bdd6c0eSSue Gleeson 	(void) close(fd);
1091bdd6c0eSSue Gleeson 
1101bdd6c0eSSue Gleeson 	return (ret);
1111bdd6c0eSSue Gleeson 
1121bdd6c0eSSue Gleeson usage:
1131bdd6c0eSSue Gleeson 	(void) fprintf(stderr, gettext("Usage: %s start|stop"), cmdName);
1141bdd6c0eSSue Gleeson 	(void) fprintf(stderr, "\n");
1151bdd6c0eSSue Gleeson 
1161bdd6c0eSSue Gleeson 	return (1);
1171bdd6c0eSSue Gleeson }
118