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