1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1997 - 2017 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* Linux-specific code. This is concatenated onto the generic
12 /*************************************************
13 * Load average computation *
14 *************************************************/
16 /*Linux has an apparently unique way of getting the load average, so we provide
17 a unique function here, and define OS_LOAD_AVERAGE to stop src/os.c trying to
18 provide the function. However, when compiling os.c for utilities, we may not
19 want this at all, so check that it isn't set first. */
21 #if !defined(OS_LOAD_AVERAGE) && defined(__linux__)
22 #define OS_LOAD_AVERAGE
24 /* Linux has 2 ways of returning load average:
26 (1) Do a read on /proc/loadavg
27 (2) Use the sysinfo library function and syscall
29 The latter is simpler but in Linux 2.0 - 2.2 (and probably later releases) is
30 exceptionally slow - 10-50ms per call is not unusual and about 100x slow the
31 first method. This cripples high performance mail servers by increasing CPU
34 In Exim's very early days, it used the 1st method. Later, it switched to the
35 2nd method. Now it tries the 1st method and falls back to the 2nd if /proc is
38 #include <sys/sysinfo.h>
41 linux_slow_getloadavg(void)
45 if (sysinfo(&s) < 0) return -1;
46 avg = (double) (s.loads[0]) / (1<<SI_LOAD_SHIFT);
47 return (int)(avg * 1000.0);
56 int fd = open ("/proc/loadavg", O_RDONLY);
57 if (fd == -1) return linux_slow_getloadavg();
58 count = read (fd, buffer, sizeof(buffer));
60 if (count <= 0) return linux_slow_getloadavg();
61 count = sscanf (buffer, "%lf", &avg);
62 if (count < 1) return linux_slow_getloadavg();
63 return (int)(avg * 1000.0);
65 #endif /* OS_LOAD_AVERAGE */
71 /*************************************************
72 * Finding interface addresses *
73 *************************************************/
75 /* This function is not required for utilities; we cut it out if
76 FIND_RUNNING_INTERFACES is already defined. */
78 #ifndef FIND_RUNNING_INTERFACES
80 /* This code, contributed by Jason Gunthorpe, appears to be the current
81 way of finding IPv6 interfaces in Linux. It first calls the common function in
82 order to find IPv4 interfaces, then grobbles around to find the others. Jason
83 said, "This is so horrible, don't look. Slightly ripped from net-tools
84 ifconfig." It gets called by virtue of os_find_running_interfaces being defined
85 as a macro for os_find_running_interfaces_linux in the os.h-Linux file. */
88 os_find_running_interfaces_linux(void)
90 ip_address_item *yield = NULL;
93 ip_address_item *last = NULL;
94 ip_address_item *next;
96 unsigned int plen, scope, dad_status, if_idx;
101 yield = os_common_find_running_interfaces();
105 /* Open the /proc file; give up if we can't. */
107 if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) return yield;
109 /* Pick out the data from within the file, and add it on to the chain */
112 if (last != NULL) while (last->next != NULL) last = last->next;
114 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
115 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
116 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
117 &if_idx, &plen, &scope, &dad_status, devname) != EOF)
119 struct sockaddr_in6 addr;
121 /* This data has to survive for ever, so use malloc. */
123 next = store_malloc(sizeof(ip_address_item));
126 sprintf(CS next->address, "%s:%s:%s:%s:%s:%s:%s:%s",
127 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
128 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
130 /* Normalize the representation */
132 inet_pton(AF_INET6, CS next->address, &addr.sin6_addr);
133 inet_ntop(AF_INET6, &addr.sin6_addr, CS next->address, sizeof(next->address));
135 if (yield == NULL) yield = last = next; else
142 debug_printf("Actual local interface address is %s (%s)\n", last->address,
146 #endif /* HAVE_IPV6 */
151 #endif /* FIND_RUNNING_INTERFACES */
157 #include <sys/sendfile.h>
160 os_sendfile(int out, int in, off_t * off, size_t cnt)
162 return sendfile(out, in, off, cnt);
165 /* End of os.c-Linux */