Skip to content

Instantly share code, notes, and snippets.

@umitanuki
Created April 29, 2013 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umitanuki/5482907 to your computer and use it in GitHub Desktop.
Save umitanuki/5482907 to your computer and use it in GitHub Desktop.
Attempt to cause EINTER but read is not interrupted by signal...
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void
sigint_handler(int sig)
{
fprintf(stderr, "signal caught\n");
return;
}
int
make_socket(void)
{
int sock, port = 80;
struct sockaddr_in serv_addr;
struct hostent *server;
server = gethostbyname("www.google.com");
if (server == NULL)
fprintf(stderr, "gethostbyname failed\n");
sock = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
serv_addr.sin_port = htons(port);
if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
fprintf(stderr, "connect failed\n");
return sock;
}
int
make_pipe(void)
{
int pipefd[2];
if (pipe(pipefd) < 0)
fprintf(stderr, "pipe failed\n");
return pipefd[0];
}
int
main(int argc, char **argv)
{
struct sigaction act, oact;
int fd;
char buf[512];
int readlen;
int use_socket = 1;
act.sa_handler = sigint_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &act, &oact);
if (use_socket)
fd = make_socket();
else
fd = make_pipe();
readlen = read(fd, buf, 512);
if (readlen == -1)
{
int err = errno;
fprintf(stderr, "erro = %d\n", err);
}
fprintf(stderr, "readlen = %d\n", readlen);
return readlen;
}
PROG = run
OBJS = main.o
all: $(PROG)
$(PROG): $(OBJS)
$(CC) -o $(PROG) $(OBJS)
clean:
rm -f $(OBJS) $(PROG)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment