Small changes, nicer deallocation, nonblocking.

git-svn-id: file:///svn/unbound/trunk@158 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2007-03-01 15:50:57 +00:00
parent a3abab528b
commit 3a1187b2f6
5 changed files with 50 additions and 19 deletions

View file

@ -293,6 +293,12 @@ worker_create(struct daemon* daemon, int id)
log_err("socketpair: %s", strerror(errno)); log_err("socketpair: %s", strerror(errno));
return NULL; return NULL;
} }
if(!fd_set_nonblock(sv[0]) || !fd_set_nonblock(sv[1])) {
close(sv[0]);
close(sv[1]);
free(worker);
return NULL;
}
worker->cmd_send_fd = sv[0]; worker->cmd_send_fd = sv[0];
worker->cmd_recv_fd = sv[1]; worker->cmd_recv_fd = sv[1];
} }
@ -313,6 +319,10 @@ worker_init(struct worker* worker, struct config_file *cfg,
return 0; return 0;
} }
if(do_sigs) { if(do_sigs) {
ub_thread_sig_unblock(SIGHUP);
ub_thread_sig_unblock(SIGINT);
ub_thread_sig_unblock(SIGQUIT);
ub_thread_sig_unblock(SIGTERM);
worker->comsig = comm_signal_create(worker->base, worker->comsig = comm_signal_create(worker->base,
worker_sighandler, worker); worker_sighandler, worker);
if(!worker->comsig || !comm_signal_bind(worker->comsig, SIGHUP) if(!worker->comsig || !comm_signal_bind(worker->comsig, SIGHUP)
@ -323,10 +333,6 @@ worker_init(struct worker* worker, struct config_file *cfg,
worker_delete(worker); worker_delete(worker);
return 0; return 0;
} }
ub_thread_sig_unblock(SIGHUP);
ub_thread_sig_unblock(SIGINT);
ub_thread_sig_unblock(SIGQUIT);
ub_thread_sig_unblock(SIGTERM);
} else { /* !do_sigs */ } else { /* !do_sigs */
worker->comsig = 0; worker->comsig = 0;
} }
@ -393,12 +399,6 @@ worker_delete(struct worker* worker)
{ {
if(!worker) if(!worker)
return; return;
if(worker->cmd_send_fd != -1)
close(worker->cmd_send_fd);
worker->cmd_send_fd = -1;
if(worker->cmd_recv_fd != -1)
close(worker->cmd_recv_fd);
worker->cmd_recv_fd = -1;
listen_delete(worker->front); listen_delete(worker->front);
outside_network_delete(worker->back); outside_network_delete(worker->back);
comm_signal_delete(worker->comsig); comm_signal_delete(worker->comsig);
@ -406,6 +406,14 @@ worker_delete(struct worker* worker)
comm_base_delete(worker->base); comm_base_delete(worker->base);
ub_randfree(worker->rndstate); ub_randfree(worker->rndstate);
free(worker->rndstate); free(worker->rndstate);
/* close fds after deleting commpoints, to be sure.
Also epoll does not like closing fd before event_del */
if(worker->cmd_send_fd != -1)
close(worker->cmd_send_fd);
worker->cmd_send_fd = -1;
if(worker->cmd_recv_fd != -1)
close(worker->cmd_recv_fd);
worker->cmd_recv_fd = -1;
free(worker); free(worker);
} }

View file

@ -1,3 +1,8 @@
1 March 2007: Wouter
- Signals, libevent and threads work well, with libevent patch and
changes to code (close after event_del).
- set ipc pipes nonblocking.
27 February 2007: Wouter 27 February 2007: Wouter
- ub_thread_join portable definition. - ub_thread_join portable definition.
- forking is used if no threading is available. - forking is used if no threading is available.

View file

@ -89,7 +89,7 @@ verbose_print_addr(struct addrinfo *addr)
int int
create_udp_sock(struct addrinfo *addr) create_udp_sock(struct addrinfo *addr)
{ {
int s, flag; int s;
# if defined(IPV6_V6ONLY) # if defined(IPV6_V6ONLY)
int on=1; int on=1;
# endif # endif
@ -128,15 +128,8 @@ create_udp_sock(struct addrinfo *addr)
log_err("can't bind socket: %s", strerror(errno)); log_err("can't bind socket: %s", strerror(errno));
return -1; return -1;
} }
if((flag = fcntl(s, F_GETFL)) == -1) { if(!fd_set_nonblock(s))
log_err("can't fcntl F_GETFL: %s", strerror(errno));
flag = 0;
}
flag |= O_NONBLOCK;
if(fcntl(s, F_SETFL, flag) == -1) {
log_err("can't fcntl F_SETFL: %s", strerror(errno));
return -1; return -1;
}
return s; return s;
} }

View file

@ -39,6 +39,8 @@
#include "config.h" #include "config.h"
#include "util/net_help.h" #include "util/net_help.h"
#include "util/log.h"
#include <fcntl.h>
/** returns true is string addr is an ip6 specced address. */ /** returns true is string addr is an ip6 specced address. */
int int
@ -69,3 +71,19 @@ write_socket(int s, const void *buf, size_t size)
} }
return 1; return 1;
} }
int
fd_set_nonblock(int s)
{
int flag;
if((flag = fcntl(s, F_GETFL)) == -1) {
log_err("can't fcntl F_GETFL: %s", strerror(errno));
flag = 0;
}
flag |= O_NONBLOCK;
if(fcntl(s, F_SETFL, flag) == -1) {
log_err("can't fcntl F_SETFL: %s", strerror(errno));
return 0;
}
return 1;
}

View file

@ -59,4 +59,11 @@ int str_is_ip6(const char* str);
int int
write_socket(int s, const void *buf, size_t size); write_socket(int s, const void *buf, size_t size);
/**
* Set fd nonblocking.
* @param s: file descriptor.
* @return: 0 on error (error is printed to log).
*/
int fd_set_nonblock(int s);
#endif /* NET_HELP_H */ #endif /* NET_HELP_H */