118480Sralph #ifndef lint 2*25123Sbloom static char sccsid[] = "@(#)acucntrl.c 5.5 (Berkeley) 10/09/85"; 318480Sralph #endif 418480Sralph 518480Sralph /* acucntrl - turn around tty line between dialin and dialout 618480Sralph * 718480Sralph * Usage: acucntrl {enable,disable} /dev/ttydX 818480Sralph * 918480Sralph * History: 1018480Sralph * First written by Allan Wilkes (fisher!allan) 1118480Sralph * 1218480Sralph * Modified June 8,1983 by W.Sebok (astrovax!wls) to poke kernel rather 1318480Sralph * than use kernel hack to turn on/off modem control, using subroutine 1418480Sralph * stolen from program written by Tsutomu Shimomura 1518480Sralph * {astrovax,escher}!tsutomu 1618480Sralph * 1718480Sralph * Worked over many times by W.Sebok (i.e. hacked to death) 1818480Sralph * 1918480Sralph * Operation: 2018480Sralph * disable (i.e. setup for dialing out) 2118480Sralph * (1) check input arguments 2218480Sralph * (2) look in /etc/utmp to check that the line is not in use by another 2318480Sralph * (3) disable modem control on terminal 2418480Sralph * (4) check for carrier on device 2518480Sralph * (5) change owner of device to real id 2618480Sralph * (6) edit /etc/ttys, changing the first character of the appropriate 2718480Sralph * line to 0 2818480Sralph * (7) send a hangup to process 1 to poke init to disable getty 2918480Sralph * (8) post uid name in capitals in /etc/utmp to let world know device has 3018480Sralph * been grabbed 3118480Sralph * (9) make sure that DTR is on 3218480Sralph * 3318480Sralph * enable (i.e.) restore for dialin 3418480Sralph * (1) check input arguments 3518480Sralph * (2) look in /etc/utmp to check that the line is not in use by another 3618480Sralph * (3) make sure modem control on terminal is disabled 3718480Sralph * (4) turn off DTR to make sure line is hung up 3818480Sralph * (5) condition line: clear exclusive use and set hangup on close modes 3918480Sralph * (6) turn on modem control 4018480Sralph * (7) edit /etc/ttys, changing the first character of the appropriate 4118480Sralph * line to 1 4218480Sralph * (8) send a hangup to process 1 to poke init to enable getty 4318480Sralph * (9) clear uid name for /etc/utmp 4418480Sralph */ 4518480Sralph 4618480Sralph /* #define SENSECARRIER */ 4718480Sralph 4818480Sralph #include "uucp.h" 4918480Sralph #include <sys/buf.h> 5018614Sralph #include <signal.h> 5118480Sralph #include <sys/conf.h> 5218480Sralph #ifdef BSD4_2 5323583Sbloom #include <vaxuba/ubavar.h> 5418614Sralph #else 5518480Sralph #include <sys/ubavar.h> 5618614Sralph #endif 5718480Sralph #include <sys/stat.h> 5818480Sralph #include <nlist.h> 5918480Sralph #include <sgtty.h> 6018480Sralph #include <utmp.h> 6118480Sralph #include <pwd.h> 6218480Sralph #include <stdio.h> 63*25123Sbloom #include <sys/file.h> 6418480Sralph 6518480Sralph #define NDZLINE 8 /* lines/dz */ 6618480Sralph #define NDHLINE 16 /* lines/dh */ 6718480Sralph #define NDMFLINE 8 /* lines/dmf */ 6818480Sralph 6918480Sralph #define DZ11 1 7018480Sralph #define DH11 2 7118480Sralph #define DMF 3 7218480Sralph 7318480Sralph #define NLVALUE(val) (nl[val].n_value) 7418480Sralph 7518480Sralph struct nlist nl[] = { 7618480Sralph #define CDEVSW 0 7718480Sralph { "_cdevsw" }, 7818480Sralph 7918480Sralph #define DZOPEN 1 8018480Sralph { "_dzopen" }, 8118480Sralph #define DZINFO 2 8218480Sralph { "_dzinfo" }, 8318480Sralph #define NDZ11 3 8418480Sralph { "_dz_cnt" }, 8518480Sralph #define DZSCAR 4 8618480Sralph { "_dzsoftCAR" }, 8718480Sralph 8818480Sralph #define DHOPEN 5 8918480Sralph { "_dhopen" }, 9018480Sralph #define DHINFO 6 9118480Sralph { "_dhinfo" }, 9218480Sralph #define NDH11 7 9318480Sralph { "_ndh11" }, 9418480Sralph #define DHSCAR 8 9518480Sralph { "_dhsoftCAR" }, 9618480Sralph 9718480Sralph #define DMFOPEN 9 9818480Sralph { "_dmfopen" }, 9918480Sralph #define DMFINFO 10 10018480Sralph { "_dmfinfo" }, 10118480Sralph #define NDMF 11 10218480Sralph { "_ndmf" }, 10318480Sralph #define DMFSCAR 12 10418480Sralph { "_dmfsoftCAR" }, 10518480Sralph 10618480Sralph { "\0" } 10718480Sralph }; 10818480Sralph 10918480Sralph #define ENABLE 1 11018480Sralph #define DISABLE 0 11118480Sralph 11218480Sralph char Etcutmp[] = "/etc/utmp"; 11318480Sralph char Etcttys[] = "/etc/ttys"; 11423723Sbloom #ifdef BSD4_3 115*25123Sbloom FILE *ttysfile, *nttysfile; 11623723Sbloom char NEtcttys[] = "/etc/ttys.new"; 11723723Sbloom extern long ftell(); 11823723Sbloom #endif BSD4_3 11918480Sralph char Devhome[] = "/dev"; 12018480Sralph 12118480Sralph char usage[] = "Usage: acucntrl {dis|en}able ttydX\n"; 12218480Sralph 12318480Sralph struct utmp utmp; 12418480Sralph char resettty, resetmodem; 12518480Sralph int etcutmp; 12618614Sralph off_t utmploc; 12718614Sralph off_t ttyslnbeg; 12818480Sralph 12918480Sralph #define NAMSIZ sizeof(utmp.ut_name) 13018480Sralph #define LINSIZ sizeof(utmp.ut_line) 13118480Sralph 13218480Sralph main(argc, argv) 13318480Sralph int argc; char *argv[]; 13418480Sralph { 13518480Sralph register char *p; 13618480Sralph register int i; 13718480Sralph char uname[NAMSIZ], Uname[NAMSIZ]; 13818480Sralph int enable ; 13918480Sralph char *device; 14018480Sralph int devfile; 14118480Sralph int uid, gid; 14218614Sralph off_t lseek(); 14318480Sralph struct passwd *getpwuid(); 14418480Sralph char *rindex(); 14518480Sralph extern int errno; 14618480Sralph extern char *sys_errlist[]; 14718480Sralph 14818480Sralph /* check input arguments */ 14918480Sralph if (argc!=3) { 15018480Sralph fprintf(stderr, usage); 15118480Sralph exit(1); 15218480Sralph } 15318480Sralph 15418480Sralph /* interpret command type */ 15523723Sbloom if (prefix(argv[1], "disable") || strcmp(argv[1], "dialout")==0) 15618480Sralph enable = 0; 15723723Sbloom else if (prefix(argv[1], "enable") || strcmp(argv[1], "dialin")==0) 15818480Sralph enable = 1; 15918480Sralph else { 16018480Sralph fprintf(stderr, usage); 16118480Sralph exit(1); 16218480Sralph } 16318480Sralph 16423723Sbloom device = rindex(argv[2], '/'); 16518480Sralph device = (device == NULL) ? argv[2]: device+1; 16618480Sralph 16718480Sralph /* only recognize devices of the form ttydX */ 16823723Sbloom if (strncmp(device, "ttyd", 4)!=0) { 16923723Sbloom fprintf(stderr, "Bad Device Name %s", device); 17018480Sralph exit(1); 17118480Sralph } 17218480Sralph 17318480Sralph opnttys(device); 17418480Sralph 17518480Sralph /* Get nlist info */ 17623723Sbloom nlist("/vmunix", nl); 17718480Sralph 17818480Sralph /* Chdir to /dev */ 17918480Sralph if(chdir(Devhome) < 0) { 18018480Sralph fprintf(stderr, "Cannot chdir to %s: %s\r\n", 18118480Sralph Devhome, sys_errlist[errno]); 18218480Sralph exit(1); 18318480Sralph } 18418480Sralph 18518480Sralph /* Get uid information */ 18618480Sralph uid = getuid(); 18718480Sralph gid = getgid(); 18818480Sralph 18918480Sralph p = getpwuid(uid)->pw_name; 19018480Sralph if (p==NULL) { 19123723Sbloom fprintf(stderr, "cannot get uid name\n"); 19218480Sralph exit(1); 19318480Sralph } 19418480Sralph 19518480Sralph /* to upper case */ 19618480Sralph i = 0; 19718480Sralph do { 19818480Sralph uname[i] = *p; 19918480Sralph Uname[i] = (*p>='a' && *p<='z') ? (*p - ('a'-'A')) : *p; 20018480Sralph i++; p++; 20118480Sralph } while (*p && i<NAMSIZ); 20218480Sralph 20318480Sralph 20418480Sralph /* check to see if line is being used */ 20518480Sralph if( (etcutmp = open(Etcutmp, 2)) < 0) { 20623723Sbloom fprintf(stderr, "On open %s open: %s\n", 20723723Sbloom Etcutmp, sys_errlist[errno]); 20818480Sralph exit(1); 20918480Sralph } 21018480Sralph 21123723Sbloom (void)lseek(etcutmp, utmploc, 0); 21218480Sralph 21323723Sbloom i = read(etcutmp, (char *)&utmp, sizeof(struct utmp)); 21418480Sralph 21518480Sralph if( 21618480Sralph i == sizeof(struct utmp) && 21718480Sralph utmp.ut_line[0] != '\0' && 21818480Sralph utmp.ut_name[0] != '\0' && 21918480Sralph ( 22023723Sbloom !upcase(utmp.ut_name, NAMSIZ) || 22118480Sralph ( 22218480Sralph uid != 0 && 22323723Sbloom strncmp(utmp.ut_name, Uname, NAMSIZ) != 0 22418480Sralph ) 22518480Sralph ) 22618480Sralph ) { 22718480Sralph fprintf(stderr, "%s in use by %s\n", device, utmp.ut_name); 22818480Sralph exit(2); 22918480Sralph } 23018480Sralph 23118480Sralph /* Disable modem control */ 23223723Sbloom if (setmodem(device, DISABLE) < 0) { 23323723Sbloom fprintf(stderr, "Unable to disable modem control\n"); 23418480Sralph exit(1); 23518480Sralph } 23618480Sralph 23718480Sralph if (enable) { 23818480Sralph if((devfile = open(device, 1)) < 0) { 23923723Sbloom fprintf(stderr, "On open of %s: %s\n", 24018480Sralph device, sys_errlist[errno]); 24123723Sbloom (void)setmodem(device, resetmodem); 24218480Sralph exit(1); 24318480Sralph } 24418480Sralph /* Try one last time to hang up */ 24523723Sbloom if (ioctl(devfile, (int)TIOCCDTR, (char *)0) < 0) 24623723Sbloom fprintf(stderr, "On TIOCCDTR ioctl: %s\n", 24718480Sralph sys_errlist[errno]); 24818480Sralph 24923723Sbloom if (ioctl(devfile, (int)TIOCNXCL, (char *)0) < 0) 25018480Sralph fprintf(stderr, 25118480Sralph "Cannot clear Exclusive Use on %s: %s\n", 25218480Sralph device, sys_errlist[errno]); 25318480Sralph 25423723Sbloom if (ioctl(devfile, (int)TIOCHPCL, (char *)0) < 0) 25518480Sralph fprintf(stderr, 25618480Sralph "Cannot set hangup on close on %s: %s\n", 25718480Sralph device, sys_errlist[errno]); 25818480Sralph 25918480Sralph i = resetmodem; 26018480Sralph 26123723Sbloom if (setmodem(device, ENABLE) < 0) { 26223723Sbloom fprintf(stderr, "Cannot Enable modem control\n"); 26323723Sbloom (void)setmodem(device, i); 26418480Sralph exit(1); 26518480Sralph } 26618480Sralph resetmodem=i; 26718480Sralph 26818614Sralph if (settys(ENABLE)) { 26923723Sbloom fprintf(stderr, "%s already enabled\n", device); 27018614Sralph } else { 27123723Sbloom pokeinit(device, Uname, enable); 27218614Sralph } 27323723Sbloom post(device, ""); 27418480Sralph 27518480Sralph } else { 27618480Sralph #if defined(TIOCMGET) && defined(SENSECARRIER) 27718480Sralph if (uid!=0) { 27818480Sralph int linestat = 0; 27918480Sralph 28018480Sralph /* check for presence of carrier */ 28118480Sralph sleep(2); /* need time after modem control turnoff */ 28218480Sralph 28318480Sralph if((devfile = open(device, 1)) < 0) { 28423723Sbloom fprintf(stderr, "On open of %s: %s\n", 28518480Sralph device, sys_errlist[errno]); 28623723Sbloom (void)setmodem(device, resetmodem); 28718480Sralph exit(1); 28818480Sralph } 28918480Sralph 29023723Sbloom (void)ioctl(devfile, TIOCMGET, &linestat); 29118480Sralph 29218480Sralph if (linestat&TIOCM_CAR) { 29323723Sbloom fprintf(stderr, "%s is in use (Carrier On)\n", 29418480Sralph device); 29523723Sbloom (void)setmodem(device, resetmodem); 29618480Sralph exit(2); 29718480Sralph } 29818480Sralph (void)close(devfile); 29918480Sralph } 30018480Sralph #endif TIOCMGET 30118480Sralph /* chown device */ 30218480Sralph if(chown(device, uid, gid) < 0) 30318480Sralph fprintf(stderr, "Cannot chown %s: %s\n", 30418480Sralph device, sys_errlist[errno]); 30518480Sralph 30618480Sralph 30718480Sralph /* poke init */ 30818614Sralph if(settys(DISABLE)) { 30923723Sbloom fprintf(stderr, "%s already disabled\n", device); 31018614Sralph } else { 31123723Sbloom pokeinit(device, Uname, enable); 31218614Sralph } 31323723Sbloom post(device, Uname); 314*25123Sbloom if((devfile = open(device, O_RDWR|O_NDELAY)) < 0) { 31518480Sralph fprintf(stderr, "On %s open: %s\n", 31618480Sralph device, sys_errlist[errno]); 31718480Sralph } else { 31818614Sralph if(ioctl(devfile, (int)TIOCSDTR, (char *)0) < 0) 31918480Sralph fprintf(stderr, 32018480Sralph "Cannot set DTR on %s: %s\n", 32118480Sralph device, sys_errlist[errno]); 32218480Sralph } 32318480Sralph } 32418480Sralph 32518480Sralph exit(0); 32618480Sralph } 32718480Sralph 32818480Sralph /* return true if no lower case */ 32923723Sbloom upcase(str, len) 33018480Sralph register char *str; 33118480Sralph register int len; 33218480Sralph { 33318480Sralph for (; *str, --len >= 0 ; str++) 33418480Sralph if (*str>='a' && *str<='z') 33518480Sralph return(0); 33618480Sralph return(1); 33718480Sralph } 33818480Sralph 33918480Sralph /* Post name to public */ 34023723Sbloom post(device, name) 34118480Sralph char *device, *name; 34218480Sralph { 34318614Sralph (void)time((time_t *)&utmp.ut_time); 34423583Sbloom strncpy(utmp.ut_line, device, LINSIZ); 34523583Sbloom strncpy(utmp.ut_name, name, NAMSIZ); 346*25123Sbloom if (lseek(etcutmp, utmploc, 0) < 0) 34723723Sbloom fprintf(stderr, "on lseek in /etc/utmp: %s", 34818480Sralph sys_errlist[errno]); 349*25123Sbloom if (write(etcutmp, (char *)&utmp, sizeof(utmp)) < 0) 35023723Sbloom fprintf(stderr, "on write in /etc/utmp: %s", 35118480Sralph sys_errlist[errno]); 35218480Sralph } 35318480Sralph 35418480Sralph /* poke process 1 and wait for it to do its thing */ 35523723Sbloom pokeinit(device, uname, enable) 35618480Sralph char *uname, *device; int enable; 35718480Sralph { 35818480Sralph struct utmp utmp; 35918614Sralph register int i; 36018480Sralph 36118480Sralph post(device, uname); 36218480Sralph 36318480Sralph /* poke init */ 36418480Sralph if (kill(1, SIGHUP)) { 36518480Sralph fprintf(stderr, 36618480Sralph "Cannot send hangup to init process: %s\n", 36718480Sralph sys_errlist[errno]); 36818614Sralph (void)settys(resettty); 36923723Sbloom (void)setmodem(device, resetmodem); 37018480Sralph exit(1); 37118480Sralph } 37218480Sralph 37318480Sralph if (enable) 37418480Sralph return; 37518480Sralph 37618480Sralph /* wait till init has responded, clearing the utmp entry */ 377*25123Sbloom i = 100; 37818480Sralph do { 37918480Sralph sleep(1); 380*25123Sbloom if (lseek(etcutmp, utmploc, 0) < 0) 38123723Sbloom fprintf(stderr, "On lseek in /etc/utmp: %s", 38218480Sralph sys_errlist[errno]); 383*25123Sbloom if (read(etcutmp, (char *)&utmp, sizeof utmp) < 0) 38423723Sbloom fprintf(stderr, "On read from /etc/utmp: %s", 38518480Sralph sys_errlist[errno]); 38618614Sralph } while (utmp.ut_name[0] != '\0' && --i > 0); 38718480Sralph } 38818480Sralph 38923723Sbloom #ifdef BSD4_3 39018480Sralph /* identify terminal line in ttys */ 39118480Sralph opnttys(device) 39218480Sralph char *device; 39318480Sralph { 39423723Sbloom register int ndevice; 39523723Sbloom register char *p; 39623723Sbloom char *index(); 39723723Sbloom char linebuf[BUFSIZ]; 39823723Sbloom 399*25123Sbloom ttysfile = NULL; 400*25123Sbloom do { 401*25123Sbloom if (ttysfile != NULL) { 402*25123Sbloom fclose(ttysfile); 403*25123Sbloom sleep(5); 404*25123Sbloom } 405*25123Sbloom ttysfile = fopen(Etcttys, "r"); 406*25123Sbloom if(ttysfile == NULL) { 407*25123Sbloom fprintf(stderr, "Cannot open %s: %s\n", Etcttys, 408*25123Sbloom sys_errlist[errno]); 409*25123Sbloom exit(1); 410*25123Sbloom } 411*25123Sbloom } while (flock(fileno(ttysfile), LOCK_NB|LOCK_EX) < 0); 41223723Sbloom nttysfile = fopen(NEtcttys, "w"); 41323723Sbloom if(nttysfile == NULL) { 41423723Sbloom fprintf(stderr, "Cannot open %s: %s\n", Etcttys, 41523723Sbloom sys_errlist[errno]); 41623723Sbloom exit(1); 41723723Sbloom } 41823723Sbloom 41923723Sbloom ndevice = strlen(device); 420*25123Sbloom utmploc = sizeof(utmp); 42123723Sbloom 42223723Sbloom while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) { 423*25123Sbloom if(strncmp(device, linebuf, ndevice) == 0) 424*25123Sbloom return; 425*25123Sbloom ttyslnbeg += strlen(linebuf); 42623723Sbloom utmploc += sizeof(utmp); 42723723Sbloom if (fputs(linebuf, nttysfile) == NULL) { 42823723Sbloom fprintf(stderr, "On %s write: %s\n", 42923723Sbloom Etcttys, sys_errlist[errno]); 43023723Sbloom exit(1); 43123723Sbloom } 43223723Sbloom 43323723Sbloom } 43423723Sbloom fprintf(stderr, "%s not found in %s\n", device, Etcttys); 43523723Sbloom exit(1); 43623723Sbloom } 43723723Sbloom 43823723Sbloom /* modify appropriate line in /etc/ttys to turn on/off the device */ 43923723Sbloom settys(enable) 44023723Sbloom int enable; 44123723Sbloom { 44223723Sbloom register char *cp, *cp2; 44323723Sbloom char lbuf[BUFSIZ]; 44423723Sbloom int i; 44523723Sbloom char c1, c2; 44623723Sbloom 447*25123Sbloom (void) fseek(ttysfile, ttyslnbeg, 0); 448*25123Sbloom if(fgets(lbuf, BUFSIZ, ttysfile) == NULL) { 44923723Sbloom fprintf(stderr, "On %s read: %s\n", 45023723Sbloom Etcttys, sys_errlist[errno]); 45123723Sbloom exit(1); 45223723Sbloom } 45323723Sbloom /* format is now */ 45423723Sbloom /* ttyd0 std.100 dialup on secure # comment */ 45523723Sbloom cp = lbuf; 45623723Sbloom for (i=0;*cp && i<3;i++) { 45723723Sbloom while (*cp && *cp != ' ' && *cp != '\t') 45823723Sbloom cp++; 45923723Sbloom while (*cp && (*cp == ' ' || *cp == '\t')) 46023723Sbloom cp++; 46123723Sbloom } 46223723Sbloom if (*cp == '\0') { 46323723Sbloom fprintf(stderr,"Badly formatted line in /etc/ttys:\n%s", lbuf); 46423723Sbloom exit(1); 46523723Sbloom } 46623723Sbloom c1 = *--cp; 46723723Sbloom *cp++ = '\0'; 46823723Sbloom cp2 = cp; 46923723Sbloom while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n') 47023723Sbloom cp++; 47123723Sbloom if (*cp == '\0') { 47223723Sbloom fprintf(stderr,"Badly formatted line in /etc/ttys:\n%s", lbuf); 47323723Sbloom exit(1); 47423723Sbloom } 47523723Sbloom c2 = *cp; 47623723Sbloom *cp++ = '\0'; 47723723Sbloom while (*cp && (*cp == ' ' || *cp == '\t')) 47823723Sbloom cp++; 47923723Sbloom resettty = strcmp("on", cp2) != 0; 480*25123Sbloom fprintf(nttysfile,"%s%c%s%c%s", lbuf, c1, enable ? "on" : "off", c2, cp); 481*25123Sbloom if (ferror(nttysfile)) { 48223723Sbloom fprintf(stderr, "On %s fprintf: %s\n", 48323723Sbloom NEtcttys, sys_errlist[errno]); 48423723Sbloom exit(1); 48523723Sbloom } 486*25123Sbloom while(fgets(lbuf, sizeof(lbuf) - 1, ttysfile) != NULL) { 487*25123Sbloom if (fputs(lbuf, nttysfile) == NULL) { 48823723Sbloom fprintf(stderr, "On %s write: %s\n", 48923723Sbloom NEtcttys, sys_errlist[errno]); 49023723Sbloom exit(1); 49123723Sbloom } 49223723Sbloom } 49323723Sbloom 49423723Sbloom if (enable^resettty) 49523723Sbloom (void) unlink(NEtcttys); 49623723Sbloom else { 49723723Sbloom struct stat statb; 49823723Sbloom if (stat(Etcttys, &statb) == 0) { 499*25123Sbloom fchmod(fileno(nttysfile) ,statb.st_mode); 500*25123Sbloom fchown(fileno(nttysfile), statb.st_uid, statb.st_gid); 50123723Sbloom } 50223723Sbloom (void) rename(NEtcttys, Etcttys); 50323723Sbloom } 504*25123Sbloom (void) fclose(nttysfile); 505*25123Sbloom (void) fclose(ttysfile); 50623723Sbloom return enable^resettty; 50723723Sbloom } 50823723Sbloom 50923723Sbloom #else !BSD4_3 51023723Sbloom 51123723Sbloom /* identify terminal line in ttys */ 51223723Sbloom opnttys(device) 51323723Sbloom char *device; 51423723Sbloom { 51518480Sralph register FILE *ttysfile; 51618480Sralph register int ndevice, lnsiz; 51718480Sralph register char *p; 51818480Sralph char *index(); 51923723Sbloom char linebuf[BUFSIZ]; 52018480Sralph 52118480Sralph ttysfile = fopen(Etcttys, "r"); 52218480Sralph if(ttysfile == NULL) { 52318480Sralph fprintf(stderr, "Cannot open %s: %s\n", Etcttys, 52418480Sralph sys_errlist[errno]); 52518480Sralph exit(1); 52618480Sralph } 52718480Sralph 52818480Sralph ndevice = strlen(device); 52918480Sralph ttyslnbeg = 0; 53018480Sralph utmploc = 0; 53118480Sralph 53218480Sralph while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) { 53318480Sralph lnsiz = strlen(linebuf); 53423723Sbloom if ((p = index(linebuf, '\n')) != NULL) 53518480Sralph *p = '\0'; 53618480Sralph if(strncmp(device, &linebuf[2], ndevice) == 0) { 53718480Sralph (void)fclose(ttysfile); 53818480Sralph return; 53918480Sralph } 54018480Sralph ttyslnbeg += lnsiz; 541*25123Sbloom utmploc += sizeof(utmp); 54218480Sralph } 54318480Sralph fprintf(stderr, "%s not found in %s\n", device, Etcttys); 54418480Sralph exit(1); 54518480Sralph } 54618480Sralph 54718480Sralph /* modify appropriate line in /etc/ttys to turn on/off the device */ 54818480Sralph settys(enable) 54918480Sralph int enable; 55018480Sralph { 55118480Sralph int ittysfil; 55223723Sbloom char out, in; 55318480Sralph 55418480Sralph ittysfil = open(Etcttys, 2); 55523723Sbloom if(ittysfil < 0) { 55618480Sralph fprintf(stderr, "Cannot open %s for output: %s\n", 55718480Sralph Etcttys, sys_errlist[errno]); 55818480Sralph exit(1); 55918480Sralph } 56023723Sbloom (void)lseek(ittysfil, ttyslnbeg, 0); 56123723Sbloom if(read(ittysfil, &in, 1)<0) { 56223723Sbloom fprintf(stderr, "On %s write: %s\n", 56318480Sralph Etcttys, sys_errlist[errno]); 56418480Sralph exit(1); 56518480Sralph } 56618480Sralph resettty = (in == '1'); 56718480Sralph out = enable ? '1' : '0'; 56823723Sbloom (void)lseek(ittysfil, ttyslnbeg, 0); 56923723Sbloom if(write(ittysfil, &out, 1)<0) { 57023723Sbloom fprintf(stderr, "On %s write: %s\n", 57118480Sralph Etcttys, sys_errlist[errno]); 57218480Sralph exit(1); 57318480Sralph } 57418480Sralph (void)close(ittysfil); 57518614Sralph return(in==out); 57618480Sralph } 57723723Sbloom #endif !BSD4_3 57818480Sralph 57918480Sralph /* 58023723Sbloom * Excerpted from (June 8, 1983 W.Sebok) 58118480Sralph * > ttymodem.c - enable/disable modem control for tty lines. 58218480Sralph * > 58318480Sralph * > Knows about DZ11s and DH11/DM11s. 58418480Sralph * > 23.3.83 - TS 58523723Sbloom * > modified to know about DMF's (hasn't been tested) Nov 8, 1984 - WLS 58618480Sralph */ 58718480Sralph 58818480Sralph 58923723Sbloom setmodem(ttyline, enable) 59018480Sralph char *ttyline; int enable; 59118480Sralph { 59218480Sralph dev_t dev; 59318480Sralph int kmem; 59423723Sbloom int unit, line, nlines, addr, tflags; 59518614Sralph int devtype=0; 59618614Sralph char cflags; short sflags; 59718614Sralph #ifdef BSD4_2 59818614Sralph int flags; 59918614Sralph #else 60018614Sralph short flags; 60118614Sralph #endif 60218480Sralph struct uba_device *ubinfo; 60318480Sralph struct stat statb; 60418480Sralph struct cdevsw cdevsw; 60518480Sralph 60618480Sralph if(nl[CDEVSW].n_type == 0) { 60723723Sbloom fprintf(stderr, "No namelist.\n"); 60818480Sralph return(-1); 60918480Sralph } 61018480Sralph 61118480Sralph if((kmem = open("/dev/kmem", 2)) < 0) { 61223723Sbloom fprintf(stderr, "/dev/kmem open: %s\n", sys_errlist[errno]); 61318480Sralph return(-1); 61418480Sralph } 61518480Sralph 61623723Sbloom if(stat(ttyline, &statb) < 0) { 61723723Sbloom fprintf(stderr, "%s stat: %s\n", ttyline, sys_errlist[errno]); 61818480Sralph return(-1); 61918480Sralph } 62018480Sralph 62118614Sralph if((statb.st_mode&S_IFMT) != S_IFCHR) { 62223723Sbloom fprintf(stderr, "%s is not a character device.\n",ttyline); 62318480Sralph return(-1); 62418480Sralph } 62518480Sralph 62618480Sralph dev = statb.st_rdev; 62718480Sralph (void)lseek(kmem, 62818614Sralph (off_t) &(((struct cdevsw *)NLVALUE(CDEVSW))[major(dev)]),0); 62923723Sbloom (void)read(kmem, (char *) &cdevsw, sizeof cdevsw); 63018480Sralph 63118480Sralph if((int)(cdevsw.d_open) == NLVALUE(DZOPEN)) { 63218480Sralph devtype = DZ11; 63318480Sralph unit = minor(dev) / NDZLINE; 63418480Sralph line = minor(dev) % NDZLINE; 63518480Sralph addr = (int) &(((int *)NLVALUE(DZINFO))[unit]); 63623723Sbloom (void)lseek(kmem, (off_t) NLVALUE(NDZ11), 0); 63718480Sralph } else if((int)(cdevsw.d_open) == NLVALUE(DHOPEN)) { 63818480Sralph devtype = DH11; 63918480Sralph unit = minor(dev) / NDHLINE; 64018480Sralph line = minor(dev) % NDHLINE; 64118480Sralph addr = (int) &(((int *)NLVALUE(DHINFO))[unit]); 64223723Sbloom (void)lseek(kmem, (off_t) NLVALUE(NDH11), 0); 64318480Sralph } else if((int)(cdevsw.d_open) == NLVALUE(DMFOPEN)) { 64418480Sralph devtype = DMF; 64518480Sralph unit = minor(dev) / NDMFLINE; 64618480Sralph line = minor(dev) % NDMFLINE; 64718480Sralph addr = (int) &(((int *)NLVALUE(DMFINFO))[unit]); 64823723Sbloom (void)lseek(kmem, (off_t) NLVALUE(NDMF), 0); 64918480Sralph } else { 65023723Sbloom fprintf(stderr, "Device %s (%d/%d) unknown.\n", ttyline, 65123723Sbloom major(dev), minor(dev)); 65218480Sralph return(-1); 65318480Sralph } 65418480Sralph 65523723Sbloom (void)read(kmem, (char *) &nlines, sizeof nlines); 65618480Sralph if(minor(dev) >= nlines) { 65723723Sbloom fprintf(stderr, "Sub-device %d does not exist (only %d).\n", 65823723Sbloom minor(dev), nlines); 65918480Sralph return(-1); 66018480Sralph } 66118480Sralph 66223723Sbloom (void)lseek(kmem, (off_t)addr, 0); 66323723Sbloom (void)read(kmem, (char *) &ubinfo, sizeof ubinfo); 66423723Sbloom (void)lseek(kmem, (off_t) &(ubinfo->ui_flags), 0); 66523723Sbloom (void)read(kmem, (char *) &flags, sizeof flags); 66618480Sralph 66718480Sralph tflags = 1<<line; 66818480Sralph resetmodem = ((flags&tflags) == 0); 66918480Sralph flags = enable ? (flags & ~tflags) : (flags | tflags); 67023723Sbloom (void)lseek(kmem, (off_t) &(ubinfo->ui_flags), 0); 67123723Sbloom (void)write(kmem, (char *) &flags, sizeof flags); 67218480Sralph switch(devtype) { 67318480Sralph case DZ11: 67418480Sralph if((addr = NLVALUE(DZSCAR)) == 0) { 67523723Sbloom fprintf(stderr, "No dzsoftCAR.\n"); 67618480Sralph return(-1); 67718480Sralph } 67818614Sralph cflags = flags; 67923723Sbloom (void)lseek(kmem, (off_t) &(((char *)addr)[unit]), 0); 68023723Sbloom (void)write(kmem, (char *) &cflags, sizeof cflags); 68118480Sralph break; 68218480Sralph case DH11: 68318480Sralph if((addr = NLVALUE(DHSCAR)) == 0) { 68423723Sbloom fprintf(stderr, "No dhsoftCAR.\n"); 68518480Sralph return(-1); 68618480Sralph } 68718614Sralph sflags = flags; 68823723Sbloom (void)lseek(kmem, (off_t) &(((short *)addr)[unit]), 0); 68923723Sbloom (void)write(kmem, (char *) &sflags, sizeof sflags); 69018480Sralph break; 69118480Sralph case DMF: 69218480Sralph if((addr = NLVALUE(DMFSCAR)) == 0) { 69323723Sbloom fprintf(stderr, "No dmfsoftCAR.\n"); 69418480Sralph return(-1); 69518480Sralph } 69618614Sralph cflags = flags; 69723723Sbloom (void)lseek(kmem, (off_t) &(((char *)addr)[unit]), 0); 69823723Sbloom (void)write(kmem, (char *) &flags, sizeof cflags); 69918480Sralph break; 70018480Sralph default: 70123723Sbloom fprintf(stderr, "Unknown device type\n"); 70218480Sralph return(-1); 70318480Sralph } 70418480Sralph return(0); 70518480Sralph } 70618480Sralph 70718480Sralph prefix(s1, s2) 70818480Sralph register char *s1, *s2; 70918480Sralph { 71018480Sralph register char c; 71118480Sralph 71218480Sralph while ((c = *s1++) == *s2++) 71318480Sralph if (c == '\0') 71418480Sralph return (1); 71518480Sralph return (c == '\0'); 71618480Sralph } 717