xref: /openbsd-src/sbin/ttyflags/ttyflags.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: ttyflags.c,v 1.12 2009/10/27 23:59:34 deraadt Exp $	*/
2 /*	$NetBSD: ttyflags.c,v 1.8 1996/04/09 05:20:30 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1996 Theo de Raadt
6  * Copyright (c) 1994 Christopher G. Demetriou
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Christopher G. Demetriou.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/cdefs.h>
37 #include <sys/ioctl.h>
38 
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <limits.h>
43 #include <paths.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ttyent.h>
48 #include <unistd.h>
49 
50 int all(int);
51 int ttys(char **, int);
52 int ttyflags(struct ttyent *, int);
53 void usage(void);
54 
55 int nflag, vflag;
56 
57 /*
58  * Ttyflags sets the device-specific tty flags, based on the contents
59  * of /etc/ttys.  It can either set all of the ttys' flags, or set
60  * the flags of the ttys specified on the command line.
61  */
62 int
63 main(int argc, char *argv[])
64 {
65 	int aflag, ch, rval, pflag = 0;
66 
67 	aflag = nflag = vflag = 0;
68 	while ((ch = getopt(argc, argv, "panv")) != -1)
69 		switch (ch) {
70 		case 'a':
71 			aflag = 1;
72 			break;
73 		case 'n':		/* undocumented */
74 			nflag = 1;
75 			break;
76 		case 'p':
77 			pflag = 1;
78 			break;
79 		case 'v':
80 			vflag = 1;
81 			break;
82 		case '?':
83 		default:
84 			usage();
85 		}
86 	argc -= optind;
87 	argv += optind;
88 
89 	if (aflag && argc != 0)
90 		usage();
91 
92 	if (setttyent() == 0)
93 		err(1, "setttyent");
94 
95 	if (aflag)
96 		rval = all(pflag);
97 	else
98 		rval = ttys(argv, pflag);
99 
100 	if (endttyent() == 0)
101 		warn("endttyent");
102 
103 	exit(rval);
104 }
105 
106 /*
107  * Change all /etc/ttys entries' flags.
108  */
109 int
110 all(int print)
111 {
112 	struct ttyent *tep;
113 	int rval;
114 
115 	rval = 0;
116 	for (tep = getttyent(); tep != NULL; tep = getttyent())
117 		if (ttyflags(tep, print))
118 			rval = 1;
119 	return (rval);
120 }
121 
122 /*
123  * Change the specified ttys' flags.
124  */
125 int
126 ttys(char **ttylist, int print)
127 {
128 	struct ttyent *tep;
129 	int rval;
130 
131 	rval = 0;
132 	for (; *ttylist != NULL; ttylist++) {
133 		tep = getttynam(*ttylist);
134 		if (tep == NULL) {
135 			warnx("couldn't find an entry in %s for \"%s\"",
136 			    _PATH_TTYS, *ttylist);
137 			rval = 1;
138 			continue;
139 		}
140 
141 		if (ttyflags(tep, print))
142 			rval = 1;
143 	}
144 	return (rval);
145 }
146 
147 
148 /*
149  * Actually do the work; find out what the new flags value should be,
150  * open the device, and change the flags.
151  */
152 int
153 ttyflags(struct ttyent *tep, int print)
154 {
155 	int fd, flags = 0, rval = 0, st, sep = 0;
156 	char path[PATH_MAX];
157 	char strflags[256];
158 
159 	st = tep->ty_status;
160 	strflags[0] = '\0';
161 
162 	/* Find the full device path name. */
163 	(void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name);
164 
165 	if (print == 0) {
166 		/* Convert ttyent.h flags into ioctl flags. */
167 		if (st & TTY_LOCAL) {
168 			flags |= TIOCFLAG_CLOCAL;
169 			(void)strlcat(strflags, "local", sizeof strflags);
170 			sep++;
171 		}
172 		if (st & TTY_RTSCTS) {
173 			flags |= TIOCFLAG_CRTSCTS;
174 			if (sep++)
175 				(void)strlcat(strflags, "|", sizeof strflags);
176 			(void)strlcat(strflags, "rtscts", sizeof strflags);
177 		}
178 		if (st & TTY_SOFTCAR) {
179 			flags |= TIOCFLAG_SOFTCAR;
180 			if (sep++)
181 				(void)strlcat(strflags, "|", sizeof strflags);
182 			(void)strlcat(strflags, "softcar", sizeof strflags);
183 		}
184 		if (st & TTY_MDMBUF) {
185 			flags |= TIOCFLAG_MDMBUF;
186 			if (sep++)
187 				(void)strlcat(strflags, "|", sizeof strflags);
188 			(void)strlcat(strflags, "mdmbuf", sizeof strflags);
189 		}
190 		if (vflag)
191 			printf("%s setting flags to: %s\n", path, strflags);
192 	}
193 
194 	if (nflag)
195 		return (0);
196 
197 	/* Open the device NON-BLOCKING, set the flags, and close it. */
198 	if ((fd = open(path, O_RDONLY | O_NONBLOCK, 0)) == -1) {
199 		if (!(errno == ENXIO ||
200 		      (errno == ENOENT && (st & TTY_ON) == 0)))
201 			rval = 1;
202 		if (vflag)
203 			warn("open %s", path);
204 		return (rval);
205 	}
206 	if (print == 0) {
207 		if (ioctl(fd, TIOCSFLAGS, &flags) == -1)
208 			if (errno != ENOTTY || vflag) {
209 				warn("TIOCSFLAGS on %s", path);
210 				rval = (errno != ENOTTY);
211 			}
212 	} else {
213 		if (ioctl(fd, TIOCGFLAGS, &flags) == -1)
214 			if (errno != ENOTTY || vflag) {
215 				warn("TIOCGFLAGS on %s", path);
216 				rval = (errno != ENOTTY);
217 			}
218 		if (flags & TIOCFLAG_CLOCAL) {
219 			(void)strlcat(strflags, "local", sizeof strflags);
220 			sep++;
221 		}
222 		if (flags & TIOCFLAG_CRTSCTS) {
223 			if (sep++)
224 				(void)strlcat(strflags, "|", sizeof strflags);
225 			(void)strlcat(strflags, "rtscts", sizeof strflags);
226 		}
227 		if (flags & TIOCFLAG_SOFTCAR) {
228 			if (sep++)
229 				(void)strlcat(strflags, "|", sizeof strflags);
230 			(void)strlcat(strflags, "softcar", sizeof strflags);
231 		}
232 		if (flags & TIOCFLAG_MDMBUF) {
233 			if (sep++)
234 				(void)strlcat(strflags, "|", sizeof strflags);
235 			(void)strlcat(strflags, "mdmbuf", sizeof strflags);
236 		}
237 		printf("%s flags are: %s\n", path, strflags);
238 	}
239 	if (close(fd) == -1) {
240 		warn("close %s", path);
241 		return (1);
242 	}
243 	return (rval);
244 }
245 
246 /*
247  * Print usage information when a bogus set of arguments is given.
248  */
249 void
250 usage(void)
251 {
252 	(void)fprintf(stderr, "usage: ttyflags [-pv] [-a | tty ...]\n");
253 	exit(1);
254 }
255