chiark / gitweb /
bus-proxy: drop one wrong assert()
[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 void help(void) {
40         printf("%1$s [OPTIONS...] collect [DIRECTORY]\n\n"
41                "Collect read-ahead data on early boot.\n\n"
42                "  -h --help                 Show this help\n"
43                "     --version              Show package version\n"
44                "     --files-max=INT        Maximum number of files to read ahead\n"
45                "     --file-size-max=BYTES  Maximum size of files to read ahead\n"
46                "     --timeout=USEC         Maximum time to spend collecting data\n"
47                "\n\n"
48                "%1$s [OPTIONS...] replay [DIRECTORY]\n\n"
49                "Replay collected read-ahead data on early boot.\n\n"
50                "  -h --help                 Show this help\n"
51                "     --version              Show package version\n"
52                "     --file-size-max=BYTES  Maximum size of files to read ahead\n"
53                "\n\n"
54                "%1$s [OPTIONS...] analyze [PACK-FILE]\n\n"
55                "Analyze collected read-ahead data.\n\n"
56                "  -h --help                 Show this help\n"
57                "     --version              Show package version\n",
58                program_invocation_short_name);
59 }
60
61 static int parse_argv(int argc, char *argv[]) {
62
63         enum {
64                 ARG_VERSION = 0x100,
65                 ARG_FILES_MAX,
66                 ARG_FILE_SIZE_MAX,
67                 ARG_TIMEOUT
68         };
69
70         static const struct option options[] = {
71                 { "help",          no_argument,       NULL, 'h'                },
72                 { "version",       no_argument,       NULL, ARG_VERSION        },
73                 { "files-max",     required_argument, NULL, ARG_FILES_MAX      },
74                 { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX  },
75                 { "timeout",       required_argument, NULL, ARG_TIMEOUT        },
76                 {}
77         };
78
79         int c;
80
81         assert(argc >= 0);
82         assert(argv);
83
84         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
85
86                 switch (c) {
87
88                 case 'h':
89                         help();
90                         return 0;
91
92                 case ARG_VERSION:
93                         puts(PACKAGE_STRING);
94                         puts(SYSTEMD_FEATURES);
95                         return 0;
96
97                 case ARG_FILES_MAX:
98                         if (safe_atou(optarg, &arg_files_max) < 0 || arg_files_max <= 0) {
99                                 log_error("Failed to parse maximum number of files %s.", optarg);
100                                 return -EINVAL;
101                         }
102                         break;
103
104                 case ARG_FILE_SIZE_MAX: {
105                         unsigned long long ull;
106
107                         if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
108                                 log_error("Failed to parse maximum file size %s.", optarg);
109                                 return -EINVAL;
110                         }
111
112                         arg_file_size_max = (off_t) ull;
113                         break;
114                 }
115
116                 case ARG_TIMEOUT:
117                         if (parse_sec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
118                                 log_error("Failed to parse timeout %s.", optarg);
119                                 return -EINVAL;
120                         }
121
122                         break;
123
124                 case '?':
125                         return -EINVAL;
126
127                 default:
128                         assert_not_reached("Unhandled option");
129                 }
130
131         if (optind != argc-1 &&
132             optind != argc-2) {
133                 log_error("%s: wrong number of arguments.",
134                           program_invocation_short_name);
135                 return -EINVAL;
136         }
137
138         return 1;
139 }
140
141 int main(int argc, char *argv[]) {
142         int r;
143
144         log_set_target(LOG_TARGET_SAFE);
145         log_parse_environment();
146         log_open();
147
148         umask(0022);
149
150         r = parse_argv(argc, argv);
151         if (r <= 0)
152                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
153
154         if (streq(argv[optind], "collect"))
155                 return main_collect(argv[optind+1]);
156         else if (streq(argv[optind], "replay"))
157                 return main_replay(argv[optind+1]);
158         else if (streq(argv[optind], "analyze"))
159                 return main_analyze(argv[optind+1]);
160
161         log_error("Unknown verb %s.", argv[optind]);
162         return EXIT_FAILURE;
163 }