17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*f48205beScasper * Common Development and Distribution License (the "License"). 6*f48205beScasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21462be471Sceastha /* 22*f48205beScasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23462be471Sceastha * Use is subject to license terms. 24462be471Sceastha */ 25462be471Sceastha 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 29462be471Sceastha #pragma ident "%Z%%M% %I% %E% SMI" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * ct [-h] [-v] [-w n] [-x n] [-s speed] telno ... 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * dials the given telephone number, waits for the 367c478bd9Sstevel@tonic-gate * modem to answer, and initiates a login process. 377c478bd9Sstevel@tonic-gate * 387c478bd9Sstevel@tonic-gate * ct uses several routines from uucp: 397c478bd9Sstevel@tonic-gate * - getto(flds) takes a vector of fields needed to make 407c478bd9Sstevel@tonic-gate * a connection and returns a file descriptor or -1 417c478bd9Sstevel@tonic-gate * - rddev( ... ) takes several arguments and returns lines 427c478bd9Sstevel@tonic-gate * from the /etc/uucp/Devices that match the type 437c478bd9Sstevel@tonic-gate * (in ct the type will be ACU) 447c478bd9Sstevel@tonic-gate * - fdig(string) takes a string that is zero or more 457c478bd9Sstevel@tonic-gate * alphabetic characters follow by a number (baud rate) 467c478bd9Sstevel@tonic-gate * and returns a pointer to the first digit in the string. 477c478bd9Sstevel@tonic-gate * - fn_cklock(dev) takes a device name [/dev/]term/11 and 487c478bd9Sstevel@tonic-gate * checks whether the appropriate lock file exists. It returns 497c478bd9Sstevel@tonic-gate * FAIL if it does. 507c478bd9Sstevel@tonic-gate * - rmlock(pointer) removes the lock file. In ct pointer is 517c478bd9Sstevel@tonic-gate * always CNULL (a null pointer) causing rmlock to remove 527c478bd9Sstevel@tonic-gate * all lock files associated with this execution of ct. 537c478bd9Sstevel@tonic-gate */ 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #include "uucp.h" 567c478bd9Sstevel@tonic-gate #include "sysfiles.h" 577c478bd9Sstevel@tonic-gate #include <pwd.h> 587c478bd9Sstevel@tonic-gate #include <utmpx.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #ifdef DATAKIT 617c478bd9Sstevel@tonic-gate #include <dk.h> 627c478bd9Sstevel@tonic-gate extern int dkminor(); 637c478bd9Sstevel@tonic-gate #endif 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate #define ROOT 0 667c478bd9Sstevel@tonic-gate #define SYS 3 677c478bd9Sstevel@tonic-gate #define TTYGID (gid_t) 7 /* group id for terminal */ 687c478bd9Sstevel@tonic-gate #define TTYMOD (mode_t) 0622 697c478bd9Sstevel@tonic-gate #define DEV "/dev/" 707c478bd9Sstevel@tonic-gate #define TELNOSIZE 32 /* maximum phone # size is 31 */ 717c478bd9Sstevel@tonic-gate #define LEGAL "0123456789-*#=" 727c478bd9Sstevel@tonic-gate #define USAGE "[-h] [-v] [-w n] [-x n] [-s speed] telno ..." 737c478bd9Sstevel@tonic-gate #define LOG "/var/adm/ctlog" 747c478bd9Sstevel@tonic-gate #define TTYMON "/usr/lib/saf/ttymon" 757c478bd9Sstevel@tonic-gate #define TRUE 1 767c478bd9Sstevel@tonic-gate #define FALSE 0 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate static 797c478bd9Sstevel@tonic-gate int _Status; /* exit status of child */ 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate static 827c478bd9Sstevel@tonic-gate pid_t _Pid = 0; /* process id of child */ 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate static 857c478bd9Sstevel@tonic-gate char 867c478bd9Sstevel@tonic-gate _Tty[sizeof DEV+12] = "", /* /dev/term/xx for connection device */ 877c478bd9Sstevel@tonic-gate *_Dev[D_MAX + 1], /* Filled in by rddev and used globally */ 887c478bd9Sstevel@tonic-gate _Devbuf[BUFSIZ]; /* buffer for rddev */ 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate static 917c478bd9Sstevel@tonic-gate char 927c478bd9Sstevel@tonic-gate *_Num, /* pointer to a phone number */ 937c478bd9Sstevel@tonic-gate *_Flds[7]; /* Filled in as if finds() in uucp did it */ 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate static 967c478bd9Sstevel@tonic-gate time_t _Log_on, 977c478bd9Sstevel@tonic-gate _Log_elpsd; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate static 1007c478bd9Sstevel@tonic-gate FILE *_Fdl; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate extern int optind; 1037c478bd9Sstevel@tonic-gate extern char *optarg, *fdig(); 1047c478bd9Sstevel@tonic-gate extern void cleanup(); 1057c478bd9Sstevel@tonic-gate extern struct passwd *getpwuid (); 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate extern int getto(), rddev(); 1087c478bd9Sstevel@tonic-gate static int gdev(), logproc(), exists(); 1097c478bd9Sstevel@tonic-gate static void startat(), stopat(), disconnect(), zero(); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* 1127c478bd9Sstevel@tonic-gate * These two dummy routines are needed because the uucp routines 1137c478bd9Sstevel@tonic-gate * used by ct reference them, but they will never be 1147c478bd9Sstevel@tonic-gate * called when executing from ct 1157c478bd9Sstevel@tonic-gate */ 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /*VARARGS*/ 1187c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1197c478bd9Sstevel@tonic-gate void 1207c478bd9Sstevel@tonic-gate assert (s1, s2, i1, s3, i2) 1217c478bd9Sstevel@tonic-gate char *s1, *s2, *s3; 1227c478bd9Sstevel@tonic-gate int i1, i2; 1237c478bd9Sstevel@tonic-gate { } /* for ASSERT in gnamef.c */ 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1267c478bd9Sstevel@tonic-gate void 1277c478bd9Sstevel@tonic-gate logent (s1, s2) 1287c478bd9Sstevel@tonic-gate char *s1, *s2; 1297c478bd9Sstevel@tonic-gate { } /* so we can load ulockf() */ 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate jmp_buf Sjbuf; /* used by uucp routines */ 1327c478bd9Sstevel@tonic-gate 133462be471Sceastha int 1347c478bd9Sstevel@tonic-gate main (argc, argv) 135462be471Sceastha int argc; 1367c478bd9Sstevel@tonic-gate char *argv[]; 1377c478bd9Sstevel@tonic-gate { 138462be471Sceastha int c; 1397c478bd9Sstevel@tonic-gate int found = 0, 1407c478bd9Sstevel@tonic-gate errors = 0, 1417c478bd9Sstevel@tonic-gate first = TRUE; 1427c478bd9Sstevel@tonic-gate int count, 1437c478bd9Sstevel@tonic-gate logprocflag, /* is there a login process on the line */ 1447c478bd9Sstevel@tonic-gate hangup = 1, /* hangup by default */ 1457c478bd9Sstevel@tonic-gate minutes = 0; /* number of minutes to wait for dialer */ 1467c478bd9Sstevel@tonic-gate int fdl; 1477c478bd9Sstevel@tonic-gate struct termio termio; 1487c478bd9Sstevel@tonic-gate typedef void (*save_sig)(); 1497c478bd9Sstevel@tonic-gate save_sig save_hup, 1507c478bd9Sstevel@tonic-gate save_quit, 1517c478bd9Sstevel@tonic-gate save_int; 1527c478bd9Sstevel@tonic-gate extern void setservice(), devreset(); 1537c478bd9Sstevel@tonic-gate extern int sysaccess(); 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate save_hup = signal (SIGHUP, cleanup); 1567c478bd9Sstevel@tonic-gate save_quit = signal (SIGQUIT, cleanup); 1577c478bd9Sstevel@tonic-gate save_int = signal (SIGINT, cleanup); 1587c478bd9Sstevel@tonic-gate (void) signal (SIGTERM, cleanup); 1597c478bd9Sstevel@tonic-gate (void) strcpy (Progname, "ct"); 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate setservice("cu"); 1627c478bd9Sstevel@tonic-gate if ( sysaccess(EACCESS_DEVICES) != 0 ) { 1637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ct: can't access Devices file\n"); 1647c478bd9Sstevel@tonic-gate cleanup(101); 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* Set up the _Flds vector as if finds() [from uucico] built it */ 1687c478bd9Sstevel@tonic-gate _Flds[F_NAME] = "dummy"; /* never used */ 1697c478bd9Sstevel@tonic-gate _Flds[F_TIME] = "Any"; /* never used */ 1707c478bd9Sstevel@tonic-gate _Flds[F_TYPE] = "ACU"; 1717c478bd9Sstevel@tonic-gate _Flds[F_CLASS] = "1200"; /* default at 1200 */ 1727c478bd9Sstevel@tonic-gate _Flds[F_PHONE] = ""; /* filled in by arguments */ 1737c478bd9Sstevel@tonic-gate _Flds[F_LOGIN] = ""; /* never used */ 1747c478bd9Sstevel@tonic-gate _Flds[6] = NULL; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate while ((c = getopt (argc, argv, "hvw:s:x:")) != EOF) { 1777c478bd9Sstevel@tonic-gate switch (c) { 1787c478bd9Sstevel@tonic-gate case 'h': 1797c478bd9Sstevel@tonic-gate hangup = 0; 1807c478bd9Sstevel@tonic-gate break; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate case 'v': 1837c478bd9Sstevel@tonic-gate Verbose = 1; 1847c478bd9Sstevel@tonic-gate break; 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate case 'w': 1877c478bd9Sstevel@tonic-gate minutes = atoi (optarg); 1887c478bd9Sstevel@tonic-gate if (minutes < 1) { 1897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1907c478bd9Sstevel@tonic-gate "\tusage: %s %s\n", Progname, USAGE); 1917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "(-w %s) Wait time must be > 0\n", 1927c478bd9Sstevel@tonic-gate optarg); 1937c478bd9Sstevel@tonic-gate cleanup(101); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate break; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate case 's': 1987c478bd9Sstevel@tonic-gate _Flds[F_CLASS] = optarg; 1997c478bd9Sstevel@tonic-gate break; 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate case 'x': 2027c478bd9Sstevel@tonic-gate Debug = atoi(optarg); 2037c478bd9Sstevel@tonic-gate if (Debug < 0 || Debug > 9) { 2047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2057c478bd9Sstevel@tonic-gate "\tusage: %s %s\n", Progname, USAGE); 2067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "(-x %s) value must be 0-9\n", 2077c478bd9Sstevel@tonic-gate optarg); 2087c478bd9Sstevel@tonic-gate cleanup(101); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate break; 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate case '?': 2137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\tusage: %s %s\n", Progname, USAGE); 2147c478bd9Sstevel@tonic-gate cleanup(101); 2157c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate if (optind == argc) { 2207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\tusage: %s %s\n", Progname, USAGE); 2217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "No phone numbers specified!\n"); 2227c478bd9Sstevel@tonic-gate cleanup(101); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /* check for valid phone number(s) */ 2267c478bd9Sstevel@tonic-gate for (count = argc - 1; count >= optind; --count) { 2277c478bd9Sstevel@tonic-gate _Num = argv[count]; 2287c478bd9Sstevel@tonic-gate if (strlen(_Num) >= (size_t)(TELNOSIZE - 1)) { 2297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ct: phone number too long -- %s\n", _Num); 2307c478bd9Sstevel@tonic-gate ++errors; 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate if ((int)strspn(_Num, LEGAL) < (int)strlen(_Num)) { 2337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ct: bad phone number -- %s\n", _Num); 2347c478bd9Sstevel@tonic-gate ++errors; 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate if (errors) 2387c478bd9Sstevel@tonic-gate cleanup(101); 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate /************************************************************/ 2417c478bd9Sstevel@tonic-gate /* Begin Loop: Find an available Dialer */ 2427c478bd9Sstevel@tonic-gate /************************************************************/ 2437c478bd9Sstevel@tonic-gate for (count = 0;; count++) { /* count will be wait time after first 2447c478bd9Sstevel@tonic-gate * time through the loop. 2457c478bd9Sstevel@tonic-gate * break will be used exit loop. 2467c478bd9Sstevel@tonic-gate */ 2477c478bd9Sstevel@tonic-gate if ( (found = gdev (_Flds)) > 0) { /* found a dialer */ 2487c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "Allocated dialer at %s baud\n", 2497c478bd9Sstevel@tonic-gate _Flds[F_CLASS]); 2507c478bd9Sstevel@tonic-gate break; 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate else if (found == 0) { /* no dialers of that on system */ 2537c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "No %s dialers on this system\n", 2547c478bd9Sstevel@tonic-gate fdig(_Flds[F_CLASS]) ); 2557c478bd9Sstevel@tonic-gate cleanup(101); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate if (!first) { /* not the first time in loop */ 2597c478bd9Sstevel@tonic-gate VERBOSE("%s busy", (found == -1) ? "Dialer is" : "Dialers are"); 2607c478bd9Sstevel@tonic-gate VERBOSE(" (%d minute(s))\n", count); 2617c478bd9Sstevel@tonic-gate if (count < minutes) { 2627c478bd9Sstevel@tonic-gate sleep(60); 2637c478bd9Sstevel@tonic-gate continue; 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate /* This is the end of the loop - no time left */ 2667c478bd9Sstevel@tonic-gate break; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /**************************************************************/ 2707c478bd9Sstevel@tonic-gate /* First time through loop - get wait minutes if no -w option */ 2717c478bd9Sstevel@tonic-gate /**************************************************************/ 2727c478bd9Sstevel@tonic-gate first = FALSE; 2737c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "The (%d) %s dialer%s busy\n", -found, 2747c478bd9Sstevel@tonic-gate _Flds[F_CLASS], (found == -1 ? " is" : "s are")); 2757c478bd9Sstevel@tonic-gate if (minutes) { /* -w already set wait minutes */ 2767c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "Waiting for %d minute%s\n", minutes, 2777c478bd9Sstevel@tonic-gate (minutes > 1 ? "s" : "") ); 2787c478bd9Sstevel@tonic-gate sleep(60); 2797c478bd9Sstevel@tonic-gate continue; 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate if (!isatty(0) ) { /* not a terminal - get out */ 2837c478bd9Sstevel@tonic-gate cleanup(101); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* Ask user if she/he wants to wait */ 2877c478bd9Sstevel@tonic-gate (void) fputs("Do you want to wait for dialer? (y for yes): ", stdout); 2887c478bd9Sstevel@tonic-gate if ((c = getchar ()) == EOF || tolower (c) != 'y') 2897c478bd9Sstevel@tonic-gate cleanup(101); 2907c478bd9Sstevel@tonic-gate while ( (c = getchar()) != EOF && c != '\n') 2917c478bd9Sstevel@tonic-gate ; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate (void) fputs ("Time, in minutes? ", stdout); 2947c478bd9Sstevel@tonic-gate (void) scanf ("%d", &minutes); 2957c478bd9Sstevel@tonic-gate while ( (c = getchar()) != EOF && c != '\n') 2967c478bd9Sstevel@tonic-gate ; 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate if (minutes <= 0) 2997c478bd9Sstevel@tonic-gate cleanup(101); 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate (void) fputs ("Waiting for dialer\n", stdout); 3027c478bd9Sstevel@tonic-gate sleep(60); 3037c478bd9Sstevel@tonic-gate continue; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate /************************************************************/ 3077c478bd9Sstevel@tonic-gate /* End Loop: Find an available Dialer */ 3087c478bd9Sstevel@tonic-gate /************************************************************/ 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate /* check why loop terminated */ 3117c478bd9Sstevel@tonic-gate if (found < 0) { /* no dialer found - get out */ 3127c478bd9Sstevel@tonic-gate (void) fputs("*** TIMEOUT ***\n", stdout); 3137c478bd9Sstevel@tonic-gate cleanup(101); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); 3177c478bd9Sstevel@tonic-gate /* found a dialer. now try to call */ 3187c478bd9Sstevel@tonic-gate if (!isatty(0)) 3197c478bd9Sstevel@tonic-gate hangup = 0; 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate if (hangup) { /* -h option not specified */ 3227c478bd9Sstevel@tonic-gate do { 3237c478bd9Sstevel@tonic-gate (void) fputs ("Confirm hang-up? (y/n): ", stdout); 3247c478bd9Sstevel@tonic-gate switch (c=tolower(getchar())) { 3257c478bd9Sstevel@tonic-gate case EOF: 3267c478bd9Sstevel@tonic-gate case 'n': 3277c478bd9Sstevel@tonic-gate cleanup(101); 3287c478bd9Sstevel@tonic-gate break; 3297c478bd9Sstevel@tonic-gate case 'y': 3307c478bd9Sstevel@tonic-gate break; 3317c478bd9Sstevel@tonic-gate default: 3327c478bd9Sstevel@tonic-gate while ( c != EOF && c != '\n' ) 3337c478bd9Sstevel@tonic-gate c=getchar(); 3347c478bd9Sstevel@tonic-gate break; 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate } while (c != 'y'); 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate /* close stderr if it is not redirected */ 3397c478bd9Sstevel@tonic-gate if ( isatty(2) ) { 3407c478bd9Sstevel@tonic-gate Verbose = 0; 3417c478bd9Sstevel@tonic-gate Debug = 0; 3427c478bd9Sstevel@tonic-gate (void) close (2); 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate (void) ioctl (0, TCGETA, &termio); 3467c478bd9Sstevel@tonic-gate termio.c_cflag = 0; /* speed to zero for hangup */ 3477c478bd9Sstevel@tonic-gate (void) ioctl (0, TCSETAW, &termio); /* hang up terminal */ 3487c478bd9Sstevel@tonic-gate (void) sleep (5); 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate (void) close(0); 3517c478bd9Sstevel@tonic-gate (void) close(1); 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /* Try each phone number until a connection is made, or non work */ 3547c478bd9Sstevel@tonic-gate for (count = optind; count < argc; count++) { 3557c478bd9Sstevel@tonic-gate /* call getto routine to make connection */ 3567c478bd9Sstevel@tonic-gate _Flds[F_PHONE] = argv[count]; 3577c478bd9Sstevel@tonic-gate rmlock(CNULL); /* remove temporary lock set by gdev */ 3587c478bd9Sstevel@tonic-gate devreset(); 3597c478bd9Sstevel@tonic-gate fdl = getto(_Flds); 3607c478bd9Sstevel@tonic-gate if (fdl >= 0) { 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * If there is a login process on the line, get rid 3637c478bd9Sstevel@tonic-gate * of the lock file quickly so that when the process 3647c478bd9Sstevel@tonic-gate * reads the first character, the lock file will be gone 3657c478bd9Sstevel@tonic-gate * indicating that the process should handle the data. 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate if ( (logprocflag = logproc(Dc)) ) /* really an assignment! */ 3687c478bd9Sstevel@tonic-gate rmlock(CNULL); 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate _Fdl = fdopen(fdl, "r+"); 3717c478bd9Sstevel@tonic-gate (void) sprintf(_Tty, "%s%s", DEV, Dc); 3727c478bd9Sstevel@tonic-gate /* NOTE: Dc is set in the caller routines */ 3737c478bd9Sstevel@tonic-gate break; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate /* check why the loop ended (connected or no more numbers to try) */ 3787c478bd9Sstevel@tonic-gate if (count == argc) 3797c478bd9Sstevel@tonic-gate cleanup(101); 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate /****** Successfully made connection ******/ 3827c478bd9Sstevel@tonic-gate VERBOSE("Connected\n%s", ""); 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate #ifdef DATAKIT 3857c478bd9Sstevel@tonic-gate if (!strcmp(_Dev[D_CALLER], "DK")) { 3867c478bd9Sstevel@tonic-gate strcpy(_Tty, dtnamer(dkminor(fdl))); 3877c478bd9Sstevel@tonic-gate strcpy(Dc, (strrchr(_Tty, '/')+1)); 3887c478bd9Sstevel@tonic-gate if ((_Fdl = fopen(_Tty, "r+")) == NULL) { 3897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ct: Cannot open %s, errno %d\n", 3907c478bd9Sstevel@tonic-gate _Tty, errno); 3917c478bd9Sstevel@tonic-gate cleanup(101); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate #endif 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate /* ignore some signals if they were ignored upon invocation of ct */ 3977c478bd9Sstevel@tonic-gate /* or else, have them go to graceful disconnect */ 3987c478bd9Sstevel@tonic-gate if (save_hup == SIG_IGN) 3997c478bd9Sstevel@tonic-gate (void) signal (SIGHUP, SIG_IGN); 4007c478bd9Sstevel@tonic-gate else 4017c478bd9Sstevel@tonic-gate (void) signal (SIGHUP, disconnect); 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate if (save_quit == SIG_IGN) 4047c478bd9Sstevel@tonic-gate (void) signal (SIGQUIT, SIG_IGN); 4057c478bd9Sstevel@tonic-gate else 4067c478bd9Sstevel@tonic-gate (void) signal (SIGQUIT, disconnect); 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate if (save_int == SIG_IGN) 4097c478bd9Sstevel@tonic-gate (void) signal (SIGINT, SIG_IGN); 4107c478bd9Sstevel@tonic-gate else 4117c478bd9Sstevel@tonic-gate (void) signal (SIGINT, disconnect); 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate (void) signal (SIGTERM, disconnect); 4147c478bd9Sstevel@tonic-gate (void) signal (SIGALRM, disconnect); 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate (void) sleep (2); /* time for phone line/modem to settle */ 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate _Log_on = time ((time_t *) 0); 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate /* 4217c478bd9Sstevel@tonic-gate * if there is a login process on this line, 4227c478bd9Sstevel@tonic-gate * tell the user to hit a carriage return to make 4237c478bd9Sstevel@tonic-gate * the waiting process get past the inital read, 4247c478bd9Sstevel@tonic-gate * Then exit. 4257c478bd9Sstevel@tonic-gate */ 4267c478bd9Sstevel@tonic-gate if (logprocflag) { /* there is a login process on the line */ 4277c478bd9Sstevel@tonic-gate (void) fputs("Hit carriage return ", _Fdl); 4287c478bd9Sstevel@tonic-gate (void) fclose(_Fdl); 4297c478bd9Sstevel@tonic-gate CDEBUG(4, "there is a login process; exit\n%s", ""); 4307c478bd9Sstevel@tonic-gate exit(0); 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate CDEBUG(4, "start login process (%s ", TTYMON); 4347c478bd9Sstevel@tonic-gate CDEBUG(4, "-g -h -t 60 -l %s)\n", fdig(_Flds[F_CLASS])); 4357c478bd9Sstevel@tonic-gate for (;;) { 4367c478bd9Sstevel@tonic-gate pid_t w_ret; 4377c478bd9Sstevel@tonic-gate switch(_Pid = fork()) { 4387c478bd9Sstevel@tonic-gate case -1: /* fork failed */ 4397c478bd9Sstevel@tonic-gate if ((!hangup || Verbose)) 4407c478bd9Sstevel@tonic-gate (void) fputs ("ct: can't fork for login process\n", stderr); 4417c478bd9Sstevel@tonic-gate cleanup(101); 4427c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate case 0: /* child process */ 4457c478bd9Sstevel@tonic-gate startat (); 4467c478bd9Sstevel@tonic-gate (void) close(2); 4477c478bd9Sstevel@tonic-gate /* ttymon will use open fd 0 for connection */ 4487c478bd9Sstevel@tonic-gate if ( fdl != 0 ) { 4497c478bd9Sstevel@tonic-gate (void) close(0); 4507c478bd9Sstevel@tonic-gate dup(fdl); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_DFL); /* so child will exit on hangup */ 4537c478bd9Sstevel@tonic-gate (void) execl(TTYMON, "ttymon", "-g", "-h", "-t", "60", 4547c478bd9Sstevel@tonic-gate "-l", fdig(_Flds[F_CLASS]), (char *) 0); 4557c478bd9Sstevel@tonic-gate /* exec failed */ 4567c478bd9Sstevel@tonic-gate cleanup(101); 4577c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate default: /* parent process */ 4607c478bd9Sstevel@tonic-gate break; 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate /* Parent process */ 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate while ((w_ret = wait(&_Status)) != _Pid) 4667c478bd9Sstevel@tonic-gate if (w_ret == -1 && errno != EINTR) { 4677c478bd9Sstevel@tonic-gate VERBOSE("ct: wait failed errno=%d\n", errno); 4687c478bd9Sstevel@tonic-gate cleanup(101); 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate if ((_Status & 0xff00) < 0) { 4717c478bd9Sstevel@tonic-gate if (!hangup) 4727c478bd9Sstevel@tonic-gate VERBOSE("ct: can't exec login process\n%s", ""); 4737c478bd9Sstevel@tonic-gate cleanup(101); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate stopat(_Flds[F_PHONE]); 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate rewind (_Fdl); /* flush line */ 4797c478bd9Sstevel@tonic-gate (void) fputs ("\nReconnect? ", _Fdl); 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate rewind (_Fdl); 4827c478bd9Sstevel@tonic-gate (void) alarm (20); 4837c478bd9Sstevel@tonic-gate c = getc (_Fdl); 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate if (c == EOF || tolower (c) == 'n') 4867c478bd9Sstevel@tonic-gate disconnect (0); /* normal disconnect */ 4877c478bd9Sstevel@tonic-gate while ( (c = getc(_Fdl)) != EOF && c != '\n') 4887c478bd9Sstevel@tonic-gate ; 4897c478bd9Sstevel@tonic-gate (void) alarm (0); 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate static void 4947c478bd9Sstevel@tonic-gate disconnect (code) 4957c478bd9Sstevel@tonic-gate { 4967c478bd9Sstevel@tonic-gate struct termio termio; 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate (void) alarm(0); 4997c478bd9Sstevel@tonic-gate (void) signal (SIGALRM, SIG_IGN); 5007c478bd9Sstevel@tonic-gate (void) signal (SIGINT, SIG_IGN); 5017c478bd9Sstevel@tonic-gate (void) signal (SIGTERM, SIG_IGN); 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate _Log_elpsd = time ((time_t *) 0) - _Log_on; 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate (void) ioctl (fileno(_Fdl), TCGETA, &termio); 5067c478bd9Sstevel@tonic-gate termio.c_cflag = 0; /* speed to zero for hangup */ 5077c478bd9Sstevel@tonic-gate (void) ioctl (fileno(_Fdl), TCSETAW, &termio); /* hang up terminal */ 5087c478bd9Sstevel@tonic-gate (void) fclose (_Fdl); 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate DEBUG(5, "Disconnect(%d)\n", code); 5117c478bd9Sstevel@tonic-gate VERBOSE("Disconnected\n%s", ""); 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate /* For normal disconnect or timeout on "Reconnect?" message, 5147c478bd9Sstevel@tonic-gate we already cleaned up above */ 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate if ((code != 0) && (code != SIGALRM)) 5177c478bd9Sstevel@tonic-gate stopat(_Flds[F_PHONE]); 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate cleanup(code); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate /* 5237c478bd9Sstevel@tonic-gate * clean and exit with "code" status 5247c478bd9Sstevel@tonic-gate */ 5257c478bd9Sstevel@tonic-gate void 5267c478bd9Sstevel@tonic-gate cleanup (code) 527462be471Sceastha int code; 5287c478bd9Sstevel@tonic-gate { 5297c478bd9Sstevel@tonic-gate CDEBUG(5, "cleanup(%d)\n", code); 5307c478bd9Sstevel@tonic-gate rmlock (CNULL); 5317c478bd9Sstevel@tonic-gate if (*_Tty != '\0') { 5327c478bd9Sstevel@tonic-gate CDEBUG(5, "chmod/chown %s\n", _Tty); 5337c478bd9Sstevel@tonic-gate if (chown(_Tty , UUCPUID, TTYGID) < 0 ) { 534*f48205beScasper CDEBUG(5, "Can't chown to uid=%u, ", UUCPUID); 535*f48205beScasper CDEBUG(5, "gid=%u\n", TTYGID); 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate if (chmod(_Tty , TTYMOD) < 0) { 5387c478bd9Sstevel@tonic-gate CDEBUG(5, "Can't chmod to %lo\n", (unsigned long) TTYMOD); 5397c478bd9Sstevel@tonic-gate } 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate if (_Pid) { /* kill the child process */ 5427c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); 5437c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, SIG_IGN); 5447c478bd9Sstevel@tonic-gate (void) kill (_Pid, SIGKILL); 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate exit (code); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate /* gdev() 5507c478bd9Sstevel@tonic-gate * Find an available line with a dialer on it. 5517c478bd9Sstevel@tonic-gate * Set a temporary lock file for the line. 5527c478bd9Sstevel@tonic-gate * Return: 5537c478bd9Sstevel@tonic-gate * >0 - got a dialer 5547c478bd9Sstevel@tonic-gate * <0 - failed - return the number of possible dialers 5557c478bd9Sstevel@tonic-gate * 0 - not dialers of requested class on the system. 5567c478bd9Sstevel@tonic-gate */ 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate static int 5597c478bd9Sstevel@tonic-gate gdev (flds) 5607c478bd9Sstevel@tonic-gate char *flds[]; 5617c478bd9Sstevel@tonic-gate { 5627c478bd9Sstevel@tonic-gate int count = 0; 5637c478bd9Sstevel@tonic-gate extern void devreset(); 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate devreset(); 5667c478bd9Sstevel@tonic-gate while (rddev ("ACU", _Dev, _Devbuf, D_MAX) != FAIL) { 5677c478bd9Sstevel@tonic-gate /* check caller type */ 5687c478bd9Sstevel@tonic-gate if (!EQUALS (flds[F_TYPE] /* "ACU" */, _Dev[D_TYPE])) 5697c478bd9Sstevel@tonic-gate continue; 5707c478bd9Sstevel@tonic-gate /* check class, check (and possibly set) speed */ 5717c478bd9Sstevel@tonic-gate if (!EQUALS (flds[F_CLASS] /* speed */, _Dev[D_CLASS])) 5727c478bd9Sstevel@tonic-gate continue; 5737c478bd9Sstevel@tonic-gate count++; 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate if (fn_cklock(_Dev[D_LINE]) == FAIL) 5767c478bd9Sstevel@tonic-gate continue; 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate /* found available dialer and set temporary lock */ 5797c478bd9Sstevel@tonic-gate return (count); 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate return (- count); 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate /* 5867c478bd9Sstevel@tonic-gate * Check if there is a login process active on this line. 5877c478bd9Sstevel@tonic-gate * Return: 5887c478bd9Sstevel@tonic-gate * 0 - there is no login process on this line 5897c478bd9Sstevel@tonic-gate * 1 - found a login process on this line 5907c478bd9Sstevel@tonic-gate */ 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate static int 5937c478bd9Sstevel@tonic-gate logproc(line) 5947c478bd9Sstevel@tonic-gate char *line; 5957c478bd9Sstevel@tonic-gate { 5967c478bd9Sstevel@tonic-gate struct utmpx *u; 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate while ((u = getutxent()) != NULL) { 5997c478bd9Sstevel@tonic-gate if (u->ut_type == LOGIN_PROCESS 6007c478bd9Sstevel@tonic-gate && EQUALS(u->ut_line, line) 6017c478bd9Sstevel@tonic-gate && EQUALS(u->ut_user, "LOGIN") ) { 6027c478bd9Sstevel@tonic-gate CDEBUG(7, "ut_line %s, ", u->ut_line); 6037c478bd9Sstevel@tonic-gate CDEBUG(7, "ut_user %s, ", u->ut_user); 6047c478bd9Sstevel@tonic-gate CDEBUG(7, "ut_id %.4s, ", u->ut_id); 6057c478bd9Sstevel@tonic-gate CDEBUG(7, "ut_pid %d\n", u->ut_pid); 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate /* see if the process is still active */ 6087c478bd9Sstevel@tonic-gate if (kill(u->ut_pid, 0) == 0 || errno == EPERM) { 6097c478bd9Sstevel@tonic-gate CDEBUG(4, "process still active\n%s", ""); 6107c478bd9Sstevel@tonic-gate return(1); 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate } 6137c478bd9Sstevel@tonic-gate } 6147c478bd9Sstevel@tonic-gate return(0); 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate /* 6187c478bd9Sstevel@tonic-gate * Create an entry in utmpx file if one does not already exist. 6197c478bd9Sstevel@tonic-gate */ 6207c478bd9Sstevel@tonic-gate static void 6217c478bd9Sstevel@tonic-gate startat () 6227c478bd9Sstevel@tonic-gate { 6237c478bd9Sstevel@tonic-gate struct utmpx utmpxbuf, *u; 6247c478bd9Sstevel@tonic-gate int fd; 6257c478bd9Sstevel@tonic-gate 6267c478bd9Sstevel@tonic-gate /* Set up the prototype for the utmpx structure we want to write. */ 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate u = &utmpxbuf; 6297c478bd9Sstevel@tonic-gate zero (&u -> ut_user[0], sizeof (u -> ut_user)); 6307c478bd9Sstevel@tonic-gate zero (&u -> ut_line[0], sizeof (u -> ut_line)); 6317c478bd9Sstevel@tonic-gate 6327c478bd9Sstevel@tonic-gate /* Fill in the various fields of the utmpx structure. */ 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate u -> ut_id[0] = 'c'; 6357c478bd9Sstevel@tonic-gate u -> ut_id[1] = 't'; 6367c478bd9Sstevel@tonic-gate u -> ut_id[2] = _Tty[strlen(_Tty)-2]; 6377c478bd9Sstevel@tonic-gate u -> ut_id[3] = _Tty[strlen(_Tty)-1]; 6387c478bd9Sstevel@tonic-gate u -> ut_pid = getpid (); 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate u -> ut_exit.e_termination = 0; 6417c478bd9Sstevel@tonic-gate u -> ut_exit.e_exit = 0; 6427c478bd9Sstevel@tonic-gate u -> ut_type = INIT_PROCESS; 6437c478bd9Sstevel@tonic-gate time (&u -> ut_xtime); 6447c478bd9Sstevel@tonic-gate setutxent (); /* Start at beginning of utmpx file. */ 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate /* For INIT_PROCESSes put in the name of the program in the */ 6477c478bd9Sstevel@tonic-gate /* "ut_user" field. */ 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate strncpy (&u -> ut_user[0], "ttymon", sizeof (u -> ut_user)); 6507c478bd9Sstevel@tonic-gate strncpy (&u -> ut_line[0], Dc, sizeof (u -> ut_line)); 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate /* Write out the updated entry to utmpx file. */ 6537c478bd9Sstevel@tonic-gate pututxline (u); 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate /* Now attempt to add to the end of the wtmpx file. Do not create */ 6567c478bd9Sstevel@tonic-gate /* if it doesn't already exist. Do not overwrite any info already */ 6577c478bd9Sstevel@tonic-gate /* in file. */ 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate if ((fd = open(WTMPX_FILE, O_WRONLY | O_APPEND)) != -1) { 6607c478bd9Sstevel@tonic-gate (void) write(fd, u, sizeof(*u)); 6617c478bd9Sstevel@tonic-gate (void) close(fd); 6627c478bd9Sstevel@tonic-gate } 6637c478bd9Sstevel@tonic-gate endutxent (); 6647c478bd9Sstevel@tonic-gate return; 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate /* 6687c478bd9Sstevel@tonic-gate * Change utmpx file entry to "dead". 6697c478bd9Sstevel@tonic-gate * Make entry in ct log. 6707c478bd9Sstevel@tonic-gate */ 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate static void 6737c478bd9Sstevel@tonic-gate stopat (num) 6747c478bd9Sstevel@tonic-gate char *num; 6757c478bd9Sstevel@tonic-gate { 6767c478bd9Sstevel@tonic-gate struct utmpx utmpxbuf, *u; 6777c478bd9Sstevel@tonic-gate int fd; 6787c478bd9Sstevel@tonic-gate FILE * fp; 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate /* Set up the prototype for the utmpx structure we want to write. */ 6817c478bd9Sstevel@tonic-gate 6827c478bd9Sstevel@tonic-gate setutxent(); 6837c478bd9Sstevel@tonic-gate u = &utmpxbuf; 6847c478bd9Sstevel@tonic-gate zero (&u -> ut_user[0], sizeof (u -> ut_user)); 6857c478bd9Sstevel@tonic-gate zero (&u -> ut_line[0], sizeof (u -> ut_line)); 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate /* Fill in the various fields of the utmpx structure. */ 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate u -> ut_id[0] = 'c'; 6907c478bd9Sstevel@tonic-gate u -> ut_id[1] = 't'; 6917c478bd9Sstevel@tonic-gate u -> ut_id[2] = _Tty[strlen(_Tty)-2]; 6927c478bd9Sstevel@tonic-gate u -> ut_id[3] = _Tty[strlen(_Tty)-1]; 6937c478bd9Sstevel@tonic-gate u -> ut_pid = (pid_t) _Pid; 6947c478bd9Sstevel@tonic-gate u -> ut_type = USER_PROCESS; 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate /* Find the old entry in the utmpx file with the user name and */ 6977c478bd9Sstevel@tonic-gate /* copy it back. */ 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate if (u = getutxid (u)) { 7007c478bd9Sstevel@tonic-gate utmpxbuf = *u; 7017c478bd9Sstevel@tonic-gate u = &utmpxbuf; 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate 7047c478bd9Sstevel@tonic-gate u -> ut_exit.e_termination = _Status & 0xff; 7057c478bd9Sstevel@tonic-gate u -> ut_exit.e_exit = (_Status >> 8) & 0xff; 7067c478bd9Sstevel@tonic-gate u -> ut_type = DEAD_PROCESS; 7077c478bd9Sstevel@tonic-gate time (&u -> ut_xtime); 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate /* Write out the updated entry to utmpx file. */ 7107c478bd9Sstevel@tonic-gate 7117c478bd9Sstevel@tonic-gate pututxline (u); 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate /* Now attempt to add to the end of the wtmpx file. Do not create */ 7147c478bd9Sstevel@tonic-gate /* if it doesn't already exist. Do not overwrite any info already */ 7157c478bd9Sstevel@tonic-gate /* in file. */ 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate if ((fd = open(WTMPX_FILE, O_WRONLY | O_APPEND)) != -1) { 7187c478bd9Sstevel@tonic-gate (void) write(fd, u, sizeof(*u)); 7197c478bd9Sstevel@tonic-gate (void) close(fd); 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate endutxent (); 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate /* Do the log accounting */ 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate if (exists (LOG) && (fp = fopen (LOG, "a")) != NULL) { 7267c478bd9Sstevel@tonic-gate char *aptr; 7277c478bd9Sstevel@tonic-gate int hrs, 7287c478bd9Sstevel@tonic-gate mins, 7297c478bd9Sstevel@tonic-gate secs; 7307c478bd9Sstevel@tonic-gate 7317c478bd9Sstevel@tonic-gate /* ignore user set TZ for logfile purposes */ 7327c478bd9Sstevel@tonic-gate if ( (aptr = getenv ("TZ")) != NULL ) 7337c478bd9Sstevel@tonic-gate *aptr = '\0'; 7347c478bd9Sstevel@tonic-gate 7357c478bd9Sstevel@tonic-gate (aptr = ctime (&_Log_on))[16] = '\0'; 7367c478bd9Sstevel@tonic-gate hrs = _Log_elpsd / 3600; 7377c478bd9Sstevel@tonic-gate mins = (_Log_elpsd %= 3600) / 60; 7387c478bd9Sstevel@tonic-gate secs = _Log_elpsd % 60; 7397c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%-8s ", getpwuid (getuid ()) -> pw_name); 7407c478bd9Sstevel@tonic-gate (void) fprintf(fp, "(%4s) %s ", fdig(_Flds[F_CLASS]), aptr); 7417c478bd9Sstevel@tonic-gate if (hrs) 7427c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%2d:%.2d", hrs, mins); 7437c478bd9Sstevel@tonic-gate else 7447c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %2d", mins); 7457c478bd9Sstevel@tonic-gate (void) fprintf(fp, ":%.2d %s\n", secs, num); 7467c478bd9Sstevel@tonic-gate (void) fclose (fp); 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate return; 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate static int 7527c478bd9Sstevel@tonic-gate exists (file) 7537c478bd9Sstevel@tonic-gate char *file; 7547c478bd9Sstevel@tonic-gate { 7557c478bd9Sstevel@tonic-gate struct stat statb; 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate if (stat (file, &statb) == -1 && errno == ENOENT) 7587c478bd9Sstevel@tonic-gate return (0); 7597c478bd9Sstevel@tonic-gate return (1); 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate static void 7637c478bd9Sstevel@tonic-gate zero (adr, size) 764462be471Sceastha char *adr; 765462be471Sceastha int size; 7667c478bd9Sstevel@tonic-gate { 7677c478bd9Sstevel@tonic-gate while (size--) 7687c478bd9Sstevel@tonic-gate *adr++ = '\0'; 7697c478bd9Sstevel@tonic-gate return; 7707c478bd9Sstevel@tonic-gate } 771