/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <stdio_ext.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include <string.h>
#include <signal.h>
#include <sys/resource.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <ulimit.h>
#include "sac.h"
#include "ttymon.h"
#include "tmstruct.h"
#include "tmextern.h"
static int Initialized;
extern int Retry;
static void initialize();
static void open_all();
static int set_poll();
static int check_spawnlimit();
static int mod_ttydefs();
void open_device();
void set_softcar();
extern int check_session();
extern void sigalarm();
/* can't include libdevinfo.h */
extern int di_devperm_logout(const char *);
/*
* ttymon - a port monitor under SAC
* - monitor ports, set terminal modes, baud rate
* and line discipline for the port
* - invoke service on port if connection request received
* - Usage: ttymon
* ttymon -g [options]
* Valid options are
* -h
* -d device
* -l ttylabel
* -t timeout
* -m modules
* -p prompt
*
* - ttymon without args is invoked by SAC
* - ttymon -g is invoked by process that needs to
* have login service on the fly
*/
int
{
int nfds;
extern char *lastname();
/*
* Only the superuser should execute this command.
*/
if (getuid() != 0)
return (1);
/* remember original signal mask and dispositions */
#ifdef DEBUG
#endif
/*
* SIGQUIT needs to be ignored. Otherwise, hitting ^\ from
* console kills ttymon.
*/
return (1); /*NOTREACHED*/
}
initialize();
for (;;) {
if (!Reread_flag) {
if (nfds > 0)
else
(void) pause();
}
/*
* READDB messages may arrive during poll or pause.
* So the flag needs to be checked again.
*/
if (Reread_flag) {
Reread_flag = FALSE;
re_read();
}
while (Retry) {
open_all();
}
}
}
static void
{
#ifdef DEBUG
extern opendebug();
#endif
Initialized = FALSE;
/*
* get_environ() must be called first,
* otherwise we don't know where the log file is
*/
get_environ();
openpid();
openpipes();
setup_PCpipe();
log("Starting state: %s",
#ifdef DEBUG
debug("***** ttymon in initialize *****");
log("debug mode is \t on");
#endif
/* register to receive SIGPOLL when data comes to pmpipe */
sacpoll(); /* this is needed because there may be data already */
if (Maxfiles < 0)
read_pmtab();
/*
* setup poll array
* - we allocate 10 extra pollfd so that
* we do not have to re-malloc when there is
* minor fluctuation in Nentries
*/
fatal("malloc for Pollp failed");
(void) mod_ttydefs(); /* just to initialize Mtime */
/* initialize global variables, Uucp_uid & Tty_gid */
log("no group entry for <tty>, default is used");
else
endgrent();
endpwent();
#ifdef DEBUG
#endif
log("Initialization Completed");
/* open the devices ttymon monitors */
while (Retry) {
(!((State == PM_DISABLED) &&
}
}
}
Initialized = TRUE;
}
static void free_defs();
/*
* open_all - open devices in pmtab if the entry is
* - valid, fd = 0, and pid = 0
*/
static void
open_all()
{
int check_modtime;
#ifdef DEBUG
debug("in open_all");
#endif
/*
* if we have not check modification time and
*/
if (check_modtime && mod_ttydefs()) {
free_defs();
#ifdef DEBUG
#endif
}
(!((State == PM_DISABLED) &&
if (check_modtime && mod_ttydefs()) {
free_defs();
#ifdef DEBUG
#endif
}
}
}
}
void
{
#ifdef DEBUG
debug("in set_softcar");
#endif
/*
* If soft carrier is not set one way or
* the other, leave it alone.
*/
return;
val = 1;
return;
}
}
/*
* open_device(pmptr) - open the device
* - check device lock
* - change owner of device
* - push line disciplines
* - set termio
*/
void
{
#ifdef DEBUG
debug("in open_device");
#endif
} else {
log("service <%s> is respawning too rapidly",
return;
}
== -1) {
Nlocked++;
if (Nlocked == 1) {
}
} else
return;
}
/* set close-on-exec flag */
if (tm_checklock(fd) != 0) {
Nlocked++;
if (Nlocked == 1) {
}
return;
}
if (check_session(fd) != 0) {
log("Warning -- active session exists on <%s>",
} else {
/*
* this may happen if a service is running
* and ttymon dies and is restarted,
* or another process is running on the
* port.
*/
pmptr->p_inservice = 0;
Nlocked++;
if (Nlocked == 1) {
NULL);
}
return;
}
}
pmptr->p_inservice = 0;
}
/* drop DTR */
(void) hang_up_line(fd);
/*
* After hang_up_line, the stream is in STRHUP state.
* We need to do another open to reinitialize streams
* then we can close one fd
*/
return;
}
}
#ifdef DEBUG
#endif
else
== -1) {
return;
}
}
return;
}
}
/*
* set_poll(fdp) - put all fd's in a pollfd array
* - set poll event to POLLIN and POLLMSG
* - return number of fd to be polled
*/
static int
{
int nfd = 0;
fdp++;
nfd++;
}
}
return (nfd);
}
/*
* check_spawnlimit - return 0 if spawnlimit is not reached
* - otherwise return -1
*/
static int
{
return (-1);
}
}
return (0);
}
/*
* - return TRUE if file modified
* - otherwise, return FALSE
*/
static int
{
extern long Mtime;
/* if stat failed, don't bother reread ttydefs */
return (FALSE);
}
return (TRUE);
}
return (FALSE);
}
/*
* free_defs - free the Gdef table
*/
static void
{
int i;
}
Ndefs = 0;
}
/*
* struct Gdef *get_speed(ttylabel)
* using "ttylabel". If "ttylabel" is NULL, default
* to DEFAULT
*/
struct Gdef *
{
}
return (sp);
}
/*
* setup_PCpipe() - setup the pipe between Parent and Children
* - the pipe is used for a tmchild to send its
* pid to inform ttymon that it is about to
* invoke service
* - the pipe also serves as a mean for tmchild
* to detect failure of ttymon
*/
void
{
int flag = 0;
/* set close-on-exec flag */
/* set O_NONBLOCK flag */
flag |= O_NONBLOCK;
/* set message discard mode */
/* register to receive SIGPOLL when data come */
#ifdef DEBUG
#endif
}