From aff9ec94d82bb46235da0b4be1b2e5e9c9dafbc2 Mon Sep 17 00:00:00 2001 From: Dmitry Kozlov Date: Sun, 19 Dec 2010 10:35:14 +0300 Subject: [PATCH] cli: shutdown cancel command --- accel-pptpd/cli/std_cmd.c | 12 ++++++--- accel-pptpd/cli/telnet.c | 13 +++++++++- accel-pptpd/ctrl/l2tp/l2tp.c | 47 ++++++++++++++-------------------- accel-pptpd/ctrl/pppoe/pppoe.c | 15 +++-------- accel-pptpd/ctrl/pptp/pptp.c | 17 +++--------- accel-pptpd/include/events.h | 1 - accel-pptpd/log.c | 2 +- accel-pptpd/ppp/ppp.c | 6 ++--- accel-pptpd/ppp/ppp.h | 7 ++--- accel-pptpd/triton/triton.h | 22 ++++++++-------- 10 files changed, 64 insertions(+), 78 deletions(-) diff --git a/accel-pptpd/cli/std_cmd.c b/accel-pptpd/cli/std_cmd.c index 2ff7728..f7e4b65 100644 --- a/accel-pptpd/cli/std_cmd.c +++ b/accel-pptpd/cli/std_cmd.c @@ -299,10 +299,11 @@ static void terminate_help(char * const *fields, int fields_cnt, void *client) static void shutdown_help(char * const *fields, int fields_cnt, void *client) { - cli_send(client, "shutdown [soft|hard]- shutdown daemon\r\n"); + cli_send(client, "shutdown [soft|hard|cancel]- shutdown daemon\r\n"); cli_send(client, "\t\tdefault action - send termination signals to all clients and wait everybody disconnects\r\n"); cli_send(client, "\t\tsoft - wait until all clients disconnects, don't accept new connections\r\n"); cli_send(client, "\t\thard - shutdown now, don't wait anything\r\n"); + cli_send(client, "\t\tcancel - cancel 'shutdown soft' and return to normal operation\r\n"); } static int shutdown_exec(const char *cmd, char * const *f, int f_cnt, void *cli) @@ -312,15 +313,18 @@ static int shutdown_exec(const char *cmd, char * const *f, int f_cnt, void *cli) if (f_cnt == 2) { if (!strcmp(f[1], "soft")) { - triton_event_fire(EV_SHUTDOWN_SOFT, NULL); + ppp_shutdown_soft(); return CLI_CMD_OK; } else if (!strcmp(f[1], "hard")) hard = 1; - else + else if (!strcmp(f[1], "cancel")) { + ppp_shutdown = 0; + return CLI_CMD_OK; + } else return CLI_CMD_SYNTAX; } - triton_event_fire(EV_SHUTDOWN_SOFT, NULL); + ppp_shutdown_soft(); pthread_rwlock_rdlock(&ppp_lock); list_for_each_entry(ppp, &ppp_list, entry) { diff --git a/accel-pptpd/cli/telnet.c b/accel-pptpd/cli/telnet.c index e46630f..056cb30 100644 --- a/accel-pptpd/cli/telnet.c +++ b/accel-pptpd/cli/telnet.c @@ -14,6 +14,7 @@ #include "triton.h" #include "log.h" +#include "ppp.h" #include "list.h" #include "memdebug.h" @@ -166,7 +167,13 @@ static int cli_client_sendv(struct cli_client_t *tcln, const char *fmt, va_list static int send_banner(struct telnet_client_t *cln) { - return telnet_send(cln, "accel-pptp version " ACCEL_PPTP_VERSION "\r\n", sizeof("accel-pptp version " ACCEL_PPTP_VERSION "\r\n")); + if (telnet_send(cln, "accel-pptp version " ACCEL_PPTP_VERSION "\r\n", sizeof("accel-pptp version " ACCEL_PPTP_VERSION "\r\n"))) + return -1; + if (cln->auth && ppp_shutdown) { + if (telnet_send(cln, "warning: 'shutdown soft' is in progress...\r\n", sizeof("warning: 'shutdown soft' is in progress...\r\n"))) + return -1; + } + return 0; } static int send_config(struct telnet_client_t *cln) @@ -262,6 +269,10 @@ static int telnet_input_char(struct telnet_client_t *cln, uint8_t c) return -1; } cln->auth = 1; + if (ppp_shutdown) { + if (telnet_send(cln, "warning: 'shutdown soft' is in progress...\r\n", sizeof("warning: 'shutdown soft' is in progress...\r\n"))) + return -1; + } } else if (cln->cmdline_len) { b = _malloc(sizeof(*b) + cln->cmdline_len); memcpy(b->buf, cln->cmdline, cln->cmdline_len); diff --git a/accel-pptpd/ctrl/l2tp/l2tp.c b/accel-pptpd/ctrl/l2tp/l2tp.c index 2a48aa1..e5bc96e 100644 --- a/accel-pptpd/ctrl/l2tp/l2tp.c +++ b/accel-pptpd/ctrl/l2tp/l2tp.c @@ -50,10 +50,8 @@ int conf_retransmit = 5; int conf_hello_interval = 60; const char *conf_host_name = "accel-pptp"; -static int shutdown_soft; - -static uint32_t stat_active; -static uint32_t stat_starting; +static unsigned int stat_active; +static unsigned int stat_starting; struct l2tp_serv_t { @@ -640,7 +638,7 @@ static int l2tp_recv_SCCRQ(struct l2tp_serv_t *serv, struct l2tp_packet_t *pack) struct l2tp_attr_t *framing_cap = NULL; struct l2tp_attr_t *router_id = NULL; - if (shutdown_soft) + if (ppp_shutdown) return 0; list_for_each_entry(attr, &pack->attrs, entry) { @@ -1061,38 +1059,38 @@ static struct l2tp_serv_t udp_serv = static void start_udp_server(void) { - struct sockaddr_in addr; + struct sockaddr_in addr; char *opt; udp_serv.hnd.fd = socket(PF_INET, SOCK_DGRAM, 0); - if (udp_serv.hnd.fd < 0) { - log_emerg("l2tp: socket: %s\n", strerror(errno)); - return; - } + if (udp_serv.hnd.fd < 0) { + log_emerg("l2tp: socket: %s\n", strerror(errno)); + return; + } memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(L2TP_PORT); + addr.sin_family = AF_INET; + addr.sin_port = htons(L2TP_PORT); opt = conf_get_opt("l2tp", "bind"); if (opt) addr.sin_addr.s_addr = inet_addr(opt); else addr.sin_addr.s_addr = htonl(INADDR_ANY); - - setsockopt(udp_serv.hnd.fd, SOL_SOCKET, SO_REUSEADDR, &udp_serv.hnd.fd, sizeof(udp_serv.hnd.fd)); - setsockopt(udp_serv.hnd.fd, SOL_SOCKET, SO_NO_CHECK, &udp_serv.hnd.fd, sizeof(udp_serv.hnd.fd)); - if (bind (udp_serv.hnd.fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) { - log_emerg("l2tp: failed to bind socket: %s\n", strerror(errno)); + setsockopt(udp_serv.hnd.fd, SOL_SOCKET, SO_REUSEADDR, &udp_serv.hnd.fd, sizeof(udp_serv.hnd.fd)); + setsockopt(udp_serv.hnd.fd, SOL_SOCKET, SO_NO_CHECK, &udp_serv.hnd.fd, sizeof(udp_serv.hnd.fd)); + + if (bind (udp_serv.hnd.fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) { + log_emerg("l2tp: failed to bind socket: %s\n", strerror(errno)); close(udp_serv.hnd.fd); - return; - } + return; + } if (fcntl(udp_serv.hnd.fd, F_SETFL, O_NONBLOCK)) { - log_emerg("pptp: failed to set nonblocking mode: %s\n", strerror(errno)); + log_emerg("pptp: failed to set nonblocking mode: %s\n", strerror(errno)); close(udp_serv.hnd.fd); - return; + return; } memcpy(&udp_serv.addr, &addr, sizeof(addr)); @@ -1112,11 +1110,6 @@ static int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt, return CLI_CMD_OK; } -static void ev_shutdown_soft(void) -{ - shutdown_soft = 1; -} - static void __init l2tp_init(void) { char *opt; @@ -1153,7 +1146,5 @@ static void __init l2tp_init(void) start_udp_server(); cli_register_simple_cmd2(&show_stat_exec, NULL, 2, "show", "stat"); - - triton_event_register_handler(EV_SHUTDOWN_SOFT, (triton_event_func)ev_shutdown_soft); } diff --git a/accel-pptpd/ctrl/pppoe/pppoe.c b/accel-pptpd/ctrl/pppoe/pppoe.c index 89525bc..8d79ea9 100644 --- a/accel-pptpd/ctrl/pppoe/pppoe.c +++ b/accel-pptpd/ctrl/pppoe/pppoe.c @@ -62,8 +62,6 @@ char *conf_ac_name; int conf_ifname_in_sid; char *conf_pado_delay; -static int shutdown_soft; - static mempool_t conn_pool; static mempool_t pado_pool; @@ -631,7 +629,7 @@ static void pado_timer(struct triton_timer_t *t) { struct delayed_pado_t *pado = container_of(t, typeof(*pado), timer); - if (!shutdown_soft) + if (!ppp_shutdown) pppoe_send_PADO(pado->serv, pado->addr, pado->host_uniq, pado->relay_sid, pado->service_name); free_delayed_pado(pado); @@ -648,7 +646,7 @@ static void pppoe_recv_PADI(struct pppoe_serv_t *serv, uint8_t *pack, int size) int n, service_match = 0; struct delayed_pado_t *pado; - if (shutdown_soft || pado_delay == -1) + if (ppp_shutdown || pado_delay == -1) return; if (hdr->sid) { @@ -744,7 +742,7 @@ static void pppoe_recv_PADR(struct pppoe_serv_t *serv, uint8_t *pack, int size) int n, service_match = 0; struct pppoe_conn_t *conn; - if (shutdown_soft) + if (ppp_shutdown) return; if (!memcmp(ethhdr->h_dest, bc_addr, ETH_ALEN)) { @@ -1112,11 +1110,6 @@ static void _server_stop(struct pppoe_serv_t *serv) pthread_mutex_unlock(&serv->lock); } -static void ev_shutdown_soft(void) -{ - shutdown_soft = 1; -} - void pppoe_server_free(struct pppoe_serv_t *serv) { struct delayed_pado_t *pado; @@ -1221,7 +1214,5 @@ static void __init pppoe_init(void) if (!conf_ac_name) conf_ac_name = _strdup("accel-pptp"); - - triton_event_register_handler(EV_SHUTDOWN_SOFT, (triton_event_func)ev_shutdown_soft); } diff --git a/accel-pptpd/ctrl/pptp/pptp.c b/accel-pptpd/ctrl/pptp/pptp.c index 887db37..c1513a0 100644 --- a/accel-pptpd/ctrl/pptp/pptp.c +++ b/accel-pptpd/ctrl/pptp/pptp.c @@ -56,12 +56,10 @@ static int conf_timeout = 5; static int conf_echo_interval = 0; static int conf_echo_failure = 3; static int conf_verbose = 0; -static int shutdown_soft; - static mempool_t conn_pool; -static uint32_t stat_starting; -static uint32_t stat_active; +static unsigned int stat_starting; +static unsigned int stat_active; static int pptp_read(struct triton_md_handler_t *h); static int pptp_write(struct triton_md_handler_t *h); @@ -618,7 +616,7 @@ static int pptp_connect(struct triton_md_handler_t *h) continue; } - if (shutdown_soft) { + if (ppp_shutdown) { close(sock); continue; } @@ -700,14 +698,9 @@ static int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt, return CLI_CMD_OK; } -static void ev_shutdown_soft(void) -{ - shutdown_soft = 1; -} - static void __init pptp_init(void) { - struct sockaddr_in addr; + struct sockaddr_in addr; char *opt; serv.hnd.fd = socket(PF_INET, SOCK_STREAM, 0); @@ -767,7 +760,5 @@ static void __init pptp_init(void) triton_context_wakeup(&serv.ctx); cli_register_simple_cmd2(show_stat_exec, NULL, 2, "show", "stat"); - - triton_event_register_handler(EV_SHUTDOWN_SOFT, (triton_event_func)ev_shutdown_soft); } diff --git a/accel-pptpd/include/events.h b/accel-pptpd/include/events.h index 3e4a23b..5958f27 100644 --- a/accel-pptpd/include/events.h +++ b/accel-pptpd/include/events.h @@ -13,7 +13,6 @@ #define EV_CTRL_FINISHED 8 #define EV_PPP_PRE_UP 9 #define EV_PPP_ACCT_START 10 -#define EV_SHUTDOWN_SOFT 11 #define EV_IP_CHANGED 100 #define EV_SHAPER 101 #define EV_MPPE_KEYS 102 diff --git a/accel-pptpd/log.c b/accel-pptpd/log.c index 7381427..5fbd93a 100644 --- a/accel-pptpd/log.c +++ b/accel-pptpd/log.c @@ -31,7 +31,7 @@ struct _log_msg_t int level; struct timeval timestamp; struct list_head chunks; - uint8_t refs; + unsigned int refs; }; static int log_level; diff --git a/accel-pptpd/ppp/ppp.c b/accel-pptpd/ppp/ppp.c index f5abbab..84c05eb 100644 --- a/accel-pptpd/ppp/ppp.c +++ b/accel-pptpd/ppp/ppp.c @@ -32,7 +32,7 @@ __export LIST_HEAD(ppp_list); static LIST_HEAD(layers); int __export sock_fd; -static int ppp_shutdown; +int __export ppp_shutdown; static unsigned long long seq; #if __WORDSIZE == 32 @@ -602,7 +602,7 @@ struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *ppp, struct ppp_layer return NULL; } -static void ev_shutdown_soft(void) +void ppp_shutdown_soft(void) { ppp_shutdown = 1; @@ -659,8 +659,6 @@ static void __init init(void) //log_emerg("ppp: failed to open seq-file (%s): %s\n", opt, strerror(errno)); seq = (unsigned long long)random() * (unsigned long long)random(); - triton_event_register_handler(EV_SHUTDOWN_SOFT, (triton_event_func)ev_shutdown_soft); - atexit(save_seq); } diff --git a/accel-pptpd/ppp/ppp.h b/accel-pptpd/ppp/ppp.h index 814a270..d7f9816 100644 --- a/accel-pptpd/ppp/ppp.h +++ b/accel-pptpd/ppp/ppp.h @@ -147,9 +147,9 @@ struct ppp_handler_t struct ppp_stat_t { - uint32_t active; - uint32_t starting; - uint32_t finishing; + unsigned int active; + unsigned int starting; + unsigned int finishing; }; struct ppp_t *alloc_ppp(void); @@ -173,6 +173,7 @@ int ppp_register_layer(const char *name, struct ppp_layer_t *); void ppp_unregister_layer(struct ppp_layer_t *); struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *, struct ppp_layer_t *); +extern int ppp_shutdown; void ppp_shutdown_soft(void); extern int conf_ppp_verbose; diff --git a/accel-pptpd/triton/triton.h b/accel-pptpd/triton/triton.h index d61253b..799560b 100644 --- a/accel-pptpd/triton/triton.h +++ b/accel-pptpd/triton/triton.h @@ -52,17 +52,17 @@ struct conf_sect_t struct triton_stat_t { - uint32_t mempool_allocated; - uint32_t mempool_available; - uint32_t thread_count; - uint32_t thread_active; - uint32_t context_count; - uint32_t context_sleeping; - uint32_t context_pending; - uint32_t md_handler_count; - uint32_t md_handler_pending; - uint32_t timer_count; - uint32_t timer_pending; + unsigned int mempool_allocated; + unsigned int mempool_available; + unsigned int thread_count; + unsigned int thread_active; + unsigned int context_count; + unsigned int context_sleeping; + unsigned int context_pending; + unsigned int md_handler_count; + unsigned int md_handler_pending; + unsigned int timer_count; + unsigned int timer_pending; time_t start_time; }; -- GitLab