Make close_all_tables() static.
[adu.git] / adu.c
1 /*
2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file adu.c \brief The main functions used by all modes of operation. */
8 #include "adu.h"
9 #include <dirent.h> /* readdir() */
10 #include <pwd.h>
11 #include "format.h"
12 #include "user.h"
13 #include "select.cmdline.h"
14 #include "select.h"
15 #include "cmdline.h"
16 #include "fd.h"
17 #include "string.h"
18 #include "error.h"
19
20 DEFINE_ERRLIST;
21 int osl_errno;
22
23 /** In case a signal is received, its number is stored here. */
24 static int signum;
25
26 /** Command line and config file options. */
27 struct gengetopt_args_info conf;
28
29 /** Options passed to --select-options. */
30 struct select_args_info select_conf;
31
32
33 /**
34 * The table containing the directory names and statistics.
35 */
36 struct osl_table *dir_table = NULL;
37
38 static struct osl_column_description dir_table_cols[] = {
39 [DT_NAME] = {
40 .storage_type = OSL_MAPPED_STORAGE,
41 .storage_flags = 0,
42 .name = "dir",
43 },
44 [DT_NUM] = {
45 .storage_type = OSL_MAPPED_STORAGE,
46 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
47 .name = "num",
48 .compare_function = uint64_compare,
49 .data_size = sizeof(uint64_t)
50 },
51 [DT_PARENT_NUM] = {
52 .storage_type = OSL_MAPPED_STORAGE,
53 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
54 .name = "parent_num",
55 .compare_function = size_compare,
56 .data_size = sizeof(uint64_t)
57 },
58 [DT_BYTES] = {
59 .storage_type = OSL_MAPPED_STORAGE,
60 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
61 .compare_function = size_compare,
62 .name = "num_bytes",
63 .data_size = sizeof(uint64_t)
64 },
65 [DT_FILES] = {
66 .storage_type = OSL_MAPPED_STORAGE,
67 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
68 .compare_function = size_compare,
69 .name = "num_files",
70 .data_size = sizeof(uint64_t)
71 }
72 };
73
74 static struct osl_table_description dir_table_desc = {
75 .name = "dir_table",
76 .num_columns = NUM_DT_COLUMNS,
77 .flags = 0,
78 .column_descriptions = dir_table_cols,
79 };
80
81 /**
82 * The log function.
83 *
84 * \param ll Loglevel.
85 * \param fmt Usual format string.
86 *
87 * All XXX_LOG() macros use this function.
88 */
89 __printf_2_3 void __log(int ll, const char* fmt,...)
90 {
91 va_list argp;
92 FILE *outfd;
93 struct tm *tm;
94 time_t t1;
95 char str[255] = "";
96
97 if (ll < conf.loglevel_arg)
98 return;
99 outfd = stderr;
100 time(&t1);
101 tm = localtime(&t1);
102 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
103 fprintf(outfd, "%s ", str);
104 va_start(argp, fmt);
105 vfprintf(outfd, fmt, argp);
106 va_end(argp);
107 }
108
109 static void close_dir_table(void)
110 {
111 int ret;
112
113 if (!dir_table)
114 return;
115 NOTICE_LOG("closing dir table\n");
116 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
117 if (ret < 0)
118 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
119 free((char *)dir_table_desc.dir);
120 dir_table = NULL;
121 }
122
123 static void close_all_tables(void)
124 {
125 close_dir_table();
126 close_user_tables();
127 }
128
129 static void signal_handler(int s)
130 {
131 signum = s;
132 }
133
134 void check_signals(void)
135 {
136 if (likely(!signum))
137 return;
138 EMERG_LOG("caught signal %d\n", signum);
139 close_all_tables();
140 exit(EXIT_FAILURE);
141 }
142
143 static int init_signals(void)
144 {
145 if (signal(SIGINT, &signal_handler) == SIG_ERR)
146 return -E_SIGNAL_SIG_ERR;
147 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
148 return -E_SIGNAL_SIG_ERR;
149 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
150 return -E_SIGNAL_SIG_ERR;
151 return 1;
152 }
153
154 int open_dir_table(int create)
155 {
156
157 if (dir_table)
158 return 1;
159 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
160
161 if (create) {
162 NOTICE_LOG("creating dir table\n");
163 int ret = osl(osl_create_table(&dir_table_desc));
164 if (ret < 0) {
165 free((char *)dir_table_desc.dir);
166 return ret;
167 }
168 }
169 INFO_LOG("opening dir table\n");
170 return osl(osl_open_table(&dir_table_desc, &dir_table));
171 }
172
173 static int check_args(void)
174 {
175 if (conf.create_given && !conf.base_dir_given)
176 return -E_SYNTAX;
177
178 /* remove trailing slashes from base-dir arg */
179 if (conf.base_dir_given) {
180 size_t len = strlen(conf.base_dir_arg);
181 for (;;) {
182 if (!len) /* empty string */
183 return -ERRNO_TO_ERROR(EINVAL);
184 if (!--len) /* length 1 is always OK */
185 break;
186 if (conf.base_dir_arg[len] != '/')
187 break; /* no trailing slash, also OK */
188 conf.base_dir_arg[len] = '\0';
189 }
190 }
191 return 1;
192 }
193
194 static int print_complete_help_and_die(void)
195 {
196 const char **line;
197
198 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
199 printf("%s\n\n", gengetopt_args_info_purpose);
200 printf("%s\n\n", gengetopt_args_info_usage);
201
202 if (conf.help_given)
203 line = gengetopt_args_info_help;
204 else
205 line = gengetopt_args_info_detailed_help;
206 for (; *line; line++)
207 printf("%s\n", *line);
208
209 if (conf.help_given)
210 line = select_args_info_help;
211 else
212 line = select_args_info_detailed_help;
213 printf("Select options:\n");
214 for (; *line; line++)
215 printf("%s\n", *line);
216
217 printf("Interactive commands:\n");
218 print_interactive_help();
219 cmdline_parser_free(&conf);
220 select_cmdline_parser_free(&select_conf);
221 exit(EXIT_FAILURE);
222 }
223
224 int main(int argc, char **argv)
225 {
226 int ret;
227 struct cmdline_parser_params params = {
228 .override = 1,
229 .initialize = 1,
230 .check_required = 0,
231 .check_ambiguity = 0,
232 .print_errors = 0
233 };
234 select_cmdline_parser_init(&select_conf);
235 cmdline_parser_init(&conf);
236 /* ignore errors and print complete help if --help was given */
237 cmdline_parser_ext(argc, argv, &conf, &params);
238 if (conf.help_given || conf.detailed_help_given)
239 print_complete_help_and_die();
240 cmdline_parser_free(&conf);
241 params.check_required = 1;
242 params.check_ambiguity = 1;
243 params.print_errors = 1;
244 ret = cmdline_parser_ext(argc, argv, &conf, &params);
245 if (ret)
246 exit(EXIT_FAILURE);
247
248 ret = check_args();
249 if (ret < 0)
250 goto out;
251 ret = init_signals();
252 if (ret < 0)
253 goto out;
254 ret = -E_SYNTAX;
255 if (conf.select_given)
256 ret = com_select();
257 else if (conf.create_given)
258 ret = com_create();
259 else
260 ret = com_interactive();
261 if (ret < 0)
262 goto out;
263 out:
264 close_all_tables();
265 if (ret < 0) {
266 ERROR_LOG("%s\n", adu_strerror(-ret));
267 return -EXIT_FAILURE;
268 }
269 cmdline_parser_free(&conf);
270 select_cmdline_parser_free(&select_conf);
271 return EXIT_SUCCESS;
272 }