Skip to content

Instantly share code, notes, and snippets.

@christopherdeutsch
Created July 25, 2012 20:43
Show Gist options
  • Select an option

  • Save christopherdeutsch/3178561 to your computer and use it in GitHub Desktop.

Select an option

Save christopherdeutsch/3178561 to your computer and use it in GitHub Desktop.
execnofd - run a program after closing file descriptors >2
/* execnofd
*
* exec a program after closing all fd's > 2
*
* Syntax: execnofd <command> <options>
*
* Example: execnofd ls -l /home
*
*/
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int maxfd = sysconf(_SC_OPEN_MAX);
for(int i = 3; i < maxfd; i++) {
close(i);
}
execvp(argv[0], &argv[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment