Merge commit 'fml/master'
[adu.git] / adu.h
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.h Global definitions. */
8
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <time.h> /* time(), localtime() */
15 #include <unistd.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <stdarg.h>
19 #include <inttypes.h>
20 #include <string.h>
21 #include <assert.h>
22 #include <osl.h>
23 #include "gcc-compat.h"
24
25 /** debug loglevel, gets really noisy */
26 #define DEBUG 1
27 /** still noisy, but won't fill your disk */
28 #define INFO 2
29 /** normal, but significant event */
30 #define NOTICE 3
31 /** unexpected event that can be handled */
32 #define WARNING 4
33 /** unhandled error condition */
34 #define ERROR 5
35 /** system might be unreliable */
36 #define CRIT 6
37 /** last message before exit */
38 #define EMERG 7
39
40 /** Log messages with lower priority than that will not be compiled in. */
41 #define COMPILE_TIME_LOGLEVEL 0
42
43 /** \cond */
44 #if DEBUG > COMPILE_TIME_LOGLEVEL
45 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
46 #else
47 #define DEBUG_LOG(...) do {;} while (0)
48 #endif
49
50 #if INFO > COMPILE_TIME_LOGLEVEL
51 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
52 #else
53 #define INFO_LOG(...) do {;} while (0)
54 #endif
55
56 #if NOTICE > COMPILE_TIME_LOGLEVEL
57 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
58 #else
59 #define NOTICE_LOG(...) do {;} while (0)
60 #endif
61
62 #if WARNING > COMPILE_TIME_LOGLEVEL
63 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
64 #else
65 #define WARNING_LOG(...) do {;} while (0)
66 #endif
67
68 #if ERROR > COMPILE_TIME_LOGLEVEL
69 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
70 #else
71 #define ERROR_LOG(...) do {;} while (0)
72 #endif
73
74 #if CRIT > COMPILE_TIME_LOGLEVEL
75 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
76 #else
77 #define CRIT_LOG(...) do {;} while (0)
78 #endif
79
80 #if EMERG > COMPILE_TIME_LOGLEVEL
81 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
82 #else
83 #define EMERG_LOG(...)
84 #endif
85 /** \endcond */
86
87 /**
88 * Write a log message to a dynamically allocated string.
89 *
90 * \param fmt Usual format string.
91 * \param p Result pointer.
92 *
93 * \sa printf(3). */
94 #define VSPRINTF(fmt, p) \
95 { \
96 int n; \
97 size_t size = 100; \
98 p = adu_malloc(size); \
99 while (1) { \
100 va_list ap; \
101 /* Try to print in the allocated space. */ \
102 va_start(ap, fmt); \
103 n = vsnprintf(p, size, fmt, ap); \
104 va_end(ap); \
105 /* If that worked, return the string. */ \
106 if (n > -1 && n < size) \
107 break; \
108 /* Else try again with more space. */ \
109 if (n > -1) /* glibc 2.1 */ \
110 size = n + 1; /* precisely what is needed */ \
111 else /* glibc 2.0 */ \
112 size *= 2; /* twice the old size */ \
113 p = adu_realloc(p, size); \
114 } \
115 }
116
117 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
118 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
119
120 /** The columns of the directory table. */
121 enum dir_table_columns {
122 /** The name of the directory. */
123 DT_NAME,
124 /** The dir count number. */
125 DT_NUM,
126 /** The number of the parent directory. */
127 DT_PARENT_NUM,
128 /** The number of bytes of all regular files. */
129 DT_BYTES,
130 /** The number of all regular files. */
131 DT_FILES,
132 /** Number of columns in this table. */
133 NUM_DT_COLUMNS
134 };
135
136 /** The columns of the id table. */
137 enum user_table_columns {
138 /** The numer of the directory. */
139 UT_DIR_NUM,
140 /** The number of bytes of all regular files in this dir owned by this id. */
141 UT_BYTES,
142 /** The number of files in this dir owned by this id. */
143 UT_FILES,
144 /** Number of columns in this table. */
145 NUM_UT_COLUMNS
146 };
147
148 enum uid_info_flags {
149 /** Whether this slot of the hash table is used. */
150 UI_FL_SLOT_USED = 1,
151 /** Whether this uid should be taken into account. */
152 UI_FL_ADMISSIBLE = 2,
153 };
154
155 struct user_info {
156 uint32_t uid;
157 uint32_t flags;
158 char *pw_name;
159 struct osl_table *table;
160 uint64_t files;
161 uint64_t bytes;
162 uint64_t dirs;
163 struct osl_table_description *desc;
164 };
165
166 struct uid_range {
167 uint32_t low;
168 uint32_t high;
169 };
170
171 enum search_uid_flags {
172 OPEN_USER_TABLE = 1,
173 CREATE_USER_TABLE = 2,
174 };
175
176 #define FOR_EACH_UID_RANGE(ur, urs) for (ur = urs; ur->low <= ur->high; ur++)
177
178 extern uint32_t num_uids;
179 extern struct osl_table *dir_table;
180 extern struct gengetopt_args_info conf;
181 extern struct select_args_info select_conf;
182
183 /* adu.c */
184 __printf_2_3 void __log(int, const char*, ...);
185 int open_dir_table(int create);
186 void check_signals(void);
187 void close_all_tables(void);
188 char *get_uid_list_name(void);
189 void create_hash_table(unsigned bits);
190 int search_uid(uint32_t uid, struct uid_range *urs,
191 enum search_uid_flags flags, struct user_info **ui_ptr);
192 int for_each_admissible_user(int (*func)(struct user_info *, void *),
193 void *data);
194 void sort_hash_table(int (*comp)(const void *, const void *));
195
196 /* select.c */
197 int run_select_query(struct uid_range *admissible_uids);
198 int com_select(void);
199
200 /* create.h */
201 int com_create(void);
202 int com_interactive(void);