1 /* $OpenBSD: ttyflags.c,v 1.15 2019/06/04 15:45:58 otto 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/ioctl.h> 37 38 #include <err.h> 39 #include <errno.h> 40 #include <fcntl.h> 41 #include <limits.h> 42 #include <paths.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <ttyent.h> 47 #include <unistd.h> 48 49 int all(int); 50 int ttys(char **, int); 51 int ttyflags(struct ttyent *, int); 52 void usage(void); 53 54 int nflag, vflag; 55 56 /* 57 * Ttyflags sets the device-specific tty flags, based on the contents 58 * of /etc/ttys. It can either set all of the ttys' flags, or set 59 * the flags of the ttys specified on the command line. 60 */ 61 int 62 main(int argc, char *argv[]) 63 { 64 int aflag, ch, rval, pflag = 0; 65 66 aflag = nflag = vflag = 0; 67 while ((ch = getopt(argc, argv, "panv")) != -1) 68 switch (ch) { 69 case 'a': 70 aflag = 1; 71 break; 72 case 'n': /* undocumented */ 73 nflag = 1; 74 break; 75 case 'p': 76 pflag = 1; 77 break; 78 case 'v': 79 vflag = 1; 80 break; 81 case '?': 82 default: 83 usage(); 84 } 85 argc -= optind; 86 argv += optind; 87 88 if (aflag && argc != 0) 89 usage(); 90 91 if (setttyent() == 0) 92 err(1, "setttyent"); 93 94 if (aflag) 95 rval = all(pflag); 96 else 97 rval = ttys(argv, pflag); 98 99 if (endttyent() == 0) 100 warn("endttyent"); 101 102 exit(rval); 103 } 104 105 /* 106 * Change all /etc/ttys entries' flags. 107 */ 108 int 109 all(int print) 110 { 111 struct ttyent *tep; 112 int rval; 113 114 rval = 0; 115 for (tep = getttyent(); tep != NULL; tep = getttyent()) { 116 /* pseudo-tty ignore TIOCSFLAGS, so don't bother */ 117 if (tep->ty_type == NULL || 118 strcmp(tep->ty_type, "network") == 0) 119 continue; 120 if (ttyflags(tep, print)) 121 rval = 1; 122 } 123 return (rval); 124 } 125 126 /* 127 * Change the specified ttys' flags. 128 */ 129 int 130 ttys(char **ttylist, int print) 131 { 132 struct ttyent *tep; 133 int rval; 134 135 rval = 0; 136 for (; *ttylist != NULL; ttylist++) { 137 tep = getttynam(*ttylist); 138 if (tep == NULL) { 139 warnx("couldn't find an entry in %s for \"%s\"", 140 _PATH_TTYS, *ttylist); 141 rval = 1; 142 continue; 143 } 144 145 if (ttyflags(tep, print)) 146 rval = 1; 147 } 148 return (rval); 149 } 150 151 152 /* 153 * Actually do the work; find out what the new flags value should be, 154 * open the device, and change the flags. 155 */ 156 int 157 ttyflags(struct ttyent *tep, int print) 158 { 159 int fd, flags = 0, rval = 0, st, sep = 0; 160 char path[PATH_MAX]; 161 char strflags[256]; 162 163 st = tep->ty_status; 164 strflags[0] = '\0'; 165 166 /* Find the full device path name. */ 167 (void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name); 168 169 if (print == 0) { 170 /* Convert ttyent.h flags into ioctl flags. */ 171 if (st & TTY_LOCAL) { 172 flags |= TIOCFLAG_CLOCAL; 173 (void)strlcat(strflags, "local", sizeof strflags); 174 sep++; 175 } 176 if (st & TTY_RTSCTS) { 177 flags |= TIOCFLAG_CRTSCTS; 178 if (sep++) 179 (void)strlcat(strflags, "|", sizeof strflags); 180 (void)strlcat(strflags, "rtscts", sizeof strflags); 181 } 182 if (st & TTY_SOFTCAR) { 183 flags |= TIOCFLAG_SOFTCAR; 184 if (sep++) 185 (void)strlcat(strflags, "|", sizeof strflags); 186 (void)strlcat(strflags, "softcar", sizeof strflags); 187 } 188 if (st & TTY_MDMBUF) { 189 flags |= TIOCFLAG_MDMBUF; 190 if (sep++) 191 (void)strlcat(strflags, "|", sizeof strflags); 192 (void)strlcat(strflags, "mdmbuf", sizeof strflags); 193 } 194 if (vflag) 195 printf("%s setting flags to: %s\n", path, strflags); 196 } 197 198 if (nflag) 199 return (0); 200 201 /* Open the device NON-BLOCKING, set the flags, and close it. */ 202 if ((fd = open(path, O_RDONLY | O_NONBLOCK, 0)) == -1) { 203 if (!(errno == ENXIO || 204 (errno == ENOENT && (st & TTY_ON) == 0))) 205 rval = 1; 206 if (vflag) 207 warn("open %s", path); 208 return (rval); 209 } 210 if (print == 0) { 211 if (ioctl(fd, TIOCSFLAGS, &flags) == -1) 212 if (errno != ENOTTY || vflag) { 213 warn("TIOCSFLAGS on %s", path); 214 rval = (errno != ENOTTY); 215 } 216 } else { 217 if (ioctl(fd, TIOCGFLAGS, &flags) == -1) 218 if (errno != ENOTTY || vflag) { 219 warn("TIOCGFLAGS on %s", path); 220 rval = (errno != ENOTTY); 221 } 222 if (flags & TIOCFLAG_CLOCAL) { 223 (void)strlcat(strflags, "local", sizeof strflags); 224 sep++; 225 } 226 if (flags & TIOCFLAG_CRTSCTS) { 227 if (sep++) 228 (void)strlcat(strflags, "|", sizeof strflags); 229 (void)strlcat(strflags, "rtscts", sizeof strflags); 230 } 231 if (flags & TIOCFLAG_SOFTCAR) { 232 if (sep++) 233 (void)strlcat(strflags, "|", sizeof strflags); 234 (void)strlcat(strflags, "softcar", sizeof strflags); 235 } 236 if (flags & TIOCFLAG_MDMBUF) { 237 if (sep++) 238 (void)strlcat(strflags, "|", sizeof strflags); 239 (void)strlcat(strflags, "mdmbuf", sizeof strflags); 240 } 241 printf("%s flags are: %s\n", path, strflags); 242 } 243 if (close(fd) == -1) { 244 warn("close %s", path); 245 return (1); 246 } 247 return (rval); 248 } 249 250 /* 251 * Print usage information when a bogus set of arguments is given. 252 */ 253 void 254 usage(void) 255 { 256 (void)fprintf(stderr, "usage: ttyflags [-pv] [-a | tty ...]\n"); 257 exit(1); 258 } 259