This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

SIGALRM doesn't work?


Hi, could someone verify that the following program does not cause an alarm signal to be generated?  Does SIGALRM work at all with b18?  That's the version I have on my NT workstation.  The NT system id is Microsoft Windows NT 4.00.1381.  Does this help identify my system?  I have 64 meg ram, 1 gig harddrive, and connected to the my lan through an ne2000 clone.

--jc

------------------------------------------ begin ---------------------------------------------
/*
 * socket.c
 */

#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

long jiffies = 0xffffff00L;

int
cbreak(struct termios *saved)
	{
	struct termios tio;
	if (tcgetattr(STDIN_FILENO, saved) < 0)
		{
		fprintf(stderr, "tcgetattr failed.\n");
		return -1;
		}
	tio = *saved;
	tio.c_lflag &= ~(ICANON);
	tio.c_cc[VMIN] = 0;
	tio.c_cc[VTIME] = 0;
	if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) < 0)
		{
		fprintf(stderr, "tcsetattr failed.\n");
		return -1;
		}
	return 0;
	}

int
server_wait(int port)
	{
	int lfd, cfd; /* Listen file descriptor. */
	struct sockaddr_in saddr, caddr; /* Server/client address structure. */
	int clen;

	/* Get socket file descriptor. */
	if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		{
		perror("socket");
		return -1;
		}

	/* Bind to address. */
	memset(&saddr, 0, sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_addr.s_addr = htonl(INADDR_ANY);
	saddr.sin_port = htons(port);
	if (bind(lfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
		{
		perror("bind");
		return -1;
		}

	/* Listen on socket. */
	if (listen(lfd, 1) < 0)
		{
		perror("bind");
		return -1;
		}

	/* Accept new connections, block until one is found. */
	if ((cfd = accept(lfd, (struct sockaddr *) &caddr, &clen)) < 0)
		{
		perror("accept");
		return -1;
		}

	fprintf(stderr, "Connected with %s.\n", inet_ntoa(caddr.sin_addr));
	close(lfd); /* Close listen file descriptor. */
	return cfd;
	}

int
client_wait(const char *ip_addr, int port)
	{
	int cfd;
	struct sockaddr_in saddr;

	/* Get socket file descriptor. */
	if ((cfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		{
		perror("socket");
		return -1;
		}

	/* Connect to server. */
	memset(&saddr, 0, sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_addr.s_addr = inet_addr(ip_addr);
	saddr.sin_port = htons(port);
	if (connect(cfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
		{
		perror("connect");
		return -1;
		}

	return cfd;
	}

int
socket_handler(int fd)
	{
	int len;
	char buf[256];
	if ((len = read(fd, buf, sizeof(buf))) < 0)
		{
		fprintf(stderr, "Read failed with %d.\n", len);
		return -1;
		}
	printf("%s", buf);
	fflush(stdout);
	if (buf[0] == 'q')
		return -1;
	return 0;
	}

int
terminal_handler(int fd)
	{
	int len;
	char buf[256];
	if ((len = read(STDIN_FILENO, buf, sizeof(buf))) < 0)
		{
		fprintf(stderr, "Read failed with %d.\n", len);
		return -1;
		}
	write(fd, buf, len);
	if (buf[0] == 'q')
		return -1;
	return 0;
	}

void
timer_handler()
	{
	fprintf(stderr, "This program has been in the main loop for %ld seconds.\n", jiffies);
	++jiffies;
	alarm(1);
	}

int
main(int argc, char *argv[])
	{
	struct termios saved_tio;
	struct sigaction sact;
	int sockfd = -1;

	if (argc > 1)
		{
		if (argv[1][1] == 'c' && argc == 4)
			{
			fprintf(stderr, "Waiting for server connection.\n");
			sockfd = client_wait(argv[2], atoi(argv[3]));
			}
		else if (argv[1][1] == 's' && argc == 3)
			{
			fprintf(stderr, "Waiting for client connection.\n");
			sockfd = server_wait(atoi(argv[2]));
			}
		else
			goto error_handler;
		}
	else
		goto error_handler;

	if (sockfd < 0)
		{
		fprintf(stderr, "Link layer connection failed.\n");
		goto error_handler;
		}
	if (cbreak(&saved_tio) < 0)
		goto error_handler;
	sact.sa_handler = timer_handler;
	sigemptyset(&sact.sa_mask);
	sact.sa_flags = 0;
	if (sigaction(SIGALRM, &sact, 0) < 0)
		{
		fprintf(stderr, "Can't register signal handler for alarm.\n");
		goto error_handler;
		}
	alarm(1);

	fprintf(stderr, "Entering main loop.\n");
	for (;;)
		{
		struct timeval tv = { 0, 1000 };
		fd_set rdset;
		int fds;

		FD_ZERO(&rdset);
		FD_SET(STDIN_FILENO, &rdset);
		FD_SET(sockfd, &rdset);

		fds = select(sockfd+1, &rdset, 0, 0, 0);
		if (fds < 0)
			{
			if (errno == EINTR)
				{
				fprintf(stderr, "Interrupted, continuing.\n");
				continue;
				}
			else
				break;
			}

		if (fds > 0 && FD_ISSET(STDIN_FILENO, &rdset))
			{
			--fds;
			if (terminal_handler(sockfd) < 0)
				break;
			}

		if (fds > 0 && FD_ISSET(sockfd, &rdset))
			{
			--fds;
			if (socket_handler(sockfd) < 0)
				break;
			}
		}

	tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio);
	close(sockfd);
	return 0;

error_handler:
	fprintf(stderr, "syntax:  %s [-c | -s ip_addr] port\n", argv[0]);
	return 1;
	}
------------------------------------------ end -----------------------------------------------
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]