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