static int open_socket(OmlNetOutStream* self) { if (strcmp(self->protocol, "tcp") == 0) { Socket* sock; if ((sock = socket_tcp_out_new("sock", (char*)self->host, self->port)) == NULL) { return 0; } self->socket = sock; self->header_written = 0; } else { logerror("Net_stream: unsupported transport protocol '%s'\n", self->protocol); return 0; } // Catching SIGPIPE signals if the associated socket is closed // TODO: Not exactly sure if this is completely right for all application situations. struct sigaction new_action, old_action; new_action.sa_handler = termination_handler; sigemptyset (&new_action.sa_mask); new_action.sa_flags = 0; sigaction (SIGPIPE, NULL, &old_action); if (old_action.sa_handler != SIG_IGN) sigaction (SIGPIPE, &new_action, NULL); return 1; }
/** Open an OComm Socket with the parameters of this OmlNetOutStream * * This function tries to register a signal handler to catch closed sockets * (SIGPIPE), but sometimes doesn't (XXX: the conditions need to be clarified). * * \param self OmlNetOutStream containing the parameters * \return 1 on success, 0 on error * * \see signal_handler */ static int open_socket(OmlNetOutStream* self) { struct sigaction new_action, old_action; if(self->socket) { socket_free(self->socket); self->socket = NULL; } if (strcmp(self->protocol, "tcp") == 0) { Socket* sock; if ((sock = socket_tcp_out_new(self->dest, self->host, self->service)) == NULL) { return 0; } self->socket = sock; self->header_written = 0; } else { logerror("%s: Unsupported transport protocol '%s'\n", self->dest, self->protocol); return 0; } // Catching SIGPIPE signals if the associated socket is closed // TODO: Not exactly sure if this is completely right for all application situations. new_action.sa_handler =signal_handler; sigemptyset (&new_action.sa_mask); new_action.sa_flags = 0; sigaction (SIGPIPE, NULL, &old_action); /* XXX: Shouldn't we set up the handler ONLY if the old one is SIG_IGN? */ if (old_action.sa_handler != SIG_IGN) { sigaction (SIGPIPE, &new_action, NULL); } return 1; }