view rvinterf/lowlevel/rvifmain.c @ 179:ebfa9657d03d

rvinterf: ignore SIGPIPE
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 23 Nov 2013 22:21:20 +0000
parents 7ab6b29e76bb
children f5f8776f7c66
line wrap: on
line source

/*
 * This module contains the main() function for rvinterf
 */

#include <sys/types.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include "../pktmux.h"
#include "../localsock.h"
#include "client.h"

extern int target_fd, listener;
extern char *baudrate_name;

extern u_char rxpkt[];
extern size_t rxpkt_len;

struct client *client_head;

char *logfname;
FILE *logF;
time_t logtime;
int background;
int max_fd;

main(argc, argv)
	char **argv;
{
	extern char *optarg;
	extern int optind;
	int c;
	fd_set fds;
	struct client *cli, **clip;

	while ((c = getopt(argc, argv, "bB:d:l:")) != EOF)
		switch (c) {
		case 'b':
			background++;
			continue;
		case 'B':
			baudrate_name = optarg;
			continue;
		case 'd':
			target_fd = atoi(optarg);
			continue;
		case 'l':
			logfname = optarg;
			continue;
		case '?':
		default:
usage:			fprintf(stderr,
				"usage: %s [options] ttyport\n", argv[0]);
			exit(1);
		}
	if (target_fd <= 0) {
		if (argc - optind != 1)
			goto usage;
		open_target_serial(argv[optind]);
	}
	max_fd = target_fd;

	set_serial_nonblock(0);
	setlinebuf(stdout);
	if (logfname) {
		logF = fopen(logfname, "w");
		if (!logF) {
			perror(logfname);
			exit(1);
		}
		fprintf(logF, "*** Log of rvinterf session ***\n");
		setlinebuf(logF);
	}
	create_listener_socket();
	if (background) {
		c = fork();
		if (c < 0) {
			perror("fork");
			exit(1);
		}
		if (c) {
			printf("rvinterf forked into background (pid %d)\n", c);
			exit(0);
		}
	}
	signal(SIGPIPE, SIG_IGN);
	for (;;) {
		FD_ZERO(&fds);
		FD_SET(target_fd, &fds);
		FD_SET(listener, &fds);
		for (clip = &client_head; cli = *clip; ) {
			if (cli->rx_state == 2) {
				close(cli->fd);
				*clip = cli->next;
				free(cli);
				continue;
			}
			FD_SET(cli->fd, &fds);
			clip = &cli->next;
		}
		c = select(max_fd+1, &fds, 0, 0, 0);
		time(&logtime);
		if (c < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			exit(1);
		}
		if (FD_ISSET(target_fd, &fds))
			process_serial_rx();
		if (FD_ISSET(listener, &fds))
			handle_listener_select();
		for (cli = client_head; cli; cli = cli->next)
			if (FD_ISSET(cli->fd, &fds))
				handle_client_select(cli);
	}
}

handle_rx_packet()
{
	switch (rxpkt[0]) {
	case RVT_RV_HEADER:
		if (rxpkt_len < 6)
			goto unknown;
		if (!background || logF)
			print_rv_trace();
		if (client_head)
			forward_rv_trace();
		return;
	case RVT_L1_HEADER:
		if (!background || logF)
			print_l1_trace();
		if (client_head)
			forward_nonrvt_pkt();
		return;
	case RVT_L23_HEADER:
		if (!background || logF)
			print_g23_trace();
		if (client_head)
			forward_nonrvt_pkt();
		return;
	case RVT_TM_HEADER:
		if (!background || logF)
			print_etm_output_raw();
		if (client_head)
			forward_nonrvt_pkt();
		return;
	default:
	unknown:
		print_unknown_packet();
	}
}