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