xref: /illumos-gate/usr/src/cmd/ttymon/tmutil.c (revision 3bb2c156)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*3bb2c156SToomas Soome /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <fcntl.h>
407c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
417c478bd9Sstevel@tonic-gate #include <sys/sad.h>
427c478bd9Sstevel@tonic-gate #include "ttymon.h"
437c478bd9Sstevel@tonic-gate #include "tmstruct.h"
447c478bd9Sstevel@tonic-gate #include "tmextern.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	NSTRPUSH	9	/* should agree with the tunable in	*/
47*3bb2c156SToomas Soome 				/* /etc/master.d/kernel			*/
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  *	check_device - check to see if the device exists,
517c478bd9Sstevel@tonic-gate  *		     - and if it is a character device
527c478bd9Sstevel@tonic-gate  *		     - return 0 if everything is ok. Otherwise, return -1
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate int
check_device(char * device)557c478bd9Sstevel@tonic-gate check_device(char *device)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	struct stat statbuf;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if ((device == NULL) || (*device == '\0')) {
607c478bd9Sstevel@tonic-gate 		log("error -- device field is missing");
61*3bb2c156SToomas Soome 		return (-1);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 	if (*device != '/') {
647c478bd9Sstevel@tonic-gate 		log("error -- must specify full path name for \"%s\".", device);
65*3bb2c156SToomas Soome 		return (-1);
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	if (access(device, 0) == 0) {
68*3bb2c156SToomas Soome 		if (stat(device, &statbuf) < 0) {
697c478bd9Sstevel@tonic-gate 			log("stat(%s) failed: %s", device, strerror(errno));
70*3bb2c156SToomas Soome 			return (-1);
717c478bd9Sstevel@tonic-gate 		}
727c478bd9Sstevel@tonic-gate 		if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
737c478bd9Sstevel@tonic-gate 			log("error -- \"%s\" not character special device",
747c478bd9Sstevel@tonic-gate 			    device);
75*3bb2c156SToomas Soome 			return (-1);
767c478bd9Sstevel@tonic-gate 		}
77*3bb2c156SToomas Soome 	} else {
787c478bd9Sstevel@tonic-gate 		log("error -- device \"%s\" does not exist", device);
79*3bb2c156SToomas Soome 		return (-1);
807c478bd9Sstevel@tonic-gate 	}
81*3bb2c156SToomas Soome 	return (0);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  *	check_cmd - check to see if the cmd file exists,
867c478bd9Sstevel@tonic-gate  *		  - and if it is executable
877c478bd9Sstevel@tonic-gate  *		  - return 0 if everything is ok. Otherwise, return -1
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate int
check_cmd(char * cmd)907c478bd9Sstevel@tonic-gate check_cmd(char *cmd)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	struct stat statbuf;
937c478bd9Sstevel@tonic-gate 	char	tbuf[BUFSIZ];
947c478bd9Sstevel@tonic-gate 	char	*tp = tbuf;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if ((cmd == NULL) || (*cmd == '\0')) {
977c478bd9Sstevel@tonic-gate 		log("error -- server command is missing");
98*3bb2c156SToomas Soome 		return (-1);
997c478bd9Sstevel@tonic-gate 	}
100*3bb2c156SToomas Soome 	(void) strcpy(tp, cmd);
101*3bb2c156SToomas Soome 	(void) strtok(tp, " \t");
1027c478bd9Sstevel@tonic-gate 	if (*tp != '/') {
1037c478bd9Sstevel@tonic-gate 		log("error -- must specify full path name for \"%s\".", tp);
104*3bb2c156SToomas Soome 		return (-1);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	if (access(tp, 0) == 0) {
107*3bb2c156SToomas Soome 		if (stat(tp, &statbuf) < 0) {
1087c478bd9Sstevel@tonic-gate 			log("stat(%s) failed.", tp);
109*3bb2c156SToomas Soome 			return (-1);
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 		if (!(statbuf.st_mode & 0111)) {
1127c478bd9Sstevel@tonic-gate 			log("error -- \"%s\" not executable\n", tp);
113*3bb2c156SToomas Soome 			return (-1);
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 		if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
1167c478bd9Sstevel@tonic-gate 			log("error -- \"%s\" not a regular file", tp);
117*3bb2c156SToomas Soome 			return (-1);
1187c478bd9Sstevel@tonic-gate 		}
119*3bb2c156SToomas Soome 	} else {
1207c478bd9Sstevel@tonic-gate 		log("error -- \"%s\" does not exist", tp);
121*3bb2c156SToomas Soome 		return (-1);
1227c478bd9Sstevel@tonic-gate 	}
123*3bb2c156SToomas Soome 	return (0);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * strcheck(sp, flag)	- check string
1287c478bd9Sstevel@tonic-gate  *				- if flag == ALNUM, all char. are expected to
1297c478bd9Sstevel@tonic-gate  *				  be alphanumeric
1307c478bd9Sstevel@tonic-gate  *				- if flag == NUM, all char. are expected to
1317c478bd9Sstevel@tonic-gate  *				  be digits and the number must be >= 0
1327c478bd9Sstevel@tonic-gate  *				- return 0 if successful, -1 if failed.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate int
strcheck(char * sp,int flag)135*3bb2c156SToomas Soome strcheck(char *sp, int flag)
1367c478bd9Sstevel@tonic-gate {
137*3bb2c156SToomas Soome 	char	*cp;
138*3bb2c156SToomas Soome 
1397c478bd9Sstevel@tonic-gate 	if (flag == NUM) {
1407c478bd9Sstevel@tonic-gate 		for (cp = sp; *cp; cp++) {
1417c478bd9Sstevel@tonic-gate 			if (!isdigit(*cp)) {
142*3bb2c156SToomas Soome 				return (-1);
1437c478bd9Sstevel@tonic-gate 			}
1447c478bd9Sstevel@tonic-gate 		}
145*3bb2c156SToomas Soome 	} else {	/* (flag == ALNUM) */
1467c478bd9Sstevel@tonic-gate 		for (cp = sp; *cp; cp++) {
1477c478bd9Sstevel@tonic-gate 			if (!isalnum(*cp)) {
148*3bb2c156SToomas Soome 				return (-1);
1497c478bd9Sstevel@tonic-gate 			}
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 	}
152*3bb2c156SToomas Soome 	return (0);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * vml(modules)	- validate a list of modules
1577c478bd9Sstevel@tonic-gate  *		- return 0 if successful, -1 if failed
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate int
vml(char * modules)160*3bb2c156SToomas Soome vml(char *modules)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	char	*modp, *svmodp;
1637c478bd9Sstevel@tonic-gate 	int	i, fd;
1647c478bd9Sstevel@tonic-gate 	struct str_mlist newmods[NSTRPUSH];	/* modlist for newlist	*/
1657c478bd9Sstevel@tonic-gate 	struct str_list	newlist;		/* modules to be pushed	*/
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if ((modules == NULL) || (*modules == '\0'))
168*3bb2c156SToomas Soome 		return (0);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	newlist.sl_modlist = newmods;
1717c478bd9Sstevel@tonic-gate 	newlist.sl_nmods = NSTRPUSH;
1727c478bd9Sstevel@tonic-gate 	if ((modp = malloc(strlen(modules) + 1)) == NULL) {
1737c478bd9Sstevel@tonic-gate 		log("vml: malloc failed");
1747c478bd9Sstevel@tonic-gate 		return (-1);
1757c478bd9Sstevel@tonic-gate 	};
1767c478bd9Sstevel@tonic-gate 	svmodp = modp;
177*3bb2c156SToomas Soome 	(void) strcpy(modp, modules);
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * pull mod names out of comma-separated list
1807c478bd9Sstevel@tonic-gate 	 */
181*3bb2c156SToomas Soome 	for (i = 0, modp = strtok(modp, ",");
182*3bb2c156SToomas Soome 	    modp != NULL; i++, modp = strtok(NULL, ",")) {
183*3bb2c156SToomas Soome 		if (i >= NSTRPUSH) {
1847c478bd9Sstevel@tonic-gate 			log("too many modules in <%s>", modules);
1857c478bd9Sstevel@tonic-gate 			i = -1;
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 		}
188*3bb2c156SToomas Soome 		(void) strncpy(newlist.sl_modlist[i].l_name, modp, FMNAMESZ);
1897c478bd9Sstevel@tonic-gate 		newlist.sl_modlist[i].l_name[FMNAMESZ] = '\0';
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	free(svmodp);
1927c478bd9Sstevel@tonic-gate 	if (i == -1)
1937c478bd9Sstevel@tonic-gate 		return (-1);
1947c478bd9Sstevel@tonic-gate 	newlist.sl_nmods = i;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Is it a valid list of modules?
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	if ((fd = open(USERDEV, O_RDWR)) == -1) {
2007c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
201*3bb2c156SToomas Soome 			log("Warning - can't validate module list, "
202*3bb2c156SToomas Soome 			    "/dev/sad/user busy");
203*3bb2c156SToomas Soome 			return (0);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		log("open /dev/sad/user failed: %s", strerror(errno));
206*3bb2c156SToomas Soome 		return (-1);
2077c478bd9Sstevel@tonic-gate 	}
208*3bb2c156SToomas Soome 	if ((i = ioctl(fd, SAD_VML, &newlist)) < 0) {
2097c478bd9Sstevel@tonic-gate 		log("Validate modules ioctl failed, modules = <%s>: %s",
2107c478bd9Sstevel@tonic-gate 		    modules, strerror(errno));
211*3bb2c156SToomas Soome 		(void) close(fd);
212*3bb2c156SToomas Soome 		return (-1);
2137c478bd9Sstevel@tonic-gate 	}
214*3bb2c156SToomas Soome 	if (i != 0) {
2157c478bd9Sstevel@tonic-gate 		log("Error - invalid STREAMS module list <%s>.", modules);
216*3bb2c156SToomas Soome 		(void) close(fd);
217*3bb2c156SToomas Soome 		return (-1);
2187c478bd9Sstevel@tonic-gate 	}
219*3bb2c156SToomas Soome 	(void) close(fd);
220*3bb2c156SToomas Soome 	return (0);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * copystr(s1, s2) - copy string s2 to string s1
2257c478bd9Sstevel@tonic-gate  *		   - also put '\' in front of ':'
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate void
copystr(char * s1,char * s2)228*3bb2c156SToomas Soome copystr(char *s1, char *s2)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	while (*s2) {
2317c478bd9Sstevel@tonic-gate 		if (*s2 == ':') {
2327c478bd9Sstevel@tonic-gate 			*s1++ = '\\';
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		*s1++ = *s2++;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	*s1 = '\0';
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
2407c478bd9Sstevel@tonic-gate void
cons_printf(const char * fmt,...)2417c478bd9Sstevel@tonic-gate cons_printf(const char *fmt, ...)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN * 2]; /* enough space for msg including a path */
2447c478bd9Sstevel@tonic-gate 	int fd;
2457c478bd9Sstevel@tonic-gate 	va_list ap;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2487c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
2497c478bd9Sstevel@tonic-gate 	va_end(ap);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if ((fd = open(CONSOLE, O_WRONLY|O_NOCTTY)) != -1)
2527c478bd9Sstevel@tonic-gate 		(void) write(fd, buf, strlen(buf) + 1);
2537c478bd9Sstevel@tonic-gate 	(void) close(fd);
2547c478bd9Sstevel@tonic-gate }
255