Simplify user table closing.
[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 }
123
124 static void signal_handler(int s)
125 {
126 signum = s;
127 }
128
129 void check_signals(void)
130 {
131 if (likely(!signum))
132 return;
133 EMERG_LOG("caught signal %d\n", signum);
134 close_all_tables();
135 exit(EXIT_FAILURE);
136 }
137
138 static int init_signals(void)
139 {
140 if (signal(SIGINT, &signal_handler) == SIG_ERR)
141 return -E_SIGNAL_SIG_ERR;
142 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
143 return -E_SIGNAL_SIG_ERR;
144 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
145 return -E_SIGNAL_SIG_ERR;
146 return 1;
147 }
148
149 int open_dir_table(int create)
150 {
151 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
152
153 if (create) {
154 int ret = osl(osl_create_table(&dir_table_desc));
155 if (ret < 0) {
156 free((char *)dir_table_desc.dir);
157 return ret;
158 }
159 }
160 return osl(osl_open_table(&dir_table_desc, &dir_table));
161 }
162
163 static int check_args(void)
164 {
165 if (conf.create_given && !conf.base_dir_given)
166 return -E_SYNTAX;
167
168 /* remove trailing slashes from base-dir arg */
169 if (conf.base_dir_given) {
170 size_t len = strlen(conf.base_dir_arg);
171 for (;;) {
172 if (!len) /* empty string */
173 return -ERRNO_TO_ERROR(EINVAL);
174 if (!--len) /* length 1 is always OK */
175 break;
176 if (conf.base_dir_arg[len] != '/')
177 break; /* no trailing slash, also OK */
178 conf.base_dir_arg[len] = '\0';
179 }
180 }
181 return 1;
182 }
183
184 static int print_complete_help_and_die(void)
185 {
186 const char **line;
187 select_cmdline_parser_init(&select_conf);
188
189 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
190 printf("%s\n\n", gengetopt_args_info_purpose);
191 printf("%s\n\n", gengetopt_args_info_usage);
192
193 if (conf.help_given)
194 line = gengetopt_args_info_help;
195 else
196 line = gengetopt_args_info_detailed_help;
197 for (; *line; line++)
198 printf("%s\n", *line);
199
200 if (conf.help_given)
201 line = select_args_info_help;
202 else
203 line = select_args_info_detailed_help;
204 printf("Select options:\n");
205 for (; *line; line++)
206 printf("%s\n", *line);
207
208 printf("Interactive commands:\n");
209 print_interactive_help();
210 exit(EXIT_FAILURE);
211 }
212
213 int main(int argc, char **argv)
214 {
215 int ret;
216 struct cmdline_parser_params params = {
217 .override = 0,
218 .initialize = 1,
219 .check_required = 0,
220 .check_ambiguity = 0,
221 .print_errors = 0
222 };
223 /* ignore errors and print complete help if --help was given */
224 cmdline_parser_ext(argc, argv, &conf, &params);
225 if (conf.help_given || conf.detailed_help_given)
226 print_complete_help_and_die();
227 params.check_required = 1;
228 params.check_ambiguity = 1;
229 params.print_errors = 1;
230 ret = cmdline_parser_ext(argc, argv, &conf, &params);
231 if (ret)
232 exit(EXIT_FAILURE);
233
234 ret = check_args();
235 if (ret < 0)
236 goto out;
237 ret = init_signals();
238 if (ret < 0)
239 goto out;
240 ret = -E_SYNTAX;
241 if (conf.select_given)
242 ret = com_select();
243 else if (conf.create_given)
244 ret = com_create();
245 else
246 ret = com_interactive();
247 if (ret < 0)
248 goto out;
249 out:
250 if (ret < 0) {
251 ERROR_LOG("%s\n", adu_strerror(-ret));
252 return -EXIT_FAILURE;
253 }
254 return EXIT_SUCCESS;
255 }