Skip to content

Commit

Permalink
util: add reset
Browse files Browse the repository at this point in the history
Signed-off-by: hexian000 <[email protected]>
  • Loading branch information
hexian000 committed Apr 22, 2023
1 parent 0915f0e commit 9213d07
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ http_read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
assert(ctx->content > ctx->rbuf.data);
const size_t offset = ctx->content - ctx->rbuf.data;
const size_t want = ctx->content_length + 1;
const size_t cap = ctx->rbuf.cap - offset;
if (want > cap) {
const size_t content_cap = ctx->rbuf.cap - offset;
if (want > content_cap) {
/* no enough buffer */
ev_io_stop(loop, watcher);
http_write_error(ctx, HTTP_ENTITY_TOO_LARGE);
Expand Down Expand Up @@ -521,15 +521,15 @@ static void http_handle_stats(
""
"Ruleset Memory : %s\n",
heap_total);
const char *s = ruleset_stats(ruleset, dt);
if (s != NULL) {
const char *str = ruleset_stats(ruleset, dt);
if (str != NULL) {
(void)buf_appendf(
&ctx->wbuf,
"\n"
"Ruleset Stats\n"
"================\n"
"%s\n",
s);
str);
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static struct {
#if WITH_TPROXY
bool tproxy : 1;
#endif
bool want_reset : 1;
int verbosity;
int resolve_pf;
const char *user_name;
Expand Down Expand Up @@ -297,6 +298,7 @@ int main(int argc, char **argv)
LOGI("server start");
ev_run(loop, 0);

LOGI("server stop");
if (apiserver != NULL) {
server_stop(apiserver, loop);
server_free(apiserver);
Expand All @@ -311,6 +313,11 @@ int main(int argc, char **argv)
ev_signal_stop(loop, &args.w_sigint);
ev_signal_stop(loop, &args.w_sigterm);

if (args.want_reset) {
LOGI("reloading binaries");
reset(argv);
}

LOGI("program terminated normally.");
return EXIT_SUCCESS;
}
Expand All @@ -322,13 +329,15 @@ signal_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents)

switch (watcher->signum) {
case SIGPIPE:
case SIGHUP: {
LOGV_F("signal %d ignored", watcher->signum);
} break;
break;
case SIGHUP:
args.want_reset = true;
/* fallthrough */
case SIGINT:
case SIGTERM: {
case SIGTERM:
LOGI_F("signal %d received, breaking", watcher->signum);
ev_break(loop, EVBREAK_ALL);
} break;
break;
}
}
4 changes: 2 additions & 2 deletions src/ruleset.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ static int parse_ipv6_(lua_State *L)
}
const lua_Unsigned *addr = (void *)&in6;
#if LUA_MAXUNSIGNED >= UINT64_MAX
lua_pushinteger(L, read_uint64((const void *)&addr[0]));
lua_pushinteger(L, read_uint64((const void *)&addr[1]));
lua_pushinteger(L, (lua_Integer)read_uint64((const void *)&addr[0]));
lua_pushinteger(L, (lua_Integer)read_uint64((const void *)&addr[1]));
return 2;
#else
lua_pushinteger(L, read_uint32((const void *)&addr[0]));
Expand Down
4 changes: 2 additions & 2 deletions src/ruleset.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ struct ev_loop;
struct ruleset;

struct ruleset *ruleset_new(struct ev_loop *loop, const struct config *conf);
bool ruleset_invoke(struct ruleset *r, const char *code, const size_t len);
bool ruleset_load(struct ruleset *r, const char *code, const size_t len);
bool ruleset_invoke(struct ruleset *r, const char *code, size_t len);
bool ruleset_load(struct ruleset *r, const char *code, size_t len);
bool ruleset_loadfile(struct ruleset *r, const char *filename);
void ruleset_gc(struct ruleset *r);
void ruleset_free(struct ruleset *r);
Expand Down
8 changes: 8 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "util.h"
#include "utils/check.h"

#include <unistd.h>
#include <pwd.h>
Expand Down Expand Up @@ -49,3 +50,10 @@ void drop_privileges(const char *user)
LOGW_F("unable to drop user privileges: %s", strerror(err));
}
}

void reset(char **argv)
{
(void)execv(argv[0], argv);
const int err = errno;
FAILMSGF("execv: %s", strerror(err));
}
2 changes: 2 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ struct event_cb {

void drop_privileges(const char *user);

void reset(char **argv);

#endif /* UTIL_H */

0 comments on commit 9213d07

Please sign in to comment.