Skip to content

Instantly share code, notes, and snippets.

@gwisp2
Created February 21, 2023 15:14
Show Gist options
  • Select an option

  • Save gwisp2/aec6b92922d6e2252263e95ce507bc98 to your computer and use it in GitHub Desktop.

Select an option

Save gwisp2/aec6b92922d6e2252263e95ce507bc98 to your computer and use it in GitHub Desktop.

Revisions

  1. gwisp2 created this gist Feb 21, 2023.
    21 changes: 21 additions & 0 deletions cleanexec.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    #include <errno.h>
    #include <string.h>

    int main(int argc, char *argv[]) {
    if (argc < 2) {
    printf("No path to executable to start provided\n");
    return 255;
    }
    if (syscall(SYS_close_range, 3U, ~0U, 0U)) {
    printf("Failed to close file descriptors: %s\n", strerror(errno));
    return 255;
    }
    if (execv(argv[1], &argv[1])) {
    printf("Exec %s failed: %s\n", argv[1], strerror(errno));
    return 255;
    }
    return 0;
    }