1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
--- /dev/null
+++ b/trigger/Makefile
@@ -0,0 +1,44 @@
+include ../Makefile.inc
+LUA_VERSION=5.1
+PREFIX_SEARCH=/usr /usr/local /opt/local
+LUA_PLUGINDIR=$(firstword \
+ $(foreach ldir,$(subst ;, ,$(shell lua -e 'print(package.cpath)')), \
+ $(if $(findstring lib/lua/,$(ldir)),$(patsubst %/?.so,%,$(ldir))) \
+ ) \
+)
+
+# find lua prefix
+LUA_PREFIX=$(firstword \
+ $(foreach prefix,$(PREFIX_SEARCH),\
+ $(if $(wildcard $(prefix)/include/lua.h),$(prefix)) \
+ ) \
+)
+
+libdir=$(prefix)/libs
+luadir=$(if $(LUA_PLUGINDIR),$(LUA_PLUGINDIR),$(libdir)/lua/$(LUA_VERSION))
+luainc=$(shell pkg-config --silence-errors --cflags lua$(LUA_VERSION))
+
+CPPFLAGS=-I.. $(if $(luainc),$(luainc), -I$(LUA_PREFIX)/include)
+LIBS=-L.. -luci $(shell pkg-config --silence-errors --libs lua$(LUA_VERSION))
+
+PLUGIN_LD=$(CC)
+ifeq ($(OS),Darwin)
+ PLUGIN_LDFLAGS=-bundle
+else
+ PLUGIN_LDFLAGS=-shared -Wl,-soname,$(SHLIB_FILE)
+endif
+
+all: uci_trigger.so
+
+uci_trigger.so: uci_trigger.o
+ $(PLUGIN_LD) $(PLUGIN_LDFLAGS) -o $@ $^ $(LIBS)
+
+%.o: %.c
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(FPIC) -c -o $@ $<
+
+install:
+ mkdir -p $(DESTDIR)$(luadir)
+ $(INSTALL) -m0644 uci_trigger.so $(DESTDIR)$(luadir)/
+
+clean:
+ rm -f *.so *.o uci_trigger.so
--- /dev/null
+++ b/trigger/uci_trigger.c
@@ -0,0 +1,132 @@
+#include <sys/types.h>
+#include <sys/time.h>
+#include <stdbool.h>
+#include <string.h>
+#include <stdio.h>
+#include <lualib.h>
+#include <lauxlib.h>
+#include "uci.h"
+
+// #define DEBUG
+
+static int refcount = 0;
+static lua_State *gL = NULL;
+static bool prepared = false;
+
+struct trigger_set_op {
+ struct uci_package *p;
+ struct uci_history *h;
+};
+
+static int trigger_set(lua_State *L)
+{
+ struct trigger_set_op *so =
+ (struct trigger_set_op *)lua_touserdata(L, 1);
+ struct uci_package *p = so->p;
+ struct uci_history *h = so->h;
+ struct uci_context *ctx = p->ctx;
+
+ /* ignore non-standard savedirs/configdirs
+ * in order to not trigger events on uci state changes */
+ if (strcmp(ctx->savedir, UCI_SAVEDIR) ||
+ strcmp(ctx->confdir, UCI_CONFDIR))
+ return 0;
+
+ if (!prepared) {
+ lua_getglobal(L, "require");
+ lua_pushstring(L, "uci.trigger");
+ lua_call(L, 1, 0);
+
+ lua_getglobal(L, "uci");
+ lua_getfield(L, -1, "trigger");
+ lua_getfield(L, -1, "load_modules");
+ lua_call(L, 0, 0);
+ prepared = true;
+ } else {
+ lua_getglobal(L, "uci");
+ lua_getfield(L, -1, "trigger");
+ }
+
+ lua_getfield(L, -1, "set");
+ lua_createtable(L, 4, 0);
+
+ lua_pushstring(L, p->e.name);
+ lua_rawseti(L, -2, 1);
+ if (h->section) {
+ lua_pushstring(L, h->section);
+ lua_rawseti(L, -2, 2);
+ }
+ if (h->e.name) {
+ lua_pushstring(L, h->e.name);
+ lua_rawseti(L, -2, 3);
+ }
+ if (h->value) {
+ lua_pushstring(L, h->value);
+ lua_rawseti(L, -2, 4);
+ }
+ lua_call(L, 1, 0);
+ lua_pop(L, 2);
+ return 0;
+}
+
+#ifdef DEBUG
+
+static int report (lua_State *L, int status) {
+ if (status && !lua_isnil(L, -1)) {
+ const char *msg = lua_tostring(L, -1);
+ if (msg == NULL) msg = "(error object is not a string)";
+ fprintf(stderr, "ERROR: %s\n", msg);
+ lua_pop(L, 1);
+ }
+ return status;
+}
+
+#else
+
+static inline int report(lua_State *L, int status) {
+ return status;
+}
+
+#endif
+
+static void trigger_set_hook(const struct uci_hook_ops *ops, struct uci_package *p, struct uci_history *h)
+{
+ struct trigger_set_op so;
+
+ so.p = p;
+ so.h = h;
+ report(gL, lua_cpcall(gL, &trigger_set, &so));
+}
+
+static struct uci_hook_ops hook = {
+ .set = trigger_set_hook,
+};
+
+static int trigger_attach(struct uci_context *ctx)
+{
+ if (!gL) {
+ gL = luaL_newstate();
+ if (!gL)
+ return -1;
+ luaL_openlibs(gL);
+
+ refcount++;
+ }
+ uci_add_hook(ctx, &hook);
+ return 0;
+}
+
+static void trigger_detach(struct uci_context *ctx)
+{
+ if (gL && (--refcount <= 0)) {
+ lua_close(gL);
+ gL = NULL;
+ refcount = 0;
+ prepared = false;
+ }
+}
+
+struct uci_plugin_ops uci_plugin = {
+ .attach = trigger_attach,
+ .detach = trigger_detach,
+};
|