602b28d
--- apcupsd-3.14.1/src/apcnis.c.cloexec	2006-08-13 11:00:15.000000000 -0600
602b28d
+++ apcupsd-3.14.1/src/apcnis.c	2007-08-01 16:26:35.000000000 -0600
602b28d
@@ -155,6 +155,9 @@
602b28d
       sleep(5 * 60);
602b28d
    }
602b28d
 
602b28d
+   /* Close the socket on exec - avoid leaked file descriptors */
602b28d
+   fcntl(sockfd, F_SETFD, FD_CLOEXEC);
602b28d
+
602b28d
    /* Reuse old sockets */
602b28d
 #ifndef HAVE_MINGW
602b28d
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&turnon, sizeof(turnon)) < 0) {
602b28d
--- apcupsd-3.14.1/src/apcupsd.c.cloexec	2006-09-23 11:42:47.000000000 -0600
602b28d
+++ apcupsd-3.14.1/src/apcupsd.c	2007-08-01 16:27:14.000000000 -0600
602b28d
@@ -280,6 +280,9 @@
602b28d
       if (ups->event_fd < 0) {
602b28d
          log_event(ups, LOG_WARNING, "Could not open events file %s: %s\n",
602b28d
             ups->eventfile, strerror(errno));
602b28d
+      } else {
602b28d
+         /* Close the file on exec - avoid leaked file descriptors */
602b28d
+         fcntl(ups->event_fd, F_SETFD, FD_CLOEXEC);
602b28d
       }
602b28d
    }
602b28d
 
602b28d
--- apcupsd-3.14.1/src/drivers/usb/linux/linux-usb.c.cloexec	2007-08-01 16:30:28.000000000 -0600
602b28d
+++ apcupsd-3.14.1/src/drivers/usb/linux/linux-usb.c	2007-08-01 16:42:55.000000000 -0600
602b28d
@@ -182,8 +182,11 @@
602b28d
    /* Retry 10 times */
602b28d
    for (i = 0; i < 10; i++) {
602b28d
       my_data->fd = open_device(ups->device, ups);
602b28d
-      if (my_data->fd != -1)
602b28d
+      if (my_data->fd != -1) {
602b28d
+         /* Close the device on exec - avoid leaked file descriptors */
602b28d
+         fcntl(my_data->fd, F_SETFD, FD_CLOEXEC);
602b28d
          return 1;
602b28d
+      }
602b28d
       sleep(1);
602b28d
    }
602b28d
 
602b28d
@@ -206,6 +209,8 @@
602b28d
             asnprintf(devname, sizeof(devname), "%s%d", hiddev[j], k);
602b28d
             my_data->fd = open_device(devname, ups);
602b28d
             if (my_data->fd != -1) {
602b28d
+               /* Close the device on exec - avoid leaked file descriptors */
602b28d
+               fcntl(my_data->fd, F_SETFD, FD_CLOEXEC);
602b28d
                /* Successful open, save device name and return */
602b28d
                astrncpy(ups->device, devname, sizeof(ups->device));
602b28d
                return 1;