1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <logging.h>
26 #include <platform.h>
27 
usage(void)28 static void usage(void)
29 {
30     printf("Runs the given command without inheriting any file descriptors.\n"
31            "Usage: no_fds [options]\n"
32            "Options:\n"
33            "      --no-std\n"
34            "        Even stdin/stdout/stderr will not be inherited.\n"
35            "  -h, --help\n"
36            "        This help screen.\n");
37 }
38 
close_on_exec(int fd)39 static void close_on_exec(int fd)
40 {
41 #ifdef _WIN32
42     SetHandleInformation((HANDLE)_get_osfhandle(fd), HANDLE_FLAG_INHERIT, 0);
43 #else
44     long flags = fcntl(fd, F_GETFD);
45     flags |= FD_CLOEXEC;
46     fcntl(fd, F_SETFD, flags);
47 #endif
48 }
49 
main(int argc,char ** argv)50 int main(int argc, char **argv)
51 {
52     bool std = true;
53     int startarg = 1;
54 
55     for (int arg = 1; arg < argc; arg++)
56     {
57         if (strcmp(argv[arg], "--no-std") == 0)
58         {
59             std = false;
60             startarg++;
61         }
62         else if (strcmp(argv[arg], "-h") == 0 || strcmp(argv[arg], "--help") == 0)
63         {
64             usage();
65             return 0;
66         }
67         else if (argv[arg][0] == '-')
68         {
69             fprintf(stderr, "Unknown option: %s\n", argv[arg]);
70             usage();
71             return 1;
72         }
73         else
74         {
75             break;
76         }
77     }
78 
79     closefrom(3);
80 
81     if (!std)
82     {
83         for (int fd = 0; fd < 3; fd++)
84         {
85             close_on_exec(fd);
86         }
87     }
88 
89     char *new_args[argc - startarg + 1];
90     for (int i = startarg; i < argc; i++)
91     {
92         new_args[i - startarg] = argv[i];
93     }
94     new_args[argc - startarg] = NULL;
95 
96     execvp(new_args[0], new_args);
97 
98     fprintf(stderr, "Could not execute command '%s': %s\n", new_args[0], GetErrorStr());
99 
100     return 1;
101 }
102