chiark / gitweb /
bus: actually, the kernel does enforce validity of bus names...
[elogind.git] / src / readahead / readahead.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29
30 #include "util.h"
31 #include "def.h"
32 #include "build.h"
33 #include "readahead-common.h"
34
35 unsigned arg_files_max = 16*1024;
36 off_t arg_file_size_max = READAHEAD_FILE_SIZE_MAX;
37 usec_t arg_timeout = 2*USEC_PER_MINUTE;
38
39 static int help(void) {
40
41         printf("%s [OPTIONS...] collect [DIRECTORY]\n\n"
42                "Collect read-ahead data on early boot.\n\n"
43                "  -h --help                 Show this help\n"
44                "     --version              Show package version\n"
45                "     --max-files=INT        Maximum number of files to read ahead\n"
46                "     --file-size-max=BYTES  Maximum size of files to read ahead\n"
47                "     --timeout=USEC         Maximum time to spend collecting data\n\n\n",
48                program_invocation_short_name);
49
50         printf("%s [OPTIONS...] replay [DIRECTORY]\n\n"
51                "Replay collected read-ahead data on early boot.\n\n"
52                "  -h --help                 Show this help\n"
53                "     --version              Show package version\n"
54                "     --file-size-max=BYTES  Maximum size of files to read ahead\n\n\n",
55                program_invocation_short_name);
56
57         printf("%s [OPTIONS...] analyze [PACK FILE]\n\n"
58                "Analyze collected read-ahead data.\n\n"
59                "  -h --help                 Show this help\n"
60                "     --version              Show package version\n",
61                program_invocation_short_name);
62
63         return 0;
64 }
65
66 static int parse_argv(int argc, char *argv[]) {
67
68         enum {
69                 ARG_VERSION = 0x100,
70                 ARG_FILES_MAX,
71                 ARG_FILE_SIZE_MAX,
72                 ARG_TIMEOUT
73         };
74
75         static const struct option options[] = {
76                 { "help",          no_argument,       NULL, 'h'                },
77                 { "version",       no_argument,       NULL, ARG_VERSION        },
78                 { "files-max",     required_argument, NULL, ARG_FILES_MAX      },
79                 { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX  },
80                 { "timeout",       required_argument, NULL, ARG_TIMEOUT        },
81                 {}
82         };
83
84         int c;
85
86         assert(argc >= 0);
87         assert(argv);
88
89         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
90
91                 switch (c) {
92
93                 case 'h':
94                         return help();
95
96                 case ARG_VERSION:
97                         puts(PACKAGE_STRING);
98                         puts(SYSTEMD_FEATURES);
99                         return 0;
100
101                 case ARG_FILES_MAX:
102                         if (safe_atou(optarg, &arg_files_max) < 0 || arg_files_max <= 0) {
103                                 log_error("Failed to parse maximum number of files %s.", optarg);
104                                 return -EINVAL;
105                         }
106                         break;
107
108                 case ARG_FILE_SIZE_MAX: {
109                         unsigned long long ull;
110
111                         if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
112                                 log_error("Failed to parse maximum file size %s.", optarg);
113                                 return -EINVAL;
114                         }
115
116                         arg_file_size_max = (off_t) ull;
117                         break;
118                 }
119
120                 case ARG_TIMEOUT:
121                         if (parse_sec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
122                                 log_error("Failed to parse timeout %s.", optarg);
123                                 return -EINVAL;
124                         }
125
126                         break;
127
128                 case '?':
129                         return -EINVAL;
130
131                 default:
132                         assert_not_reached("Unhandled option");
133                 }
134         }
135
136         if (optind != argc-1 &&
137             optind != argc-2) {
138                 help();
139                 return -EINVAL;
140         }
141
142         return 1;
143 }
144
145 int main(int argc, char *argv[]) {
146         int r;
147
148         log_set_target(LOG_TARGET_SAFE);
149         log_parse_environment();
150         log_open();
151
152         umask(0022);
153
154         r = parse_argv(argc, argv);
155         if (r <= 0)
156                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
157
158         if (streq(argv[optind], "collect"))
159                 return main_collect(argv[optind+1]);
160         else if (streq(argv[optind], "replay"))
161                 return main_replay(argv[optind+1]);
162         else if (streq(argv[optind], "analyze"))
163                 return main_analyze(argv[optind+1]);
164
165         log_error("Unknown verb %s.", argv[optind]);
166         return EXIT_FAILURE;
167 }