Add create.c.
[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 /** The columns of the directory table. */
122 enum dir_table_columns {
123         /** The name of the directory. */
124         DT_NAME,
125         /** The dir count number. */
126         DT_NUM,
127         /** The number of the parent directory. */
128         DT_PARENT_NUM,
129         /** The number of bytes of all regular files. */
130         DT_BYTES,
131         /** The number of all regular files. */
132         DT_FILES,
133         /** Number of columns in this table. */
134         NUM_DT_COLUMNS
135 };
136
137 /** The columns of the id table. */
138 enum user_table_columns {
139         /** The numer of the directory. */
140         UT_DIR_NUM,
141         /** The number of bytes of all regular files in this dir owned by this id. */
142         UT_BYTES,
143         /** The number of files in this dir owned by this id. */
144         UT_FILES,
145         /** Number of columns in this table. */
146         NUM_UT_COLUMNS
147 };
148
149 enum uid_info_flags {
150         /** Whether this slot of the hash table is used. */
151         UI_FL_SLOT_USED = 1,
152         /** Whether this uid should be taken into account. */
153         UI_FL_ADMISSIBLE = 2,
154 };
155
156 struct user_info {
157         uint32_t uid;
158         uint32_t flags;
159         char *pw_name;
160         struct osl_table *table;
161         uint64_t files;
162         uint64_t bytes;
163         uint64_t dirs;
164         struct osl_table_description *desc;
165 };
166
167 struct uid_range {
168         uint32_t low;
169         uint32_t high;
170 };
171
172 enum search_uid_flags {
173         OPEN_USER_TABLE = 1,
174         CREATE_USER_TABLE = 2,
175 };
176
177 extern uint32_t num_uids;
178 extern struct osl_table *dir_table;
179 extern uint64_t num_dirs;
180 extern uint64_t num_files;
181 extern uint64_t num_bytes;
182 extern struct gengetopt_args_info conf;
183
184 /* adu.c */
185 __printf_2_3 void __log(int, const char*, ...);
186 int open_dir_table(int create);
187 void check_signals(void);
188 void close_all_tables(void);
189 char *get_uid_list_name(void);
190 void create_hash_table(unsigned bits);
191 int search_uid(uint32_t uid, enum search_uid_flags flags,
192                 struct user_info **ui_ptr);
193 int for_each_admissible_user(int (*func)(struct user_info *, void *),
194                 void *data);
195
196 /* select.c */
197 int com_select(void);
198
199 /* create.h */
200 int com_create(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 }