Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Synchronet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Main
Synchronet
Commits
3714b76c
Commit
3714b76c
authored
22 years ago
by
deuce
Browse files
Options
Downloads
Patches
Plain Diff
Fix locking for BSD / FreeBSD w/Deuces sane fcntl() kernel hack.
Add more locking tests to wraptest.c
parent
afa6e86c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/xpdev/filewrap.c
+35
-13
35 additions, 13 deletions
src/xpdev/filewrap.c
src/xpdev/filewrap.h
+9
-0
9 additions, 0 deletions
src/xpdev/filewrap.h
src/xpdev/wraptest.c
+16
-0
16 additions, 0 deletions
src/xpdev/wraptest.c
with
60 additions
and
13 deletions
src/xpdev/filewrap.c
+
35
−
13
View file @
3714b76c
...
...
@@ -83,9 +83,11 @@ long DLLCALL filelength(int fd)
/* Sets a lock on a portion of a file */
int
DLLCALL
lock
(
int
fd
,
long
pos
,
int
len
)
{
int
flags
;
#if defined(F_SANERDLCKNO) || !defined(BSD)
struct
flock
alock
;
#ifndef F_SANEWRLCKNO
int
flags
;
if
((
flags
=
fcntl
(
fd
,
F_GETFL
))
==-
1
)
return
-
1
;
...
...
@@ -93,16 +95,22 @@ int DLLCALL lock(int fd, long pos, int len)
alock
.
l_type
=
F_RDLCK
;
/* set read lock to prevent writes */
else
alock
.
l_type
=
F_WRLCK
;
/* set write lock to prevent all access */
#else
alock
.
l_type
=
F_SANEWRLCKNO
;
#endif
alock
.
l_whence
=
L_SET
;
/* SEEK_SET */
alock
.
l_start
=
pos
;
alock
.
l_len
=
len
;
if
(
fcntl
(
fd
,
F_SETLK
,
&
alock
)
==-
1
)
return
(
-
1
);
#endif
#ifndef F_SANEWRLCKNO
/* use flock (doesn't work over NFS) */
if
(
flock
(
fd
,
LOCK_EX
|
LOCK_NB
)
!=
0
)
return
(
-
1
);
#endif
return
(
0
);
}
...
...
@@ -110,18 +118,26 @@ int DLLCALL lock(int fd, long pos, int len)
/* Removes a lock from a file record */
int
DLLCALL
unlock
(
int
fd
,
long
pos
,
int
len
)
{
struct
flock
alock
;
#if defined(F_SANEUNLCK) || !defined(BSD)
struct
flock
alock
;
#ifdef F_SANEUNLCK
alock
.
l_type
=
F_SANEUNLCK
;
/* remove the lock */
#else
alock
.
l_type
=
F_UNLCK
;
/* remove the lock */
#endif
alock
.
l_whence
=
L_SET
;
alock
.
l_start
=
pos
;
alock
.
l_len
=
len
;
if
(
fcntl
(
fd
,
F_SETLK
,
&
alock
)
==-
1
)
return
(
-
1
);
#endif
#ifndef F_SANEUNLCK
/* use flock (doesn't work over NFS) */
if
(
flock
(
fd
,
LOCK_UN
|
LOCK_NB
)
!=
0
)
return
(
-
1
);
#endif
return
(
0
);
}
...
...
@@ -130,7 +146,9 @@ int DLLCALL unlock(int fd, long pos, int len)
int
DLLCALL
sopen
(
char
*
fn
,
int
access
,
int
share
)
{
int
fd
;
#ifndef F_SANEWRLCKNO
int
flock_op
=
LOCK_NB
;
/* non-blocking */
#endif
struct
flock
alock
;
if
((
fd
=
open
(
fn
,
access
,
S_IREAD
|
S_IWRITE
))
<
0
)
...
...
@@ -139,6 +157,20 @@ int DLLCALL sopen(char *fn, int access, int share)
if
(
share
==
SH_DENYNO
)
/* no lock needed */
return
fd
;
#if defined(F_SANEWRLCKNO) || !defined(BSD)
/* use fcntl (doesn't work correctly with threads) */
alock
.
l_type
=
share
;
alock
.
l_whence
=
L_SET
;
alock
.
l_start
=
0
;
alock
.
l_len
=
0
;
/* lock to EOF */
if
(
fcntl
(
fd
,
F_SETLK
,
&
alock
)
==-
1
)
{
close
(
fd
);
return
-
1
;
}
#endif
#ifndef F_SANEWRLCKNO
/* use flock (doesn't work over NFS) */
if
(
share
==
SH_DENYRW
)
flock_op
|=
LOCK_EX
;
...
...
@@ -150,17 +182,7 @@ int DLLCALL sopen(char *fn, int access, int share)
close
(
fd
);
return
(
-
1
);
}
/* use fcntl (doesn't work correctly with threads) */
alock
.
l_type
=
share
;
alock
.
l_whence
=
L_SET
;
alock
.
l_start
=
0
;
alock
.
l_len
=
0
;
/* lock to EOF */
if
(
fcntl
(
fd
,
F_SETLK
,
&
alock
)
==-
1
)
{
close
(
fd
);
return
-
1
;
}
#endif
return
fd
;
}
...
...
This diff is collapsed.
Click to expand it.
src/xpdev/filewrap.h
+
9
−
0
View file @
3714b76c
...
...
@@ -78,8 +78,17 @@
#define O_DENYNONE (1<<31)
/* req'd for Baja/nopen compatibility */
#define SH_DENYNO 2 // no locks
#ifdef F_SANEWRLCKNO
#define SH_DENYRW F_SANEWRLCKNO // exclusive lock
#else
#define SH_DENYRW F_WRLCK // exclusive lock
#endif
#ifdef F_SANERDLCKNO
#define SH_DENYWR F_SANERDLCKNO // shareable lock
#else
#define SH_DENYWR F_RDLCK // shareable lock
#endif
#define chsize(fd,size) ftruncate(fd,size)
#define tell(fd) lseek(fd,0,SEEK_CUR)
...
...
This diff is collapsed.
Click to expand it.
src/xpdev/wraptest.c
+
16
−
0
View file @
3714b76c
...
...
@@ -46,6 +46,7 @@ int main()
DIRENT
*
dirent
;
thread_data_t
thread_data
;
int
fd
;
int
fd2
;
/* Show platform details */
DESCRIBE_COMPILER
(
compiler
);
...
...
@@ -115,6 +116,21 @@ int main()
printf
(
"!FAILURE file locking
\n
"
);
else
printf
(
"SUCCESS! Record locking
\n
"
);
if
((
fd2
=
sopen
(
LOCK_FNAME
,
O_RDWR
,
SH_DENYRW
))
!=-
1
)
{
printf
(
"SUCCESS! Cannot reopen SH_DENYRW while lock is held
\n
"
);
close
(
fd2
);
}
else
{
printf
(
"!FAILURE can reopen SH_DENYRW while lock is held
\n
"
);
}
if
(
unlock
(
fd
,
LOCK_OFFSET
,
LOCK_LEN
))
printf
(
"!FAILURE unlock() non-functional
\n
"
);
if
(
lock
(
fd
,
LOCK_OFFSET
+
LOCK_LEN
+
1
,
LOCK_LEN
))
printf
(
"SUCCESS! Cannot re-lock after non-overlapping unlock()
\n
"
);
else
printf
(
"!FAILURE can re-lock after non-overlappping unlock()
\n
"
);
if
(
lock
(
fd
,
LOCK_OFFSET
,
LOCK_LEN
))
printf
(
"!FAILURE cannot re-lock unlocked area
\n
"
);
close
(
fd
);
/* getch test */
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment