xref: /titanic_51/usr/src/cmd/ttymon/ttyadm.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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <ctype.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
40*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
41*7c478bd9Sstevel@tonic-gate #include "tmstruct.h"
42*7c478bd9Sstevel@tonic-gate #include "ttymon.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  *	ttyadm 	- format ttymon specific information and
46*7c478bd9Sstevel@tonic-gate  *		  print it to stdout
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  *	Usage: 	ttyadm [options] -d device -s service -l ttylabel
49*7c478bd9Sstevel@tonic-gate  *		ttyadm -V
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  *		valid options are:
52*7c478bd9Sstevel@tonic-gate  *		-c
53*7c478bd9Sstevel@tonic-gate  *		-h
54*7c478bd9Sstevel@tonic-gate  *		-b
55*7c478bd9Sstevel@tonic-gate  *		-I
56*7c478bd9Sstevel@tonic-gate  *		-S y|n
57*7c478bd9Sstevel@tonic-gate  *		-T term
58*7c478bd9Sstevel@tonic-gate  *		-r count
59*7c478bd9Sstevel@tonic-gate  *		-t timeout
60*7c478bd9Sstevel@tonic-gate  *		-p prompt
61*7c478bd9Sstevel@tonic-gate  *		-m modules
62*7c478bd9Sstevel@tonic-gate  *		-i msg
63*7c478bd9Sstevel@tonic-gate  */
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate static void usage();
66*7c478bd9Sstevel@tonic-gate static	int	check_label();
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate extern	int	check_device();
69*7c478bd9Sstevel@tonic-gate extern	int	check_cmd();
70*7c478bd9Sstevel@tonic-gate extern	int	vml();
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate int
73*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	int c;			/* option letter */
76*7c478bd9Sstevel@tonic-gate 	int errflg = 0;		/* error indicator */
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	struct pmtab *ptr;
79*7c478bd9Sstevel@tonic-gate 	char *timeout = "";
80*7c478bd9Sstevel@tonic-gate 	char *count = "";
81*7c478bd9Sstevel@tonic-gate 	char prompt[BUFSIZ];
82*7c478bd9Sstevel@tonic-gate 	char dmsg[BUFSIZ];
83*7c478bd9Sstevel@tonic-gate 	char ttyflags[BUFSIZ], *tf;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	int  dflag = 0;		/* -d seen */
86*7c478bd9Sstevel@tonic-gate 	int  sflag = 0;		/* -s seen */
87*7c478bd9Sstevel@tonic-gate 	int  lflag = 0;		/* -l seen */
88*7c478bd9Sstevel@tonic-gate 	int  mflag = 0;		/* -m seen */
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	extern	void 	copystr();
91*7c478bd9Sstevel@tonic-gate 	extern	char	*optarg;
92*7c478bd9Sstevel@tonic-gate 	extern	int	optind;
93*7c478bd9Sstevel@tonic-gate 	extern	int	strcheck();
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	if (argc == 1)
96*7c478bd9Sstevel@tonic-gate 		usage();
97*7c478bd9Sstevel@tonic-gate 	if ((ptr = ALLOC_PMTAB) == PNULL) {
98*7c478bd9Sstevel@tonic-gate 		(void)fprintf(stderr, "calloc failed\n");
99*7c478bd9Sstevel@tonic-gate 		exit(1);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	ptr->p_modules = "";
102*7c478bd9Sstevel@tonic-gate 	ptr->p_dmsg = "";
103*7c478bd9Sstevel@tonic-gate 	ptr->p_termtype = "";
104*7c478bd9Sstevel@tonic-gate 	ptr->p_softcar = "";
105*7c478bd9Sstevel@tonic-gate 	ptr->p_prompt = "login\\: ";
106*7c478bd9Sstevel@tonic-gate 	ttyflags[0] = '\0';
107*7c478bd9Sstevel@tonic-gate 	tf = ttyflags;
108*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "IT:S:Vd:s:chbr:t:l:m:p:i:")) != -1) {
109*7c478bd9Sstevel@tonic-gate 		switch (c) {
110*7c478bd9Sstevel@tonic-gate 		case 'V':
111*7c478bd9Sstevel@tonic-gate 			if ((argc > 2) || (optind < argc))
112*7c478bd9Sstevel@tonic-gate 				usage();
113*7c478bd9Sstevel@tonic-gate 			(void)fprintf(stdout,"%d\n", PMTAB_VERS);
114*7c478bd9Sstevel@tonic-gate 			exit(0);
115*7c478bd9Sstevel@tonic-gate 			break;	/*NOTREACHED*/
116*7c478bd9Sstevel@tonic-gate 		case 'd':
117*7c478bd9Sstevel@tonic-gate 			ptr->p_device = optarg;
118*7c478bd9Sstevel@tonic-gate 			dflag = 1;
119*7c478bd9Sstevel@tonic-gate 			break;
120*7c478bd9Sstevel@tonic-gate 		case 'c':
121*7c478bd9Sstevel@tonic-gate 			tf = strcat(tf,"c");
122*7c478bd9Sstevel@tonic-gate 			break;
123*7c478bd9Sstevel@tonic-gate 		case 'h':
124*7c478bd9Sstevel@tonic-gate 			tf = strcat(tf,"h");
125*7c478bd9Sstevel@tonic-gate 			break;
126*7c478bd9Sstevel@tonic-gate 		case 'b':
127*7c478bd9Sstevel@tonic-gate 			tf = strcat(tf,"b");
128*7c478bd9Sstevel@tonic-gate 			break;
129*7c478bd9Sstevel@tonic-gate 		case 'I':
130*7c478bd9Sstevel@tonic-gate 			tf = strcat(tf,"I");
131*7c478bd9Sstevel@tonic-gate 			break;
132*7c478bd9Sstevel@tonic-gate 		case 'r':
133*7c478bd9Sstevel@tonic-gate 			tf = strcat(tf,"r");
134*7c478bd9Sstevel@tonic-gate 			count = optarg;
135*7c478bd9Sstevel@tonic-gate 			if (strcheck(optarg,NUM) != 0) {
136*7c478bd9Sstevel@tonic-gate 				(void)fprintf(stderr,
137*7c478bd9Sstevel@tonic-gate 		"Invalid argument for \"-r\" -- positive number expected.\n");
138*7c478bd9Sstevel@tonic-gate 				usage();
139*7c478bd9Sstevel@tonic-gate 			}
140*7c478bd9Sstevel@tonic-gate 			break;
141*7c478bd9Sstevel@tonic-gate 		case 'T':
142*7c478bd9Sstevel@tonic-gate 			ptr->p_termtype = optarg;
143*7c478bd9Sstevel@tonic-gate 			break;
144*7c478bd9Sstevel@tonic-gate 		case 'S':
145*7c478bd9Sstevel@tonic-gate 			switch (*optarg) {
146*7c478bd9Sstevel@tonic-gate 			case 'Y':
147*7c478bd9Sstevel@tonic-gate 			case 'y':
148*7c478bd9Sstevel@tonic-gate 				ptr->p_softcar = "y";
149*7c478bd9Sstevel@tonic-gate 				break;
150*7c478bd9Sstevel@tonic-gate 			case 'N':
151*7c478bd9Sstevel@tonic-gate 			case 'n':
152*7c478bd9Sstevel@tonic-gate 				ptr->p_softcar = "n";
153*7c478bd9Sstevel@tonic-gate 				break;
154*7c478bd9Sstevel@tonic-gate 			default:
155*7c478bd9Sstevel@tonic-gate 				usage();
156*7c478bd9Sstevel@tonic-gate 			}
157*7c478bd9Sstevel@tonic-gate 			break;
158*7c478bd9Sstevel@tonic-gate 		case 's':
159*7c478bd9Sstevel@tonic-gate 			ptr->p_server = optarg;
160*7c478bd9Sstevel@tonic-gate 			sflag = 1;
161*7c478bd9Sstevel@tonic-gate 			break;
162*7c478bd9Sstevel@tonic-gate 		case 't':
163*7c478bd9Sstevel@tonic-gate 			timeout = optarg;
164*7c478bd9Sstevel@tonic-gate 			if (strcheck(optarg,NUM) != 0) {
165*7c478bd9Sstevel@tonic-gate 				(void)fprintf(stderr,
166*7c478bd9Sstevel@tonic-gate 		"Invalid argument for \"-t\" -- positive number expected.\n");
167*7c478bd9Sstevel@tonic-gate 				usage();
168*7c478bd9Sstevel@tonic-gate 			}
169*7c478bd9Sstevel@tonic-gate 			break;
170*7c478bd9Sstevel@tonic-gate 		case 'l':
171*7c478bd9Sstevel@tonic-gate 			ptr->p_ttylabel = optarg;
172*7c478bd9Sstevel@tonic-gate 			lflag = 1;
173*7c478bd9Sstevel@tonic-gate 			break;
174*7c478bd9Sstevel@tonic-gate 		case 'm':
175*7c478bd9Sstevel@tonic-gate 			ptr->p_modules = optarg;
176*7c478bd9Sstevel@tonic-gate 			mflag = 1;
177*7c478bd9Sstevel@tonic-gate 			break;
178*7c478bd9Sstevel@tonic-gate 		case 'p':
179*7c478bd9Sstevel@tonic-gate 			ptr->p_prompt = prompt;
180*7c478bd9Sstevel@tonic-gate 			copystr(ptr->p_prompt,optarg);
181*7c478bd9Sstevel@tonic-gate 			break;
182*7c478bd9Sstevel@tonic-gate 		case 'i':
183*7c478bd9Sstevel@tonic-gate 			ptr->p_dmsg = dmsg;
184*7c478bd9Sstevel@tonic-gate 			copystr(ptr->p_dmsg,optarg);
185*7c478bd9Sstevel@tonic-gate 			break;
186*7c478bd9Sstevel@tonic-gate 		case '?':
187*7c478bd9Sstevel@tonic-gate 			usage();
188*7c478bd9Sstevel@tonic-gate 			break;
189*7c478bd9Sstevel@tonic-gate 		}
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 	if (optind < argc)
192*7c478bd9Sstevel@tonic-gate 		usage();
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	if ((!dflag) || (!sflag) || (!lflag))
195*7c478bd9Sstevel@tonic-gate 		usage();
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	if (check_device(ptr->p_device) != 0)
198*7c478bd9Sstevel@tonic-gate 		errflg++;
199*7c478bd9Sstevel@tonic-gate 	if (check_cmd(ptr->p_server) != 0)
200*7c478bd9Sstevel@tonic-gate 		errflg++;
201*7c478bd9Sstevel@tonic-gate 	if (check_label(ptr->p_ttylabel) != 0)
202*7c478bd9Sstevel@tonic-gate 		errflg++;
203*7c478bd9Sstevel@tonic-gate 	if (mflag && (vml(ptr->p_modules) != 0))
204*7c478bd9Sstevel@tonic-gate 		errflg++;
205*7c478bd9Sstevel@tonic-gate 	if (errflg)
206*7c478bd9Sstevel@tonic-gate 		exit(1);
207*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stdout, "%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:",
208*7c478bd9Sstevel@tonic-gate 			ptr->p_device, ttyflags, count, ptr->p_server,
209*7c478bd9Sstevel@tonic-gate 			timeout, ptr->p_ttylabel, ptr->p_modules,
210*7c478bd9Sstevel@tonic-gate 			ptr->p_prompt, ptr->p_dmsg, ptr->p_termtype,
211*7c478bd9Sstevel@tonic-gate 			ptr->p_softcar);
212*7c478bd9Sstevel@tonic-gate 	return (0);
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate /*
216*7c478bd9Sstevel@tonic-gate  * usage - print out a usage message
217*7c478bd9Sstevel@tonic-gate  */
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate static void
220*7c478bd9Sstevel@tonic-gate usage()
221*7c478bd9Sstevel@tonic-gate {
222*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "Usage:\tttyadm [ options ] -d device -s service -l ttylabel\n");
223*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\tttyadm -V\n");
224*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\n\tValid options are:\n");
225*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-c\n");
226*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-h\n");
227*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-b\n");
228*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-I\n");
229*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-S y|n\n");
230*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-T term\n");
231*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-r count\n");
232*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-t timeout\n");
233*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-p prompt\n");
234*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-m modules\n");
235*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr, "\t-i msg\n");
236*7c478bd9Sstevel@tonic-gate 	exit(1);
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate /*
240*7c478bd9Sstevel@tonic-gate  *	check_label	- if ttylabel exists in /etc/ttydefs, return 0
241*7c478bd9Sstevel@tonic-gate  *			- otherwise, return -1
242*7c478bd9Sstevel@tonic-gate  */
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate static int
245*7c478bd9Sstevel@tonic-gate check_label(ttylabel)
246*7c478bd9Sstevel@tonic-gate char	*ttylabel;
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	FILE *fp;
249*7c478bd9Sstevel@tonic-gate 	extern	int	find_label();
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	if ((ttylabel == NULL) || (*ttylabel == '\0')) {
252*7c478bd9Sstevel@tonic-gate 		(void)fprintf(stderr, "error -- ttylabel is missing");
253*7c478bd9Sstevel@tonic-gate 		return(-1);
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(TTYDEFS, "r")) == NULL) {
256*7c478bd9Sstevel@tonic-gate 		(void)fprintf(stderr, "error -- \"%s\" does not exist, can't verify ttylabel <%s>\n", TTYDEFS, ttylabel);
257*7c478bd9Sstevel@tonic-gate 		return(-1);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 	if (find_label(fp,ttylabel)) {
260*7c478bd9Sstevel@tonic-gate 		(void)fclose(fp);
261*7c478bd9Sstevel@tonic-gate 		return(0);
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 	(void)fclose(fp);
264*7c478bd9Sstevel@tonic-gate 	(void)fprintf(stderr,"error -- can't find ttylabel <%s> in \"%s\"\n",
265*7c478bd9Sstevel@tonic-gate 		ttylabel, TTYDEFS);
266*7c478bd9Sstevel@tonic-gate 	return(-1);
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate /*
270*7c478bd9Sstevel@tonic-gate  *	log	- print a message to stderr
271*7c478bd9Sstevel@tonic-gate  */
272*7c478bd9Sstevel@tonic-gate void
273*7c478bd9Sstevel@tonic-gate log(const char *msg, ...)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	va_list ap;
276*7c478bd9Sstevel@tonic-gate 	va_start(ap, msg);
277*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, msg, ap);
278*7c478bd9Sstevel@tonic-gate 	va_end(ap);
279*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
280*7c478bd9Sstevel@tonic-gate }
281