summaryrefslogtreecommitdiff
blob: aeaa78b35420caa9f8879b4451e331cd14b77dda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Close the socket on exec - avoid leaked file descriptors, patch from Fedora

--- a/src/apcnis.c
+++ b/src/apcnis.c
@@ -157,6 +157,9 @@ void do_server(UPSINFO *ups)
       sleep(5 * 60);
    }
 
+   /* Close the socket on exec - avoid leaked file descriptors */
+   fcntl(sockfd, F_SETFD, FD_CLOEXEC);
+
    /* Reuse old sockets */
 #ifndef HAVE_MINGW
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&turnon, sizeof(turnon)) < 0) {
--- a/src/apcupsd.c
+++ b/src/apcupsd.c
@@ -212,6 +212,9 @@ int main(int argc, char *argv[])
       if (ups->event_fd < 0) {
          log_event(ups, LOG_WARNING, "Could not open events file %s: %s\n",
             ups->eventfile, strerror(errno));
+      } else {
+         /* Close the file on exec - avoid leaked file descriptors */
+         fcntl(ups->event_fd, F_SETFD, FD_CLOEXEC);
       }
    }
 
--- a/src/drivers/usb/linux/linux-usb.c
+++ b/src/drivers/usb/linux/linux-usb.c
@@ -246,8 +246,11 @@ bool LinuxUsbUpsDriver::open_usb_device()
    /* Retry 10 times */
    for (i = 0; i < 10; i++) {
       _fd = open_device(_ups->device);
-      if (_fd != -1)
+      if (_fd != -1) {
+         /* Close the device on exec - avoid leaked file descriptors */
+         fcntl(_fd, F_SETFD, FD_CLOEXEC);
          return true;
+      }
       sleep(1);
    }
 
@@ -270,6 +273,7 @@ auto_detect:
             asnprintf(devname, sizeof(devname), "%s%d", hiddev[j], k);
             _fd = open_device(devname);
             if (_fd != -1) {
+               fcntl(_fd, F_SETFD, FD_CLOEXEC);
                /* Successful open, save device name and return */
                strlcpy(_ups->device, devname, sizeof(_ups->device));
                return true;