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