diff options
Diffstat (limited to 'src/firejail/run_files.c')
-rw-r--r-- | src/firejail/run_files.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/src/firejail/run_files.c b/src/firejail/run_files.c new file mode 100644 index 000000000..42303c07b --- /dev/null +++ b/src/firejail/run_files.c | |||
@@ -0,0 +1,133 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014-2018 Firejail Authors | ||
3 | * | ||
4 | * This file is part of firejail project | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | |||
21 | #include "firejail.h" | ||
22 | #include "../include/pid.h" | ||
23 | |||
24 | static void delete_x11_run_file(pid_t pid) { | ||
25 | char *fname; | ||
26 | if (asprintf(&fname, "%s/%d", RUN_FIREJAIL_X11_DIR, pid) == -1) | ||
27 | errExit("asprintf"); | ||
28 | int rv = unlink(fname); | ||
29 | (void) rv; | ||
30 | free(fname); | ||
31 | } | ||
32 | |||
33 | static void delete_profile_run_file(pid_t pid) { | ||
34 | char *fname; | ||
35 | if (asprintf(&fname, "%s/%d", RUN_FIREJAIL_PROFILE_DIR, pid) == -1) | ||
36 | errExit("asprintf"); | ||
37 | int rv = unlink(fname); | ||
38 | (void) rv; | ||
39 | free(fname); | ||
40 | } | ||
41 | |||
42 | static void delete_name_run_file(pid_t pid) { | ||
43 | char *fname; | ||
44 | if (asprintf(&fname, "%s/%d", RUN_FIREJAIL_NAME_DIR, pid) == -1) | ||
45 | errExit("asprintf"); | ||
46 | int rv = unlink(fname); | ||
47 | (void) rv; | ||
48 | free(fname); | ||
49 | } | ||
50 | |||
51 | void delete_bandwidth_run_file(pid_t pid) { | ||
52 | char *fname; | ||
53 | if (asprintf(&fname, "%s/%d-bandwidth", RUN_FIREJAIL_BANDWIDTH_DIR, (int) pid) == -1) | ||
54 | errExit("asprintf"); | ||
55 | unlink(fname); | ||
56 | free(fname); | ||
57 | } | ||
58 | |||
59 | static void delete_network_run_file(pid_t pid) { | ||
60 | char *fname; | ||
61 | if (asprintf(&fname, "%s/%d-netmap", RUN_FIREJAIL_NETWORK_DIR, (int) pid) == -1) | ||
62 | errExit("asprintf"); | ||
63 | unlink(fname); | ||
64 | free(fname); | ||
65 | } | ||
66 | |||
67 | |||
68 | |||
69 | void delete_run_files(pid_t pid) { | ||
70 | delete_bandwidth_run_file(pid); | ||
71 | delete_network_run_file(pid); | ||
72 | delete_name_run_file(pid); | ||
73 | delete_profile_run_file(pid); | ||
74 | delete_x11_run_file(pid); | ||
75 | } | ||
76 | |||
77 | void set_name_run_file(pid_t pid) { | ||
78 | char *fname; | ||
79 | if (asprintf(&fname, "%s/%d", RUN_FIREJAIL_NAME_DIR, pid) == -1) | ||
80 | errExit("asprintf"); | ||
81 | |||
82 | // the file is deleted first | ||
83 | FILE *fp = fopen(fname, "w"); | ||
84 | if (!fp) { | ||
85 | fprintf(stderr, "Error: cannot create %s\n", fname); | ||
86 | exit(1); | ||
87 | } | ||
88 | fprintf(fp, "%s\n", cfg.name); | ||
89 | |||
90 | // mode and ownership | ||
91 | SET_PERMS_STREAM(fp, 0, 0, 0644); | ||
92 | fclose(fp); | ||
93 | } | ||
94 | |||
95 | |||
96 | void set_x11_run_file(pid_t pid, int display) { | ||
97 | char *fname; | ||
98 | if (asprintf(&fname, "%s/%d", RUN_FIREJAIL_X11_DIR, pid) == -1) | ||
99 | errExit("asprintf"); | ||
100 | |||
101 | // the file is deleted first | ||
102 | FILE *fp = fopen(fname, "w"); | ||
103 | if (!fp) { | ||
104 | fprintf(stderr, "Error: cannot create %s\n", fname); | ||
105 | exit(1); | ||
106 | } | ||
107 | fprintf(fp, "%d\n", display); | ||
108 | |||
109 | // mode and ownership | ||
110 | SET_PERMS_STREAM(fp, 0, 0, 0644); | ||
111 | fclose(fp); | ||
112 | } | ||
113 | |||
114 | void set_profile_run_file(pid_t pid, const char *fname) { | ||
115 | char *runfile; | ||
116 | if (asprintf(&runfile, "%s/%d", RUN_FIREJAIL_PROFILE_DIR, pid) == -1) | ||
117 | errExit("asprintf"); | ||
118 | |||
119 | EUID_ROOT(); | ||
120 | // the file is deleted first | ||
121 | FILE *fp = fopen(runfile, "w"); | ||
122 | if (!fp) { | ||
123 | fprintf(stderr, "Error: cannot create %s\n", runfile); | ||
124 | exit(1); | ||
125 | } | ||
126 | fprintf(fp, "%s\n", fname); | ||
127 | |||
128 | // mode and ownership | ||
129 | SET_PERMS_STREAM(fp, 0, 0, 0644); | ||
130 | fclose(fp); | ||
131 | EUID_USER(); | ||
132 | free(runfile); | ||
133 | } | ||