From 3866231ce4dd27533e086490b84b5363d788721d Mon Sep 17 00:00:00 2001 From: Daniel Wagner Date: Sun, 23 Oct 2011 14:16:29 +0200 Subject: [PATCH] core: Add SOCK_CLOEXEC to socket() Make sure all file descriptor are closed. --- src/6to4.c | 6 +++--- src/dnsproxy.c | 4 ++-- src/inet.c | 56 ++++++++++++++++++++++++++++---------------------------- src/iptables.c | 2 +- src/ntp.c | 2 +- src/rtnl.c | 4 ++-- src/tethering.c | 4 ++-- 7 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/6to4.c b/src/6to4.c index 509be26..35540bf 100644 --- a/src/6to4.c +++ b/src/6to4.c @@ -118,7 +118,7 @@ static int rtnl_open(struct rtnl_handle *rth) memset(rth, 0, sizeof(*rth)); - rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); if (rth->fd < 0) { connman_error("Can not open netlink socket: %s", strerror(errno)); @@ -213,7 +213,7 @@ static int tunnel_create(struct in_addr *addr) strncpy(ifr.ifr_name, "sit0", IFNAMSIZ); ifr.ifr_ifru.ifru_data = (void *)&p; - fd = socket(AF_INET, SOCK_DGRAM, 0); + fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); ret = ioctl(fd, SIOCADDTUNNEL, &ifr); if (ret) connman_error("add tunnel %s failed: %s", ifr.ifr_name, @@ -245,7 +245,7 @@ static void tunnel_destroy() strncpy(ifr.ifr_name, "tun6to4", IFNAMSIZ); ifr.ifr_ifru.ifru_data = (void *)&p; - fd = socket(AF_INET, SOCK_DGRAM, 0); + fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (fd < 0) { connman_error("socket failed: %s", strerror(errno)); return; diff --git a/src/dnsproxy.c b/src/dnsproxy.c index c889a65..960207f 100644 --- a/src/dnsproxy.c +++ b/src/dnsproxy.c @@ -1349,12 +1349,12 @@ static int create_dns_listener(int protocol, struct listener_data *ifdata) switch (protocol) { case IPPROTO_UDP: proto = "UDP"; - type = SOCK_DGRAM; + type = SOCK_DGRAM | SOCK_CLOEXEC; break; case IPPROTO_TCP: proto = "TCP"; - type = SOCK_STREAM; + type = SOCK_STREAM | SOCK_CLOEXEC; break; default: diff --git a/src/inet.c b/src/inet.c index f7f72b8..d0a3edc 100644 --- a/src/inet.c +++ b/src/inet.c @@ -150,7 +150,7 @@ int __connman_inet_modify_address(int cmd, int flags, return err; } - sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); + sk = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE); if (sk < 0) return -errno; @@ -177,7 +177,7 @@ int connman_inet_ifindex(const char *name) if (name == NULL) return -1; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -1; @@ -202,7 +202,7 @@ char *connman_inet_ifname(int index) if (index < 0) return NULL; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return NULL; @@ -224,7 +224,7 @@ short int connman_inet_ifflags(int index) struct ifreq ifr; int sk, err; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -errno; @@ -254,7 +254,7 @@ int connman_inet_ifup(int index) struct ifreq ifr; int sk, err; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -errno; @@ -296,7 +296,7 @@ int connman_inet_ifdown(int index) struct ifreq ifr; int sk, err; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -errno; @@ -341,7 +341,7 @@ static char *index2addr(int index) if (index < 0) return NULL; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return NULL; @@ -384,7 +384,7 @@ static char *index2ident(int index, const char *prefix) if (index < 0) return NULL; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return NULL; @@ -428,7 +428,7 @@ connman_bool_t connman_inet_is_cfg80211(int index) struct ifreq ifr; int sk; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return FALSE; @@ -653,7 +653,7 @@ int connman_inet_add_network_route(int index, const char *host, struct sockaddr_in addr; int sk, err; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -1; @@ -715,7 +715,7 @@ int connman_inet_del_network_route(int index, const char *host) struct sockaddr_in addr; int sk, err; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -1; @@ -773,7 +773,7 @@ int connman_inet_del_ipv6_network_route(int index, const char *host, rt.rtmsg_metric = 1; rt.rtmsg_ifindex = index; - sk = socket(AF_INET6, SOCK_DGRAM, 0); + sk = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) { err = -1; goto out; @@ -824,7 +824,7 @@ int connman_inet_add_ipv6_network_route(int index, const char *host, rt.rtmsg_metric = 1; rt.rtmsg_ifindex = index; - sk = socket(AF_INET6, SOCK_DGRAM, 0); + sk = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) { err = -1; goto out; @@ -867,7 +867,7 @@ int connman_inet_set_ipv6_gateway_address(int index, const char *gateway) rt.rtmsg_dst_len = 0; rt.rtmsg_ifindex = index; - sk = socket(AF_INET6, SOCK_DGRAM, 0); + sk = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) { err = -1; goto out; @@ -904,7 +904,7 @@ int connman_inet_clear_ipv6_gateway_address(int index, const char *gateway) rt.rtmsg_dst_len = 0; rt.rtmsg_ifindex = index; - sk = socket(AF_INET6, SOCK_DGRAM, 0); + sk = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) { err = -1; goto out; @@ -927,7 +927,7 @@ int connman_inet_set_gateway_address(int index, const char *gateway) struct sockaddr_in addr; int sk, err; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -1; @@ -978,7 +978,7 @@ int connman_inet_set_gateway_interface(int index) DBG(""); - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -1; @@ -1024,7 +1024,7 @@ int connman_inet_set_ipv6_gateway_interface(int index) DBG(""); - sk = socket(PF_INET6, SOCK_DGRAM, 0); + sk = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -1; @@ -1069,7 +1069,7 @@ int connman_inet_clear_gateway_address(int index, const char *gateway) DBG(""); - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -1; @@ -1120,7 +1120,7 @@ int connman_inet_clear_gateway_interface(int index) DBG(""); - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -1; @@ -1166,7 +1166,7 @@ int connman_inet_clear_ipv6_gateway_interface(int index) DBG(""); - sk = socket(PF_INET6, SOCK_DGRAM, 0); + sk = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return -1; @@ -1219,7 +1219,7 @@ connman_bool_t connman_inet_compare_subnet(int index, const char *host) return -1; host_addr = _host_addr.s_addr; - sk = socket(PF_INET, SOCK_DGRAM, 0); + sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return FALSE; @@ -1257,7 +1257,7 @@ int connman_inet_remove_from_bridge(int index, const char *bridge) if (bridge == NULL) return -EINVAL; - sk = socket(AF_INET, SOCK_STREAM, 0); + sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sk < 0) return sk; @@ -1286,7 +1286,7 @@ int connman_inet_add_to_bridge(int index, const char *bridge) if (bridge == NULL) return -EINVAL; - sk = socket(AF_INET, SOCK_STREAM, 0); + sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sk < 0) return sk; @@ -1312,7 +1312,7 @@ int connman_inet_set_mtu(int index, int mtu) struct ifreq ifr; int sk, err; - sk = socket(AF_INET, SOCK_DGRAM, 0); + sk = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return sk; @@ -1339,7 +1339,7 @@ int connman_inet_setup_tunnel(char *tunnel, int mtu) if (tunnel == NULL) return -EINVAL; - sk = socket(AF_INET, SOCK_DGRAM, 0); + sk = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) return sk; @@ -1576,7 +1576,7 @@ static int ndisc_send_unspec(int type, int oif, const struct in6_addr *dest) DBG(""); - fd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW); + fd = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW); if (fd < 0) return -errno; @@ -1690,7 +1690,7 @@ int __connman_inet_ipv6_send_rs(int index, int timeout, data->user_data = user_data; data->rs_timeout = g_timeout_add_seconds(timeout, rs_timeout_cb, data); - sk = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); + sk = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6); if (sk < 0) return -errno; diff --git a/src/iptables.c b/src/iptables.c index 0440cdf..68219ee 100644 --- a/src/iptables.c +++ b/src/iptables.c @@ -1167,7 +1167,7 @@ static struct connman_iptables *iptables_init(char *table_name) if (table->info == NULL) goto err; - table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); + table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW); if (table->ipt_sock < 0) goto err; diff --git a/src/ntp.c b/src/ntp.c index 32ae714..fa4f624 100644 --- a/src/ntp.c +++ b/src/ntp.c @@ -266,7 +266,7 @@ static void start_ntp(const char *server) if (channel_watch > 0) return; - fd = socket(PF_INET, SOCK_DGRAM, 0); + fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (fd < 0) { connman_error("Failed to open time server socket"); return; diff --git a/src/rtnl.c b/src/rtnl.c index d605917..39afb12 100644 --- a/src/rtnl.c +++ b/src/rtnl.c @@ -110,7 +110,7 @@ static connman_bool_t wext_interface(char *ifname) struct iwreq wrq; int fd, err; - fd = socket(PF_INET, SOCK_DGRAM, 0); + fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (fd < 0) return FALSE; @@ -1568,7 +1568,7 @@ int __connman_rtnl_init(void) interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_interface); - sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); + sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE); if (sk < 0) return -1; diff --git a/src/tethering.c b/src/tethering.c index 0357530..c362b8d 100644 --- a/src/tethering.c +++ b/src/tethering.c @@ -206,7 +206,7 @@ static int create_bridge(const char *name) DBG("name %s", name); - sk = socket(AF_INET, SOCK_STREAM, 0); + sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sk < 0) return -EOPNOTSUPP; @@ -231,7 +231,7 @@ static int remove_bridge(const char *name) DBG("name %s", name); - sk = socket(AF_INET, SOCK_STREAM, 0); + sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sk < 0) return -EOPNOTSUPP; -- 2.7.4