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