2002-02-22 21:15:21 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
* Purpose ...............: mbtask - mode portlists
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
2006-02-04 13:39:51 +00:00
|
|
|
* Copyright (C) 1997-2006
|
2002-02-22 21:15:21 +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"
|
2002-02-22 21:15:21 +00:00
|
|
|
#include "taskutil.h"
|
2002-12-28 17:29:06 +00:00
|
|
|
#include "../lib/nodelist.h"
|
2002-02-22 21:15:21 +00:00
|
|
|
#include "ports.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LOCKDIR
|
2004-12-28 15:30:52 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
2002-02-22 21:15:21 +00:00
|
|
|
#define LOCKDIR "/var/spool/lock"
|
|
|
|
#else
|
|
|
|
#define LOCKDIR "/var/lock"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LCKPREFIX LOCKDIR"/LCK.."
|
|
|
|
#define LCKTMP LOCKDIR"/TMP."
|
|
|
|
|
|
|
|
|
|
|
|
extern char ttyfn[]; /* TTY file name */
|
|
|
|
extern time_t tty_time; /* TTY update time */
|
2002-02-24 13:52:13 +00:00
|
|
|
extern int rescan; /* Master rescan flag */
|
2002-02-22 21:15:21 +00:00
|
|
|
pp_list *pl = NULL; /* Portlist */
|
|
|
|
|
2002-02-24 17:17:58 +00:00
|
|
|
int pots_lines = 0; /* POTS (Modem) lines */
|
|
|
|
int isdn_lines = 0; /* ISDN lines */
|
|
|
|
int pots_free = 0; /* POTS (Modem) lines free */
|
|
|
|
int isdn_free = 0; /* ISDN lines free */
|
|
|
|
|
2002-02-22 21:15:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tidy the portlist
|
|
|
|
*/
|
|
|
|
void tidy_portlist(pp_list **);
|
|
|
|
void tidy_portlist(pp_list ** fdp)
|
|
|
|
{
|
|
|
|
pp_list *tmp, *old;
|
|
|
|
|
|
|
|
for (tmp = *fdp; tmp; tmp = old) {
|
|
|
|
old = tmp->next;
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
*fdp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a port to the portlist
|
|
|
|
*/
|
|
|
|
void fill_portlist(pp_list **, pp_list *);
|
|
|
|
void fill_portlist(pp_list **fdp, pp_list *new)
|
|
|
|
{
|
|
|
|
pp_list *tmp, *ta;
|
|
|
|
|
|
|
|
tmp = (pp_list *)malloc(sizeof(pp_list));
|
2003-12-06 16:35:33 +00:00
|
|
|
memset(tmp, 0, sizeof(tmp));
|
2002-02-22 21:15:21 +00:00
|
|
|
tmp->next = NULL;
|
|
|
|
strncpy(tmp->tty, new->tty, 6);
|
|
|
|
tmp->mflags = new->mflags;
|
|
|
|
tmp->dflags = new->dflags;
|
2003-04-02 21:36:47 +00:00
|
|
|
tmp->locktime = 0;
|
2002-02-22 21:15:21 +00:00
|
|
|
|
|
|
|
if (*fdp == NULL) {
|
|
|
|
*fdp = tmp;
|
|
|
|
} else {
|
|
|
|
for (ta = *fdp; ta; ta = ta->next)
|
|
|
|
if (ta->next == NULL) {
|
|
|
|
ta->next = (pp_list *)tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build a list of available dialout ports.
|
|
|
|
*/
|
|
|
|
void load_ports()
|
|
|
|
{
|
2002-12-30 22:13:33 +00:00
|
|
|
FILE *fp;
|
|
|
|
pp_list new;
|
|
|
|
int stdflag;
|
2006-05-22 12:09:15 +00:00
|
|
|
char *p, *q, *capflags = NULL;
|
2002-12-30 22:13:33 +00:00
|
|
|
nodelist_modem **tmpm;
|
|
|
|
|
2002-02-22 21:15:21 +00:00
|
|
|
tidy_portlist(&pl);
|
|
|
|
if ((fp = fopen(ttyfn, "r")) == NULL) {
|
2002-12-28 17:29:06 +00:00
|
|
|
Syslog('?', "$Can't open %s", ttyfn);
|
2002-02-22 21:15:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fread(&ttyinfohdr, sizeof(ttyinfohdr), 1, fp);
|
|
|
|
|
2002-02-24 17:17:58 +00:00
|
|
|
pots_lines = isdn_lines = 0;
|
|
|
|
|
2002-02-22 21:15:21 +00:00
|
|
|
while (fread(&ttyinfo, ttyinfohdr.recsize, 1, fp) == 1) {
|
|
|
|
if (((ttyinfo.type == POTS) || (ttyinfo.type == ISDN)) && (ttyinfo.available) && (ttyinfo.callout)) {
|
2002-02-24 17:17:58 +00:00
|
|
|
|
|
|
|
if (ttyinfo.type == POTS)
|
|
|
|
pots_lines++;
|
|
|
|
if (ttyinfo.type == ISDN)
|
|
|
|
isdn_lines++;
|
|
|
|
|
2002-02-22 21:15:21 +00:00
|
|
|
memset(&new, 0, sizeof(new));
|
|
|
|
strncpy(new.tty, ttyinfo.tty, 6);
|
|
|
|
|
2003-08-03 15:58:26 +00:00
|
|
|
capflags = xstrcpy((char *)"flags:");
|
2002-02-22 21:15:21 +00:00
|
|
|
stdflag = TRUE;
|
|
|
|
q = xstrcpy(ttyinfo.flags);
|
|
|
|
for (p = q; p; p = q) {
|
|
|
|
if ((q = strchr(p, ',')))
|
|
|
|
*q++ = '\0';
|
|
|
|
if ((strncasecmp(p, "U", 1) == 0) && (strlen(p) == 1)) {
|
|
|
|
stdflag = FALSE;
|
|
|
|
} else {
|
2002-12-30 22:13:33 +00:00
|
|
|
for (tmpm = &nl_pots; *tmpm; tmpm=&((*tmpm)->next))
|
2003-08-03 15:58:26 +00:00
|
|
|
if (strcasecmp(p, (*tmpm)->name) == 0) {
|
2002-12-30 22:13:33 +00:00
|
|
|
new.mflags |= (*tmpm)->value;
|
2003-08-03 15:58:26 +00:00
|
|
|
capflags = xstrcat(capflags, (char *)" ");
|
|
|
|
capflags = xstrcat(capflags, (*tmpm)->name);
|
|
|
|
}
|
2002-12-30 22:13:33 +00:00
|
|
|
for (tmpm = &nl_isdn; *tmpm; tmpm=&((*tmpm)->next))
|
2003-08-03 15:58:26 +00:00
|
|
|
if (strcasecmp(p, (*tmpm)->name) == 0) {
|
2002-12-30 22:13:33 +00:00
|
|
|
new.dflags |= (*tmpm)->value;
|
2003-08-03 15:58:26 +00:00
|
|
|
capflags = xstrcat(capflags, (char *)" ");
|
|
|
|
capflags = xstrcat(capflags, (*tmpm)->name);
|
|
|
|
}
|
2002-02-22 21:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-22 11:35:47 +00:00
|
|
|
free(q);
|
2002-02-24 17:17:58 +00:00
|
|
|
|
2003-08-03 15:58:26 +00:00
|
|
|
Syslog('+', "Found line %s, %s", new.tty, capflags);
|
2002-02-22 21:15:21 +00:00
|
|
|
fill_portlist(&pl, &new);
|
2003-08-03 15:58:26 +00:00
|
|
|
if (capflags)
|
|
|
|
free(capflags);
|
2006-05-22 12:09:15 +00:00
|
|
|
capflags = NULL;
|
2002-02-22 21:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
2002-02-24 17:17:58 +00:00
|
|
|
|
2002-02-22 21:15:21 +00:00
|
|
|
fclose(fp);
|
|
|
|
tty_time = file_time(ttyfn);
|
2006-05-22 12:09:15 +00:00
|
|
|
if (pots_lines || isdn_lines)
|
|
|
|
Syslog('+', "Detected %d modem port%s and %d ISDN port%s",
|
2002-06-05 19:30:31 +00:00
|
|
|
pots_lines, (pots_lines == 1)?"":"s", isdn_lines, (isdn_lines == 1)?"":"s");
|
2002-02-22 21:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-04 13:39:51 +00:00
|
|
|
void unload_ports()
|
|
|
|
{
|
|
|
|
tidy_portlist(&pl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-22 21:15:21 +00:00
|
|
|
/*
|
2002-02-24 13:52:13 +00:00
|
|
|
* Check status of all modem/ISDN ports.
|
|
|
|
* If something is changed set the master rescan flag.
|
2002-02-22 21:15:21 +00:00
|
|
|
*/
|
|
|
|
void check_ports(void)
|
|
|
|
{
|
|
|
|
pp_list *tpl;
|
2005-10-09 13:37:11 +00:00
|
|
|
char *lckname, *progname;
|
2002-02-22 21:15:21 +00:00
|
|
|
FILE *lf;
|
2002-02-24 17:17:58 +00:00
|
|
|
int tmppid, changed = FALSE;
|
2002-02-22 21:15:21 +00:00
|
|
|
pid_t rempid = 0;
|
|
|
|
|
2002-02-24 17:17:58 +00:00
|
|
|
pots_free = isdn_free = 0;
|
2005-09-11 13:07:42 +00:00
|
|
|
lckname = calloc(PATH_MAX, sizeof(char));
|
2002-02-24 17:17:58 +00:00
|
|
|
|
2002-02-22 21:15:21 +00:00
|
|
|
for (tpl = pl; tpl; tpl = tpl->next) {
|
2005-09-11 13:07:42 +00:00
|
|
|
snprintf(lckname, PATH_MAX, "%s%s", LCKPREFIX, tpl->tty);
|
2002-02-22 21:15:21 +00:00
|
|
|
if ((lf = fopen(lckname, "r")) == NULL) {
|
|
|
|
if (tpl->locked) {
|
|
|
|
tpl->locked = 0;
|
2002-12-28 17:29:06 +00:00
|
|
|
Syslog('+', "Port %s is now free after %d seconds", tpl->tty, tpl->locktime);
|
2002-05-19 11:24:01 +00:00
|
|
|
if (tpl->locktime > 4)
|
|
|
|
/*
|
|
|
|
* Good, set master rescan flag if longer then 4 seconds locked.
|
|
|
|
*/
|
|
|
|
changed = TRUE;
|
2002-02-22 21:15:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fscanf(lf, "%d", &tmppid);
|
|
|
|
rempid = tmppid;
|
|
|
|
fclose(lf);
|
|
|
|
if (kill(rempid, 0) && (errno == ESRCH)) {
|
2002-12-28 17:29:06 +00:00
|
|
|
Syslog('+', "Stale lockfile for %s, unlink", tpl->tty);
|
2002-02-22 21:15:21 +00:00
|
|
|
unlink(lckname);
|
2002-02-24 17:17:58 +00:00
|
|
|
changed = TRUE;
|
2002-02-22 21:15:21 +00:00
|
|
|
} else {
|
|
|
|
if (!tpl->locked) {
|
|
|
|
tpl->locked = rempid;
|
2002-05-19 11:24:01 +00:00
|
|
|
tpl->locktime = 0;
|
2005-10-09 13:37:11 +00:00
|
|
|
progname = calloc(PATH_MAX, sizeof(char));
|
|
|
|
if (pid2prog(rempid, progname, PATH_MAX) == 0)
|
|
|
|
Syslog('+', "Port %s locked, pid %d (%s)", tpl->tty, rempid, progname);
|
|
|
|
else
|
|
|
|
Syslog('+', "Port %s locked, pid %d", tpl->tty, rempid);
|
|
|
|
free(progname);
|
2002-05-19 11:24:01 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Count locktime
|
|
|
|
*/
|
|
|
|
tpl->locktime++;
|
|
|
|
if (tpl->locktime == 5) {
|
|
|
|
changed = TRUE;
|
2002-12-28 17:29:06 +00:00
|
|
|
Syslog('+', "Port %s locked for 5 seconds, forcing scan", tpl->tty);
|
2002-05-19 11:24:01 +00:00
|
|
|
}
|
2002-02-22 21:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-02-24 17:17:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now count free ports
|
|
|
|
*/
|
|
|
|
if (tpl->mflags && !tpl->locked)
|
|
|
|
pots_free++;
|
|
|
|
if (tpl->dflags && !tpl->locked)
|
|
|
|
isdn_free++;
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
rescan = TRUE;
|
2002-12-28 17:29:06 +00:00
|
|
|
Syslog('p', "Free ports: pots=%d isdn=%d", pots_free, isdn_free);
|
2002-02-24 17:17:58 +00:00
|
|
|
}
|
2002-02-22 21:15:21 +00:00
|
|
|
}
|
2005-09-11 13:07:42 +00:00
|
|
|
free(lckname);
|
2002-02-22 21:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|