]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/sysctl.c
manager: mkdir /run/systemd/system when starting up
[thirdparty/systemd.git] / src / sysctl.c
CommitLineData
8e1bd70d
LP
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>
8e1bd70d
LP
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
c1b664d0 34static int apply_sysctl(const char *property, const char *value) {
8e1bd70d 35 char *p, *n;
c1b664d0 36 int r = 0, k;
8e1bd70d
LP
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");
c1b664d0 42 return -ENOMEM;
8e1bd70d
LP
43 }
44
45 n = stpcpy(p, PROC_SYS_PREFIX);
46 strcpy(n, property);
47
48 for (; *n; n++)
49 if (*n == '.')
50 *n = '/';
51
c1b664d0 52 if ((k = write_one_line_file(p, value)) < 0) {
5707631e 53
c1b664d0
LP
54 log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
55 "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
24a35973 56
c1b664d0
LP
57 if (k != -ENOENT && r == 0)
58 r = k;
8e1bd70d
LP
59 }
60
61 free(p);
c1b664d0
LP
62
63 return r;
8e1bd70d
LP
64}
65
c1b664d0 66static int apply_file(const char *path, bool ignore_enoent) {
8e1bd70d 67 FILE *f;
c1b664d0 68 int r = 0;
8e1bd70d
LP
69
70 assert(path);
71
72 if (!(f = fopen(path, "re"))) {
c1b664d0
LP
73 if (ignore_enoent && errno == ENOENT)
74 return 0;
75
8e1bd70d 76 log_error("Failed to open file '%s', ignoring: %m", path);
c1b664d0 77 return -errno;
8e1bd70d
LP
78 }
79
80 while (!feof(f)) {
81 char l[LINE_MAX], *p, *value;
c1b664d0 82 int k;
8e1bd70d
LP
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);
c1b664d0 89 r = -errno;
8e1bd70d
LP
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);
c1b664d0
LP
103
104 if (r == 0)
105 r = -EINVAL;
8e1bd70d
LP
106 continue;
107 }
108
109 *value = 0;
110 value++;
111
c1b664d0
LP
112 if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0)
113 r = k;
8e1bd70d
LP
114 }
115
116finish:
117 fclose(f);
c1b664d0
LP
118
119 return r;
8e1bd70d
LP
120}
121
c1b664d0
LP
122static int scandir_filter(const struct dirent *d) {
123 assert(d);
8e1bd70d 124
c1b664d0 125 if (ignore_file(d->d_name))
8e1bd70d
LP
126 return 0;
127
c1b664d0
LP
128 if (d->d_type != DT_REG &&
129 d->d_type != DT_LNK &&
130 d->d_type != DT_UNKNOWN)
8e1bd70d
LP
131 return 0;
132
c1b664d0
LP
133 return endswith(d->d_name, ".conf");
134}
135
136static 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 }
8e1bd70d 148
c1b664d0
LP
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}
8e1bd70d
LP
172
173int main(int argc, char *argv[]) {
c1b664d0 174 int r = 0;
8e1bd70d
LP
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)
c1b664d0 186 r = apply_file(argv[1], false);
8e1bd70d 187 else {
c1b664d0
LP
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;
8e1bd70d
LP
194 }
195
c1b664d0 196 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
8e1bd70d 197}