xref: /illumos-gate/usr/src/cmd/ttymon/ttyadm.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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*3bb2c156SToomas Soome /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <stdarg.h>
377c478bd9Sstevel@tonic-gate #include "tmstruct.h"
38*3bb2c156SToomas Soome #include "tmextern.h"
397c478bd9Sstevel@tonic-gate #include "ttymon.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
42*3bb2c156SToomas Soome  *	ttyadm	- format ttymon specific information and
437c478bd9Sstevel@tonic-gate  *		  print it to stdout
447c478bd9Sstevel@tonic-gate  *
45*3bb2c156SToomas Soome  *	Usage:	ttyadm [options] -d device -s service -l ttylabel
467c478bd9Sstevel@tonic-gate  *		ttyadm -V
47*3bb2c156SToomas Soome  *
487c478bd9Sstevel@tonic-gate  *		valid options are:
497c478bd9Sstevel@tonic-gate  *		-c
507c478bd9Sstevel@tonic-gate  *		-h
517c478bd9Sstevel@tonic-gate  *		-b
527c478bd9Sstevel@tonic-gate  *		-I
537c478bd9Sstevel@tonic-gate  *		-S y|n
547c478bd9Sstevel@tonic-gate  *		-T term
557c478bd9Sstevel@tonic-gate  *		-r count
567c478bd9Sstevel@tonic-gate  *		-t timeout
577c478bd9Sstevel@tonic-gate  *		-p prompt
587c478bd9Sstevel@tonic-gate  *		-m modules
597c478bd9Sstevel@tonic-gate  *		-i msg
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
62*3bb2c156SToomas Soome static void usage(void);
63*3bb2c156SToomas Soome static	int	check_label(char *);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])667c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	int c;			/* option letter */
697c478bd9Sstevel@tonic-gate 	int errflg = 0;		/* error indicator */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	struct pmtab *ptr;
727c478bd9Sstevel@tonic-gate 	char *timeout = "";
737c478bd9Sstevel@tonic-gate 	char *count = "";
747c478bd9Sstevel@tonic-gate 	char prompt[BUFSIZ];
757c478bd9Sstevel@tonic-gate 	char dmsg[BUFSIZ];
767c478bd9Sstevel@tonic-gate 	char ttyflags[BUFSIZ], *tf;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	int  dflag = 0;		/* -d seen */
797c478bd9Sstevel@tonic-gate 	int  sflag = 0;		/* -s seen */
807c478bd9Sstevel@tonic-gate 	int  lflag = 0;		/* -l seen */
817c478bd9Sstevel@tonic-gate 	int  mflag = 0;		/* -m seen */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (argc == 1)
847c478bd9Sstevel@tonic-gate 		usage();
85*3bb2c156SToomas Soome 	if ((ptr = ALLOC_PMTAB) == NULL) {
86*3bb2c156SToomas Soome 		(void) fprintf(stderr, "calloc failed\n");
877c478bd9Sstevel@tonic-gate 		exit(1);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	ptr->p_modules = "";
907c478bd9Sstevel@tonic-gate 	ptr->p_dmsg = "";
917c478bd9Sstevel@tonic-gate 	ptr->p_termtype = "";
927c478bd9Sstevel@tonic-gate 	ptr->p_softcar = "";
937c478bd9Sstevel@tonic-gate 	ptr->p_prompt = "login\\: ";
947c478bd9Sstevel@tonic-gate 	ttyflags[0] = '\0';
957c478bd9Sstevel@tonic-gate 	tf = ttyflags;
967c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "IT:S:Vd:s:chbr:t:l:m:p:i:")) != -1) {
977c478bd9Sstevel@tonic-gate 		switch (c) {
987c478bd9Sstevel@tonic-gate 		case 'V':
997c478bd9Sstevel@tonic-gate 			if ((argc > 2) || (optind < argc))
1007c478bd9Sstevel@tonic-gate 				usage();
101*3bb2c156SToomas Soome 			(void) fprintf(stdout, "%d\n", PMTAB_VERS);
1027c478bd9Sstevel@tonic-gate 			exit(0);
1037c478bd9Sstevel@tonic-gate 			break;	/*NOTREACHED*/
1047c478bd9Sstevel@tonic-gate 		case 'd':
1057c478bd9Sstevel@tonic-gate 			ptr->p_device = optarg;
1067c478bd9Sstevel@tonic-gate 			dflag = 1;
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 		case 'c':
109*3bb2c156SToomas Soome 			tf = strcat(tf, "c");
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		case 'h':
112*3bb2c156SToomas Soome 			tf = strcat(tf, "h");
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		case 'b':
115*3bb2c156SToomas Soome 			tf = strcat(tf, "b");
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 		case 'I':
118*3bb2c156SToomas Soome 			tf = strcat(tf, "I");
1197c478bd9Sstevel@tonic-gate 			break;
1207c478bd9Sstevel@tonic-gate 		case 'r':
121*3bb2c156SToomas Soome 			tf = strcat(tf, "r");
1227c478bd9Sstevel@tonic-gate 			count = optarg;
123*3bb2c156SToomas Soome 			if (strcheck(optarg, NUM) != 0) {
124*3bb2c156SToomas Soome 				(void) fprintf(stderr,
1257c478bd9Sstevel@tonic-gate 		"Invalid argument for \"-r\" -- positive number expected.\n");
1267c478bd9Sstevel@tonic-gate 				usage();
1277c478bd9Sstevel@tonic-gate 			}
1287c478bd9Sstevel@tonic-gate 			break;
1297c478bd9Sstevel@tonic-gate 		case 'T':
1307c478bd9Sstevel@tonic-gate 			ptr->p_termtype = optarg;
1317c478bd9Sstevel@tonic-gate 			break;
1327c478bd9Sstevel@tonic-gate 		case 'S':
1337c478bd9Sstevel@tonic-gate 			switch (*optarg) {
1347c478bd9Sstevel@tonic-gate 			case 'Y':
1357c478bd9Sstevel@tonic-gate 			case 'y':
1367c478bd9Sstevel@tonic-gate 				ptr->p_softcar = "y";
1377c478bd9Sstevel@tonic-gate 				break;
1387c478bd9Sstevel@tonic-gate 			case 'N':
1397c478bd9Sstevel@tonic-gate 			case 'n':
1407c478bd9Sstevel@tonic-gate 				ptr->p_softcar = "n";
1417c478bd9Sstevel@tonic-gate 				break;
1427c478bd9Sstevel@tonic-gate 			default:
1437c478bd9Sstevel@tonic-gate 				usage();
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		case 's':
1477c478bd9Sstevel@tonic-gate 			ptr->p_server = optarg;
1487c478bd9Sstevel@tonic-gate 			sflag = 1;
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 		case 't':
1517c478bd9Sstevel@tonic-gate 			timeout = optarg;
152*3bb2c156SToomas Soome 			if (strcheck(optarg, NUM) != 0) {
153*3bb2c156SToomas Soome 				(void) fprintf(stderr,
1547c478bd9Sstevel@tonic-gate 		"Invalid argument for \"-t\" -- positive number expected.\n");
1557c478bd9Sstevel@tonic-gate 				usage();
1567c478bd9Sstevel@tonic-gate 			}
1577c478bd9Sstevel@tonic-gate 			break;
1587c478bd9Sstevel@tonic-gate 		case 'l':
1597c478bd9Sstevel@tonic-gate 			ptr->p_ttylabel = optarg;
1607c478bd9Sstevel@tonic-gate 			lflag = 1;
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		case 'm':
1637c478bd9Sstevel@tonic-gate 			ptr->p_modules = optarg;
1647c478bd9Sstevel@tonic-gate 			mflag = 1;
1657c478bd9Sstevel@tonic-gate 			break;
1667c478bd9Sstevel@tonic-gate 		case 'p':
1677c478bd9Sstevel@tonic-gate 			ptr->p_prompt = prompt;
168*3bb2c156SToomas Soome 			copystr(ptr->p_prompt, optarg);
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 		case 'i':
1717c478bd9Sstevel@tonic-gate 			ptr->p_dmsg = dmsg;
172*3bb2c156SToomas Soome 			copystr(ptr->p_dmsg, optarg);
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		case '?':
1757c478bd9Sstevel@tonic-gate 			usage();
1767c478bd9Sstevel@tonic-gate 			break;
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	if (optind < argc)
1807c478bd9Sstevel@tonic-gate 		usage();
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if ((!dflag) || (!sflag) || (!lflag))
1837c478bd9Sstevel@tonic-gate 		usage();
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (check_device(ptr->p_device) != 0)
1867c478bd9Sstevel@tonic-gate 		errflg++;
1877c478bd9Sstevel@tonic-gate 	if (check_cmd(ptr->p_server) != 0)
1887c478bd9Sstevel@tonic-gate 		errflg++;
1897c478bd9Sstevel@tonic-gate 	if (check_label(ptr->p_ttylabel) != 0)
1907c478bd9Sstevel@tonic-gate 		errflg++;
1917c478bd9Sstevel@tonic-gate 	if (mflag && (vml(ptr->p_modules) != 0))
1927c478bd9Sstevel@tonic-gate 		errflg++;
1937c478bd9Sstevel@tonic-gate 	if (errflg)
1947c478bd9Sstevel@tonic-gate 		exit(1);
195*3bb2c156SToomas Soome 	(void) fprintf(stdout, "%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:",
196*3bb2c156SToomas Soome 	    ptr->p_device, ttyflags, count, ptr->p_server,
197*3bb2c156SToomas Soome 	    timeout, ptr->p_ttylabel, ptr->p_modules,
198*3bb2c156SToomas Soome 	    ptr->p_prompt, ptr->p_dmsg, ptr->p_termtype,
199*3bb2c156SToomas Soome 	    ptr->p_softcar);
2007c478bd9Sstevel@tonic-gate 	return (0);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * usage - print out a usage message
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate static void
usage(void)208*3bb2c156SToomas Soome usage(void)
2097c478bd9Sstevel@tonic-gate {
210*3bb2c156SToomas Soome 	(void) fprintf(stderr,
211*3bb2c156SToomas Soome 	    "Usage:\tttyadm [ options ] -d device -s service -l ttylabel\n");
212*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\tttyadm -V\n");
213*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\n\tValid options are:\n");
214*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-c\n");
215*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-h\n");
216*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-b\n");
217*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-I\n");
218*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-S y|n\n");
219*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-T term\n");
220*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-r count\n");
221*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-t timeout\n");
222*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-p prompt\n");
223*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-m modules\n");
224*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\t-i msg\n");
2257c478bd9Sstevel@tonic-gate 	exit(1);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  *	check_label	- if ttylabel exists in /etc/ttydefs, return 0
2307c478bd9Sstevel@tonic-gate  *			- otherwise, return -1
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static int
check_label(char * ttylabel)234*3bb2c156SToomas Soome check_label(char *ttylabel)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	FILE *fp;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if ((ttylabel == NULL) || (*ttylabel == '\0')) {
239*3bb2c156SToomas Soome 		(void) fprintf(stderr, "error -- ttylabel is missing");
240*3bb2c156SToomas Soome 		return (-1);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	if ((fp = fopen(TTYDEFS, "r")) == NULL) {
243*3bb2c156SToomas Soome 		(void) fprintf(stderr, "error -- \"%s\" does not exist, "
244*3bb2c156SToomas Soome 		    "can't verify ttylabel <%s>\n", TTYDEFS, ttylabel);
245*3bb2c156SToomas Soome 		return (-1);
246*3bb2c156SToomas Soome 	}
247*3bb2c156SToomas Soome 	if (find_label(fp, ttylabel)) {
248*3bb2c156SToomas Soome 		(void) fclose(fp);
249*3bb2c156SToomas Soome 		return (0);
2507c478bd9Sstevel@tonic-gate 	}
251*3bb2c156SToomas Soome 	(void) fclose(fp);
252*3bb2c156SToomas Soome 	(void) fprintf(stderr, "error -- can't find ttylabel <%s> in \"%s\"\n",
253*3bb2c156SToomas Soome 	    ttylabel, TTYDEFS);
254*3bb2c156SToomas Soome 	return (-1);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  *	log	- print a message to stderr
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate void
log(const char * msg,...)2617c478bd9Sstevel@tonic-gate log(const char *msg, ...)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	va_list ap;
2647c478bd9Sstevel@tonic-gate 	va_start(ap, msg);
2657c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, msg, ap);
2667c478bd9Sstevel@tonic-gate 	va_end(ap);
2677c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
2687c478bd9Sstevel@tonic-gate }
269