ocloexec
authorAnas Nashif <anas.nashif@intel.com>
Sun, 4 Nov 2012 14:55:36 +0000 (06:55 -0800)
committerZhang zhengguang <zhengguang.zhang@intel.com>
Fri, 12 Dec 2014 04:50:59 +0000 (12:50 +0800)
17 files changed:
configure.in
fad-gifc.c
fad-glifc.c
inet.c
nametoaddr.c
pcap-bt-linux.c
pcap-can-linux.c
pcap-canusb-linux.c
pcap-linux.c
pcap-netfilter-linux.c
pcap-nit.c
pcap-sita.c
pcap-snit.c
pcap-snoop.c
pcap-usb-linux.c
savefile.c
sf-pcap.c

index 4530aed..3e28d23 100644 (file)
@@ -20,7 +20,9 @@ AC_INIT(pcap.c)
 AC_CANONICAL_SYSTEM
 
 AC_LBL_C_INIT_BEFORE_CC(V_CCOPT, V_INCLS)
-AC_PROG_CC
+AC_PROG_CC_STDC
+AC_USE_SYSTEM_EXTENSIONS
+AC_SYS_LARGEFILE
 AC_LBL_C_INIT(V_CCOPT, V_INCLS)
 AC_LBL_SHLIBS_INIT
 AC_LBL_C_INLINE
index 5c3357c..1af801a 100644 (file)
@@ -152,7 +152,7 @@ pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
        /*
         * Create a socket from which to fetch the list of interfaces.
         */
-       fd = socket(AF_INET, SOCK_DGRAM, 0);
+       fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
        if (fd < 0) {
                (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
                    "socket: %s", pcap_strerror(errno));
index 1a820ad..045808a 100644 (file)
@@ -95,7 +95,7 @@ pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
         * Create a socket from which to fetch the list of interfaces,
         * and from which to fetch IPv4 information.
         */
-       fd4 = socket(AF_INET, SOCK_DGRAM, 0);
+       fd4 = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
        if (fd4 < 0) {
                (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
                    "socket: %s", pcap_strerror(errno));
@@ -105,7 +105,7 @@ pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
        /*
         * Create a socket from which to fetch IPv6 information.
         */
-       fd6 = socket(AF_INET6, SOCK_DGRAM, 0);
+       fd6 = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, 0);
        if (fd6 < 0) {
                (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
                    "socket: %s", pcap_strerror(errno));
diff --git a/inet.c b/inet.c
index 036b2db..82beb06 100644 (file)
--- a/inet.c
+++ b/inet.c
@@ -506,7 +506,7 @@ get_if_description(const char *name)
         */
        memset(&ifrdesc, 0, sizeof ifrdesc);
        strlcpy(ifrdesc.ifr_name, name, sizeof ifrdesc.ifr_name);
-       s = socket(AF_INET, SOCK_DGRAM, 0);
+       s = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
        if (s >= 0) {
 #ifdef __FreeBSD__
                /*
@@ -894,7 +894,7 @@ pcap_lookupnet(device, netp, maskp, errbuf)
                return 0;
        }
 
-       fd = socket(AF_INET, SOCK_DGRAM, 0);
+       fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
        if (fd < 0) {
                (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
                    pcap_strerror(errno));
index e6483a3..1bcaa89 100644 (file)
@@ -438,7 +438,7 @@ pcap_ether_hostton(const char *name)
        static int init = 0;
 
        if (!init) {
-               fp = fopen(PCAP_ETHERS_FILE, "r");
+               fp = fopen(PCAP_ETHERS_FILE, "re");
                ++init;
                if (fp == NULL)
                        return (NULL);
index a32691d..ea70f17 100644 (file)
@@ -80,8 +80,8 @@ bt_findalldevs(pcap_if_t **alldevsp, char *err_str)
        struct hci_dev_req *dev_req;
        int i, sock;
        int ret = 0;
-
-       sock  = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
+       
+       sock  = socket(AF_BLUETOOTH, SOCK_RAW|SOCK_CLOEXEC, BTPROTO_HCI);
        if (sock < 0)
        {
                /* if bluetooth is not supported this this is not fatal*/ 
@@ -214,7 +214,7 @@ bt_activate(pcap_t* handle)
        handlep->dev_id = dev_id;
        
        /* Create HCI socket */
-       handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
+       handle->fd = socket(AF_BLUETOOTH, SOCK_RAW|SOCK_CLOEXEC, BTPROTO_HCI);
        if (handle->fd < 0) {
                snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
                    "Can't create raw socket: %s", strerror(errno));
@@ -318,7 +318,7 @@ bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *us
 
        /* ignore interrupt system call error */
        do {
-               ret = recvmsg(handle->fd, &msg, 0);
+               ret = recvmsg(handle->fd, &msg, MSG_CMSG_CLOEXEC);
                if (handle->break_loop)
                {
                        handle->break_loop = 0;
index 3a9e8cb..48db8e5 100644 (file)
@@ -161,7 +161,7 @@ can_activate(pcap_t* handle)
        handle->stats_op = can_stats_linux;
 
        /* Create socket */
-       handle->fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+       handle->fd = socket(PF_CAN, SOCK_RAW|SOCK_CLOEXEC, CAN_RAW);
        if (handle->fd < 0)
        {
                snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s",
@@ -235,7 +235,7 @@ can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u
 
        do
        {
-               pkth.caplen = recvmsg(handle->fd, &msg, 0);
+               pkth.caplen = recvmsg(handle->fd, &msg, MSG_CMSG_CLOEXEC);
                if (handle->break_loop)
                {
                        handle->break_loop = 0;
index a72f469..fd2d49f 100644 (file)
@@ -36,6 +36,7 @@
 #include "config.h"
 #endif
 
+#include <pthread.h>
 #include <libusb-1.0/libusb.h>
 
 #include <stdlib.h>
@@ -289,8 +290,7 @@ static int canusb_startcapture(struct pcap_canusb* this)
 {
     int pipefd[2];
 
-    if (pipe(pipefd) == -1)
-        return -1;
+  if (pipe2(pipefd, O_CLOEXEC) == -1) return -1;
 
     this->rdpipe = pipefd[0];
     this->wrpipe = pipefd[1];
index 48749c1..62771f5 100644 (file)
@@ -962,7 +962,7 @@ pcap_can_set_rfmon_linux(pcap_t *handle)
         * (We assume that if we have Wireless Extensions support
         * we also have PF_PACKET support.)
         */
-       sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+       sock_fd = socket(PF_PACKET, SOCK_RAW|SOCK_CLOEXEC, htons(ETH_P_ALL));
        if (sock_fd == -1) {
                (void)snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
                    "socket: %s", pcap_strerror(errno));
@@ -1556,7 +1556,7 @@ pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
                }
 
 #if defined(HAVE_PACKET_AUXDATA) && defined(HAVE_LINUX_TPACKET_AUXDATA_TP_VLAN_TCI)
-               packet_len = recvmsg(handle->fd, &msg, MSG_TRUNC);
+               packet_len = recvmsg(handle->fd, &msg, MSG_TRUNC|MSG_CMSG_CLOEXEC);
 #else /* defined(HAVE_PACKET_AUXDATA) && defined(HAVE_LINUX_TPACKET_AUXDATA_TP_VLAN_TCI) */
                fromlen = sizeof(from);
                packet_len = recvfrom(
@@ -2116,7 +2116,7 @@ scan_sys_class_net(pcap_if_t **devlistp, char *errbuf)
        /*
         * Create a socket from which to fetch interface information.
         */
-       fd = socket(AF_INET, SOCK_DGRAM, 0);
+       fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
        if (fd < 0) {
                (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
                    "socket: %s", pcap_strerror(errno));
@@ -2234,7 +2234,7 @@ scan_proc_net_dev(pcap_if_t **devlistp, char *errbuf)
        /*
         * Create a socket from which to fetch interface information.
         */
-       fd = socket(AF_INET, SOCK_DGRAM, 0);
+       fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
        if (fd < 0) {
                (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
                    "socket: %s", pcap_strerror(errno));
@@ -3022,8 +3022,8 @@ activate_new(pcap_t *handle)
         * try a SOCK_RAW socket for the raw interface.
         */
        sock_fd = is_any_device ?
-               socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) :
-               socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+               socket(PF_PACKET, SOCK_DGRAM|SOCK_CLOEXEC, htons(ETH_P_ALL)) :
+               socket(PF_PACKET, SOCK_RAW|SOCK_CLOEXEC, htons(ETH_P_ALL));
 
        if (sock_fd == -1) {
                if (errno == EINVAL || errno == EAFNOSUPPORT) {
@@ -3140,7 +3140,7 @@ activate_new(pcap_t *handle)
                                         "close: %s", pcap_strerror(errno));
                                return PCAP_ERROR;
                        }
-                       sock_fd = socket(PF_PACKET, SOCK_DGRAM,
+                       sock_fd = socket(PF_PACKET, SOCK_DGRAM|SOCK_CLOEXEC,
                            htons(ETH_P_ALL));
                        if (sock_fd == -1) {
                                snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
@@ -5572,7 +5572,7 @@ activate_old(pcap_t *handle)
 
        /* Open the socket */
 
-       handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
+       handle->fd = socket(PF_INET, SOCK_PACKET|SOCK_CLOEXEC, htons(ETH_P_ALL));
        if (handle->fd == -1) {
                snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
                         "socket: %s", pcap_strerror(errno));
index 2a5812b..b4770f4 100644 (file)
@@ -486,7 +486,7 @@ netfilter_activate(pcap_t* handle)
        handle->stats_op = netfilter_stats_linux;
 
        /* Create netlink socket */
-       handle->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
+       handle->fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_NETFILTER);
        if (handle->fd < 0) {
                snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s", errno, pcap_strerror(errno));
                return PCAP_ERROR;
@@ -633,7 +633,7 @@ netfilter_findalldevs(pcap_if_t **alldevsp, char *err_str)
 {
        int sock;
        
-       sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
+       sock = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_NETFILTER);
        if (sock < 0) {
                /* if netlink is not supported this is not fatal */
                if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
index ea81eb5..c106861 100644 (file)
@@ -278,7 +278,7 @@ pcap_activate_nit(pcap_t *p)
                p->snapshot = 96;
 
        memset(p, 0, sizeof(*p));
-       p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
+       p->fd = fd = socket(AF_NIT, SOCK_RAW|SOCK_CLOEXEC, NITPROTO_RAW);
        if (fd < 0) {
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
                    "socket: %s", pcap_strerror(errno));
index d507e1e..4dd7a04 100644 (file)
@@ -324,7 +324,7 @@ static int open_with_IOP(unit_t  *u, int flag) {
        u->serv_addr->sin_addr.s_addr   = inet_addr(ip);
        u->serv_addr->sin_port                  = htons(IOP_SNIFFER_PORT);
 
-       if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+       if ((sockfd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0)) < 0) {
                fprintf(stderr, "pcap can't open a socket for connecting to IOP at %s\n", ip);
                return 0;
        }
index e6232c2..0992af9 100644 (file)
@@ -316,9 +316,9 @@ pcap_activate_snit(pcap_t *p)
         * the device in question) can be indicated at open
         * time.
         */
-       p->fd = fd = open(dev, O_RDWR);
+       p->fd = fd = open(dev, O_RDWR|O_CLOEXEC);
        if (fd < 0 && errno == EACCES)
-               p->fd = fd = open(dev, O_RDONLY);
+               p->fd = fd = open(dev, O_RDONLY|O_CLOEXEC);
        if (fd < 0) {
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dev,
                    pcap_strerror(errno));
index 7993e70..fcd37d0 100644 (file)
@@ -210,7 +210,7 @@ pcap_activate_snoop(pcap_t *p)
        int snooplen;
        struct ifreq ifr;
 
-       fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);
+       fd = socket(PF_RAW, SOCK_RAW|SOCK_CLOEXEC, RAWPROTO_SNOOP);
        if (fd < 0) {
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snoop socket: %s",
                    pcap_strerror(errno));
index bdabf00..f03f71b 100644 (file)
@@ -261,7 +261,7 @@ probe_devices(int bus)
 
                snprintf(buf, sizeof(buf), "/dev/bus/usb/%03d/%s", bus, data->d_name);
                
-               fd = open(buf, O_RDWR);
+               fd = open(buf, O_RDWR|O_CLOEXEC);
                if (fd == -1)
                        continue;
 
@@ -363,8 +363,8 @@ usb_activate(pcap_t* handle)
        }
 
        /*now select the read method: try to open binary interface */
-       snprintf(full_path, USB_LINE_LEN, LINUX_USB_MON_DEV"%d", handlep->bus_index);  
-       handle->fd = open(full_path, O_RDONLY, 0);
+       snprintf(full_path, USB_LINE_LEN, LINUX_USB_MON_DEV"%d", handlep->bus_index);   
+       handle->fd = open(full_path, O_RDONLY|O_CLOEXEC, 0);
        if (handle->fd >= 0)
        {
                if (handle->opt.rfmon) {
@@ -403,7 +403,7 @@ usb_activate(pcap_t* handle)
        else {
                /*Binary interface not available, try open text interface */
                snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR"/%dt", handlep->bus_index);  
-               handle->fd = open(full_path, O_RDONLY, 0);
+               handle->fd = open(full_path, O_RDONLY|O_CLOEXEC, 0);
                if (handle->fd < 0)
                {
                        if (errno == ENOENT)
@@ -412,8 +412,8 @@ usb_activate(pcap_t* handle)
                                 * Not found at the new location; try
                                 * the old location.
                                 */
-                               snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR_OLD"/%dt", handlep->bus_index);  
-                               handle->fd = open(full_path, O_RDONLY, 0);
+                               snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR_OLD"/%dt", handlep->bus_index);   
+                               handle->fd = open(full_path, O_RDONLY|O_CLOEXEC, 0);
                        }
                        if (handle->fd < 0) {
                                /* no more fallback, give it up*/
@@ -681,7 +681,7 @@ usb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
        int fd;
 
        snprintf(string, USB_LINE_LEN, USB_TEXT_DIR"/%ds", handlep->bus_index);
-       fd = open(string, O_RDONLY, 0);
+       fd = open(string, O_RDONLY|O_CLOEXEC, 0);
        if (fd < 0)
        {
                if (errno == ENOENT)
@@ -691,7 +691,7 @@ usb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
                         * location.
                         */
                        snprintf(string, USB_LINE_LEN, USB_TEXT_DIR_OLD"/%ds", handlep->bus_index);
-                       fd = open(string, O_RDONLY, 0);
+                       fd = open(string, O_RDONLY|O_CLOEXEC, 0);
                }
                if (fd < 0) {
                        snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
index 0ab2fde..1c43f68 100644 (file)
@@ -185,7 +185,7 @@ pcap_open_offline_with_tstamp_precision(const char *fname, u_int precision,
        }
        else {
 #if !defined(WIN32) && !defined(MSDOS)
-               fp = fopen(fname, "r");
+               fp = fopen(fname, "re");
 #else
                fp = fopen(fname, "rb");
 #endif
index 8180bc9..52d90eb 100644 (file)
--- a/sf-pcap.c
+++ b/sf-pcap.c
@@ -664,7 +664,7 @@ pcap_dump_open(pcap_t *p, const char *fname)
                fname = "standard output";
        } else {
 #if !defined(WIN32) && !defined(MSDOS)
-               f = fopen(fname, "w");
+               f = fopen(fname, "we");
 #else
                f = fopen(fname, "wb");
 #endif