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