Logs program name that locks serial ports

This commit is contained in:
Michiel Broek 2005-10-09 13:37:11 +00:00
parent 44a118ac28
commit ce1a91279d
7 changed files with 262 additions and 7 deletions

View File

@ -23,6 +23,9 @@ v0.81.0 07-Oct-2005
instead a fake ttyinfo record is created. Modem/ISDN and
console still need a valid configured tty.
mbtask:
Logs program name that locks serial ports.
mbsetup:
Removed settings for users screenlength.
In tty setup the network records are automatic removed. The

View File

@ -10,13 +10,15 @@ COMMON_SRCS = clcomm.c client.c crc.c semafore.c signame.c charset.c remask.c \
batchrd.c ftn.c pktname.c mangle.c sectest.c proglock.c magic.c \
dostran.c ftnmsg.c mbfile.c nodelock.c rawio.c strcasestr.c \
execute.c expipe.c getheader.c noderecord.c rfcaddr.c strutil.c \
faddr.c gmtoffset.c packet.c rfcdate.c term.c endian.c timers.c
faddr.c gmtoffset.c packet.c rfcdate.c term.c endian.c timers.c \
pidinfo.c
COMMON_OBJS = clcomm.o client.o crc.o semafore.o signame.o charset.o remask.o \
ftscprod.o attach.o falists.o hdr.o parsedate.o rfcmsg.o unpacker.o \
batchrd.o ftn.o pktname.o mangle.o sectest.o proglock.o rearc.o \
dostran.o ftnmsg.o mbfile.o nodelock.o rawio.o strcasestr.o magic.o \
execute.o expipe.o getheader.o noderecord.o rfcaddr.o strutil.o \
faddr.o gmtoffset.o packet.o rfcdate.o term.o endian.o timers.o
faddr.o gmtoffset.o packet.o rfcdate.o term.o endian.o timers.o \
pidinfo.o
COMMON_HDRS = mbselib.h
NODELIST_SRCS = nodelist.c
NODELIST_OBJS = nodelist.o
@ -166,6 +168,7 @@ rfcdate.o: ../config.h mbselib.h
term.o: ../config.h mbselib.h users.h
endian.o: ../config.h mbselib.h
timers.o: ../config.h mbselib.h
pidinfo.o: ../config.h mbselib.h
dbcfg.o: ../config.h mbselib.h mbse.h users.h mbsedb.h
dbdupe.o: ../config.h mbselib.h mbse.h users.h mbsedb.h
dbftn.o: ../config.h mbselib.h users.h mbsedb.h

View File

@ -2549,7 +2549,13 @@ int magic_check(char *, char *); /* Check if magic alias exists */
/************************************************************************
/*
* pidinfo.c
*/
int pid2prog(pid_t, char *, size_t); /* Find progrname for a pid */
/*************************************************************************
*
* Charset mapping
*/

119
lib/pidinfo.c Normal file
View File

@ -0,0 +1,119 @@
/*****************************************************************************
*
* $Id$
* Purpose ...............: Pid utilities
*
*****************************************************************************
* Copyright (C) 1997-2005
*
* 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, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*****************************************************************************/
#include "../config.h"
#include "mbselib.h"
/*
* Find the program name for a given pid. The progname buffer
* has te be allocated with size byte.
*/
int pid2prog(pid_t pid, char *progname, size_t size)
{
#if defined(__OpenBSD__)
#define ARG_SIZE 60
static char **s, buf[ARG_SIZE];
size_t siz = 100;
char **p;
int mib[4];
#elif defined(__NetBSD__)
#define ARG_SIZE 60
static char **s;
size_t siz = 100;
int mib[4];
#else
char temp[PATH_MAX];
FILE *fp;
#endif
#if defined(__OpenBSD__)
/*
* Systems that use sysctl to get process information
*/
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = pid;
mib[3] = KERN_PROC_ARGV;
if ((s = realloc(s, siz)) == NULL) {
WriteError("pid2prog(): no memory");
return -1;
}
if (sysctl(mib, 4, s, &siz, NULL, 0) == -1) {
WriteError("$pid2prog() sysctl call failed");
return -1;
}
buf[0] = '\0';
for (p = s; *p != NULL; p++) {
if (p != s)
strlcat(buf, " ", sizeof(buf));
strlcat(buf, *p, sizeof(buf));
}
strncpy(progname, buf, size);
return 0;
#elif defined(__NetBSD__)
/*
* Systems that use sysctl to get process information
*/
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = pid;
mib[3] = KERN_PROC_ARGV;
if ((s = realloc(s, siz)) == NULL) {
WriteError("pid2prog(): no memory");
return -1;
}
if (sysctl(mib, 4, s, &siz, NULL, 0) == -1) {
WriteError("$pid2prog() sysctl call failed");
return -1;
}
// parent = xstrcpy((char *)s);
strncpy(progname, (char *)s, size);
return 0;
#else
/*
* Systems with /proc filesystem like Linux, FreeBSD
*/
snprintf(temp, PATH_MAX, "/proc/%d/cmdline", pid);
if ((fp = fopen(temp, "r")) == NULL) {
WriteError("$Can't read %s", temp);
return -1;
}
fgets(temp, PATH_MAX-1, fp);
fclose(fp);
strncpy(progname, temp, size);
return 0;
#endif
}

View File

@ -6,13 +6,13 @@ include ../Makefile.global
SRCS = callstat.c scanout.c taskcomm.c taskinfo.c taskstat.c taskibc.c \
mbtask.c outstat.c signame.c taskdisk.c taskregs.c taskutil.c \
ports.c calllist.c ping.c taskchat.c crc.c
ports.c calllist.c ping.c taskchat.c crc.c pidinfo.c
HDRS = callstat.h mbtask.h outstat.h signame.h taskdisk.h taskregs.h taskutil.h \
scanout.h taskcomm.h taskinfo.h taskstat.h taskibc.h \
ports.h calllist.h ping.h taskchat.h
OBJS = callstat.o scanout.o taskcomm.o taskinfo.o taskstat.o taskibc.o \
mbtask.o outstat.o signame.o taskdisk.o taskregs.o taskutil.o \
ports.o calllist.o ping.o taskchat.o crc.o
ports.o calllist.o ping.o taskchat.o crc.o pidinfo.o
SLIBS = ../lib/libnodelist.a
OTHER = Makefile issue issue.netbsd

119
mbtask/pidinfo.c Normal file
View File

@ -0,0 +1,119 @@
/*****************************************************************************
*
* $Id$
* Purpose ...............: Pid utilities
*
*****************************************************************************
* Copyright (C) 1997-2005
*
* 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, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*****************************************************************************/
#include "../config.h"
#include "../lib/mbselib.h"
/*
* Find the program name for a given pid. The progname buffer
* has te be allocated with size byte.
*/
int pid2prog(pid_t pid, char *progname, size_t size)
{
#if defined(__OpenBSD__)
#define ARG_SIZE 60
static char **s, buf[ARG_SIZE];
size_t siz = 100;
char **p;
int mib[4];
#elif defined(__NetBSD__)
#define ARG_SIZE 60
static char **s;
size_t siz = 100;
int mib[4];
#else
char temp[PATH_MAX];
FILE *fp;
#endif
#if defined(__OpenBSD__)
/*
* Systems that use sysctl to get process information
*/
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = pid;
mib[3] = KERN_PROC_ARGV;
if ((s = realloc(s, siz)) == NULL) {
WriteError("pid2prog(): no memory");
return -1;
}
if (sysctl(mib, 4, s, &siz, NULL, 0) == -1) {
WriteError("$pid2prog() sysctl call failed");
return -1;
}
buf[0] = '\0';
for (p = s; *p != NULL; p++) {
if (p != s)
strlcat(buf, " ", sizeof(buf));
strlcat(buf, *p, sizeof(buf));
}
strncpy(progname, buf, size);
return 0;
#elif defined(__NetBSD__)
/*
* Systems that use sysctl to get process information
*/
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = pid;
mib[3] = KERN_PROC_ARGV;
if ((s = realloc(s, siz)) == NULL) {
WriteError("pid2prog(): no memory");
return -1;
}
if (sysctl(mib, 4, s, &siz, NULL, 0) == -1) {
WriteError("$pid2prog() sysctl call failed");
return -1;
}
// parent = xstrcpy((char *)s);
strncpy(progname, (char *)s, size);
return 0;
#else
/*
* Systems with /proc filesystem like Linux, FreeBSD
*/
snprintf(temp, PATH_MAX, "/proc/%d/cmdline", pid);
if ((fp = fopen(temp, "r")) == NULL) {
WriteError("$Can't read %s", temp);
return -1;
}
fgets(temp, PATH_MAX-1, fp);
fclose(fp);
strncpy(progname, temp, size);
return 0;
#endif
}

View File

@ -183,7 +183,7 @@ void load_ports()
void check_ports(void)
{
pp_list *tpl;
char *lckname;
char *lckname, *progname;
FILE *lf;
int tmppid, changed = FALSE;
pid_t rempid = 0;
@ -215,7 +215,12 @@ void check_ports(void)
if (!tpl->locked) {
tpl->locked = rempid;
tpl->locktime = 0;
Syslog('+', "Port %s locked, pid %d", tpl->tty, rempid);
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);
} else {
/*
* Count locktime