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/mbcico/ulock.c

142 lines
3.1 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
2001-12-22 22:42:59 +00:00
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Fidonet mailer
*
*****************************************************************************
2003-12-23 18:33:42 +00:00
* Copyright (C) 1997-2003
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
2003-08-15 20:05:34 +00:00
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
2001-08-17 05:46:24 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2001-08-17 05:46:24 +00:00
#include "../lib/libs.h"
#include "../lib/clcomm.h"
#ifndef LOCKDIR
2002-01-11 19:01:00 +00:00
#if defined(__FreeBSD__) || defined(__NetBSD__)
2001-12-22 22:42:59 +00:00
#define LOCKDIR "/var/spool/lock"
#else
2001-08-17 05:46:24 +00:00
#define LOCKDIR "/var/lock"
#endif
2001-12-22 22:42:59 +00:00
#endif
2001-08-17 05:46:24 +00:00
#define LCKPREFIX LOCKDIR"/LCK.."
#define LCKTMP LOCKDIR"/TMP."
int lock(char *line)
{
pid_t mypid,rempid=0;
int tmppid;
char tmpname[256],lckname[256];
char *p;
int i,rc;
FILE *f;
rc=-1;
if ((p=strrchr(line,'/')) == NULL)
p=line;
else
p++;
mypid = getpid();
sprintf(tmpname,"%s%d",LCKTMP,mypid);
if ((f = fopen(tmpname,"w")) == NULL) {
WriteError("$ulock: can't create %s",tmpname);
return(-1);
}
fprintf(f,"%10d\n",mypid);
fclose(f);
chmod(tmpname,0444);
sprintf(lckname,"%s%s",LCKPREFIX,p);
p=lckname+strlen(lckname)-1;
*p=tolower(*p);
for (i=0; (i++<5) && ((rc = link(tmpname, lckname)) != 0) &&
(errno == EEXIST);)
{
if ((f=fopen(lckname,"r")) == NULL) {
Syslog('l', "$Can't open existing lock file");
} else {
fscanf(f,"%d",&tmppid);
rempid=tmppid;
fclose(f);
Syslog('l', "lock: file read for process %d",rempid);
}
if (kill(rempid,0) && (errno == ESRCH)) {
Syslog('l', "process inactive, unlink file");
unlink(lckname);
} else {
Syslog('l', "process active, sleep a bit");
sleep(2);
}
}
if (rc)
Syslog('l', "$ulock: result %d (errno %d)",rc,errno);
unlink(tmpname);
return(rc);
}
int ulock(char *line)
{
pid_t mypid,rempid;
int tmppid;
char lckname[256];
char *p;
int rc;
FILE *f;
rc=-1;
if ((p=strrchr(line,'/')) == NULL)
p=line;
else
p++;
mypid=getpid();
sprintf(lckname,"%s%s",LCKPREFIX,p);
p=lckname+strlen(lckname)-1;
*p=tolower(*p);
if ((f=fopen(lckname,"r")) == NULL)
{
WriteError("$cannot open lock file %s",lckname);
return rc;
}
fscanf(f,"%d",&tmppid);
rempid=tmppid;
fclose(f);
if (rempid == mypid) {
rc = unlink(lckname);
if (rc)
Syslog('l', "Unlock %s rc=%d", lckname, rc);
}
return(rc);
}