separate state and lock files, use state file information to purge ipsets
[project/firewall3.git] / utils.h
1 /*
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef __FW3_UTILS_H
20 #define __FW3_UTILS_H
21
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29 #include <sys/file.h>
30
31 #include <libubox/list.h>
32 #include <uci.h>
33
34
35 #define FW3_STATEFILE "/var/run/fw3.state"
36 #define FW3_LOCKFILE "/var/run/fw3.lock"
37
38 void warn_elem(struct uci_element *e, const char *format, ...);
39 void warn(const char *format, ...);
40 void error(const char *format, ...);
41 void info(const char *format, ...);
42
43 #define fw3_foreach(p, h) \
44 for (p = list_empty(h) ? NULL : list_first_entry(h, typeof(*p), list); \
45 list_empty(h) ? (p == NULL) : (&p->list != (h)); \
46 p = list_empty(h) ? list_first_entry(h, typeof(*p), list) \
47 : list_entry(p->list.next, typeof(*p), list))
48
49 static inline void
50 fw3_free_list(struct list_head *list)
51 {
52 struct list_head *cur, *tmp;
53
54 list_for_each_safe(cur, tmp, list)
55 {
56 list_del(cur);
57 free(cur);
58 }
59 }
60
61 #define fw3_is_family(p, f) \
62 (!p || p->family == FW3_FAMILY_ANY || p->family == f)
63
64 const char * fw3_find_command(const char *cmd);
65
66 bool fw3_stdout_pipe(void);
67 bool __fw3_command_pipe(bool silent, const char *command, ...);
68 #define fw3_command_pipe(...) __fw3_command_pipe(__VA_ARGS__, NULL)
69
70 void fw3_command_close(void);
71 void fw3_pr(const char *fmt, ...);
72
73 bool fw3_has_table(bool ipv6, const char *table);
74
75 bool fw3_lock(void);
76 void fw3_unlock(void);
77
78 bool fw3_has_state(void);
79 void fw3_write_state(void *state);
80 void fw3_remove_state(void);
81
82 #endif