Пример #1
0
int
evDeselectFD(evContext opaqueCtx, evFileID opaqueID) {
	evContext_p *ctx = opaqueCtx.opaque;
	evFile *del = opaqueID.opaque;
	evFile *cur;
	int mode, eventmask;

	if (!del) {
		evPrintf(ctx, 11, "evDeselectFD(NULL) ignored\n");
		errno = EINVAL;
		return (-1);
	}

	evPrintf(ctx, 1, "evDeselectFD(fd %d, mask 0x%x)\n",
		 del->fd, del->eventmask);

	/* Get the mode.  Unless the file has been closed, errors are bad. */
	mode = fcntl(del->fd, F_GETFL, NULL);
	if (mode == -1 && errno != EBADF)
		EV_ERR(errno);

	/* Remove from the list of files. */
	if (del->prev != NULL)
		del->prev->next = del->next;
	else
		ctx->files = del->next;
	if (del->next != NULL)
		del->next->prev = del->prev;

	/* Remove from the fd table. */
	if (del->fdprev != NULL)
		del->fdprev->fdnext = del->fdnext;
	else
		ctx->fdTable[del->fd] = del->fdnext;
	if (del->fdnext != NULL)
		del->fdnext->fdprev = del->fdprev;

	/*
	 * If the file descriptor does not appear in any other select() entry,
	 * and if !EV_WASNONBLOCK, and if we got no EBADF when we got the mode
	 * earlier, then: restore the fd to blocking status.
	 */
	if (!(cur = FindFD(ctx, del->fd, EV_MASK_ALL)) &&
	    !FD_ISSET(del->fd, &ctx->nonblockBefore) &&
	    mode != -1) {
		/*
		 * Note that we won't return an error status to the caller if
		 * this fcntl() fails since (a) we've already done the work
		 * and (b) the caller didn't ask us anything about O_NONBLOCK.
		 */
#ifdef USE_FIONBIO_IOCTL
		int off = 1;
		(void) ioctl(del->fd, FIONBIO, (char *)&off);
#else
		(void) fcntl(del->fd, F_SETFL, mode & ~PORT_NONBLOCK);
#endif
	}

	/*
	 * Now find all other uses of this descriptor and OR together an event
	 * mask so that we don't turn off {rd,wr,ex}Next bits that some other
	 * file event is using.  As an optimization, stop if the event mask
	 * fills.
	 */
	eventmask = 0;
	for ((void)NULL;
	     cur != NULL && eventmask != EV_MASK_ALL;
	     cur = cur->next)
		if (cur->fd == del->fd)
			eventmask |= cur->eventmask;

	/* OK, now we know which bits we can clear out. */
	if (!(eventmask & EV_READ)) {
		FD_CLR(del->fd, &ctx->rdNext);
		if (FD_ISSET(del->fd, &ctx->rdLast)) {
			FD_CLR(del->fd, &ctx->rdLast);
			ctx->fdCount--;
		}
	}
	if (!(eventmask & EV_WRITE)) {
		FD_CLR(del->fd, &ctx->wrNext);
		if (FD_ISSET(del->fd, &ctx->wrLast)) {
			FD_CLR(del->fd, &ctx->wrLast);
			ctx->fdCount--;
		}
	}
	if (!(eventmask & EV_EXCEPT)) {
		FD_CLR(del->fd, &ctx->exNext);
		if (FD_ISSET(del->fd, &ctx->exLast)) {
			FD_CLR(del->fd, &ctx->exLast);
			ctx->fdCount--;
		}
	}

	/* If this was the maxFD, find the new one. */
	if (del->fd == ctx->fdMax) {
		ctx->fdMax = -1;
		for (cur = ctx->files; cur; cur = cur->next)
			if (cur->fd > ctx->fdMax)
				ctx->fdMax = cur->fd;
	}

	/* If this was the fdNext, cycle that to the next entry. */
	if (del == ctx->fdNext)
		ctx->fdNext = del->next;

	evPrintf(ctx, 5,
	      "evDeselectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
		 del->fd, eventmask,
		 (u_long)ctx->rdNext.fds_bits[0],
		 (u_long)ctx->wrNext.fds_bits[0],
		 (u_long)ctx->exNext.fds_bits[0]);

	/* Couldn't free it before now since we were using fields out of it. */
	FREE(del);

	return (0);
}
Пример #2
0
int
evSelectFD(evContext opaqueCtx,
	   int fd,
	   int eventmask,
	   evFileFunc func,
	   void *uap,
	   evFileID *opaqueID
) {
	evContext_p *ctx = opaqueCtx.opaque;
	evFile *id;
	int mode;

	evPrintf(ctx, 1,
		 "evSelectFD(ctx %#x, fd %d, mask 0x%x, func %#x, uap %#x)\n",
		 ctx, fd, eventmask, func, uap);
	if (eventmask == 0 || (eventmask & ~EV_MASK_ALL) != 0)
		EV_ERR(EINVAL);
	if (fd > ctx->highestFD)
		EV_ERR(EINVAL);
	OK(mode = fcntl(fd, F_GETFL, NULL));	/* side effect: validate fd. */

	/*
	 * The first time we touch a file descriptor, we need to check to see
	 * if the application already had it in O_NONBLOCK mode and if so, all
	 * of our deselect()'s have to leave it in O_NONBLOCK.  If not, then
	 * all but our last deselect() has to leave it in O_NONBLOCK.
	 */
	id = FindFD(ctx, fd, EV_MASK_ALL);
	if (id == NULL) {
		if (mode & PORT_NONBLOCK)
			FD_SET(fd, &ctx->nonblockBefore);
		else {
#ifdef USE_FIONBIO_IOCTL
			int on = 1;
			OK(ioctl(fd, FIONBIO, (char *)&on));
#else
			OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK));
#endif
			FD_CLR(fd, &ctx->nonblockBefore);
		}
	}

	/*
	 * If this descriptor is already in use, search for it again to see
	 * if any of the eventmask bits we want to set are already captured.
	 * We cannot usefully capture the same fd event more than once in the
	 * same context.
	 */
	if (id != NULL && FindFD(ctx, fd, eventmask) != NULL)
		EV_ERR(ETOOMANYREFS);

	/* Allocate and fill. */
	OKNEW(id);
	id->func = func;
	id->uap = uap;
	id->fd = fd;
	id->eventmask = eventmask;

	/*
	 * Insert at head.  Order could be important for performance if we
	 * believe that evGetNext()'s accesses to the fd_sets will be more
	 * serial and therefore more cache-lucky if the list is ordered by
	 * ``fd.''  We do not believe these things, so we don't do it.
	 *
	 * The interesting sequence is where GetNext() has cached a select()
	 * result and the caller decides to evSelectFD() on some descriptor.
	 * Since GetNext() starts at the head, it can miss new entries we add
	 * at the head.  This is not a serious problem since the event being
	 * evSelectFD()'d for has to occur before evSelectFD() is called for
	 * the file event to be considered "missed" -- a real corner case.
	 * Maintaining a "tail" pointer for ctx->files would fix this, but I'm
	 * not sure it would be ``more correct.''
	 */
	if (ctx->files != NULL)
		ctx->files->prev = id;
	id->prev = NULL;
	id->next = ctx->files;
	ctx->files = id;

	/* Insert into fd table. */
	if (ctx->fdTable[fd] != NULL)
		ctx->fdTable[fd]->fdprev = id;
	id->fdprev = NULL;
	id->fdnext = ctx->fdTable[fd];
	ctx->fdTable[fd] = id;

	/* Turn on the appropriate bits in the {rd,wr,ex}Next fd_set's. */
	if (eventmask & EV_READ)
		FD_SET(fd, &ctx->rdNext);
	if (eventmask & EV_WRITE)
		FD_SET(fd, &ctx->wrNext);
	if (eventmask & EV_EXCEPT)
		FD_SET(fd, &ctx->exNext);

	/* Update fdMax. */
	if (fd > ctx->fdMax)
		ctx->fdMax = fd;

	/* Remember the ID if the caller provided us a place for it. */
	if (opaqueID)
		opaqueID->opaque = id;

	evPrintf(ctx, 5,
		"evSelectFD(fd %d, mask 0x%x): new masks: 0x%lx 0x%lx 0x%lx\n",
		 fd, eventmask,
		 (u_long)ctx->rdNext.fds_bits[0],
		 (u_long)ctx->wrNext.fds_bits[0],
		 (u_long)ctx->exNext.fds_bits[0]);

	return (0);
}
Пример #3
0
static void ll_printf(u4_t type, conn_t *c, const char *fmt, va_list ap)
{
	int i, sl;
	char *s, *cp;
	#define VBUF 1024
	
	if (!do_sdr) {
	//if (!background_mode) {
		if ((buf = (char*) malloc(VBUF)) == NULL)
			panic("log malloc");
		vsnprintf(buf, VBUF, fmt, ap);

		// remove our override and call the actual underlying printf
		#undef printf
			printf("%s", buf);
		#define printf ALT_PRINTF
		
		evPrintf(EC_EVENT, EV_PRINTF, -1, "printf", buf);
	
		if (buf) free(buf);
		buf = 0;
		return;
	}
	
	if (appending) {
		s = last_s;
	} else {
		brem = VBUF;
		if ((buf = (char*) malloc(VBUF)) == NULL)
			panic("log malloc");
		s = buf;
		start_s = s;
	}

	vsnprintf(s, brem, fmt, ap);
	sl = strlen(s);		// because vsnprintf returns length disregarding limit, not the actual length
	brem -= sl+1;
	
	cp = &s[sl-1];
	if (*cp != '\n' && brem && !(type & PRINTF_MSG)) {
		last_s = cp+1;
		appending = true;
		return;
	} else {
		appending = false;
	}
	
	// for logging, don't print an empty line at all
	if ((type & (PRINTF_REG | PRINTF_LOG)) && (!background_mode || strcmp(start_s, "\n") != 0)) {

		// remove non-ASCII since "systemctl status" gives [blob] message
		// unlike "systemctl log" which prints correctly
		int sl = strlen(buf);
		for (i=0; i < sl; i++)
			if (buf[i] > 0x7f) buf[i] = '?';

		char up_chan_stat[64], *s = up_chan_stat;
		
		// uptime
		u4_t up = timer_sec();
		u4_t sec = up % 60; up /= 60;
		u4_t min = up % 60; up /= 60;
		u4_t hr  = up % 24; up /= 24;
		u4_t days = up;
		if (days)
			sl = sprintf(s, "%dd:%02d:%02d:%02d ", days, hr, min, sec);
		else
			sl = sprintf(s, "%d:%02d:%02d ", hr, min, sec);
		s += sl;
	
		// show state of all rx channels
		rx_chan_t *rx;
		for (rx = rx_chan, i=0; rx < &rx_chan[RX_CHANS]; rx++, i++) {
			*s++ = rx->busy? '0'+i : '.';
		}
		*s++ = ' ';
		
		// show rx channel number if message is associated with a particular rx channel
		if (c != NULL) {
			for (i=0; i < RX_CHANS; i++)
				*s++ = (i == c->rx_channel)? '0'+i : ' ';
		} else {
			for (i=0; i < RX_CHANS; i++) *s++ = ' ';
		}
		*s = 0;
		
		if (((type & PRINTF_LOG) && (background_mode || log_foreground_mode)) || log_ordinary_printfs) {
			syslog(LOG_INFO, "%s %s", up_chan_stat, buf);
		}
	
		time_t t;
		char tb[32];
		time(&t);
		ctime_r(&t, tb);
		tb[24]=0;
		
		// remove our override and call the actual underlying printf
		#undef printf
			printf("%s %s %s", tb, up_chan_stat, buf);
		#define printf ALT_PRINTF

		evPrintf(EC_EVENT, EV_PRINTF, -1, "printf", buf);
	}
	
	// attempt to also record message remotely
	if ((type & PRINTF_MSG) && msgs_mc) {
		if (type & PRINTF_FF)
			send_encoded_msg_mc(msgs_mc, "MSG", "status_msg", "\f%s", buf);
		else
			send_encoded_msg_mc(msgs_mc, "MSG", "status_msg", "%s", buf);
	}
	
	if (buf) free(buf);
	buf = 0;
}