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