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