]>
Commit | Line | Data |
---|---|---|
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" | |
db1413d7 | 30 | #include "strv.h" |
8e1bd70d LP |
31 | #include "util.h" |
32 | ||
33 | #define PROC_SYS_PREFIX "/proc/sys/" | |
34 | ||
c1b664d0 | 35 | static int apply_sysctl(const char *property, const char *value) { |
8e1bd70d | 36 | char *p, *n; |
c1b664d0 | 37 | int r = 0, k; |
8e1bd70d LP |
38 | |
39 | log_debug("Setting '%s' to '%s'", property, value); | |
40 | ||
41 | if (!(p = new(char, sizeof(PROC_SYS_PREFIX) + strlen(property)))) { | |
42 | log_error("Out of memory"); | |
c1b664d0 | 43 | return -ENOMEM; |
8e1bd70d LP |
44 | } |
45 | ||
46 | n = stpcpy(p, PROC_SYS_PREFIX); | |
47 | strcpy(n, property); | |
48 | ||
49 | for (; *n; n++) | |
50 | if (*n == '.') | |
51 | *n = '/'; | |
52 | ||
c1b664d0 | 53 | if ((k = write_one_line_file(p, value)) < 0) { |
5707631e | 54 | |
c1b664d0 LP |
55 | log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING, |
56 | "Failed to write '%s' to '%s': %s", value, p, strerror(-k)); | |
24a35973 | 57 | |
c1b664d0 LP |
58 | if (k != -ENOENT && r == 0) |
59 | r = k; | |
8e1bd70d LP |
60 | } |
61 | ||
62 | free(p); | |
c1b664d0 LP |
63 | |
64 | return r; | |
8e1bd70d LP |
65 | } |
66 | ||
c1b664d0 | 67 | static int apply_file(const char *path, bool ignore_enoent) { |
8e1bd70d | 68 | FILE *f; |
c1b664d0 | 69 | int r = 0; |
8e1bd70d LP |
70 | |
71 | assert(path); | |
72 | ||
73 | if (!(f = fopen(path, "re"))) { | |
c1b664d0 LP |
74 | if (ignore_enoent && errno == ENOENT) |
75 | return 0; | |
76 | ||
8e1bd70d | 77 | log_error("Failed to open file '%s', ignoring: %m", path); |
c1b664d0 | 78 | return -errno; |
8e1bd70d LP |
79 | } |
80 | ||
db1413d7 | 81 | log_debug("apply: %s\n", path); |
8e1bd70d LP |
82 | while (!feof(f)) { |
83 | char l[LINE_MAX], *p, *value; | |
c1b664d0 | 84 | int k; |
8e1bd70d LP |
85 | |
86 | if (!fgets(l, sizeof(l), f)) { | |
87 | if (feof(f)) | |
88 | break; | |
89 | ||
90 | log_error("Failed to read file '%s', ignoring: %m", path); | |
c1b664d0 | 91 | r = -errno; |
8e1bd70d LP |
92 | goto finish; |
93 | } | |
94 | ||
95 | p = strstrip(l); | |
96 | ||
97 | if (!*p) | |
98 | continue; | |
99 | ||
100 | if (strchr(COMMENTS, *p)) | |
101 | continue; | |
102 | ||
103 | if (!(value = strchr(p, '='))) { | |
104 | log_error("Line is not an assignment in file '%s': %s", path, value); | |
c1b664d0 LP |
105 | |
106 | if (r == 0) | |
107 | r = -EINVAL; | |
8e1bd70d LP |
108 | continue; |
109 | } | |
110 | ||
111 | *value = 0; | |
112 | value++; | |
113 | ||
c1b664d0 LP |
114 | if ((k = apply_sysctl(strstrip(p), strstrip(value))) < 0 && r == 0) |
115 | r = k; | |
8e1bd70d LP |
116 | } |
117 | ||
118 | finish: | |
119 | fclose(f); | |
c1b664d0 LP |
120 | |
121 | return r; | |
8e1bd70d LP |
122 | } |
123 | ||
8e1bd70d | 124 | int main(int argc, char *argv[]) { |
c1b664d0 | 125 | int r = 0; |
8e1bd70d LP |
126 | |
127 | if (argc > 2) { | |
128 | log_error("This program expects one or no arguments."); | |
129 | return EXIT_FAILURE; | |
130 | } | |
131 | ||
132 | log_set_target(LOG_TARGET_AUTO); | |
133 | log_parse_environment(); | |
134 | log_open(); | |
135 | ||
136 | if (argc > 1) | |
c1b664d0 | 137 | r = apply_file(argv[1], false); |
8e1bd70d | 138 | else { |
db1413d7 | 139 | char **files, **f; |
c1b664d0 | 140 | |
44143309 KS |
141 | apply_file("/etc/sysctl.conf", true); |
142 | ||
143 | r = conf_files_list(&files, ".conf", | |
144 | "/run/sysctl.d", | |
145 | "/etc/sysctl.d", | |
40013ebc | 146 | "/usr/local/lib/sysctl.d", |
44143309 | 147 | "/usr/lib/sysctl.d", |
223a3558 | 148 | "/lib/sysctl.d", |
44143309 KS |
149 | NULL); |
150 | if (r < 0) { | |
151 | log_error("Failed to enumerate sysctl.d files: %s", strerror(-r)); | |
152 | goto finish; | |
153 | } | |
db1413d7 KS |
154 | |
155 | STRV_FOREACH(f, files) { | |
156 | int k; | |
157 | ||
158 | k = apply_file(*f, true); | |
159 | if (k < 0 && r == 0) | |
160 | r = k; | |
161 | } | |
162 | ||
163 | strv_free(files); | |
8e1bd70d | 164 | } |
44143309 | 165 | finish: |
c1b664d0 | 166 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
8e1bd70d | 167 | } |