chiark / gitweb /
util: detect CLONE_NEWPID namespaces, and cache results
[elogind.git] / src / sysctl.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <limits.h>
28
29 #include "log.h"
30 #include "util.h"
31
32 #define PROC_SYS_PREFIX "/proc/sys/"
33
34 static int apply_sysctl(const char *property, const char *value) {
35         char *p, *n;
36         int r = 0, k;
37
38         log_debug("Setting '%s' to '%s'", property, value);
39
40         if (!(p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)))) {
41                 log_error("Out of memory");
42                 return -ENOMEM;
43         }
44
45         n = stpcpy(p, PROC_SYS_PREFIX);
46         strcpy(n, property);
47
48         for (; *n; n++)
49                 if (*n == '.')
50                         *n = '/';
51
52         if ((k = write_one_line_file(p, value)) < 0) {
53
54                 log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
55                          "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
56
57                 if (k != -ENOENT && r == 0)
58                         r = k;
59         }
60
61         free(p);
62
63         return r;
64 }
65
66 static int apply_file(const char *path, bool ignore_enoent) {
67         FILE *f;
68         int r = 0;
69
70         assert(path);
71
72         if (!(f = fopen(path, "re"))) {
73                 if (ignore_enoent && errno == ENOENT)
74                         return 0;
75
76                 log_error("Failed to open file '%s', ignoring: %m", path);
77                 return -errno;
78         }
79
80         while (!feof(f)) {
81                 char l[LINE_MAX], *p, *value;
82                 int k;
83
84                 if (!fgets(l, sizeof(l), f)) {
85                         if (feof(f))
86                                 break;
87
88                         log_error("Failed to read file '%s', ignoring: %m", path);
89                         r = -errno;
90                         goto finish;
91                 }
92
93                 p = strstrip(l);
94
95                 if (!*p)
96                         continue;
97
98                 if (strchr(COMMENTS, *p))
99                         continue;
100
101                 if (!(value = strchr(p, '='))) {
102                         log_error("Line is not an assignment in file '%s': %s", path, value);
103
104                         if (r == 0)
105                                 r = -EINVAL;
106                         continue;
107                 }
108
109                 *value = 0;
110                 value++;
111
112                 if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0)
113                         r = k;
114         }
115
116 finish:
117         fclose(f);
118
119         return r;
120 }
121
122 static int scandir_filter(const struct dirent *d) {
123         assert(d);
124
125         if (ignore_file(d->d_name))
126                 return 0;
127
128         if (d->d_type != DT_REG &&
129             d->d_type != DT_LNK &&
130             d->d_type != DT_UNKNOWN)
131                 return 0;
132
133         return endswith(d->d_name, ".conf");
134 }
135
136 static int apply_tree(const char *path) {
137         struct dirent **de = NULL;
138         int n, i, r = 0;
139
140         if ((n = scandir(path, &de, scandir_filter, alphasort)) < 0) {
141
142                 if (errno == ENOENT)
143                         return 0;
144
145                 log_error("Failed to enumerate %s files: %m", path);
146                 return -errno;
147         }
148
149         for (i = 0; i < n; i++) {
150                 char *fn;
151                 int k;
152
153                 k = asprintf(&fn, "%s/%s", path, de[i]->d_name);
154                 free(de[i]);
155
156                 if (k < 0) {
157                         log_error("Failed to allocate file name.");
158
159                         if (r == 0)
160                                 r = -ENOMEM;
161                         continue;
162                 }
163
164                 if ((k = apply_file(fn, true)) < 0 && r == 0)
165                         r = k;
166         }
167
168         free(de);
169
170         return r;
171 }
172
173 int main(int argc, char *argv[]) {
174         int r = 0;
175
176         if (argc > 2) {
177                 log_error("This program expects one or no arguments.");
178                 return EXIT_FAILURE;
179         }
180
181         log_set_target(LOG_TARGET_AUTO);
182         log_parse_environment();
183         log_open();
184
185         if (argc > 1)
186                 r = apply_file(argv[1], false);
187         else {
188                 int k;
189
190                 r = apply_file("/etc/sysctl.conf", true);
191
192                 if ((k = apply_tree("/etc/sysctl.d")) < 0 && r == 0)
193                         r = k;
194         }
195
196         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
197 }