Skip to content

Commit

Permalink
- fix some memory leaks
Browse files Browse the repository at this point in the history
- removed SIGKILL handler (SIGKILL cannot be caught)
- exit when aped compiled without event handler
  • Loading branch information
harmer authored and paraboul committed May 25, 2010
1 parent 42d6a12 commit 6e62786
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/config.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -152,3 +152,21 @@ apeconfig *ape_config_load(const char *filename)
return conf; return conf;
} }


void ape_config_free(apeconfig *conf)
{
apeconfig *tmp_s;
apeconfig_def *tmp_d;

while (conf != NULL) {
while (conf->def != NULL) {
tmp_d = conf->def->next;
free(conf->def->val);
free(conf->def);
conf->def = tmp_d;
}
tmp_s = conf->next;
free(conf);
conf = tmp_s;
}
}

7 changes: 4 additions & 3 deletions src/config.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
#define APE_CONFIG_FILE "ape.conf" #define APE_CONFIG_FILE "ape.conf"




struct _apeconfig_def { typedef struct _apeconfig_def {
char *val; char *val;
struct _apeconfig_def *next; struct _apeconfig_def *next;
char key[33]; char key[33];
}; } apeconfig_def;


typedef struct apeconfig { typedef struct apeconfig {
struct _apeconfig_def *def; apeconfig_def *def;
struct apeconfig *next; struct apeconfig *next;
char section[33]; char section[33];
} apeconfig; } apeconfig;
Expand All @@ -42,6 +42,7 @@ typedef struct apeconfig {
apeconfig *ape_config_load(const char *filename); apeconfig *ape_config_load(const char *filename);
char *ape_config_get_key(apeconfig *conf, const char *key); char *ape_config_get_key(apeconfig *conf, const char *key);
apeconfig *ape_config_get_section(apeconfig *conf, const char *section); apeconfig *ape_config_get_section(apeconfig *conf, const char *section);
void ape_config_free(apeconfig *conf);


#define CONFIG_VAL(section, key, srv) \ #define CONFIG_VAL(section, key, srv) \
(ape_config_get_key(ape_config_get_section(srv, #section), #key) == NULL ? "" : ape_config_get_key(ape_config_get_section(srv, #section), #key)) (ape_config_get_key(ape_config_get_section(srv, #section), #key) == NULL ? "" : ape_config_get_key(ape_config_get_section(srv, #section), #key))
Expand Down
30 changes: 27 additions & 3 deletions src/entry.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ int main(int argc, char **argv)


signal(SIGINT, &signal_handler); signal(SIGINT, &signal_handler);
signal(SIGTERM, &signal_handler); signal(SIGTERM, &signal_handler);
signal(SIGKILL, &signal_handler);


if (VTICKS_RATE < 1) { if (VTICKS_RATE < 1) {
printf("[ERR] TICKS_RATE cant be less than 1\n"); printf("[ERR] TICKS_RATE cant be less than 1\n");
Expand All @@ -158,6 +157,8 @@ int main(int argc, char **argv)


ape_log_init(g_ape); ape_log_init(g_ape);


fdev.handler = EVENT_UNKNOWN;

#ifdef USE_EPOLL_HANDLER #ifdef USE_EPOLL_HANDLER
fdev.handler = EVENT_EPOLL; fdev.handler = EVENT_EPOLL;
#endif #endif
Expand All @@ -174,7 +175,10 @@ int main(int argc, char **argv)
g_ape->timers.timers = NULL; g_ape->timers.timers = NULL;
g_ape->timers.ntimers = 0; g_ape->timers.ntimers = 0;
g_ape->events = &fdev; g_ape->events = &fdev;
events_init(g_ape, &g_ape->basemem); if (events_init(g_ape, &g_ape->basemem) == -1) {
printf("Fatal error: APE compiled without an event handler... exiting\n");
return 0;
};


serverfd = servers_init(g_ape); serverfd = servers_init(g_ape);


Expand Down Expand Up @@ -303,14 +307,34 @@ int main(int argc, char **argv)
unlink(pidfile); unlink(pidfile);
} }


free(confs_path);

timers_free(g_ape);

events_free(g_ape);

transport_free(g_ape);

hashtbl_free(g_ape->hLogin); hashtbl_free(g_ape->hLogin);
hashtbl_free(g_ape->hSessid); hashtbl_free(g_ape->hSessid);
hashtbl_free(g_ape->hLusers); hashtbl_free(g_ape->hLusers);
hashtbl_free(g_ape->hPubid);


hashtbl_free(g_ape->hCallback); hashtbl_free(g_ape->hCallback);


free(g_ape->bufout);

ape_config_free(srv);

int i;
for (i = 0; i < g_ape->basemem; i++) {
if (g_ape->co[i] != NULL) {
free(g_ape->co[i]);
}
}
free(g_ape->co);

free(g_ape->plugins); free(g_ape->plugins);
//free(srv);
free(g_ape); free(g_ape);


return 0; return 0;
Expand Down
7 changes: 7 additions & 0 deletions src/events.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ int events_init(acetables *g_ape, int *basemem)
return -1; return -1;
} }


void events_free(acetables *g_ape)
{
if (g_ape->events->handler != EVENT_UNKNOWN) {
free(g_ape->events->events);
}
}

int events_add(struct _fdevent *ev, int fd, int bitadd) int events_add(struct _fdevent *ev, int fd, int bitadd)
{ {
if (ev->add(ev, fd, bitadd) == -1) { if (ev->add(ev, fd, bitadd) == -1) {
Expand Down
2 changes: 2 additions & 0 deletions src/events.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@


/* Events handler */ /* Events handler */
typedef enum { typedef enum {
EVENT_UNKNOWN,
EVENT_EPOLL, /* Linux */ EVENT_EPOLL, /* Linux */
EVENT_KQUEUE, /* BSD */ EVENT_KQUEUE, /* BSD */
EVENT_DEVPOLL, /* Solaris */ EVENT_DEVPOLL, /* Solaris */
Expand Down Expand Up @@ -72,6 +73,7 @@ struct _fdevent {
}; };


int events_init(acetables *g_ape, int *basemem); int events_init(acetables *g_ape, int *basemem);
void events_free(acetables *g_ape);
int events_add(struct _fdevent *ev, int fd, int bitadd); int events_add(struct _fdevent *ev, int fd, int bitadd);
int events_poll(struct _fdevent *ev, int timeout_ms); int events_poll(struct _fdevent *ev, int timeout_ms);
int events_get_current_fd(struct _fdevent *ev, int i); int events_get_current_fd(struct _fdevent *ev, int i);
Expand Down
12 changes: 12 additions & 0 deletions src/ticks.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -155,3 +155,15 @@ int get_first_timer_ms(acetables *g_ape)
return -1; return -1;
} }


/* Delete all timers and deallocate memory */
void timers_free(acetables *g_ape)
{
struct _ticks_callback *timers = g_ape->timers.timers;
struct _ticks_callback *prev;

while (timers != NULL) {
prev = timers;
timers = timers->next;
free(prev);
}
}
1 change: 1 addition & 0 deletions src/ticks.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct _ticks_callback *add_periodical(unsigned int msec, int times, void *callb
void del_timer_identifier(unsigned int identifier, acetables *g_ape); void del_timer_identifier(unsigned int identifier, acetables *g_ape);
struct _ticks_callback *get_timer_identifier(unsigned int identifier, acetables *g_ape); struct _ticks_callback *get_timer_identifier(unsigned int identifier, acetables *g_ape);
int get_first_timer_ms(acetables *g_ape); int get_first_timer_ms(acetables *g_ape);
void timers_free(acetables *g_ape);


#define add_ticked(x, y) add_periodical(VTICKS_RATE, 0, x, y, g_ape) #define add_ticked(x, y) add_periodical(VTICKS_RATE, 0, x, y, g_ape)


Expand Down
14 changes: 14 additions & 0 deletions src/transports.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -126,3 +126,17 @@ void transport_start(acetables *g_ape)
g_ape->transports.websocket.properties.padding.right.len = 1; g_ape->transports.websocket.properties.padding.right.len = 1;


} }

void transport_free(acetables *g_ape)
{
free(g_ape->transports.websocket.properties.padding.right.val);
free(g_ape->transports.websocket.properties.padding.left.val);
free(g_ape->transports.sse.properties.padding.right.val);
free(g_ape->transports.sse.properties.padding.left.val);
free(g_ape->transports.xhrstreaming.properties.padding.right.val);

if (g_ape->transports.jsonp.properties.padding.left.val != NULL) {
free(g_ape->transports.jsonp.properties.padding.left.val);
free(g_ape->transports.jsonp.properties.padding.right.val);
}
}
1 change: 1 addition & 0 deletions src/transports.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef enum {
struct _transport_open_same_host_p transport_open_same_host(subuser *sub, ape_socket *client, transport_t transport); struct _transport_open_same_host_p transport_open_same_host(subuser *sub, ape_socket *client, transport_t transport);
void transport_data_completly_sent(subuser *sub, transport_t transport); void transport_data_completly_sent(subuser *sub, transport_t transport);
void transport_start(acetables *g_ape); void transport_start(acetables *g_ape);
void transport_free(acetables *g_ape);
struct _transport_properties *transport_get_properties(transport_t transport, acetables *g_ape); struct _transport_properties *transport_get_properties(transport_t transport, acetables *g_ape);


#endif #endif

0 comments on commit 6e62786

Please sign in to comment.