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