Merge LuCIttpd
[project/luci.git] / libs / lucittpd / src / main.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
28 #include <netdb.h>
29 #include <arpa/inet.h>
30 #include <sys/wait.h>
31 #include <signal.h>
32 #include <features.h>
33
34 #include <lib/uci.h>
35 #include <lib/log.h>
36 #include <lib/signal.h>
37 #include <lib/luaplugin.h>
38
39 #ifndef __UCLIBC__
40 #include <sys/sendfile.h>
41 #endif
42
43 #define BACKLOG 10
44
45 static int port = 0;
46 static const char *plugin_path = NULL;
47 static struct luaplugin_ctx ctx;
48 static struct luaplugin_entry *e;
49 static struct timeval timeout;
50
51 static void load_config(void)
52 {
53 timeout.tv_usec = 0;
54
55 static struct uci_context* uci = 0;
56 uci = ucix_init("lucittpd");
57 if(uci)
58 {
59 plugin_path = ucix_get_option(uci, "lucittpd", "lucittpd", "path");
60 port = ucix_get_option_int(uci, "lucittpd", "lucittpd", "port", 80);
61 timeout.tv_sec = ucix_get_option_int(uci, "lucittpd", "lucittpd", "timeout", 90);
62 } else {
63 port = 8080;
64 timeout.tv_sec = 90;
65 }
66 if(!plugin_path)
67 plugin_path = strdup("/usr/lib/lucittpd/plugins/");
68
69 // ToDo: Check why below command segfaults in uci_free_context
70 //ucix_cleanup(uci);
71 }
72
73 static int webuci_read(lua_State *L)
74 {
75 int len = luaL_checkinteger(L, 1);
76 if (len <= 0) {
77 return luaL_argerror(L, 1, "too low");
78 }
79
80 char *buffer = malloc(len);
81 if (!buffer) {
82 return luaL_error(L, "malloc() failed");
83 }
84
85 int sockfd = lua_tointeger(L, lua_upvalueindex(1));
86
87 len = read(sockfd, buffer, len);
88 if (len > 0) {
89 lua_pushlstring(L, buffer, len);
90 free(buffer);
91 } else {
92 free(buffer);
93 lua_pushnil(L);
94 lua_pushinteger(L, (len == 0) ? 0 : errno);
95 return 2;
96 }
97
98 /* fprintf(stderr, "%s:%s[%d] %d %d\n", __FILE__, __func__, __LINE__, sockfd, len); */
99
100 return 1;
101 }
102
103 static int webuci_close(lua_State *L)
104 {
105 int sockfd = lua_tointeger(L, lua_upvalueindex(1));
106 int result = shutdown(sockfd, SHUT_RDWR);
107 close(sockfd);
108 /*log_printf("%s:%s[%d] %d %d\n", __FILE__, __func__, __LINE__, sockfd, result);*/
109
110 if (result < 0) {
111 lua_pushnil(L);
112 lua_pushinteger(L, errno);
113 return 2;
114 } else {
115 lua_pushboolean(L, 1);
116 return 1;
117 }
118 }
119
120 static int webuci_write(lua_State *L)
121 {
122 luaL_checktype(L, 1, LUA_TSTRING);
123
124 size_t len;
125 const char *data = lua_tolstring(L, 1, &len);
126 int sockfd = lua_tointeger(L, lua_upvalueindex(1));
127
128 len = send(sockfd, data, len, 0);
129 /*log_printf("%s:%s[%d] %d %d - %s\n", __FILE__, __func__, __LINE__, sockfd, len, data);*/
130 if (len < 0) {
131 lua_pushnil(L);
132 lua_pushinteger(L, errno);
133 return 2;
134 } else {
135 lua_pushinteger(L, len);
136 return 1;
137 }
138 }
139
140 static int webuci_sendfile(lua_State *L)
141 {
142 FILE **fp = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
143 if (*fp == NULL) {
144 return luaL_error(L, "attempt to use a closed file");
145 }
146
147 off_t offset = luaL_checkinteger(L, 2);
148 size_t size = luaL_checkinteger(L, 3);
149
150 int sockfd = lua_tointeger(L, lua_upvalueindex(1));
151
152 int cork = 1;
153 setsockopt(sockfd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
154
155 #ifdef __UCLIBC__
156 // uclibc is teh sux, it does not implement sendfile correctly
157 char tmp[1024];
158 size_t c, toread = size, oldpos = ftell(*fp);
159
160 fseek(*fp, offset, SEEK_SET);
161
162 while(toread > 0 && (c = fread(tmp, 1, (toread < 1024) ? toread : 1024, *fp)) > 0)
163 {
164 size += c;
165 toread -= c;
166 write(sockfd, tmp, c);
167 }
168
169 fseek(*fp, oldpos, SEEK_SET);
170 #else
171 size = sendfile(sockfd, fileno(*fp), &offset, size);
172 /*log_printf("%s:%s[%d] %d %d - %d\n", __FILE__, __func__, __LINE__, sockfd, fileno(*fp), size);*/
173 #endif
174
175 cork = 0;
176 setsockopt(sockfd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
177
178 if (size < 1) {
179 lua_pushnil(L);
180 lua_pushinteger(L, errno);
181 } else {
182 lua_pushinteger(L, size);
183 lua_pushinteger(L, offset);
184 }
185
186 return 2;
187 }
188
189
190 static void load_luci(const char *plugindir)
191 {
192 luaplugin_init(&ctx, plugindir);
193 luaplugin_scan(&ctx);
194
195 list_for_each_entry(e, &ctx.entries, list)
196 {
197 lua_pushstring(ctx.L, "initialize");
198 luaplugin_call(e, 0);
199 }
200
201 list_for_each_entry(e, &ctx.entries, list)
202 {
203 lua_pushstring(ctx.L, "register");
204 luaplugin_call(e, 0);
205 }
206
207 list_for_each_entry(e, &ctx.entries, list)
208 {
209 lua_pushstring(ctx.L, "filter");
210 luaplugin_call(e, 0);
211 }
212 }
213
214 static void run_luci(int sockfd)
215 {
216 lua_pushinteger(ctx.L, sockfd);
217 lua_pushcclosure(ctx.L, webuci_read, 1);
218 lua_setfield(ctx.L, LUA_GLOBALSINDEX, "webuci_read");
219
220 lua_pushinteger(ctx.L, sockfd);
221 lua_pushcclosure(ctx.L, webuci_write, 1);
222 lua_setfield(ctx.L, LUA_GLOBALSINDEX, "webuci_write");
223
224 lua_pushinteger(ctx.L, sockfd);
225 lua_pushcclosure(ctx.L, webuci_close, 1);
226 lua_setfield(ctx.L, LUA_GLOBALSINDEX, "webuci_close");
227
228 lua_pushinteger(ctx.L, sockfd);
229 lua_pushcclosure(ctx.L, webuci_sendfile, 1);
230 lua_setfield(ctx.L, LUA_GLOBALSINDEX, "webuci_sendfile");
231
232 list_for_each_entry(e, &ctx.entries, list)
233 {
234 lua_pushstring(ctx.L, "accept");
235 luaplugin_call(e, 0);
236 }
237 }
238
239 static void cleanup_luci(void)
240 {
241 luaplugin_done(&ctx);
242 }
243
244 int main(int argc, char **argv)
245 {
246 int sockfd, new_fd;
247 struct sockaddr_storage their_addr;
248 socklen_t sin_size;
249 int yes = 1;
250 struct sockaddr_in myaddr;
251
252 log_start(1);
253
254 load_config();
255
256 setup_signals();
257
258 /* used by sdk to override plugin dir */
259 if(argc != 2)
260 {
261 load_luci(plugin_path);
262 } else {
263 load_luci(argv[1]);
264 port = 8080;
265 }
266
267 myaddr.sin_family = AF_INET;
268 myaddr.sin_port = htons(port);
269 //inet_pton(AF_INET, "63.161.169.137", &myaddr.sin_addr.s_addr);
270 myaddr.sin_addr.s_addr = INADDR_ANY;
271
272 sockfd = socket(PF_INET, SOCK_STREAM, 0);
273
274 if(sockfd == -1)
275 {
276 perror("server: socket");
277 exit(1);
278 }
279
280 if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
281 {
282 perror("setsockopt");
283 exit(1);
284 }
285
286 if(bind(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1)
287 {
288 close(sockfd);
289 perror("server: bind");
290 exit(1);
291 }
292
293 if(listen(sockfd, BACKLOG) == -1)
294 {
295 perror("listen");
296 exit(1);
297 }
298
299 /*log_printf("server: waiting for connections...\n");*/
300
301 while(1)
302 {
303 sin_size = sizeof their_addr;
304 new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
305 if(new_fd == -1)
306 {
307 perror("accept");
308 continue;
309 }
310
311 /*inet_ntop(their_addr.ss_family,
312 (void*)&((struct sockaddr_in*)&their_addr)->sin_addr, s, sizeof s);
313 log_printf("server: got connection from %s\n", s);*/
314
315 if(!fork())
316 {
317 /* child */
318 close(sockfd);
319
320 setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
321 setsockopt(new_fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
322
323 run_luci(new_fd);
324 cleanup_luci();
325 close(new_fd);
326
327 exit(0);
328 }
329 close(new_fd);
330 }
331
332 return 0;
333 }