File: locking.c

package info (click to toggle)
cucipop 1.31-5
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 236 kB
  • ctags: 379
  • sloc: ansic: 2,260; sh: 148; makefile: 92
file content (197 lines) | stat: -rw-r--r-- 4,494 bytes parent folder | download | duplicates (3)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/************************************************************************
 *	Whatever is needed for (un)locking files in various ways	*
 *									*
 *	Copyright (c) 1990-1997, S.R. van den Berg, The Netherlands	*
 *	#include "../README"						*
 ************************************************************************/
#ifdef RCS
static /*const*/char rcsid[]=
 "$Id: locking.c,v 1.7 1998/05/13 16:57:40 srb Exp $";
#endif
#include "config.h"

#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <errno.h>

#include "cucipop.h"
#include "locking.h"

#ifndef USEfcntl_lock
#ifndef USElockf
#ifndef USEflock
#ifndef USEdot_lock
#error No locking mechanism enabled
#endif
#define fdlock(fd)	0
#define fdunlock()	0
#endif
#endif
#endif
#ifndef fdlock
static int fdlock(int fd);
static int fdunlock(void);
#endif

#ifndef tell
#define tell(fd)	lseek(fd,0L,SEEK_CUR)
#endif

static char*lastlocked;
static const char lockext[]=".lock";

void unlockmbox(void)
{ fdunlock();
#ifdef USEdot_lock
  blocksignals();
  if(lastlocked)
     unlink(lastlocked),free(lastlocked),lastlocked=0;
#endif
}

int lockmbox(int fd,const char*mbox)
{ char*ll;
  if(fdlock(fd))
     return 0;
#ifdef USEdot_lock
  if(ll=malloc(strlen(mbox)+STRLEN(lockext)+1))
   { strcpy(ll,mbox);strcat(ll,lockext);
     for(;;)
      { blocksignals();
	switch(xcreat(ll,0644))
	 { case 0:lastlocked=ll;setsignals();
	      return 1;
	   default:setsignals();
thatsall:     free(ll);
	      return 0;
	   case -1:setsignals();sleep(LOCKSLEEP);
	      if(spoofcheck>=0)
	       { int fdsockin=fileno(sockin),res;char c;
		 fcntl(fdsockin,F_SETFL,(long)O_NONBLOCK);
		 res=read(fdsockin,&c,1);fcntl(fdsockin,F_SETFL,0L);
		 switch(res)
		  { case 0:
		       goto thatsall;
		    case 1:spoofcheck=-1;ungetc(c,sockin);
		  }
	       }
	 }
      }
   }
  return 0;
#else
  return 1;
#endif
}
	/* if you've ever wondered what conditional compilation was good for */
#ifndef fdlock						/* watch closely :-) */
#ifdef USEflock
#ifndef SYS_FILE_H_MISSING
#include <sys/file.h>
#endif
#define REITflock	1
#else
#define REITflock	0
#endif /* USEflock */
static int oldfdlock= -1;			    /* the fd we locked last */
#ifndef NOfcntl_lock
static struct flock flck;		/* why can't it be a local variable? */
#define REITfcntl	1
#else
#define REITfcntl	0
#endif /* NOfcntl_lock */
#ifdef USElockf
static off_t oldlockoffset;
#define REITlockf	1
#else
#define REITlockf	0
#endif /* USElockf */

static int fdlock(int fd)
{ int ret;
#if REITfcntl+REITflock+REITlockf>1
  for(;;syslog(LOG_DEBUG,"reiterating kernel-lock"),sleep(LOCKSLEEP))
#endif
   {
#ifdef USElockf
     oldlockoffset=0;
#endif
#ifndef NOfcntl_lock
     flck.l_type=F_WRLCK;flck.l_whence=SEEK_SET;flck.l_len=0;
#ifdef USElockf
     flck.l_start=oldlockoffset;
#else
     flck.l_start=tell(fd);
#endif
#endif
#ifndef NOfcntl_lock
     ret=fcntl(fd,F_SETLKW,&flck);
#ifdef USElockf
     if((ret|=lockf(fd,F_TLOCK,(off_t)0))&&(errno==EAGAIN||errno==EACCES||
      errno==EWOULDBLOCK))
ufcntl:
      { flck.l_type=F_UNLCK;fcntl(fd,F_SETLK,&flck);
	continue;
      }
#ifdef USEflock
     if((ret|=flock(fd,LOCK_EX|LOCK_NB))&&(errno==EAGAIN||errno==EACCES||
      errno==EWOULDBLOCK))
      { lockf(fd,F_ULOCK,(off_t)0);
	goto ufcntl;
      }
#endif /* USEflock */
#else /* USElockf */
#ifdef USEflock
     if((ret|=flock(fd,LOCK_EX|LOCK_NB))&&(errno==EAGAIN||errno==EACCES||
      errno==EWOULDBLOCK))
      { flck.l_type=F_UNLCK;fcntl(fd,F_SETLK,&flck);
	continue;
      }
#endif /* USEflock */
#endif /* USElockf */
#else /* NOfcntl_lock */
#ifdef USElockf
     ret=lockf(fd,F_LOCK,(off_t)0);
#ifdef USEflock
     if((ret|=flock(fd,LOCK_EX|LOCK_NB))&&(errno==EAGAIN||errno==EACCES||
      errno==EWOULDBLOCK))
      { lockf(fd,F_ULOCK,(off_t)0);
	continue;
      }
#endif /* USEflock */
#else /* USElockf */
#ifdef USEflock
     ret=flock(fd,LOCK_EX);
#endif /* USEflock */
#endif /* USElockf */
#endif /* NOfcntl_lock */
     oldfdlock=fd;
     return ret;
   }
}

static int fdunlock(void)
{ int i;
  if(oldfdlock<0)
     return -1;
  i=0;
#ifdef USEflock
  i|=flock(oldfdlock,LOCK_UN);
#endif
#ifdef USElockf
  ;{ off_t curp=tell(oldfdlock);
     lseek(oldfdlock,oldlockoffset,SEEK_SET);
     i|=lockf(oldfdlock,F_ULOCK,(off_t)0);lseek(oldfdlock,curp,SEEK_SET);
   }
#endif
#ifndef NOfcntl_lock
  flck.l_type=F_UNLCK;i|=fcntl(oldfdlock,F_SETLK,&flck);
#endif
  oldfdlock= -1;
  return i;
}
#endif /* fdlock */