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

141 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
*
*****************************************************************************
2005-08-28 11:34:24 +00:00
* Copyright (C) 1997-2005
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"
2004-02-21 17:22:00 +00:00
#include "../lib/mbselib.h"
2001-08-17 05:46:24 +00:00
#ifndef LOCKDIR
2004-12-28 15:30:52 +00:00
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
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();
2005-08-28 13:45:26 +00:00
snprintf(tmpname,256,"%s%d",LCKTMP,mypid);
2001-08-17 05:46:24 +00:00
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);
2005-08-28 13:45:26 +00:00
snprintf(lckname,256,"%s%s",LCKPREFIX,p);
2001-08-17 05:46:24 +00:00
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();
2005-08-28 13:45:26 +00:00
snprintf(lckname,256,"%s%s",LCKPREFIX,p);
2001-08-17 05:46:24 +00:00
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);
}