This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/mbtask/taskutil.c

669 lines
14 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
2001-12-23 14:15:58 +00:00
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: MBSE BBS Task Manager, utilities
*
*****************************************************************************
2006-01-29 16:47:32 +00:00
* Copyright (C) 1997-2006
2001-08-17 05:46:24 +00:00
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* MBSE BBS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MBSE BBS; see the file COPYING. If not, write to the Free
* Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*****************************************************************************/
2002-06-30 13:20:57 +00:00
#include "../config.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbselib.h"
2001-08-17 05:46:24 +00:00
#include "signame.h"
#include "scanout.h"
#include "taskutil.h"
2002-07-07 12:20:51 +00:00
pid_t mypid; /* Original parent pid if child */
int oserr; /* Last OS error number */
extern struct sysconfig CFG;
extern struct _fidonethdr fidonethdr;
extern struct _fidonet fidonet;
extern struct taskrec TCFG;
2005-10-11 20:49:41 +00:00
unsigned int lcrc = 0, tcrc = 1;
2002-07-07 12:20:51 +00:00
int lcnt = 0, lchr;
static char *pbuff = NULL;
2001-08-17 05:46:24 +00:00
static char *mon[] = {
2006-01-29 16:47:32 +00:00
(char *)"Jan",(char *)"Feb",(char *)"Mar",
(char *)"Apr",(char *)"May",(char *)"Jun",
(char *)"Jul",(char *)"Aug",(char *)"Sep",
(char *)"Oct",(char *)"Nov",(char *)"Dec"
2001-08-17 05:46:24 +00:00
};
/************************************************************************
*
* Loging procedures.
*/
void WriteError(const char *format, ...)
{
char *outputstr;
va_list va_ptr;
outputstr = calloc(10240, sizeof(char));
va_start(va_ptr, format);
2005-08-29 21:03:28 +00:00
vsnprintf(outputstr, 10240, format, va_ptr);
va_end(va_ptr);
Syslog('?', outputstr);
free(outputstr);
}
2001-08-17 05:46:24 +00:00
/*
* general log for this server
*/
void Syslog(int grade, const char *format, ...)
2001-08-17 05:46:24 +00:00
{
2006-01-29 16:47:32 +00:00
va_list va_ptr;
char outstr[1024], datestr[21];
int oldmask, debug;
FILE *logfile = NULL, *debugfile;
char *logname = NULL, *debugname;
time_t now;
struct tm ptm;
2003-09-09 19:39:51 +00:00
debug = isalpha(grade);
va_start(va_ptr, format);
2005-08-29 21:03:28 +00:00
vsnprintf(outstr, 1024, format, va_ptr);
2003-09-09 19:39:51 +00:00
va_end(va_ptr);
2002-12-28 20:29:26 +00:00
2003-09-09 19:39:51 +00:00
tcrc = StringCRC32(outstr);
if (tcrc == lcrc) {
lcnt++;
return;
}
lcrc = tcrc;
if (!debug) {
2002-12-28 20:29:26 +00:00
logname = calloc(PATH_MAX, sizeof(char));
oldmask=umask(066);
2005-08-29 21:03:28 +00:00
snprintf(logname, PATH_MAX, "%s/log/mbtask.log", getenv("MBSE_ROOT"));
2002-12-28 20:29:26 +00:00
logfile = fopen(logname, "a");
umask(oldmask);
if (logfile == NULL) {
printf("Cannot open logfile \"%s\"\n", logname);
free(logname);
return;
}
2003-09-09 19:39:51 +00:00
}
2002-12-28 20:29:26 +00:00
2003-09-09 19:39:51 +00:00
debugname = calloc(PATH_MAX, sizeof(char));
oldmask=umask(066);
2005-08-29 21:03:28 +00:00
snprintf(debugname, PATH_MAX, "%s/log/%s", getenv("MBSE_ROOT"), CFG.debuglog);
2003-09-09 19:39:51 +00:00
debugfile = fopen(debugname, "a");
umask(oldmask);
if (debugfile == NULL) {
printf("Cannot open logfile \"%s\"\n", debugname);
free(debugname);
if (!debug) {
free(logname);
fclose(logfile);
2002-12-28 20:29:26 +00:00
}
2003-09-09 19:39:51 +00:00
return;
}
2006-01-29 16:47:32 +00:00
now = time(NULL);
localtime_r(&now, &ptm);
snprintf(datestr, 21, "%02d-%s-%04d %02d:%02d:%02d", ptm.tm_mday, mon[ptm.tm_mon], ptm.tm_year+1900,
ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
2003-09-09 19:39:51 +00:00
if (lcnt) {
lcnt++;
2006-01-29 16:47:32 +00:00
fprintf(debugfile, "%c %s mbtask[%d] last message repeated %d times\n", lchr, datestr, mypid, lcnt);
2003-09-09 19:39:51 +00:00
if (!debug)
2006-01-29 16:47:32 +00:00
fprintf(logfile, "%c %s mbtask[%d] last message repeated %d times\n", lchr, datestr, mypid, lcnt);
2003-09-09 19:39:51 +00:00
}
lcnt = 0;
2002-12-28 20:29:26 +00:00
2003-09-09 19:39:51 +00:00
if (!debug) {
2006-01-29 16:47:32 +00:00
fprintf(logfile, "%c %s mbtask[%d] ", grade, datestr, mypid);
2002-12-28 20:29:26 +00:00
fprintf(logfile, *outstr == '$' ? outstr+1 : outstr);
if (*outstr == '$')
fprintf(logfile, ": %s\n", strerror(errno));
else
fprintf(logfile, "\n");
fflush(logfile);
2003-09-09 19:39:51 +00:00
if (fclose(logfile) != 0)
printf("Cannot close logfile \"%s\"\n", logname);
2002-12-28 20:29:26 +00:00
free(logname);
}
2003-09-09 19:39:51 +00:00
2006-01-29 16:47:32 +00:00
fprintf(debugfile, "%c %s mbtask[%d] ", grade, datestr, mypid);
2003-09-09 19:39:51 +00:00
fprintf(debugfile, *outstr == '$' ? outstr+1 : outstr);
if (*outstr == '$')
fprintf(debugfile, ": %s\n", strerror(errno));
else
fprintf(debugfile, "\n");
fflush(debugfile);
if (fclose(debugfile) != 0)
printf("Cannot close logfile \"%s\"\n", debugname);
lchr = grade;
free(debugname);
2002-12-28 20:29:26 +00:00
return;
2001-08-17 05:46:24 +00:00
}
/*
* user log process
*/
int ulog(char *fn, char *grade, char *prname, char *prpid, char *format)
{
2006-01-29 16:47:32 +00:00
int i, oldmask;
FILE *fp;
time_t now;
struct tm ptm;
char datestr[21];
2002-12-28 20:29:26 +00:00
oldmask = umask(066);
fp = fopen(fn, "a");
2002-12-28 20:29:26 +00:00
umask(oldmask);
if (fp == NULL) {
2002-12-28 20:29:26 +00:00
oserr = errno;
Syslog('!', "$Cannot open user logfile %s", fn);
return -1;
}
2006-01-29 16:47:32 +00:00
now = time(NULL);
localtime_r(&now, &ptm);
snprintf(datestr, 21, "%02d-%s-%04d %02d:%02d:%02d", ptm.tm_mday, mon[ptm.tm_mon], ptm.tm_year+1900,
ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
fprintf(fp, "%s %s %s[%s] ", grade, datestr, prname, prpid);
for (i = 0; i < strlen(format); i++) {
if (iscntrl(format[i])) {
fputc('^', fp);
fputc(format[i] + 64, fp);
} else {
fputc(format[i], fp);
}
}
fputc('\n', fp);
fflush(fp);
if (fclose(fp) != 0) {
2002-12-28 20:29:26 +00:00
oserr = errno;
Syslog('!', "$Cannot close user logfile %s", fn);
return -1;
}
return 0;
2001-08-17 05:46:24 +00:00
}
char *xstrcpy(char *src)
{
2002-12-28 20:29:26 +00:00
char *tmp;
2001-08-17 05:46:24 +00:00
2002-12-28 20:29:26 +00:00
if (src == NULL)
return(NULL);
tmp = malloc(strlen(src)+1);
strcpy(tmp, src);
return tmp;
2001-08-17 05:46:24 +00:00
}
char *xstrcat(char *src, char *add)
{
2002-12-28 20:29:26 +00:00
char *tmp;
size_t size = 0;
if ((add == NULL) || (strlen(add) == 0))
return src;
if (src)
size = strlen(src);
size += strlen(add);
tmp = malloc(size + 1);
*tmp = '\0';
if (src) {
strcpy(tmp, src);
free(src);
}
strcat(tmp, add);
return tmp;
2001-08-17 05:46:24 +00:00
}
void CreateSema(char *sem)
{
2005-09-11 13:07:42 +00:00
char *temp;
2004-12-29 13:06:09 +00:00
FILE *fp;
int oldmask;
2002-12-28 20:29:26 +00:00
2005-09-11 13:07:42 +00:00
temp = calloc(PATH_MAX, sizeof(char));
2005-08-29 21:03:28 +00:00
snprintf(temp, PATH_MAX, "%s/var/sema/%s", getenv("MBSE_ROOT"), sem);
2005-09-11 13:07:42 +00:00
if (access(temp, F_OK) == 0) {
free(temp);
2002-12-28 20:29:26 +00:00
return;
2005-09-11 13:07:42 +00:00
}
oldmask = umask(002);
2004-12-29 13:17:51 +00:00
if ((fp = fopen(temp, "w")))
2004-12-29 13:06:09 +00:00
fclose(fp);
2002-12-28 20:29:26 +00:00
else
Syslog('?', "Can't create semafore %s", temp);
2005-09-11 13:07:42 +00:00
free(temp);
umask(oldmask);
2001-08-17 05:46:24 +00:00
}
void TouchSema(char *sem)
{
2005-09-11 13:07:42 +00:00
char *temp;
2004-12-29 13:06:09 +00:00
FILE *fp;
int oldmask;
2002-12-28 20:29:26 +00:00
2005-09-11 13:07:42 +00:00
temp = calloc(PATH_MAX, sizeof(char));
2005-08-29 21:03:28 +00:00
snprintf(temp, PATH_MAX, "%s/var/sema/%s", getenv("MBSE_ROOT"), sem);
oldmask = umask(002);
2005-09-11 13:07:42 +00:00
if ((fp = fopen(temp, "w"))) {
2004-12-29 13:06:09 +00:00
fclose(fp);
2005-09-11 13:07:42 +00:00
} else
2002-12-28 20:29:26 +00:00
Syslog('?', "Can't touch semafore %s", temp);
2005-09-11 13:07:42 +00:00
free(temp);
umask(oldmask);
2001-08-17 05:46:24 +00:00
}
void RemoveSema(char *sem)
{
2005-09-11 13:07:42 +00:00
char *temp;
2001-08-17 05:46:24 +00:00
2005-09-11 13:07:42 +00:00
temp = calloc(PATH_MAX, sizeof(char));
2005-08-29 21:03:28 +00:00
snprintf(temp, PATH_MAX, "%s/var/sema/%s", getenv("MBSE_ROOT"), sem);
2005-09-11 13:07:42 +00:00
if (access(temp, F_OK)) {
free(temp);
2002-12-28 20:29:26 +00:00
return;
2005-09-11 13:07:42 +00:00
}
2002-12-28 20:29:26 +00:00
if (unlink(temp) == -1)
Syslog('?', "Can't remove semafore %s", temp);
2005-09-11 13:07:42 +00:00
free(temp);
2001-08-17 05:46:24 +00:00
}
int IsSema(char *sem)
{
2005-09-11 13:07:42 +00:00
char *temp;
int rc;
2001-08-17 05:46:24 +00:00
2005-09-11 13:07:42 +00:00
temp = calloc(PATH_MAX, sizeof(temp));
2005-08-29 21:03:28 +00:00
snprintf(temp, PATH_MAX, "%s/var/sema/%s", getenv("MBSE_ROOT"), sem);
2005-09-11 13:07:42 +00:00
rc = (access(temp, F_OK) == 0);
free(temp);
return rc;
2001-08-17 05:46:24 +00:00
}
/*
* Test if the given file exists. The second option is:
* R_OK - test for Read rights
* W_OK - test for Write rights
* X_OK - test for eXecute rights
* F_OK - test file presence only
*/
int file_exist(char *path, int mode)
{
2002-12-28 20:29:26 +00:00
if (access(path, mode) != 0)
return errno;
2001-08-17 05:46:24 +00:00
2002-12-28 20:29:26 +00:00
return 0;
2001-08-17 05:46:24 +00:00
}
2004-12-29 13:06:09 +00:00
2001-08-17 05:46:24 +00:00
/*
* Make directory tree, the name must end with a /
*/
2002-01-19 13:04:55 +00:00
int mkdirs(char *name, mode_t mode)
2001-08-17 05:46:24 +00:00
{
2002-12-28 20:29:26 +00:00
char buf[PATH_MAX], *p, *q;
int rc, last = 0, oldmask;
memset(&buf, 0, sizeof(buf));
strncpy(buf, name, sizeof(buf)-1);
buf[sizeof(buf)-1] = '\0';
p = buf+1;
oldmask = umask(000);
while ((q = strchr(p, '/'))) {
*q = '\0';
rc = mkdir(buf, mode);
last = errno;
*q = '/';
p = q+1;
}
umask(oldmask);
if ((last == 0) || (last == EEXIST)) {
return TRUE;
} else {
Syslog('?', "$mkdirs(%s)", name);
return FALSE;
}
2001-08-17 05:46:24 +00:00
}
/*
* Return size of file, or -1 if file doesn't exist
*/
2005-10-11 20:49:41 +00:00
int file_size(char *path)
2001-08-17 05:46:24 +00:00
{
2002-12-28 20:29:26 +00:00
static struct stat sb;
2001-08-17 05:46:24 +00:00
2002-12-28 20:29:26 +00:00
if (stat(path, &sb) == -1)
return -1;
2001-08-17 05:46:24 +00:00
2002-12-28 20:29:26 +00:00
return sb.st_size;
2001-08-17 05:46:24 +00:00
}
/*
* Return time of file, or -1 if file doen't exist, which is
* the same as 1 second before 1 jan 1970. You may test the
* result on -1 since time_t is actualy a long integer.
*/
time_t file_time(char *path)
{
2002-12-28 20:29:26 +00:00
static struct stat sb;
2001-08-17 05:46:24 +00:00
2002-12-28 20:29:26 +00:00
if (stat(path, &sb) == -1)
return -1;
2001-08-17 05:46:24 +00:00
2002-12-28 20:29:26 +00:00
return sb.st_mtime;
2001-08-17 05:46:24 +00:00
}
2004-12-29 13:06:09 +00:00
2001-08-17 05:46:24 +00:00
/*
2002-12-28 20:29:26 +00:00
* Return ASCII string for node, the bits in 'fl' set the output format.
2001-08-17 05:46:24 +00:00
*/
2002-12-28 20:29:26 +00:00
char *ascfnode(faddr *a, int fl)
2001-08-17 05:46:24 +00:00
{
2002-12-28 20:29:26 +00:00
static char buf[128];
buf[0] = '\0';
if ((fl & 0x08) && (a->zone))
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 10, "%u:",a->zone);
2002-12-28 20:29:26 +00:00
if (fl & 0x04)
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 10, "%u/",a->net);
2002-12-28 20:29:26 +00:00
if (fl & 0x02)
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 10, "%u",a->node);
2002-12-28 20:29:26 +00:00
if ((fl & 0x01) && (a->point))
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 10, ".%u",a->point);
2002-12-28 20:29:26 +00:00
if ((fl & 0x10) && (strlen(a->domain)))
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 14, "@%s",a->domain);
2002-12-28 20:29:26 +00:00
return buf;
2001-08-17 05:46:24 +00:00
}
2002-12-28 20:29:26 +00:00
/*
* Return ASCII string for node, the bits in 'fl' set the output format.
*/
char *fido2str(fidoaddr a, int fl)
{
static char buf[128];
buf[0] = '\0';
if ((fl & 0x08) && (a.zone))
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 10, "%u:",a.zone);
2002-12-28 20:29:26 +00:00
if (fl & 0x04)
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 10, "%u/",a.net);
2002-12-28 20:29:26 +00:00
if (fl & 0x02)
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 10, "%u",a.node);
2002-12-28 20:29:26 +00:00
if ((fl & 0x01) && (a.point))
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 10, ".%u",a.point);
2002-12-28 20:29:26 +00:00
if ((fl & 0x10) && (strlen(a.domain)))
2005-08-29 21:03:28 +00:00
snprintf(buf+strlen(buf), 14, "@%s",a.domain);
2002-12-28 20:29:26 +00:00
return buf;
}
2004-12-29 13:06:09 +00:00
2001-08-17 05:46:24 +00:00
char *Dos2Unix(char *dosname)
{
2004-12-29 13:06:09 +00:00
char buf[PATH_MAX];
static char buf2[PATH_MAX];
char *p, *q;
memset(&buf, 0, sizeof(buf));
memset(&buf2, 0, sizeof(buf2));
2005-08-29 21:03:28 +00:00
snprintf(buf, PATH_MAX, "%s", dosname);
2004-12-29 13:06:09 +00:00
p = buf;
if (strlen(CFG.dospath)) {
if (strncasecmp(p, CFG.dospath, strlen(CFG.dospath)) == 0) {
strcpy((char *)buf2, CFG.uxpath);
for (p+=strlen(CFG.dospath), q = buf2 + strlen(buf2); *p; p++, q++)
*q = ((*p) == '\\')?'/':tolower(*p);
*q = '\0';
p = buf2;
} else {
if (strncasecmp(p, CFG.uxpath, strlen(CFG.uxpath)) == 0) {
for (p+=strlen(CFG.uxpath), q = buf2 + strlen(buf2); *p; p++, q++)
*q = ((*p) == '\\')?'/':tolower(*p);
*q = '\0';
p = buf2;
}
2001-08-17 05:46:24 +00:00
}
2004-12-29 13:06:09 +00:00
}
return buf2;
2001-08-17 05:46:24 +00:00
}
static char *dow[] = {(char *)"su", (char *)"mo", (char *)"tu", (char *)"we",
(char *)"th", (char *)"fr", (char *)"sa"};
char *dayname(void)
{
2004-12-29 12:39:47 +00:00
time_t tt;
struct tm ptm;
static char buf[3];
tt = time(NULL);
#if defined(__OpenBSD__)
localtime_r(&tt, &ptm);
#else
ptm = *localtime(&tt);
#endif
2005-08-29 21:03:28 +00:00
snprintf(buf, 3, "%s", dow[ptm.tm_wday]);
2004-12-29 12:39:47 +00:00
return buf;
2001-08-17 05:46:24 +00:00
}
void InitFidonet(void)
{
2004-12-29 13:06:09 +00:00
memset(&fidonet, 0, sizeof(fidonet));
2001-08-17 05:46:24 +00:00
}
int SearchFidonet(unsigned short zone)
{
2004-12-29 13:06:09 +00:00
FILE *fil;
char fidonet_fil[PATH_MAX];
int i;
2001-08-17 05:46:24 +00:00
2005-08-29 21:03:28 +00:00
snprintf(fidonet_fil, PATH_MAX, "%s/etc/fidonet.data", getenv("MBSE_ROOT"));
2004-12-29 13:06:09 +00:00
if ((fil = fopen(fidonet_fil, "r")) == NULL) {
2001-08-17 05:46:24 +00:00
return FALSE;
2004-12-29 13:06:09 +00:00
}
fread(&fidonethdr, sizeof(fidonethdr), 1, fil);
while (fread(&fidonet, fidonethdr.recsize, 1, fil) == 1) {
for (i = 0; i < 6; i++) {
if (zone == fidonet.zone[i]) {
fclose(fil);
return TRUE;
}
}
}
fclose(fil);
return FALSE;
2001-08-17 05:46:24 +00:00
}
char *xmalloc(size_t);
char *xmalloc(size_t size)
{
char *tmp;
tmp = malloc(size);
if (!tmp)
abort();
return tmp;
}
char *printable(char *s, int l)
{
int len;
char *p;
if (pbuff)
free(pbuff);
pbuff=NULL;
if (s == NULL)
return (char *)"(null)";
if (l > 0)
len=l;
else if (l == 0)
len=strlen(s);
else {
len=strlen(s);
if (len > -l)
len=-l;
}
pbuff=(char*)xmalloc(len*3+1);
p=pbuff;
while (len--) {
if (isprint(*(unsigned char*)s))
*p++=*s;
else switch (*s) {
case '\\': *p++='\\'; *p++='\\'; break;
case '\r': *p++='\\'; *p++='r'; break;
case '\n': *p++='\\'; *p++='n'; break;
case '\t': *p++='\\'; *p++='t'; break;
case '\b': *p++='\\'; *p++='b'; break;
2005-08-29 21:03:28 +00:00
default: snprintf(p, 4, "\\%02x", (*s & 0xff)); p+=3; break;
}
s++;
}
*p='\0';
return pbuff;
}
char *clencode(char *s)
{
char Base16Code[]="0123456789ABCDEF";
static char *buf;
char *p, *q;
if (buf)
free(buf);
buf = NULL;
if (s == NULL)
return NULL;
if ((buf = malloc(2 * strlen(s) + 1 * sizeof(char))) == NULL) {
Syslog('+', "clencode: out of memory:string too long:\"%s\"", s);
return s;
}
for (p = s, q = buf; *p != '\0';) {
if ((! isascii(*p)) || (*p == ',') || (*p == ';')) {
*q++ = '\\';
*q++ = Base16Code[(*p >> 4) & 0x0f];
*q++ = Base16Code[*p & 0x0f];
p++;
} else if (*p == '\\') {
*q++ = '\\';
*q++ = *p++;
} else {
*q++ = *p++;
}
}
*q = '\0';
return buf;
}
char *cldecode(char *s)
{
char *p, *q;
int i;
if ((s == NULL) || (! strlen(s))) {
return (char *)"";
}
for (p = s, q = s; *p; p++) {
if (*p == '\\') {
if (*(p + 1) == '\\') {
*q++ = *p++;
} else {
sscanf(p + 1, "%02x", &i);
*q++ = i;
p += 2;
}
} else {
*q++ = *p;
}
}
*q = '\0';
return s;
}