chiark / gitweb /
systemctl: spawn pager only for commands that generates long output
[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                 log_warning("Failed to write '%s' to '%s': %s", value, p, strerror(-r));
57
58                 if (r != -ENOENT)
59                         exit_code = r;
60         }
61
62         free(p);
63 }
64
65 static void apply_file(const char *path) {
66         FILE *f;
67
68         assert(path);
69
70         if (!(f = fopen(path, "re"))) {
71                 log_error("Failed to open file '%s', ignoring: %m", path);
72                 exit_code = -errno;
73                 return;
74         }
75
76         while (!feof(f)) {
77                 char l[LINE_MAX], *p, *value;
78
79                 if (!fgets(l, sizeof(l), f)) {
80                         if (feof(f))
81                                 break;
82
83                         log_error("Failed to read file '%s', ignoring: %m", path);
84                         exit_code = -errno;
85                         goto finish;
86                 }
87
88                 p = strstrip(l);
89
90                 if (!*p)
91                         continue;
92
93                 if (strchr(COMMENTS, *p))
94                         continue;
95
96                 if (!(value = strchr(p, '='))) {
97                         log_error("Line is not an assignment in file '%s': %s", path, value);
98                         exit_code = -EINVAL;
99                         continue;
100                 }
101
102                 *value = 0;
103                 value++;
104
105                 apply_sysctl(strstrip(p), strstrip(value));
106         }
107
108 finish:
109         fclose(f);
110 }
111
112 static int nftw_cb(
113                 const char *fpath,
114                 const struct stat *sb,
115                 int tflag,
116                 struct FTW *ftwbuf) {
117
118         if (tflag != FTW_F)
119                 return 0;
120
121         if (ignore_file(fpath + ftwbuf->base))
122                 return 0;
123
124         if (!endswith(fpath, ".conf"))
125                 return 0;
126
127         apply_file(fpath);
128         return 0;
129 };
130
131 int main(int argc, char *argv[]) {
132
133         if (argc > 2) {
134                 log_error("This program expects one or no arguments.");
135                 return EXIT_FAILURE;
136         }
137
138         log_set_target(LOG_TARGET_AUTO);
139         log_parse_environment();
140         log_open();
141
142         if (argc > 1)
143                 nftw(argv[1], nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
144         else {
145                 nftw("/etc/sysctl.conf", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
146                 nftw("/etc/sysctl.d", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
147         }
148
149         return exit_code < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
150 }