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