From 3ae9d86019fec5182cf10935deb0f0835304e7b8 Mon Sep 17 00:00:00 2001 From: Michiel Broek Date: Sun, 11 Sep 2005 13:07:42 +0000 Subject: [PATCH] Code cleanup and memory optimisation --- ChangeLog | 3 +++ mbtask/calllist.c | 3 +-- mbtask/mbtask.c | 5 ++--- mbtask/outstat.c | 8 ++------ mbtask/ping.c | 4 ---- mbtask/ports.c | 6 ++++-- mbtask/scanout.c | 5 ++++- mbtask/taskchat.c | 18 ++++++++++-------- mbtask/taskdisk.c | 8 ++++++-- mbtask/taskinfo.c | 29 +++++++++++++++-------------- mbtask/taskutil.c | 32 +++++++++++++++++++++++--------- 11 files changed, 70 insertions(+), 51 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2db1b737..92afe8c1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -32,6 +32,9 @@ v0.71.6 02-Sep-2005 Removed Italian default record. Rewrote code to create default language records. + mbtask: + Code cleanup and memory optimisation. + v0.71.5 18-Aug-2005 - 02-Sep-2005 diff --git a/mbtask/calllist.c b/mbtask/calllist.c index 4214ce19..8c2b7332 100644 --- a/mbtask/calllist.c +++ b/mbtask/calllist.c @@ -4,7 +4,7 @@ * Purpose ...............: mbtask - calllist * ***************************************************************************** - * Copyright (C) 1997-2004 + * Copyright (C) 1997-2005 * * Michiel Broek FIDO: 2:280/2802 * Beekmansbos 10 @@ -125,7 +125,6 @@ int check_calllist(void) } } } - Syslog('o', "%d system%s to call", call_work, (call_work == 1)?"":"s"); } else { if (s_scanout) sem_set((char *)"scanout", FALSE); diff --git a/mbtask/mbtask.c b/mbtask/mbtask.c index a278ac47..9d80b86c 100644 --- a/mbtask/mbtask.c +++ b/mbtask/mbtask.c @@ -1397,7 +1397,8 @@ void *scheduler(void) calllist[call_entry].addr.node, calllist[call_entry].addr.net, calllist[call_entry].addr.zone, calllist[call_entry].addr.domain); } else { - snprintf(opts, 41, "%sf%u.n%u.z%u.%s", port, calllist[call_entry].addr.node, calllist[call_entry].addr.net, + snprintf(opts, 41, "%sf%u.n%u.z%u.%s", port, calllist[call_entry].addr.node, + calllist[call_entry].addr.net, calllist[call_entry].addr.zone, calllist[call_entry].addr.domain); } calllist[call_entry].taskpid = launch(cmd, opts, (char *)"mbcico", calllist[call_entry].callmode); @@ -1446,8 +1447,6 @@ int main(int argc, char **argv) signal(i, (void (*))start_shutdown); else if (i == SIGCHLD) signal(i, SIG_DFL); -// else if ((i != SIGKILL) && (i != SIGSTOP)) -// signal(i, SIG_IGN); } /* diff --git a/mbtask/outstat.c b/mbtask/outstat.c index f882c443..35c31376 100644 --- a/mbtask/outstat.c +++ b/mbtask/outstat.c @@ -74,7 +74,8 @@ int load_node(fidoaddr n) memset(&nodes, 0, sizeof(nodes)); return FALSE; } - + free(temp); + fread(&nodeshdr, sizeof(nodeshdr), 1, fp); while (fread(&nodes, nodeshdr.recsize, 1, fp) == 1) { fseek(fp, nodeshdr.filegrp + nodeshdr.mailgrp, SEEK_CUR); @@ -83,7 +84,6 @@ int load_node(fidoaddr n) if ((n.zone == nodes.Aka[i].zone) && (n.net == nodes.Aka[i].net) && (n.node == nodes.Aka[i].node) && (n.point == nodes.Aka[i].point)) { fclose(fp); - free(temp); return TRUE; } } @@ -91,7 +91,6 @@ int load_node(fidoaddr n) fclose(fp); memset(&nodes, 0, sizeof(nodes)); - free(temp); return FALSE; } @@ -457,8 +456,6 @@ int outstat() } rc = load_node(tmp->addr); -// Syslog('o', "Load node %s rc=%s, NoCall=%s, NoTCP=%s", fido2str(tmp->addr, 0x0f), rc?"true":"false", -// nodes.NoCall?"True":"False", (itnmask + ibnmask + ifcmask)?"False":"True"); /* * Zone Mail Hours, only use Fidonet Hours. @@ -533,7 +530,6 @@ int outstat() T_window = TRUE; } } -// Syslog('o', "T_window=%s, iszmh=%s", T_window?"true":"false", iszmh?"true":"false"); strcpy(flstr,"...... .... .."); /* diff --git a/mbtask/ping.c b/mbtask/ping.c index bb6177d0..bdb92df7 100644 --- a/mbtask/ping.c +++ b/mbtask/ping.c @@ -262,7 +262,6 @@ int ping_receive(struct in_addr addr) Syslog('?', "$poll/select failed"); return -3; } -// Syslog('p', "poll_thread: poll interrupted rc=%d events=%04x", rc, pfd.revents); if (pfd.revents & POLLIN || pfd.revents & POLLERR || pfd.revents & POLLHUP || pfd.revents & POLLNVAL) { sl = sizeof(ffrom); @@ -337,8 +336,6 @@ void *ping_thread(void *dummy) static time_t pingsend; time_t now; -printf("Start ping thread\n"); - Syslog('+', "Starting ping thread"); pingresult[1] = pingresult[2] = FALSE; pingnr = 2; @@ -455,7 +452,6 @@ printf("Start ping thread\n"); } } - Syslog('p', "Ping thread closing socket %d", ping_isocket); if ((rc = close(ping_isocket))) { WriteError("$ping thread error socket close"); } diff --git a/mbtask/ports.c b/mbtask/ports.c index c510ed11..fcf7f4f3 100644 --- a/mbtask/ports.c +++ b/mbtask/ports.c @@ -183,15 +183,16 @@ void load_ports() void check_ports(void) { pp_list *tpl; - char lckname[256]; + char *lckname; FILE *lf; int tmppid, changed = FALSE; pid_t rempid = 0; pots_free = isdn_free = 0; + lckname = calloc(PATH_MAX, sizeof(char)); for (tpl = pl; tpl; tpl = tpl->next) { - snprintf(lckname, 256, "%s%s", LCKPREFIX, tpl->tty); + snprintf(lckname, PATH_MAX, "%s%s", LCKPREFIX, tpl->tty); if ((lf = fopen(lckname, "r")) == NULL) { if (tpl->locked) { tpl->locked = 0; @@ -241,6 +242,7 @@ void check_ports(void) Syslog('p', "Free ports: pots=%d isdn=%d", pots_free, isdn_free); } } + free(lckname); } diff --git a/mbtask/scanout.c b/mbtask/scanout.c index fe9866a6..ce4411f7 100644 --- a/mbtask/scanout.c +++ b/mbtask/scanout.c @@ -54,13 +54,14 @@ extern struct _fidonet fidonet; static int scan_dir(int (*)(faddr*, char, int, char *), char *, int); static int scan_dir(int (*fn)(faddr *, char, int, char *), char *dname, int ispoint) { - char fname[PATH_MAX], flavor = '?'; + char *fname, flavor = '?'; DIR *dp = NULL; struct dirent *de; int rc = 0, isflo, fage; time_t t_start; t_start = time(NULL); + fname = calloc(PATH_MAX, sizeof(char)); if ((dp = opendir(dname)) == NULL) { Syslog('-', "Creating directory %s", dname); @@ -71,6 +72,7 @@ static int scan_dir(int (*fn)(faddr *, char, int, char *), char *dname, int ispo (void)mkdirs(fname, 0770); if ((dp = opendir(dname)) == NULL) { Syslog('o' ,"\"%s\" cannot be opened, proceed",MBSE_SS(dname)); + free(fname); return 0; } } @@ -164,6 +166,7 @@ static int scan_dir(int (*fn)(faddr *, char, int, char *), char *dname, int ispo exout: closedir(dp); + free(fname); return rc; } diff --git a/mbtask/taskchat.c b/mbtask/taskchat.c index ed8d1123..627ae508 100644 --- a/mbtask/taskchat.c +++ b/mbtask/taskchat.c @@ -131,10 +131,11 @@ void system_msg(pid_t pid, char *msg) */ void system_shout(const char *format, ...) { - char buf[512]; + char *buf; va_list va_ptr; usr_list *tmpu; + buf = calloc(512, sizeof(char)); va_start(va_ptr, format); vsnprintf(buf, 512, format, va_ptr); va_end(va_ptr); @@ -143,6 +144,8 @@ void system_shout(const char *format, ...) if (tmpu->pid) { system_msg(tmpu->pid, buf); } + + free(buf); } @@ -350,13 +353,13 @@ void chat_cleanuser(pid_t pid) */ void chat_msg(char *channel, char *nick, char *msg) { - char buf[128], *logm; + char buf[79], *logm; usr_list *tmpu; if (nick == NULL) - snprintf(buf, 128, "%s", msg); + snprintf(buf, 79, "%s", msg); else - snprintf(buf, 128, "<%s> %s", nick, msg); + snprintf(buf, 79, "<%s> %s", nick, msg); if (CFG.iAutoLog && strlen(CFG.chat_log)) { logm = calloc(PATH_MAX, sizeof(char)); @@ -364,7 +367,6 @@ void chat_msg(char *channel, char *nick, char *msg) ulog(logm, (char *)"+", channel, (char *)"-1", buf); free(logm); } - buf[79] = '\0'; for (tmpu = users; tmpu; tmpu = tmpu->next) { if (strlen(tmpu->channel) && (strcmp(tmpu->channel, channel) == 0)) { @@ -457,7 +459,7 @@ char *chat_connect(char *data) char *chat_close(char *data) { - static char buf[200]; + static char buf[81]; char *pid; usr_list *tmpu; @@ -474,12 +476,12 @@ char *chat_close(char *data) send_all("QUIT %s@%s Leaving chat\r\n", tmpu->name, CFG.myfqdn); del_user(&users, CFG.myfqdn, tmpu->name); Syslog('-', "Closing chat for pid %s", pid); - snprintf(buf, 200, "100:0;"); + snprintf(buf, 81, "100:0;"); return buf; } } Syslog('-', "Pid %s was not connected to chatserver"); - snprintf(buf, 200, "100:1,*** ERROR - Not connected to server;"); + snprintf(buf, 81, "100:1,*** ERROR - Not connected to server;"); return buf; } diff --git a/mbtask/taskdisk.c b/mbtask/taskdisk.c index 23abf047..4adc6715 100644 --- a/mbtask/taskdisk.c +++ b/mbtask/taskdisk.c @@ -483,7 +483,7 @@ void add_path(char *lpath) void *disk_thread(void) { FILE *fp; - char *temp; + char *temp = NULL; mfs_list *tmp; int rc; @@ -592,7 +592,7 @@ void *disk_thread(void) if (T_Shutdown) break; - + snprintf(temp, PATH_MAX, "%s/etc/fgroups.data", getenv("MBSE_ROOT")); if ((fp = fopen(temp, "r"))) { Syslog('d', "+ %s", temp); @@ -654,6 +654,7 @@ void *disk_thread(void) fclose(fp); } free(temp); + temp = NULL; Syslog('d', "All directories added"); /* @@ -682,6 +683,9 @@ void *disk_thread(void) } tidy_mfslist(&mfs); + if (temp) + free(temp); + temp = NULL; disk_run = FALSE; Syslog('+', "Disk thread stopped"); pthread_exit(NULL); diff --git a/mbtask/taskinfo.c b/mbtask/taskinfo.c index 1d9aad76..ea055b96 100644 --- a/mbtask/taskinfo.c +++ b/mbtask/taskinfo.c @@ -70,23 +70,24 @@ char *get_sysinfo(void) char *get_lastcallercount(void) { - static char buf[SS_BUFSIZE]; - char *temp; - FILE *fp; + static char buf[41]; + char *temp; + FILE *fp; - snprintf(buf, SS_BUFSIZE, "201:1,16;"); - temp = calloc(PATH_MAX, sizeof(char)); - snprintf(temp, PATH_MAX, "%s/etc/lastcall.data", getenv("MBSE_ROOT")); - if ((fp = fopen(temp, "r")) == NULL) { - free(temp); - return buf; - } - fread(&LCALLhdr, sizeof(LCALLhdr), 1, fp); - fseek(fp, 0, SEEK_END); - snprintf(buf, SS_BUFSIZE, "100:1,%ld;", ((ftell(fp) - LCALLhdr.hdrsize) / LCALLhdr.recsize)); - fclose(fp); + snprintf(buf, 41, "100:1,0;"); + temp = calloc(PATH_MAX, sizeof(char)); + snprintf(temp, PATH_MAX, "%s/etc/lastcall.data", getenv("MBSE_ROOT")); + if ((fp = fopen(temp, "r")) == NULL) { free(temp); return buf; + } + + fread(&LCALLhdr, sizeof(LCALLhdr), 1, fp); + fseek(fp, 0, SEEK_END); + snprintf(buf, 41, "100:1,%ld;", ((ftell(fp) - LCALLhdr.hdrsize) / LCALLhdr.recsize)); + fclose(fp); + free(temp); + return buf; } diff --git a/mbtask/taskutil.c b/mbtask/taskutil.c index d3d009ad..74fd5577 100644 --- a/mbtask/taskutil.c +++ b/mbtask/taskutil.c @@ -283,18 +283,22 @@ char *xstrcat(char *src, char *add) void CreateSema(char *sem) { - char temp[PATH_MAX]; + char *temp; FILE *fp; int oldmask; + temp = calloc(PATH_MAX, sizeof(char)); snprintf(temp, PATH_MAX, "%s/var/sema/%s", getenv("MBSE_ROOT"), sem); - if (access(temp, F_OK) == 0) + if (access(temp, F_OK) == 0) { + free(temp); return; + } oldmask = umask(002); if ((fp = fopen(temp, "w"))) fclose(fp); else Syslog('?', "Can't create semafore %s", temp); + free(temp); umask(oldmask); } @@ -302,16 +306,18 @@ void CreateSema(char *sem) void TouchSema(char *sem) { - char temp[PATH_MAX]; + char *temp; FILE *fp; int oldmask; + temp = calloc(PATH_MAX, sizeof(char)); snprintf(temp, PATH_MAX, "%s/var/sema/%s", getenv("MBSE_ROOT"), sem); oldmask = umask(002); - if ((fp = fopen(temp, "w"))) + if ((fp = fopen(temp, "w"))) { fclose(fp); - else + } else Syslog('?', "Can't touch semafore %s", temp); + free(temp); umask(oldmask); } @@ -319,23 +325,31 @@ void TouchSema(char *sem) void RemoveSema(char *sem) { - char temp[PATH_MAX]; + char *temp; + temp = calloc(PATH_MAX, sizeof(char)); snprintf(temp, PATH_MAX, "%s/var/sema/%s", getenv("MBSE_ROOT"), sem); - if (access(temp, F_OK)) + if (access(temp, F_OK)) { + free(temp); return; + } if (unlink(temp) == -1) Syslog('?', "Can't remove semafore %s", temp); + free(temp); } int IsSema(char *sem) { - char temp[PATH_MAX]; + char *temp; + int rc; + temp = calloc(PATH_MAX, sizeof(temp)); snprintf(temp, PATH_MAX, "%s/var/sema/%s", getenv("MBSE_ROOT"), sem); - return (access(temp, F_OK) == 0); + rc = (access(temp, F_OK) == 0); + free(temp); + return rc; }