File: lock.ph

package info (click to toggle)
dcmtk 3.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,976 kB
  • sloc: cpp: 193,852; ansic: 46,292; sh: 4,020; makefile: 3,969; perl: 3,278; lex: 94
file content (194 lines) | stat: -rw-r--r-- 4,511 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
#!/usr/bin/perl
#
#  Copyright (C) 1996-2010, OFFIS e.V.
#  All rights reserved.  See COPYRIGHT file for details.
#
#  This software and supporting documentation were developed by
#
#    OFFIS e.V.
#    R&D Division Health
#    Escherweg 2
#    D-26121 Oldenburg, Germany
#
#  for CEN/TC251/WG4 as a contribution to the Computer Assisted Radiology
#  (CAR) 1996 DICOM Demonstration.
#
#
# Module: dcmwlm (WWW Component)
#
# Author: Marco Eichelberg
#
# Purpose:
#   This module contains perl procedures which allow to set/unset
#   read and write locks on one certain file (which is stored in a
#   global variable).
#   The procedures use the perl fcntl() call instead of flock() in order
#   to be "compatible" with the dcmtk Basic Worklist Management components
#   written in C++, on both BSD and System V platforms.
#   Since the "struct flock" required for fcntl() is defined differently
#   on every system, this perl script relies on a C program "preplock" which
#   initialises the flock structure from <fcntl.h> and passes the result
#   to perl.
#
# Last Update:      $Author: joergr $
# Update Date:      $Date: 2010-10-14 13:02:01 $
# CVS/RCS Revision: $Revision: 1.2 $
# Status:           $State: Exp $
#
# CVS/RCS Log
#   $Log: lock.ph,v $
#   Revision 1.2  2010-10-14 13:02:01  joergr
#   Updated copyright header. Added reference to COPYRIGHT file.
#
#   Revision 1.1  2002/12/03 12:16:06  wilkens
#   Added files und functionality from the dcmtk/wlisctn folder to dcmtk/dcmwlm
#   so that dcmwlm can now completely replace wlistctn in the public domain part
#   of dcmtk. Pertaining to this replacement requirement, another optional return
#   key attribute was integrated into the wlm utilities.
#
#
#

require 'prefs.ph';

$fcntldata=`$prefs{'preplock'}`;
eval $fcntldata;

$fd_inuse=0;   # 0=no_lock, 1=read_lock, 2=write_lock

#
# usage: &set_readlock('pathname');
#   returns 1 if successful, 0 upon failure.
#   modifies the global variable $fd_inuse and the file descriptor fd.
#
sub set_readlock
{
  local($lockname, $last, $fcntldata);

  if ((@_ != 1)||($_[0] eq ''))
  {
    printf(stderr "error: NULL path not allowed\n");
    return 0;
  }
  $lockname = $_[0];

  if ($fd_inuse ne 0)
  {
    printf(stderr "error: nested read locks not allowed!\n");
    return 0;
  }

  $last = chop($lockname);
  if ($last eq '/')
  {
    $lockname = join('','+<',$lockname,$last,$prefs{'lockfile'});
  } else {
    $lockname = join('','+<',$lockname,$last,'/',$prefs{'lockfile'});
  }

  if (!open(fd, $lockname))
  {
     $fd_inuse = 0;
     printf(stderr "error: cannot open file %s\n",$lockname);
     return 0;
  } else {
    $fd_inuse=1;
  }

  $fcntldata = $const_RDLCK_STRUCT;

  if (-1 eq fcntl(fd,$const_F_SETLKW, $fcntldata))
  {
    printf(stderr "error: cannot set read lock on file %s\n",$lockname);
    close(fd);
    $fd_inuse = 0;
    return 0;
  }

  return 1;
}

#
# usage: &set_writelock('pathname');
#   returns 1 if successful, 0 upon failure.
#   modifies the global variable $fd_inuse and the file descriptor fd.
#
sub set_writelock
{
  local($lockname, $last, $fcntldata);

  if ((@_ != 1)||($_[0] eq ''))
  {
    printf(stderr "error: NULL path not allowed\n");
    return 0;
  }
  $lockname = $_[0];

  if ($fd_inuse eq 2)
  {
    printf(stderr "error: nested write locks not allowed!\n");
    return 0;
  }

  $last = chop($lockname);
  if ($last eq '/')
  {
    $lockname = join('','+<',$lockname,$last,$prefs{'lockfile'});
  } else {
    $lockname = join('','+<',$lockname,$last,'/',$prefs{'lockfile'});
  }

  if ($fd_inuse eq 0)
  {
    if (!open(fd, $lockname))
    {
       $fd_inuse = 0;
       printf(stderr "error: cannot open file %s\n",$lockname);
       return 0;
    }
  }

  $fcntldata = $const_WRLCK_STRUCT;

  if (-1 eq fcntl(fd,$const_F_SETLKW, $fcntldata))
  {
    printf(stderr "error: cannot set write lock on file %s\n",$lockname);
    close(fd);
    $fd_inuse = 0;
    return 0;
  }

  $fd_inuse=2;
  return 1;
}

#
# usage: &release_lock;
#   returns 1 if successful, 0 upon failure.
#   modifies the global variable $fd_inuse and the file descriptor fd.
#
sub release_lock
{
  local($lockname, $last, $fcntldata);

  if ($fd_inuse eq 0)
  {
    printf(stderr "error: no lock to release!\n");
    return 0;
  }

  $fcntldata = $const_UNLCK_STRUCT;

  if (-1 eq fcntl(fd,$const_F_SETLKW, $fcntldata))
  {
    printf(stderr "error: cannot release lock.\n");
    close(fd);
    $fd_inuse = 0;
    return 0;
  }

  $fd_inuse = 0;
  return 1;
}

1;