/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "utils.h"
#include <locale.h>
#include <poll.h>
#include <setjmp.h>
#include <signal.h>
#include <strings.h>
#include <stropts.h>
#include <syslog.h>
#include <sys/sysmsg_impl.h>
#include <sys/sysmacros.h>
#include <sys/systeminfo.h>
#define CONFIG 0
/* useful data structures for lock function */
static char usage[] =
"Usage: \n"
"\tconsadm [ -p ] [ -a device ... ]\n"
"\tconsadm [ -p ] [ -d device ... ]\n"
"\tconsadm [ -p ]\n";
/* data structures ... */
static char conshdr[] =
"#\n# consadm.conf\n#"
"# Configuration parameters for console message redirection.\n"
"# Do NOT edit this file by hand -- use consadm(1m) instead.\n"
"#\n";
/* command line arguments */
static int display;
static int persist;
static int addflag;
static int deleteflag;
/* function headers */
static void setaux(char *);
static void unsetaux(char *);
static void getconsole(void);
static void setfallback(char *argv[]);
static void removefallback(void);
static void fallbackdaemon(void);
static void persistlist(void);
static int verifyarg(char *, int);
static int safeopen(char *);
static void catch_term(void);
static void catch_alarm(void);
static void catch_hup(void);
static void cleanup_on_exit(void);
static void addtolist(char *);
static void removefromlist(char *);
static int pathcmp(char *, char *);
static int lckfunc(int, int);
typedef void (*sig_handler_t)();
static int getlock(void);
/*
* In main, return codes carry the following meaning:
* 0 - successful
* 1 - error during the command execution
*/
int
{
int index;
int c;
if (p == NULL)
p = argv[0];
else
p++;
pname = p;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
if (getuid() != 0)
/*
* Handle normal termination signals that may be received.
*/
/*
* To make sure persistent state gets removed.
*/
return (E_SUCCESS);
}
if (argc == 1)
display++;
else {
switch (c) {
case 'a':
addflag++;
break;
case 'd':
deleteflag++;
break;
case 'p':
persist++;
break;
default:
/*NOTREACHED*/
}
}
}
if (display) {
getconsole();
return (E_SUCCESS);
}
if (addflag && deleteflag) {
return (E_ERROR);
}
if (addflag) {
return (E_ERROR);
}
/* separately check every device path specified */
return (E_ERROR);
}
if (persist)
}
/*
* consoles at this time.
*/
return (E_SUCCESS);
} else if (deleteflag) {
return (E_ERROR);
}
/* separately check every device path specified */
return (E_ERROR);
}
if (persist && deleteflag)
}
/*
* kill off daemon and restart with
* new list of auxiliary consoles
*/
return (E_SUCCESS);
} else if (persist) {
return (E_ERROR);
}
persistlist();
return (E_SUCCESS);
} else {
return (E_ERROR);
}
} /* main */
/* for daemon to handle termination from user command */
static void
{
}
/* handle lack of carrier on open */
static void
{
}
/* caught a sighup */
static void
{
/*
* ttymon sends sighup to consadmd because it has the serial
* port open. We catch the signal here, but process it
* within fallbackdaemon(). We ignore the signal if the
* errno returned was EINTR.
*/
}
/* Remove persistent state on receiving signal. */
static void
{
(void) unlink(CONSADMLOCK);
}
/*
*/
static void
{
int fd;
/*
* Let setting duplicate device be warning, consadm
* must proceed to set persistence if requested.
*/
dev);
}
}
/*
* Send ioctl to device specified and
* Remove the entry from the list of auxiliary devices.
*/
static void
{
int fd;
} else
}
static int
getlock(void)
{
int lckfd;
else
}
(void) unlink(CONSADMLOCK);
}
return (lckfd);
}
static void
{
int len;
/* update file of devices configured to get console msgs. */
/* Open new file */
CONSCONFIG, (int)getpid());
(void) unlink(CONSADMLOCK);
}
/* Add header to new file */
/* Check that the file doesn't already exist */
continue;
/* they match so use name passed in. */
} else
}
}
/* User specified persistent settings */
(void) unlink(CONSADMLOCK);
}
/* The list in CONSCONFIG gives the persistence capability in the proto */
static void
{
int lckfd;
char len;
/* update file of devices configured to get console msgs. */
(void) unlink(CONSADMLOCK);
return;
}
/* Open new file */
CONSCONFIG, (int)getpid());
(void) unlink(CONSADMLOCK);
}
/* Add header to new file */
/*
* Check whether the path duplicates what is already in the
* file.
*/
/* skip comments */
continue;
/* they match so don't write it */
continue;
}
}
/* Remove the file if there aren't any auxiliary consoles */
if (newcontents)
else {
(void) unlink(CONSCONFIG);
}
(void) unlink(CONSADMLOCK);
}
static int
{
return (0);
return (0);
return (1);
}
/*
* Display configured consoles.
*/
static void
getconsole(void)
{
int fd;
if (bufsize == 0)
return;
if (p == NULL) {
break;
}
*p++ = '\0';
ptr = p;
}
}
/*
* It is supposed that if the device supports TIOCMGET then it
* might be a serial device.
*/
static boolean_t
{
int modem_state;
return (B_TRUE);
else
return (B_FALSE);
}
static boolean_t
{
int modem_state;
return ((modem_state & TIOCM_CAR) != 0);
else {
return (B_FALSE);
}
}
static void
{
/*
* kill off any existing daemon
* remove /etc/consadm.pid
*/
/* kick off a daemon */
(void) close(0);
(void) close(1);
(void) close(2);
} else if (pid == -1)
/* write daemon pid to file */
(void) unlink(CONSADMLOCK);
}
/*
* Remove the daemon that would have implemented the automatic
* fallback in event of carrier loss on the serial console.
*/
static void
removefallback(void)
{
int pid;
/* file doesn't exist, so no work to do */
return;
(void) unlink(SETCONSOLEPID);
return;
}
/*
* Don't shoot ourselves in the foot by killing init,
* sched, pageout, or fsflush.
*/
(void) unlink(SETCONSOLEPID);
return;
}
/*
* kill off the existing daemon listed in
* /etc/consadm.pid
*/
(void) unlink(SETCONSOLEPID);
}
/*
* parameter passed in will always be the auxiliary device.
* The daemon will not start after the last device has been removed.
*/
static void
fallbackdaemon(void)
{
char **devpaths;
int index;
int pollagain;
/*
* catch SIGTERM cause it might be coming from user via consadm
*/
/*
* catch SIGHUP cause it might be coming from a disconnect
*/
if (bufsize == 0)
return;
if (p == NULL) {
cachesize++;
break;
}
p++;
cachesize++;
ptr = p;
}
if (p == NULL) {
ptr);
break;
}
if (!has_carrier(fd)) {
"no carrier on %s, device will not be monitored.\n"),
ptr);
break;
} else {
nfds++;
break;
}
break;
}
*p++ = '\0';
ptr = p;
continue;
}
if (!has_carrier(fd)) {
"no carrier on %s, device will not be monitored.\n"),
ptr);
ptr = p;
continue;
} else {
nfds++;
break;
}
ptr = p;
}
/* no point polling if no devices with carrier */
if (nfds == 0)
return;
for (;;) {
/* daemon sleeps waiting for a hangup on the console */
if (ret == -1) {
/* Check if ttymon is trying to get rid of us */
continue;
return;
} else if (ret == 0) {
INFTIM);
return;
} else {
/* Go through poll list looking for events. */
/* expected result */
POLLHUP) {
/*
* unsetaux console. Take out of list
* of current auxiliary consoles.
*/
"lost carrier, unsetting console %s\n"),
"%s: lost carrier, unsetting auxiliary device %s",
continue;
}
POLLERR) {
continue;
"unexpected poll result 0x%x\n"),
continue;
}
}
/* check whether any left to poll */
continue;
else
return;
}
}
}
static void
persistlist(void)
{
int lckfd;
/* skip comments */
continue;
}
}
(void) unlink(CONSADMLOCK);
}
static int
{
int fd;
int ret = 0;
ret = 1;
goto err_exit;
}
if (dev[0] != '/') {
ret = 1;
goto err_exit;
}
/* they match */
ret = 1;
goto err_exit;
}
ret = 1;
goto err_exit;
}
/* Delete operation doesn't require this checking */
if (flag) {
ret = 1;
}
goto err_exit;
}
if (!modem_support(fd)) {
ret = 1;
goto err_exit;
}
/* Only verify carrier if it's an add operation */
if (flag) {
if (!has_carrier(fd)) {
ret = 1;
goto err_exit;
}
}
return (ret);
}
/*
* Open the pseudo device, but be prepared to catch sigalarm if we block
* cause there isn't any carrier present.
*/
static int
{
int fd;
return (-1);
(void) alarm(5);
/* The sysmsg driver sets NONBLOCK and NDELAY, but what the hell */
return (-1);
(void) alarm(0);
return (fd);
}
static int
{
}