Permalink
Browse files

Handle SIGPIPE

  • Loading branch information...
1 parent 01a8e84 commit 417d27e3734409866a287a44ed27559c9581e4ea Sound and Fury committed Sep 17, 2010
Showing with 56 additions and 2 deletions.
  1. +36 −2 irc.c
  2. +5 −0 irc.h
  3. +14 −0 quirc.c
  4. +1 −0 quirc.h
View
@@ -8,6 +8,11 @@
#include "irc.h"
+void handle_sigpipe(int sig)
+{
+ sigpipe=1;
+}
+
int irc_connect(char *server, char *portno, fd_set *master, int *fdmax)
{
int serverhandle;
@@ -126,7 +131,8 @@ int autoconnect(fd_set *master, int *fdmax, servlist *serv)
int irc_tx(int fd, char * packet)
{
- char pq[512];
+ sigpipe=0;
+ char pq[512];
low_quote(packet, pq);
unsigned long l=min(strlen(pq), 511);
unsigned long p=0;
@@ -135,12 +141,27 @@ int irc_tx(int fd, char * packet)
signed long j=send(fd, pq+p, l-p, 0);
if(j<1)
{
+ if(sigpipe)
+ break;
if(errno==EINTR)
continue;
return(p); // Something went wrong with send()!
}
p+=j;
}
+ if(sigpipe)
+ {
+ #ifdef HAVE_DEBUG
+ if(debug)
+ {
+ char tmsg[32+strlen(pq)];
+ sprintf(tmsg, "%d, %lu bytes: %s", fd, p, pq);
+ w_buf_print(0, c_status, tmsg, "DBG SIGPIPE tx: ");
+ }
+ #endif // HAVE_DEBUG
+ sigpipe=0;
+ return(-1);
+ }
send(fd, "\n", 1, 0);
#ifdef HAVE_DEBUG
if(debug)
@@ -150,11 +171,17 @@ int irc_tx(int fd, char * packet)
w_buf_print(0, c_status, tmsg, "DBG tx: ");
}
#endif // HAVE_DEBUG
+ if(sigpipe)
+ {
+ sigpipe=0;
+ return(-1);
+ }
return(l); // Return the number of bytes sent
}
int irc_rx(int fd, char ** data)
{
+ sigpipe=0;
char buf[512];
unsigned long int l=0;
bool cr=false;
@@ -173,6 +200,8 @@ int irc_rx(int fd, char ** data)
}
else if(bytes<0)
{
+ if(sigpipe)
+ break;
if(errno==EINTR)
continue;
int b;
@@ -194,9 +223,14 @@ int irc_rx(int fd, char ** data)
{
char rmsg[32+strlen(buf)];
sprintf(rmsg, "%d, %lu bytes: %s", fd, l, buf);
- w_buf_print(0, c_status, rmsg, "DBG rx: ");
+ w_buf_print(0, c_status, rmsg, sigpipe?"DBG SIGPIPE rx: ":"DBG rx: ");
}
#endif // HAVE_DEBUG
+ if(sigpipe)
+ {
+ sigpipe=0;
+ return(-1);
+ }
if(!*data)
return(1);
return(0);
View
@@ -21,6 +21,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
+#include <signal.h>
typedef enum
{
@@ -48,6 +49,10 @@ message;
#define MQUOTE '\020'
+volatile sig_atomic_t sigpipe;
+
+void handle_sigpipe(int sig);
+
int irc_connect(char *server, char *portno, fd_set *master, int *fdmax); // non-blocking
int irc_conn_rest(int b, char *nick, char *username, char *fullname); // call this when the non-blocking connect() has finished
int autoconnect(fd_set *master, int *fdmax, servlist *serv);
View
@@ -23,6 +23,20 @@ int main(int argc, char *argv[])
#ifdef USE_MTRACE
mtrace();
#endif // USE_MTRACE
+
+ sigpipe=0;
+ struct sigaction sa;
+ sa.sa_handler = handle_sigpipe;
+ sa.sa_flags=0;
+ sigemptyset(&sa.sa_mask);
+
+ if(sigaction(SIGPIPE, &sa, NULL)==-1)
+ {
+ fprintf(stderr, "Failed to set SIGPIPE handler\n");
+ perror("sigaction");
+ return(1);
+ }
+
int infc=fcntl(STDIN_FILENO, F_GETFD);
char *fcfail=NULL;
if(infc>=0)
View
@@ -14,6 +14,7 @@
#include <sys/time.h>
#include <ctype.h>
#include <errno.h>
+#include <signal.h>
#ifdef USE_MTRACE
#include <mcheck.h>
#endif // USE_MTRACE

0 comments on commit 417d27e

Please sign in to comment.