File: ulock.c

package info (click to toggle)
ifmail 2.14tx8.10-32
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: ansic: 30,328; perl: 4,955; yacc: 839; makefile: 716; sh: 424; cpp: 235; lex: 206; awk: 24
file content (119 lines) | stat: -rw-r--r-- 2,350 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <sys/stat.h>
#include "lutil.h"

#ifndef LOCKDIR
#define LOCKDIR "/usr/spool/uucp"
#endif

#define LCKPREFIX LOCKDIR"/LCK.."
#define LCKTMP LOCKDIR"/TMP."

#ifdef DONT_HAVE_PID_T
#define pid_t int
#endif

int lock(line)
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)
	{
		debug(4,"lock cannot create %s",tmpname);
		return(-1);
	}
#if defined(ASCII_LOCKFILES)
	fprintf(f,"%10d\n",mypid);
#elif defined(BINARY_LOCKFILES)
	fwrite(&mypid,sizeof(mypid),1,f);
#else
#error Must define ASCII_LOCKFILES or BINARY_LOCKFILES
#endif
	fclose(f);
	chmod(tmpname,0444);
	sprintf(lckname,"%s%s",LCKPREFIX,p);
	p=lckname+strlen(lckname)-1;
	*p=tolower(*p);
	debug(4,"Trying to create %s for %d",lckname,mypid);
	for (i=0; (i++<5) && ((rc=link(tmpname,lckname)) != 0) && 
				(errno == EEXIST); )
	{
		if ((f=fopen(lckname,"r")) == NULL)
		{
			debug(4,"cannot open existing lock file");
		}
		else
		{
#if defined(ASCII_LOCKFILES)
			fscanf(f,"%d",&tmppid);
			rempid=tmppid;
#elif defined(BINARY_LOCKFILES)
			fread(&rempid,sizeof(rempid),1,f);
#endif
			fclose(f);
			debug(4,"lock file read for process %d",rempid);
		}
		if (kill(rempid,0) && (errno ==  ESRCH))
		{
			debug(4,"process inactive, unlink file");
			unlink(lckname);
		}
		else
		{
			debug(4,"process active, sleep a bit");
			sleep(2);
		}
	}
	unlink(tmpname);
	debug(4,"lock result %d (errno %d)",rc,errno);
	return(rc);
}

int ulock(line)
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)
	{
		logerr("$cannot open lock file %s",lckname);
		return rc;
	}
#if defined(ASCII_LOCKFILES)
	fscanf(f,"%d",&tmppid);
	rempid=tmppid;
#elif defined(BINARY_LOCKFILES)
	fread(&rempid,sizeof(rempid),1,f);
#endif
	fclose(f);
	if (rempid ==  mypid)
		rc=unlink(lckname);
	return(rc);
}