xref: /titanic_51/usr/src/cmd/ttymon/tmutil.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <unistd.h>
34*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <ctype.h>
40*7c478bd9Sstevel@tonic-gate #include <string.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
42*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/sad.h>
45*7c478bd9Sstevel@tonic-gate #include "ttymon.h"
46*7c478bd9Sstevel@tonic-gate #include "tmstruct.h"
47*7c478bd9Sstevel@tonic-gate #include "tmextern.h"
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #define	NSTRPUSH	9	/* should agree with the tunable in	*/
50*7c478bd9Sstevel@tonic-gate 				/* 		/etc/master.d/kernel	*/
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  *	check_device - check to see if the device exists,
54*7c478bd9Sstevel@tonic-gate  *		     - and if it is a character device
55*7c478bd9Sstevel@tonic-gate  *		     - return 0 if everything is ok. Otherwise, return -1
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate int
58*7c478bd9Sstevel@tonic-gate check_device(char *device)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	struct stat statbuf;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	if ((device == NULL) || (*device == '\0')) {
63*7c478bd9Sstevel@tonic-gate 		log("error -- device field is missing");
64*7c478bd9Sstevel@tonic-gate 		return(-1);
65*7c478bd9Sstevel@tonic-gate 	}
66*7c478bd9Sstevel@tonic-gate 	if (*device != '/') {
67*7c478bd9Sstevel@tonic-gate 		log("error -- must specify full path name for \"%s\".", device);
68*7c478bd9Sstevel@tonic-gate 		return(-1);
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 	if (access(device, 0) == 0) {
71*7c478bd9Sstevel@tonic-gate 		if (stat(device,&statbuf) < 0) {
72*7c478bd9Sstevel@tonic-gate 			log("stat(%s) failed: %s", device, strerror(errno));
73*7c478bd9Sstevel@tonic-gate 			return(-1);
74*7c478bd9Sstevel@tonic-gate 		}
75*7c478bd9Sstevel@tonic-gate 		if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
76*7c478bd9Sstevel@tonic-gate 			log("error -- \"%s\" not character special device",
77*7c478bd9Sstevel@tonic-gate 			    device);
78*7c478bd9Sstevel@tonic-gate 			return(-1);
79*7c478bd9Sstevel@tonic-gate 		}
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 	else {
82*7c478bd9Sstevel@tonic-gate 		log("error -- device \"%s\" does not exist", device);
83*7c478bd9Sstevel@tonic-gate 		return(-1);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	return(0);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  *	check_cmd - check to see if the cmd file exists,
90*7c478bd9Sstevel@tonic-gate  *		  - and if it is executable
91*7c478bd9Sstevel@tonic-gate  *		  - return 0 if everything is ok. Otherwise, return -1
92*7c478bd9Sstevel@tonic-gate  */
93*7c478bd9Sstevel@tonic-gate int
94*7c478bd9Sstevel@tonic-gate check_cmd(char *cmd)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	struct stat statbuf;
97*7c478bd9Sstevel@tonic-gate 	char	tbuf[BUFSIZ];
98*7c478bd9Sstevel@tonic-gate 	char	*tp = tbuf;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if ((cmd == NULL) || (*cmd == '\0')) {
101*7c478bd9Sstevel@tonic-gate 		log("error -- server command is missing");
102*7c478bd9Sstevel@tonic-gate 		return(-1);
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 	(void)strcpy(tp,cmd);
105*7c478bd9Sstevel@tonic-gate 	(void)strtok(tp, " \t");
106*7c478bd9Sstevel@tonic-gate 	if (*tp != '/') {
107*7c478bd9Sstevel@tonic-gate 		log("error -- must specify full path name for \"%s\".", tp);
108*7c478bd9Sstevel@tonic-gate 		return(-1);
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 	if (access(tp, 0) == 0) {
111*7c478bd9Sstevel@tonic-gate 		if (stat(tp,&statbuf) < 0) {
112*7c478bd9Sstevel@tonic-gate 			log("stat(%s) failed.", tp);
113*7c478bd9Sstevel@tonic-gate 			return(-1);
114*7c478bd9Sstevel@tonic-gate 		}
115*7c478bd9Sstevel@tonic-gate 		if (!(statbuf.st_mode & 0111)) {
116*7c478bd9Sstevel@tonic-gate 			log("error -- \"%s\" not executable\n", tp);
117*7c478bd9Sstevel@tonic-gate 			return(-1);
118*7c478bd9Sstevel@tonic-gate 		}
119*7c478bd9Sstevel@tonic-gate 		if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
120*7c478bd9Sstevel@tonic-gate 			log("error -- \"%s\" not a regular file", tp);
121*7c478bd9Sstevel@tonic-gate 			return(-1);
122*7c478bd9Sstevel@tonic-gate 		}
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 	else {
125*7c478bd9Sstevel@tonic-gate 		log("error -- \"%s\" does not exist", tp);
126*7c478bd9Sstevel@tonic-gate 		return(-1);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 	return(0);
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate /*
132*7c478bd9Sstevel@tonic-gate  * strcheck(sp, flag)	- check string
133*7c478bd9Sstevel@tonic-gate  *				- if flag == ALNUM, all char. are expected to
134*7c478bd9Sstevel@tonic-gate  *				  be alphanumeric
135*7c478bd9Sstevel@tonic-gate  *				- if flag == NUM, all char. are expected to
136*7c478bd9Sstevel@tonic-gate  *				  be digits and the number must be >= 0
137*7c478bd9Sstevel@tonic-gate  *				- return 0 if successful, -1 if failed.
138*7c478bd9Sstevel@tonic-gate  */
139*7c478bd9Sstevel@tonic-gate int
140*7c478bd9Sstevel@tonic-gate strcheck(sp, flag)
141*7c478bd9Sstevel@tonic-gate char	*sp;		/* string ptr		*/
142*7c478bd9Sstevel@tonic-gate int	flag;		/* either NUM or ALNUM	*/
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	register	char	*cp;
145*7c478bd9Sstevel@tonic-gate 	if (flag == NUM) {
146*7c478bd9Sstevel@tonic-gate 		for (cp = sp; *cp; cp++) {
147*7c478bd9Sstevel@tonic-gate 			if (!isdigit(*cp)) {
148*7c478bd9Sstevel@tonic-gate 				return(-1);
149*7c478bd9Sstevel@tonic-gate 			}
150*7c478bd9Sstevel@tonic-gate 		}
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 	else {	/* (flag == ALNUM) */
153*7c478bd9Sstevel@tonic-gate 		for (cp = sp; *cp; cp++) {
154*7c478bd9Sstevel@tonic-gate 			if (!isalnum(*cp)) {
155*7c478bd9Sstevel@tonic-gate 				return(-1);
156*7c478bd9Sstevel@tonic-gate 			}
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 	return(0);
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /*
163*7c478bd9Sstevel@tonic-gate  * vml(modules)	- validate a list of modules
164*7c478bd9Sstevel@tonic-gate  *		- return 0 if successful, -1 if failed
165*7c478bd9Sstevel@tonic-gate  */
166*7c478bd9Sstevel@tonic-gate int
167*7c478bd9Sstevel@tonic-gate vml(modules)
168*7c478bd9Sstevel@tonic-gate char	*modules;
169*7c478bd9Sstevel@tonic-gate {
170*7c478bd9Sstevel@tonic-gate 	char	*modp, *svmodp;
171*7c478bd9Sstevel@tonic-gate 	int	i, fd;
172*7c478bd9Sstevel@tonic-gate 	struct str_mlist newmods[NSTRPUSH];	/* modlist for newlist	*/
173*7c478bd9Sstevel@tonic-gate 	struct str_list	newlist;		/* modules to be pushed	*/
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	if ((modules == NULL) || (*modules == '\0'))
176*7c478bd9Sstevel@tonic-gate 		return(0);
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	newlist.sl_modlist = newmods;
179*7c478bd9Sstevel@tonic-gate 	newlist.sl_nmods = NSTRPUSH;
180*7c478bd9Sstevel@tonic-gate 	if ((modp = malloc(strlen(modules) + 1)) == NULL) {
181*7c478bd9Sstevel@tonic-gate 		log("vml: malloc failed");
182*7c478bd9Sstevel@tonic-gate 		return (-1);
183*7c478bd9Sstevel@tonic-gate 	};
184*7c478bd9Sstevel@tonic-gate 	svmodp = modp;
185*7c478bd9Sstevel@tonic-gate 	(void)strcpy(modp, modules);
186*7c478bd9Sstevel@tonic-gate 	/*
187*7c478bd9Sstevel@tonic-gate 	 * pull mod names out of comma-separated list
188*7c478bd9Sstevel@tonic-gate 	 */
189*7c478bd9Sstevel@tonic-gate 	for ( i = 0, modp = strtok(modp, ",");
190*7c478bd9Sstevel@tonic-gate 	modp != NULL; i++, modp = strtok(NULL, ",") ) {
191*7c478bd9Sstevel@tonic-gate 		if ( i >= NSTRPUSH) {
192*7c478bd9Sstevel@tonic-gate 			log("too many modules in <%s>", modules);
193*7c478bd9Sstevel@tonic-gate 			i = -1;
194*7c478bd9Sstevel@tonic-gate 			break;
195*7c478bd9Sstevel@tonic-gate 		}
196*7c478bd9Sstevel@tonic-gate 		(void)strncpy(newlist.sl_modlist[i].l_name,
197*7c478bd9Sstevel@tonic-gate 					modp, FMNAMESZ);
198*7c478bd9Sstevel@tonic-gate 		newlist.sl_modlist[i].l_name[FMNAMESZ] = '\0';
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 	free(svmodp);
201*7c478bd9Sstevel@tonic-gate 	if (i == -1)
202*7c478bd9Sstevel@tonic-gate 		return (-1);
203*7c478bd9Sstevel@tonic-gate 	newlist.sl_nmods = i;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	/*
206*7c478bd9Sstevel@tonic-gate 	 * Is it a valid list of modules?
207*7c478bd9Sstevel@tonic-gate 	 */
208*7c478bd9Sstevel@tonic-gate 	if ((fd = open(USERDEV, O_RDWR)) == -1) {
209*7c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
210*7c478bd9Sstevel@tonic-gate 			log("Warning - can't validate module list, /dev/sad/user busy");
211*7c478bd9Sstevel@tonic-gate 			return(0);
212*7c478bd9Sstevel@tonic-gate 		}
213*7c478bd9Sstevel@tonic-gate 		log("open /dev/sad/user failed: %s", strerror(errno));
214*7c478bd9Sstevel@tonic-gate 		return(-1);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	if ( (i = ioctl(fd, SAD_VML, &newlist)) < 0 ) {
217*7c478bd9Sstevel@tonic-gate 		log("Validate modules ioctl failed, modules = <%s>: %s",
218*7c478bd9Sstevel@tonic-gate 		    modules, strerror(errno));
219*7c478bd9Sstevel@tonic-gate 		(void)close(fd);
220*7c478bd9Sstevel@tonic-gate 		return(-1);
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate 	if ( i != 0 ) {
223*7c478bd9Sstevel@tonic-gate 		log("Error - invalid STREAMS module list <%s>.", modules);
224*7c478bd9Sstevel@tonic-gate 		(void)close(fd);
225*7c478bd9Sstevel@tonic-gate 		return(-1);
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 	(void)close(fd);
228*7c478bd9Sstevel@tonic-gate 	return(0);
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate /*
232*7c478bd9Sstevel@tonic-gate  * copystr(s1, s2) - copy string s2 to string s1
233*7c478bd9Sstevel@tonic-gate  *		   - also put '\' in front of ':'
234*7c478bd9Sstevel@tonic-gate  */
235*7c478bd9Sstevel@tonic-gate void
236*7c478bd9Sstevel@tonic-gate copystr(s1,s2)
237*7c478bd9Sstevel@tonic-gate char	*s1, *s2;
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 	while (*s2) {
240*7c478bd9Sstevel@tonic-gate 		if (*s2 == ':') {
241*7c478bd9Sstevel@tonic-gate 			*s1++ = '\\';
242*7c478bd9Sstevel@tonic-gate 		}
243*7c478bd9Sstevel@tonic-gate 		*s1++ = *s2++;
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 	*s1 = '\0';
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
249*7c478bd9Sstevel@tonic-gate void
250*7c478bd9Sstevel@tonic-gate cons_printf(const char *fmt, ...)
251*7c478bd9Sstevel@tonic-gate {
252*7c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN * 2]; /* enough space for msg including a path */
253*7c478bd9Sstevel@tonic-gate 	int fd;
254*7c478bd9Sstevel@tonic-gate 	va_list ap;
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
257*7c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
258*7c478bd9Sstevel@tonic-gate 	va_end(ap);
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	if ((fd = open(CONSOLE, O_WRONLY|O_NOCTTY)) != -1)
261*7c478bd9Sstevel@tonic-gate 		(void) write(fd, buf, strlen(buf) + 1);
262*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
263*7c478bd9Sstevel@tonic-gate }
264