aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-07-11 16:40:53 +0200
committerMattias Andrée <maandree@kth.se>2016-07-11 16:40:53 +0200
commit3eaa6748f0f57eff6b85e3e4a249ccaeccd09bed (patch)
tree90490cee98032eac1bc01178723d89b19ba30470
parentCreate and listen to socket + do not use deprecated usleep (diff)
downloadcoopgammad-3eaa6748f0f57eff6b85e3e4a249ccaeccd09bed.tar.gz
coopgammad-3eaa6748f0f57eff6b85e3e4a249ccaeccd09bed.tar.bz2
coopgammad-3eaa6748f0f57eff6b85e3e4a249ccaeccd09bed.tar.xz
Close file descriptors
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--src/gammad.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/gammad.c b/src/gammad.c
index 9534f9f..5450399 100644
--- a/src/gammad.c
+++ b/src/gammad.c
@@ -17,6 +17,7 @@
*/
#include <libgamma.h>
+#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
@@ -430,7 +431,16 @@ int main(int argc, char** argv)
if (argc > 0)
usage();
- /* TODO Close all file descriptors above stderr */
+ /* Close all file descriptors above stderr */
+ {
+ struct rlimit rlimit;
+ if (getrlimit(RLIMIT_NOFILE, &rlimit) || (rlimit.rlim_cur == RLIM_INFINITY))
+ n = 4 << 10;
+ else
+ n = (size_t)(rlimit.rlim_cur);
+ for (i = STDERR_FILENO + 1; i < n; i++)
+ close((int)i);
+ }
/* Set umask, reset signal handlers, and reset signal mask */
umask(0);