static int kmsg_ring_fill()

in ncrx/nctx.c [99:143]


static int kmsg_ring_fill(struct kmsg_ring *ring, int devkmsg)
{
	char buf[NCRX_LINE_MAX];
	struct kmsg_slot *slot;
	int level;
	uint64_t seq;
	ssize_t len;

next_line:
	do {
		len = read(devkmsg, buf, sizeof(buf) - 1);
		/*
		 * EPIPE indicates skipped messages.  kmsgs are always
		 * stored according to their sequence numbers, so we don't
		 * need to do anything special on EPIPE.  Keep reading.
		 */
	} while (len < 0 && errno == EPIPE);

	if (len < 0) {
		if (errno == EAGAIN)
			return 0;
		return -1;
	}

	/* read seq and see if it makes sense */
	buf[len] = '\0';
	if (sscanf(buf, "%d,%"SCNu64",", &level, &seq) != 2 ||
	    seq < ring->head_seq) {
		fprintf(stderr, "Warning: malformed kmsg \"%s\"\n", buf);
		goto next_line;
	}

	/* wind ring till head is at the right slot and store */
	while (ring->head_seq < seq)
		kmsg_ring_advance(ring);

	slot = &ring->slots[ring->head];
	slot->msg = strdup(buf);
	if (!slot->msg)
		return -1;

	slot->ts = current_msec();
	kmsg_ring_advance(ring);
	goto next_line;
}