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
50a44ef6dSjacobs * Common Development and Distribution License (the "License").
60a44ef6dSjacobs * 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 */
210a44ef6dSjacobs
227c478bd9Sstevel@tonic-gate /*
230a44ef6dSjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate # include <stdarg.h>
317c478bd9Sstevel@tonic-gate # include <limits.h>
327c478bd9Sstevel@tonic-gate # include <sys/types.h>
337c478bd9Sstevel@tonic-gate # include <poll.h>
347c478bd9Sstevel@tonic-gate # include <stropts.h>
357c478bd9Sstevel@tonic-gate # include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <syslog.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate # include "lpsched.h"
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #define TURN_OFF(X,F) (void)Fcntl(X, F_SETFL, (Fcntl(X, F_GETFL, 0) & ~(F)))
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate static void conn_shutdown();
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate extern int Filter_Status;
467c478bd9Sstevel@tonic-gate extern void dispatch();
477c478bd9Sstevel@tonic-gate extern int Waitrequest;
487c478bd9Sstevel@tonic-gate void shutdown_messages();
497c478bd9Sstevel@tonic-gate static char *Message;
507c478bd9Sstevel@tonic-gate static int MaxClients = 0,
517c478bd9Sstevel@tonic-gate do_msg();
527c478bd9Sstevel@tonic-gate extern int Reserve_Fds;
537c478bd9Sstevel@tonic-gate extern int Shutdown;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate MESG *Net_md;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate ** take_message() - WAIT FOR INTERRUPT OR ONE MESSAGE FROM USER PROCESS
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate
take_message(void)617c478bd9Sstevel@tonic-gate void take_message(void)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate int bytes;
640a44ef6dSjacobs int i;
657c478bd9Sstevel@tonic-gate MESG * md;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate for (EVER) { /* not really forever...returns are in the loop */
687c478bd9Sstevel@tonic-gate if ((md = mlisten()) == NULL)
697c478bd9Sstevel@tonic-gate switch(errno) {
707c478bd9Sstevel@tonic-gate case EAGAIN:
717c478bd9Sstevel@tonic-gate case EINTR:
727c478bd9Sstevel@tonic-gate return;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate case ENOMEM:
757c478bd9Sstevel@tonic-gate mallocfail();
767c478bd9Sstevel@tonic-gate /* NOTREACHED */
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate default:
797c478bd9Sstevel@tonic-gate fail ("Unexpected streams error in mlisten (%s).\n" , PERROR);
807c478bd9Sstevel@tonic-gate }
81*2a8bcb4eSToomas Soome
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * Check for a dropped connection to a child.
847c478bd9Sstevel@tonic-gate * Normally a child should tell us that it is dying
857c478bd9Sstevel@tonic-gate * (with S_SHUTDOWN or S_SEND_CHILD), but it may have
867c478bd9Sstevel@tonic-gate * died a fast death. We'll simulate the message we
877c478bd9Sstevel@tonic-gate * wanted to get so we can use the same code to clean up.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate if ((md->event & POLLHUP) && !(md->event & POLLIN) ||
907c478bd9Sstevel@tonic-gate (md->event & (POLLERR|POLLNVAL))) {
917c478bd9Sstevel@tonic-gate switch (md->type) {
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate case MD_CHILD:
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate * If the message descriptor is found in the
967c478bd9Sstevel@tonic-gate * exec table, it must be an interface pgm,
977c478bd9Sstevel@tonic-gate * notification, etc. Otherwise, it must be
987c478bd9Sstevel@tonic-gate * a network child.
997c478bd9Sstevel@tonic-gate */
1000a44ef6dSjacobs for (i = 0; Exec_Table[i] != NULL; i++)
1010a44ef6dSjacobs if (Exec_Table[i]->md == md)
1027c478bd9Sstevel@tonic-gate break;
1037c478bd9Sstevel@tonic-gate
1040a44ef6dSjacobs if (Exec_Table[i] != NULL) {
1057c478bd9Sstevel@tonic-gate (void) putmessage(Message, S_CHILD_DONE,
1060a44ef6dSjacobs Exec_Table[i]->key, 0, 0);
1077c478bd9Sstevel@tonic-gate } else {
1087c478bd9Sstevel@tonic-gate (void) putmessage(Message, S_SHUTDOWN, 1);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate bytes = 1;
1117c478bd9Sstevel@tonic-gate break;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate default:
1147c478bd9Sstevel@tonic-gate bytes = -1;
1157c478bd9Sstevel@tonic-gate break;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate } else {
1207c478bd9Sstevel@tonic-gate if (md->readfd == -1) { /* something happened to the readfd */
1217c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "take_message: readfd is -1");
1227c478bd9Sstevel@tonic-gate return;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate bytes = mread(md, Message, MSGMAX);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate switch (bytes) {
1287c478bd9Sstevel@tonic-gate case -1:
1297c478bd9Sstevel@tonic-gate if (errno == EINTR)
1307c478bd9Sstevel@tonic-gate return;
1317c478bd9Sstevel@tonic-gate else
1327c478bd9Sstevel@tonic-gate fail ("Unexpected streams error (%s).\n" , PERROR);
1337c478bd9Sstevel@tonic-gate break;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate case 0:
1367c478bd9Sstevel@tonic-gate break;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate default:
1397c478bd9Sstevel@tonic-gate if (do_msg(md))
1407c478bd9Sstevel@tonic-gate return;
1417c478bd9Sstevel@tonic-gate break;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate ** do_msg() - HANDLE AN INCOMING MESSAGE
1487c478bd9Sstevel@tonic-gate */
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate static int
do_msg(MESG * md)1517c478bd9Sstevel@tonic-gate do_msg(MESG *md)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate int type = mtype(Message);
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate if (type != S_GOODBYE) {
1567c478bd9Sstevel@tonic-gate md->wait = 0;
1577c478bd9Sstevel@tonic-gate dispatch (type, Message, md);
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * The message may have caused the need to
1607c478bd9Sstevel@tonic-gate * schedule something, so go back and check.
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate return(1);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate return(0);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate ** calculate_nopen() - DETERMINE # FILE DESCRIPTORS AVAILABLE FOR QUEUES
1697c478bd9Sstevel@tonic-gate */
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate static void
calculate_nopen(void)1727c478bd9Sstevel@tonic-gate calculate_nopen(void)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate int fd, nopen;
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * How many file descriptorss are currently being used?
1787c478bd9Sstevel@tonic-gate */
1797c478bd9Sstevel@tonic-gate for (fd = nopen = 0; fd < OpenMax; fd++)
1807c478bd9Sstevel@tonic-gate if (fcntl(fd, F_GETFL, 0) != -1)
1817c478bd9Sstevel@tonic-gate nopen++;
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate * How many file descriptors are available for use
1857c478bd9Sstevel@tonic-gate * as open FIFOs? Leave one spare as a way to tell
1867c478bd9Sstevel@tonic-gate * clients we don't have any to spare (hmmm....) and
1877c478bd9Sstevel@tonic-gate * one for the incoming fifo.
1887c478bd9Sstevel@tonic-gate */
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate MaxClients = OpenMax;
1917c478bd9Sstevel@tonic-gate MaxClients -= nopen; /* current overhead */
1927c478bd9Sstevel@tonic-gate MaxClients -= Reserve_Fds;
1937c478bd9Sstevel@tonic-gate MaxClients -= 2; /* incoming FIFO and spare outgoing */
1947c478bd9Sstevel@tonic-gate MaxClients--; /* the requests log */
1957c478bd9Sstevel@tonic-gate MaxClients--; /* HPI routines and lpsched log */
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate return;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
conn_shutdown()2007c478bd9Sstevel@tonic-gate static void conn_shutdown ( )
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate if (!Shutdown) {
2037c478bd9Sstevel@tonic-gate note ("The public connection \"%s\", has failed.\n", Lp_FIFO);
2047c478bd9Sstevel@tonic-gate lpshut(1);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate ** init_messages() - INITIALIZE MAIN MESSAGE QUEUE
2107c478bd9Sstevel@tonic-gate */
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate void
init_messages(void)2137c478bd9Sstevel@tonic-gate init_messages(void)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate char *cmd;
2167c478bd9Sstevel@tonic-gate MESG * md;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_IGN);
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate calculate_nopen ();
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate Message = (char *)Malloc(MSGMAX);
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate (void) Chmod(Lp_Tmp, 0711);
225*2a8bcb4eSToomas Soome
2267c478bd9Sstevel@tonic-gate if ((md = mcreate(Lp_FIFO)) == NULL)
2277c478bd9Sstevel@tonic-gate fail ("Can't create public message device (%s).\n", PERROR);
2287c478bd9Sstevel@tonic-gate mon_discon(md, conn_shutdown);
229*2a8bcb4eSToomas Soome
2307c478bd9Sstevel@tonic-gate if (mlisteninit(md) != 0)
2317c478bd9Sstevel@tonic-gate if (errno == ENOMEM)
2327c478bd9Sstevel@tonic-gate mallocfail();
2337c478bd9Sstevel@tonic-gate else
2347c478bd9Sstevel@tonic-gate fail ("Unexpected streams error (%s).\n" , PERROR);
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate (void) Chmod(Lp_FIFO, 0666);
2377c478bd9Sstevel@tonic-gate return;
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
240*2a8bcb4eSToomas Soome
2417c478bd9Sstevel@tonic-gate void
shutdown_messages(void)2427c478bd9Sstevel@tonic-gate shutdown_messages(void)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate MESG *md;
245*2a8bcb4eSToomas Soome
2467c478bd9Sstevel@tonic-gate (void) Chmod(Lp_Tmp, 0700);
2477c478bd9Sstevel@tonic-gate (void) Chmod(Lp_FIFO, 0600);
2487c478bd9Sstevel@tonic-gate md = mlistenreset();
2497c478bd9Sstevel@tonic-gate mdestroy(md);
2507c478bd9Sstevel@tonic-gate }
251