attach_to_bridge(): Improve error message.
[micoforia.git] / m7a.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #define _GNU_SOURCE
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <inttypes.h>
9 #include <stdarg.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <time.h>
13 #include <errno.h>
14 #include <sys/uio.h>
15 #include <pwd.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <sys/wait.h>
20 #include <limits.h>
21
22 #include "config.h"
23
24 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
25
26 #define CMD_PTR(_cname) lls_cmd(LSG_MICOFORIA_CMD_ ## _cname, micoforia_suite)
27 #define OPT_RESULT(_cname, _oname) (lls_opt_result(\
28 LSG_MICOFORIA_ ## _cname ## _OPT_ ## _oname, \
29 (CMD_PTR(_cname) == CMD_PTR(MICOFORIA))? lpr : sublpr))
30 #define OPT_GIVEN(_cname, _oname) (lls_opt_given(OPT_RESULT(_cname, _oname)))
31 #define OPT_UINT32_VAL_N(_n, _cname, _oname) (lls_uint32_val(_n, \
32 OPT_RESULT(_cname, _oname)))
33 #define OPT_UINT32_VAL(_cname, _oname) (OPT_UINT32_VAL_N(0, _cname, _oname))
34 #define OPT_STRING_VAL_N(_n, _cname, _oname) (lls_string_val(_n, \
35 OPT_RESULT(_cname, _oname)))
36 #define OPT_STRING_VAL(_cname, _oname) (OPT_STRING_VAL_N(0, _cname, _oname))
37
38 struct micoforia_user_data {bool (*handler)(void);};
39 #define EXPORT_CMD_HANDLER(_cmd) const struct micoforia_user_data \
40 lsg_micoforia_com_ ## _cmd ## _user_data = { \
41 .handler = com_ ## _cmd \
42 };
43
44
45 __attribute__ ((warn_unused_result))
46 void *xrealloc(void *p, size_t size);
47
48 __attribute__ ((warn_unused_result))
49 void *xmalloc(size_t size);
50
51 __attribute__ ((warn_unused_result))
52 void *xzmalloc(size_t size);
53
54 void *xstrdup(const char *s);
55 char *xstrcat(char *a, const char *b);
56
57 __attribute__ ((format (printf, 1, 2))) __attribute__ ((warn_unused_result))
58 char *msg(const char *fmt, ...);
59
60 enum loglevels {LOGLEVELS, NUM_LOGLEVELS};
61 extern unsigned loglevel_arg_val;
62
63 __attribute__ ((format (printf, 2, 3)))
64 void m7a_log(int ll, const char* fmt,...);
65
66 #define DEBUG_LOG(f,...) m7a_log(LL_DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
67 #define INFO_LOG(f,...) m7a_log(LL_INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
68 #define NOTICE_LOG(f,...) m7a_log(LL_NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
69 #define WARNING_LOG(f,...) m7a_log(LL_WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
70 #define ERROR_LOG(f,...) m7a_log(LL_ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
71 #define CRIT_LOG(f,...) m7a_log(LL_CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
72 #define EMERG_LOG(f,...) m7a_log(LL_EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
73
74 __attribute__ ((noreturn))
75 __attribute__ ((format (printf, 1, 2)))
76 void die(const char *fmt, ...);
77
78 __attribute__ ((noreturn))
79 __attribute__ ((format (printf, 1, 2)))
80 void die_errno(const char *fmt, ...);
81
82 __attribute__ ((noreturn))
83 void die_empty_arg(const char *opt);
84
85 void check_range(uint32_t val, uint32_t min, uint32_t max, const char *opt);
86
87 bool xexec(char * const argv[], const struct iovec *iov);
88 void valid_fd012(void);
89 void check_name(const char *arg);
90 void parse_compound_arg(const char *arg, const char *opt, char **name, char **val);
91 char *parse_cgroup_acl(const char *arg);
92 char *make_hwaddr(const char *name, const char *bridge);
93 void parse_ifspec(const char *arg, char **bridge, uint8_t *hwaddr);
94 uint32_t atou32(const char *str, const char *opt);
95 bool remove_subdirs_recursively(const char *path);
96 void daemonize(const char *logfile);
97 bool acquire_lock(const char *string);
98 bool try_lock(const char *string, pid_t *pid);
99 bool release_lock(const char *string);
100 bool is_locked(const char *string, pid_t *pid);
101 bool attach_to_bridge(const char *iface, const char *bridge);
102 bool rename_interface(const char *before, const char *after);
103 void pretty_print_hwaddr(const uint8_t *hwaddr, char *result);
104 bool set_hwaddr(const char *iface, const uint8_t *hwaddr);
105 bool link_del(const char *iface);
106 bool link_up(const char *iface);
107 bool create_veth_device_pair(const char *name, char *peer);
108 bool set_netns(const char *iface, pid_t pid);
109 int request_fd(const char *socket_path, char *msg, int *result);
110 bool request_int(const char *socket_path, char *msg, int *result);
111 bool listen_on_unix_socket(const char *socket_path, int *result);
112 bool recv_cred_buffer(int socketfd, char *buf, size_t size,
113 int *clientfd, uid_t *uid);
114 bool pass_fd(int passfd, int socketfd);
115
116 extern int signal_pipe[2];
117 void init_signal_handling(void);
118 int next_signal(void);