chiark / gitweb /
gcc: make gcc shut up
[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 <ftw.h>
27 #include <stdio.h>
28 #include <limits.h>
29
30 #include "log.h"
31 #include "util.h"
32
33 #define PROC_SYS_PREFIX "/proc/sys/"
34
35 static int exit_code = 0;
36
37 static void apply_sysctl(const char *property, const char *value) {
38         char *p, *n;
39         int r;
40
41         log_debug("Setting '%s' to '%s'", property, value);
42
43         if (!(p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)))) {
44                 log_error("Out of memory");
45                 exit_code = -ENOMEM;
46         }
47
48         n = stpcpy(p, PROC_SYS_PREFIX);
49         strcpy(n, property);
50
51         for (; *n; n++)
52                 if (*n == '.')
53                         *n = '/';
54
55         if ((r = write_one_line_file(p, value)) < 0) {
56
57                 log_full(r == -ENOENT ? LOG_DEBUG : LOG_WARNING,
58                          "Failed to write '%s' to '%s': %s", value, p, strerror(-r));
59
60                 if (r != -ENOENT)
61                         exit_code = r;
62         }
63
64         free(p);
65 }
66
67 static void apply_file(const char *path) {
68         FILE *f;
69
70         assert(path);
71
72         if (!(f = fopen(path, "re"))) {
73                 log_error("Failed to open file '%s', ignoring: %m", path);
74                 exit_code = -errno;
75                 return;
76         }
77
78         while (!feof(f)) {
79                 char l[LINE_MAX], *p, *value;
80
81                 if (!fgets(l, sizeof(l), f)) {
82                         if (feof(f))
83                                 break;
84
85                         log_error("Failed to read file '%s', ignoring: %m", path);
86                         exit_code = -errno;
87                         goto finish;
88                 }
89
90                 p = strstrip(l);
91
92                 if (!*p)
93                         continue;
94
95                 if (strchr(COMMENTS, *p))
96                         continue;
97
98                 if (!(value = strchr(p, '='))) {
99                         log_error("Line is not an assignment in file '%s': %s", path, value);
100                         exit_code = -EINVAL;
101                         continue;
102                 }
103
104                 *value = 0;
105                 value++;
106
107                 apply_sysctl(strstrip(p), strstrip(value));
108         }
109
110 finish:
111         fclose(f);
112 }
113
114 static int nftw_cb(
115                 const char *fpath,
116                 const struct stat *sb,
117                 int tflag,
118                 struct FTW *ftwbuf) {
119
120         if (tflag != FTW_F)
121                 return 0;
122
123         if (ignore_file(fpath + ftwbuf->base))
124                 return 0;
125
126         if (!endswith(fpath, ".conf"))
127                 return 0;
128
129         apply_file(fpath);
130         return 0;
131 };
132
133 int main(int argc, char *argv[]) {
134
135         if (argc > 2) {
136                 log_error("This program expects one or no arguments.");
137                 return EXIT_FAILURE;
138         }
139
140         log_set_target(LOG_TARGET_AUTO);
141         log_parse_environment();
142         log_open();
143
144         if (argc > 1)
145                 nftw(argv[1], nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
146         else {
147                 nftw("/etc/sysctl.conf", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
148                 nftw("/etc/sysctl.d", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
149         }
150
151         return exit_code < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
152 }