[Rpm-maint] [rpm-software-management/rpm] luaext/Pexec: optimize setting CLOEXEC (#444)
Kirill Kolyshkin
notifications at github.com
Thu May 17 21:41:04 UTC 2018
For the reference, here's my quick-n-dirty benchmarking code:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
static void set_cloexec(int fd)
{
int flags = fcntl(fd, F_GETFD);
if (flags == -1 || (flags & FD_CLOEXEC))
return;
fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
}
static void set_cloexec_all(int open_max, char method)
{
const int min_fd = 3; // don't touch stdin/out/err
int fd;
if (method == 'i')
goto fallback;
// iterate over fds obtained from /proc
DIR *dir = opendir("/proc/self/fd");
if (dir == NULL) {
goto fallback;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
fd = atoi(entry->d_name);
if (fd >= min_fd)
set_cloexec(fd);
}
closedir(dir);
return;
fallback:
// iterate over all possible fds
for (fd = min_fd; fd < open_max; fd++)
set_cloexec(fd);
}
int main(int argc, char **argv) {
if (argc < 4) {
fprintf(stderr, "Usage: %s <open_max> <iterations> <method>\n", argv[0]);
fprintf(stderr, " <method> is either i (iterate) or p (use proc)\n");
return 1;
}
int open_max = atoi(argv[1]);
int iter = atoi(argv[2]);
char m = argv[3][0];
while (iter--)
set_cloexec_all(open_max, m);
return 0;
}
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/rpm-software-management/rpm/pull/444#issuecomment-390021766
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rpm.org/pipermail/rpm-maint/attachments/20180517/3d575537/attachment-0001.html>
More information about the Rpm-maint
mailing list