Доброго времени суток. Есть код демона - принимает файл и отправляет на обработку.
#include "listener.h"
#include "ssl.h"
#include <signal.h>
void clean_up_child_process( int signal_number ) {
int status;
wait( &status );
}
void server_start( struct conn_inf *data_conn ) {
int err;
int exit_code;
int connection;
int server_connection;
pid_t child_pid;
size_t client_len;
socklen_t address_length;
struct sigaction sigchld_action;
struct sockaddr_in sa_serv;
struct sockaddr_in sa_cli;
exit_code = 0;
memset( &sigchld_action, 0, sizeof( sigchld_action ) );
sigchld_action.sa_handler = &clean_up_child_process;
sigaction( SIGCHLD, &sigchld_action, NULL );
server_connection = socket( AF_INET, SOCK_STREAM, 0 );
CHK_ERR( server_connection, "socket" );
memset( &sa_serv, '\0', sizeof( sa_serv ) );
sa_serv.sin_family = AF_INET;
sa_serv.sin_addr = data_conn->addr;
sa_serv.sin_port = data_conn->port;
err = bind( server_connection, (struct sockaddr*)&sa_serv, sizeof( sa_serv ) );
CHK_ERR( err, "bind" );
err = listen( server_connection, 5 );
CHK_ERR( err, "listen" );
if ( data_conn->verbose == 1 ) {
address_length = sizeof( sa_serv );
err = getsockname( server_connection, (struct sockaddr*)&sa_serv, &address_length );
fprintf( stderr, "server listening on %s:%d.\n", inet_ntoa( sa_serv.sin_addr ), (int)ntohs( sa_serv.sin_port ) );
}
while ( 1 ) {
client_len = sizeof( sa_cli );
connection = accept( server_connection, (struct sockaddr*)&sa_cli, &client_len );
CHK_ERR( connection , "accept" );
/* fork child process */
if ( data_conn->verbose == 1 ) {
address_length = sizeof( sa_serv ); err = getpeername( connection, (struct sockaddr*)&sa_serv, &address_length );
fprintf( stderr, "Connection from %s\n", inet_ntoa( sa_serv.sin_addr ) );
}
child_pid = fork();
if ( child_pid == 0 ) { close( server_connection );
close( STDIN_FILENO );
close( STDOUT_FILENO );
exit_code = handler_connection( connection, data_conn );
close( connection );
exit( exit_code );
} else if ( child_pid > 0 ) {
close( connection );
} else {
perror ( "Could not create child process.\n" );
}
}
}
Пока не включаю обработку сигнала завершения дочернего процесса все работает, после включения выдает - прерванного системного вызова. Код обработки SIGCHLD взят из примера.