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