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
5*45916cd2Sjpk * Common Development and Distribution License (the "License").
6*45916cd2Sjpk * 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 /*
22*45916cd2Sjpk * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate # include <unistd.h>
307c478bd9Sstevel@tonic-gate # include <stdlib.h>
317c478bd9Sstevel@tonic-gate # include <string.h>
327c478bd9Sstevel@tonic-gate # include <poll.h>
337c478bd9Sstevel@tonic-gate # include <stropts.h>
347c478bd9Sstevel@tonic-gate # include <fcntl.h>
357c478bd9Sstevel@tonic-gate # include <errno.h>
367c478bd9Sstevel@tonic-gate #include <syslog.h>
37*45916cd2Sjpk #include <user_attr.h>
38*45916cd2Sjpk #include <secdb.h>
39*45916cd2Sjpk #include <pwd.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate # include "lp.h"
427c478bd9Sstevel@tonic-gate # include "msgs.h"
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #define TURN_ON(X,F) (void)Fcntl(X, F_SETFL, (Fcntl(X, F_GETFL, 0)|(F)))
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate static int NumEvents = 0;
477c478bd9Sstevel@tonic-gate static int NumCons = 0;
487c478bd9Sstevel@tonic-gate static int ConsSize= 0;
497c478bd9Sstevel@tonic-gate static int NumNewCons = 0;
507c478bd9Sstevel@tonic-gate static MESG ** Connections = NULL;
517c478bd9Sstevel@tonic-gate static struct pollfd * PollFdList = NULL;
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate int
mlisteninit(MESG * md)547c478bd9Sstevel@tonic-gate mlisteninit(MESG * md)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate if (md == NULL)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate errno = EINVAL;
597c478bd9Sstevel@tonic-gate return(-1);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate if (ConsSize > 0)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate errno = EBUSY;
657c478bd9Sstevel@tonic-gate return(-1);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate ConsSize = 20;
697c478bd9Sstevel@tonic-gate Connections = (MESG **) Malloc(ConsSize * MDSIZE);
707c478bd9Sstevel@tonic-gate PollFdList = (struct pollfd*) Malloc(ConsSize * sizeof(struct pollfd));
717c478bd9Sstevel@tonic-gate if (Connections == NULL || PollFdList == NULL)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate errno = ENOMEM;
747c478bd9Sstevel@tonic-gate return(-1);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate Connections[0] = md;
777c478bd9Sstevel@tonic-gate PollFdList[0].fd = md->readfd;
787c478bd9Sstevel@tonic-gate PollFdList[0].events = POLLIN;
797c478bd9Sstevel@tonic-gate PollFdList[0].revents = 0;
807c478bd9Sstevel@tonic-gate NumCons = 1;
817c478bd9Sstevel@tonic-gate return(0);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate int
mlistenadd(MESG * md,short events)857c478bd9Sstevel@tonic-gate mlistenadd(MESG * md, short events)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate int slack;
887c478bd9Sstevel@tonic-gate struct pollfd * fdp;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate ** See if we have room in the connection table.
927c478bd9Sstevel@tonic-gate ** Realloc(3) the table if the number of connections
937c478bd9Sstevel@tonic-gate ** changes by more than 20.
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate slack = ConsSize - (NumCons + NumNewCons + 1);
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate if (slack < 0)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate ConsSize += 20;
1017c478bd9Sstevel@tonic-gate Connections = (MESG **) Realloc(Connections, ConsSize * MDSIZE);
1027c478bd9Sstevel@tonic-gate PollFdList = (struct pollfd*) Realloc(PollFdList, ConsSize * sizeof(struct pollfd));
1037c478bd9Sstevel@tonic-gate if (Connections == NULL || PollFdList == NULL)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate errno = ENOMEM;
1067c478bd9Sstevel@tonic-gate return(-1);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate if (slack > 20)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate ConsSize -= 20;
1137c478bd9Sstevel@tonic-gate Connections = (MESG **) Realloc(Connections, ConsSize * MDSIZE);
1147c478bd9Sstevel@tonic-gate PollFdList = (struct pollfd*) Realloc(PollFdList, ConsSize * sizeof(struct pollfd));
1157c478bd9Sstevel@tonic-gate if (Connections == NULL || PollFdList == NULL)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate errno = ENOMEM;
1187c478bd9Sstevel@tonic-gate return(-1);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate fdp = PollFdList + (NumCons + NumNewCons);
1237c478bd9Sstevel@tonic-gate fdp->fd = md->readfd;
1247c478bd9Sstevel@tonic-gate fdp->events = events;
1257c478bd9Sstevel@tonic-gate fdp->revents = 0;
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate ** Now add the entry to the connection table
1297c478bd9Sstevel@tonic-gate ** NumCons will be updated above.
1307c478bd9Sstevel@tonic-gate */
1317c478bd9Sstevel@tonic-gate Connections[NumCons + NumNewCons++] = md;
1327c478bd9Sstevel@tonic-gate return(0);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate MESG *
mlistenreset(void)1367c478bd9Sstevel@tonic-gate mlistenreset ( void ) /* funcdef */
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate int x;
1397c478bd9Sstevel@tonic-gate MESG * md;
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate if (ConsSize == 0)
1427c478bd9Sstevel@tonic-gate return(NULL);
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate ConsSize = 0;
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate for (x = 1; x < NumCons; x++)
1477c478bd9Sstevel@tonic-gate (void) mdisconnect(Connections[x]);
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate md = Connections[0];
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate Free(Connections);
1527c478bd9Sstevel@tonic-gate Free(PollFdList);
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate Connections = NULL;
1557c478bd9Sstevel@tonic-gate PollFdList = NULL;
1567c478bd9Sstevel@tonic-gate NumCons = 0;
1577c478bd9Sstevel@tonic-gate NumNewCons = 0;
1587c478bd9Sstevel@tonic-gate NumEvents = 0;
1597c478bd9Sstevel@tonic-gate return(md);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate MESG *
mlisten()1637c478bd9Sstevel@tonic-gate mlisten()
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate extern uid_t Lp_Uid;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate MESG * mdp;
1687c478bd9Sstevel@tonic-gate MESG * md;
1697c478bd9Sstevel@tonic-gate MQUE * p;
1707c478bd9Sstevel@tonic-gate int flag = 0;
1717c478bd9Sstevel@tonic-gate int disconacts;
1727c478bd9Sstevel@tonic-gate int x;
1737c478bd9Sstevel@tonic-gate int y;
1747c478bd9Sstevel@tonic-gate struct pollfd * fdp;
1757c478bd9Sstevel@tonic-gate struct strrecvfd recbuf;
1767c478bd9Sstevel@tonic-gate #if defined(NOCONNLD)
1777c478bd9Sstevel@tonic-gate struct strbuf ctl;
1787c478bd9Sstevel@tonic-gate char cbuff[MSGMAX];
1797c478bd9Sstevel@tonic-gate #endif
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate #if defined(NOCONNLD)
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate ** Set up buffer for receiving messages.
1847c478bd9Sstevel@tonic-gate */
1857c478bd9Sstevel@tonic-gate ctl.buf = cbuff;
1867c478bd9Sstevel@tonic-gate ctl.maxlen = sizeof (cbuff);
1877c478bd9Sstevel@tonic-gate #endif
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate ** This loop exists to return control to poll after the
1917c478bd9Sstevel@tonic-gate ** result of poll yeilds no new connections or serviceable
1927c478bd9Sstevel@tonic-gate ** messages.
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate for (;;)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate ** If there are no unserviced events pending, call poll(2)
1987c478bd9Sstevel@tonic-gate ** and wait for a message or connection.
1997c478bd9Sstevel@tonic-gate ** NumEvents may be -1 in the event of an interrupt, hence
2007c478bd9Sstevel@tonic-gate ** <= 0
2017c478bd9Sstevel@tonic-gate */
2027c478bd9Sstevel@tonic-gate if (NumEvents <= 0)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate ** Add new connections, if any, reset connection counter
2067c478bd9Sstevel@tonic-gate */
2077c478bd9Sstevel@tonic-gate NumCons += NumNewCons;
2087c478bd9Sstevel@tonic-gate NumNewCons = 0;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate if (NumCons <= 0)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate errno = EINTR;
2137c478bd9Sstevel@tonic-gate return(NULL);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate ** Scan the connection table and remove any holes
2187c478bd9Sstevel@tonic-gate */
2197c478bd9Sstevel@tonic-gate for (x = 0; x < NumCons; x++)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate mdp = Connections[x];
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate ** Disconnected, clear the node and compress the
2257c478bd9Sstevel@tonic-gate ** tables. If the disconnect called any
2267c478bd9Sstevel@tonic-gate ** on_discon functions (disconacts > 0), return
2277c478bd9Sstevel@tonic-gate ** because there may be something to clean up.
2287c478bd9Sstevel@tonic-gate ** Finally, decrement <x> so that the next node
2297c478bd9Sstevel@tonic-gate ** doesn't get missed.
2307c478bd9Sstevel@tonic-gate */
2317c478bd9Sstevel@tonic-gate if (mdp->readfd == -1)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate disconacts = mdisconnect(mdp);
2347c478bd9Sstevel@tonic-gate NumCons--;
2357c478bd9Sstevel@tonic-gate for (y = x; y < (NumCons + NumNewCons); y++)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate Connections[y] = Connections[y + 1];
2387c478bd9Sstevel@tonic-gate PollFdList[y] = PollFdList[y + 1];
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate if (disconacts > 0)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate errno = EINTR;
2437c478bd9Sstevel@tonic-gate return(NULL);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate else
2467c478bd9Sstevel@tonic-gate x--;
2477c478bd9Sstevel@tonic-gate } else {
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate * We are in "mlisten", POLLIN is always set. We'll look
2507c478bd9Sstevel@tonic-gate * at POLLOUT possibility when mque is non-NULL.
2517c478bd9Sstevel@tonic-gate */
2527c478bd9Sstevel@tonic-gate PollFdList[x].events = POLLIN;
2537c478bd9Sstevel@tonic-gate if (mdp->mque)
2547c478bd9Sstevel@tonic-gate PollFdList[x].events |= POLLOUT;
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate ** Wait for a message or a connection.
2607c478bd9Sstevel@tonic-gate ** This call may be interrupted by alarms used
2617c478bd9Sstevel@tonic-gate ** elsewhere, so if poll fails, return NULL and
2627c478bd9Sstevel@tonic-gate ** set errno to EAGAIN.
2637c478bd9Sstevel@tonic-gate */
2647c478bd9Sstevel@tonic-gate if ((NumEvents = poll(PollFdList, NumCons, -1)) < 0)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate errno = EAGAIN;
2677c478bd9Sstevel@tonic-gate return(NULL);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate for (x = 0; x < NumCons; x++)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate mdp = Connections[x];
2747c478bd9Sstevel@tonic-gate fdp = PollFdList + x;
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate if (fdp->revents == 0)
2777c478bd9Sstevel@tonic-gate continue;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate switch (mdp->type) {
2807c478bd9Sstevel@tonic-gate case MD_MASTER:
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate ** Only valid revent is: POLLIN
2837c478bd9Sstevel@tonic-gate */
2847c478bd9Sstevel@tonic-gate if (fdp->revents != POLLIN)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate errno = EINVAL;
2877c478bd9Sstevel@tonic-gate return(NULL);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate ** Retrieve the file descriptor
2927c478bd9Sstevel@tonic-gate */
2937c478bd9Sstevel@tonic-gate if (ioctl(mdp->readfd, I_RECVFD, &recbuf) != 0)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate if (errno == EINTR)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate errno = EAGAIN;
2987c478bd9Sstevel@tonic-gate return(NULL);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate if (errno == ENXIO)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate fdp->revents = 0;
3037c478bd9Sstevel@tonic-gate NumEvents--;
3047c478bd9Sstevel@tonic-gate continue;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate #if defined(NOCONNLD)
3077c478bd9Sstevel@tonic-gate if (errno == EBADMSG)
3087c478bd9Sstevel@tonic-gate while (Getmsg(mdp, &ctl, &ctl, &flag) >= 0);
3097c478bd9Sstevel@tonic-gate #endif
3107c478bd9Sstevel@tonic-gate return(NULL);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate TURN_ON(recbuf.fd, O_NDELAY);
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate ** Now, create the message descriptor
3167c478bd9Sstevel@tonic-gate ** and populate it with what we know.
3177c478bd9Sstevel@tonic-gate */
3187c478bd9Sstevel@tonic-gate if ((md = (MESG *)Malloc(MDSIZE)) == NULL)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate errno = ENOMEM;
3217c478bd9Sstevel@tonic-gate return(NULL);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate memset(md, 0, sizeof (MESG));
3257c478bd9Sstevel@tonic-gate md->gid = recbuf.gid;
3267c478bd9Sstevel@tonic-gate md->readfd = md->writefd = recbuf.fd;
3277c478bd9Sstevel@tonic-gate md->state = MDS_IDLE;
3287c478bd9Sstevel@tonic-gate md->type = MD_UNKNOWN;
3297c478bd9Sstevel@tonic-gate md->uid = recbuf.uid;
3307c478bd9Sstevel@tonic-gate
331*45916cd2Sjpk /*
332*45916cd2Sjpk * Determine if a print administrator is contacting lpsched.
333*45916cd2Sjpk * currently, root, lp and users with the "solaris.print.admin"
334*45916cd2Sjpk * privilege are print administrators
335*45916cd2Sjpk */
3367c478bd9Sstevel@tonic-gate md->admin = (md->uid == 0 || md->uid == Lp_Uid);
337*45916cd2Sjpk if (md->admin == 0) {
338*45916cd2Sjpk struct passwd *pw = NULL;
339*45916cd2Sjpk
340*45916cd2Sjpk if ((pw = getpwuid(md->uid)) != NULL)
341*45916cd2Sjpk md->admin = chkauthattr("solaris.print.admin",
342*45916cd2Sjpk pw->pw_name);
343*45916cd2Sjpk }
344*45916cd2Sjpk
345*45916cd2Sjpk get_peer_label(md->readfd, &md->slabel);
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate if (mlistenadd(md, POLLIN) != 0)
3487c478bd9Sstevel@tonic-gate return(NULL);
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate ResetFifoBuffer (md->readfd);
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate ** Reset fdp because mlistenadd may have
3537c478bd9Sstevel@tonic-gate ** realloc()ed PollFdList and changed its
3547c478bd9Sstevel@tonic-gate ** physical location.
3557c478bd9Sstevel@tonic-gate */
3567c478bd9Sstevel@tonic-gate fdp = PollFdList + x;
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate ** Clear the event that brought us here,
3607c478bd9Sstevel@tonic-gate ** decrement the event counter, and get the
3617c478bd9Sstevel@tonic-gate ** next event.
3627c478bd9Sstevel@tonic-gate */
3637c478bd9Sstevel@tonic-gate fdp->revents = 0;
3647c478bd9Sstevel@tonic-gate NumEvents--;
3657c478bd9Sstevel@tonic-gate break;
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate case MD_CHILD:
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate ** If this connection is a child process, just
3707c478bd9Sstevel@tonic-gate ** save the event and return the message descriptor
3717c478bd9Sstevel@tonic-gate */
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate if (fdp->revents & POLLOUT) {
3747c478bd9Sstevel@tonic-gate if (mdp->mque) {
3757c478bd9Sstevel@tonic-gate if (mflush(mdp) < 0) {
3767c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG,
3777c478bd9Sstevel@tonic-gate "MD_CHILD mflush failed");
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate if (fdp->revents & POLLIN) {
3837c478bd9Sstevel@tonic-gate mdp->event = fdp->revents;
3847c478bd9Sstevel@tonic-gate NumEvents--;
3857c478bd9Sstevel@tonic-gate fdp->revents = 0;
3867c478bd9Sstevel@tonic-gate return (mdp); /* we are in listening mode */
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate NumEvents--;
3907c478bd9Sstevel@tonic-gate fdp->revents = 0;
3917c478bd9Sstevel@tonic-gate break;
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate default:
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate ** POLLNVAL means this client disconnected and
3967c478bd9Sstevel@tonic-gate ** all messages have been processed.
3977c478bd9Sstevel@tonic-gate */
3987c478bd9Sstevel@tonic-gate if (fdp->revents & POLLNVAL) /* disconnected & no msg */
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate if (mdp->readfd >= 0) {
4017c478bd9Sstevel@tonic-gate Close (mdp->readfd);
4027c478bd9Sstevel@tonic-gate if (mdp->writefd == mdp->readfd)
4037c478bd9Sstevel@tonic-gate mdp->writefd = -1;
4047c478bd9Sstevel@tonic-gate mdp->readfd = -1;
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate fdp->revents = 0;
4077c478bd9Sstevel@tonic-gate NumEvents--;
4087c478bd9Sstevel@tonic-gate continue;
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate ** POLLERR means an error message is on the
4137c478bd9Sstevel@tonic-gate ** stream. Since this is totally unexpected,
4147c478bd9Sstevel@tonic-gate ** the assumption is made that this stream will
4157c478bd9Sstevel@tonic-gate ** be flagged POLLNVAL next time through poll
4167c478bd9Sstevel@tonic-gate ** and will be removed at that time.
4177c478bd9Sstevel@tonic-gate */
4187c478bd9Sstevel@tonic-gate if (fdp->revents & POLLERR) /* uh, oh! */
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate if (mdp->readfd >= 0) {
4217c478bd9Sstevel@tonic-gate Close (mdp->readfd);
4227c478bd9Sstevel@tonic-gate if (mdp->writefd == mdp->readfd)
4237c478bd9Sstevel@tonic-gate mdp->writefd = -1;
4247c478bd9Sstevel@tonic-gate mdp->readfd = -1;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate NumEvents--;
4277c478bd9Sstevel@tonic-gate fdp->revents = 0;
4287c478bd9Sstevel@tonic-gate continue;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate ** POLLHUP means the client aborted the call.
4347c478bd9Sstevel@tonic-gate ** The client is not removed, because there may
4357c478bd9Sstevel@tonic-gate ** still be messages on the stream.
4367c478bd9Sstevel@tonic-gate */
4377c478bd9Sstevel@tonic-gate if (fdp->revents & POLLHUP) /* disconnected */
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate NumEvents--;
4407c478bd9Sstevel@tonic-gate fdp->revents = 0;
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate * MORE: This is odd. Why are we closing the
4437c478bd9Sstevel@tonic-gate * stream if there ``may still be messages''???
4447c478bd9Sstevel@tonic-gate */
4457c478bd9Sstevel@tonic-gate if (mdp->readfd >= 0) {
4467c478bd9Sstevel@tonic-gate Close (mdp->readfd);
4477c478bd9Sstevel@tonic-gate if (mdp->writefd == mdp->readfd)
4487c478bd9Sstevel@tonic-gate mdp->writefd = -1;
4497c478bd9Sstevel@tonic-gate mdp->readfd = -1;
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate continue;
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate * MORE: Why is this here??
4557c478bd9Sstevel@tonic-gate *
4567c478bd9Sstevel@tonic-gate if (mdp->type == MD_SYS_FIFO)
4577c478bd9Sstevel@tonic-gate (void) Close(mdp->writefd);
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate mdp->writefd = -1;
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate if (fdp->revents == POLLHUP)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate NumEvents--;
4647c478bd9Sstevel@tonic-gate fdp->revents = 0;
4657c478bd9Sstevel@tonic-gate (void) Close(mdp->readfd);
4667c478bd9Sstevel@tonic-gate mdp->readfd = -1;
4677c478bd9Sstevel@tonic-gate continue;
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate *
4707c478bd9Sstevel@tonic-gate */
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate ** POLLOUT means that the client had a full
4747c478bd9Sstevel@tonic-gate ** stream and messages became backlogged and
4757c478bd9Sstevel@tonic-gate ** now the stream is empty. So the queued msgs
4767c478bd9Sstevel@tonic-gate ** are sent with putmsg(2)
4777c478bd9Sstevel@tonic-gate */
4787c478bd9Sstevel@tonic-gate if (fdp->revents & POLLOUT)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate if (mdp->mque == NULL)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate NumEvents--;
4837c478bd9Sstevel@tonic-gate fdp->revents = 0;
4847c478bd9Sstevel@tonic-gate continue;
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate while (mdp->mque) {
4877c478bd9Sstevel@tonic-gate if (Putmsg(mdp, NULL, mdp->mque->dat, 0))
4887c478bd9Sstevel@tonic-gate break; /* failed for some reason */
4897c478bd9Sstevel@tonic-gate p = mdp->mque;
4907c478bd9Sstevel@tonic-gate mdp->mque = p->next;
4917c478bd9Sstevel@tonic-gate Free(p->dat->buf);
4927c478bd9Sstevel@tonic-gate Free(p->dat);
4937c478bd9Sstevel@tonic-gate Free(p);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate NumEvents--;
4967c478bd9Sstevel@tonic-gate fdp->revents = 0;
4977c478bd9Sstevel@tonic-gate continue;
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate ** POLLIN means that there is a message on the
5027c478bd9Sstevel@tonic-gate ** stream.
5037c478bd9Sstevel@tonic-gate ** Return the message descriptor to the caller
5047c478bd9Sstevel@tonic-gate ** so that the message may be received and
5057c478bd9Sstevel@tonic-gate ** processed.
5067c478bd9Sstevel@tonic-gate */
5077c478bd9Sstevel@tonic-gate if (fdp->revents & POLLIN) /* got a message */
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate NumEvents--;
5107c478bd9Sstevel@tonic-gate mdp->event = fdp->revents;
5117c478bd9Sstevel@tonic-gate fdp->revents = 0;
5127c478bd9Sstevel@tonic-gate if (mdp->type == MD_UNKNOWN)
5137c478bd9Sstevel@tonic-gate mdp->type = MD_STREAM;
5147c478bd9Sstevel@tonic-gate return(mdp);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate break;
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate # define VOID_FUNC_PTR void (*)()
5237c478bd9Sstevel@tonic-gate # define PTR_TO_VOID_FUNC_PTR void (**)()
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate int
mon_discon(MESG * md,void (* fn)())5267c478bd9Sstevel@tonic-gate mon_discon(MESG * md, void (*fn)())
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate int size = 2;
5297c478bd9Sstevel@tonic-gate void (**fnp) ();
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate if (md->on_discon)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate for (fnp = md->on_discon; *fnp; fnp++)
5347c478bd9Sstevel@tonic-gate size++;
5357c478bd9Sstevel@tonic-gate if ((md->on_discon = (PTR_TO_VOID_FUNC_PTR) Realloc (md->on_discon, size * sizeof(VOID_FUNC_PTR))) == NULL)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate errno = ENOMEM;
5387c478bd9Sstevel@tonic-gate return(-1);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate else
5427c478bd9Sstevel@tonic-gate if ((md->on_discon = (PTR_TO_VOID_FUNC_PTR) Malloc (size * sizeof(VOID_FUNC_PTR))) == NULL)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate errno = ENOMEM;
5457c478bd9Sstevel@tonic-gate return(-1);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate size--;
5497c478bd9Sstevel@tonic-gate md->on_discon[size] = NULL;
5507c478bd9Sstevel@tonic-gate size--;
5517c478bd9Sstevel@tonic-gate md->on_discon[size] = fn;
5527c478bd9Sstevel@tonic-gate return(0);
5537c478bd9Sstevel@tonic-gate }
554