mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-03 03:20:57 +00:00
Move implementation of closefrom(3) from app.c to strcompat.c
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@233358 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -22,6 +22,10 @@
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <sys/time.h> /* for getrlimit(2) */
|
||||
#include <sys/resource.h> /* for getrlimit(2) */
|
||||
#include <sys/types.h> /* for opendir(3) */
|
||||
#include <dirent.h> /* for opendir(3) */
|
||||
|
||||
#ifndef HAVE_STRSEP
|
||||
char *strsep(char **str, const char *delims)
|
||||
@@ -399,3 +403,33 @@ int ffsll(long long n)
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_CLOSEFROM
|
||||
void closefrom(int n)
|
||||
{
|
||||
long x;
|
||||
struct rlimit rl;
|
||||
DIR *dir;
|
||||
char path[16], *result;
|
||||
struct dirent *entry;
|
||||
|
||||
snprintf(path, sizeof(path), "/proc/%d/fd", (int) getpid());
|
||||
if ((dir = opendir(path))) {
|
||||
while ((entry = readdir(dir))) {
|
||||
/* Skip . and .. */
|
||||
if (entry->d_name[0] == '.') {
|
||||
continue;
|
||||
}
|
||||
if ((x = strtol(entry->d_name, &result, 10)) && x >= n) {
|
||||
close(x);
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
} else {
|
||||
getrlimit(RLIMIT_NOFILE, &rl);
|
||||
for (x = n; x < rl.rlim_cur; x++) {
|
||||
close(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user