XRootD
Loading...
Searching...
No Matches
XrdOucSxeq.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d O u c S x e q . c c */
4/* */
5/* (c) 2009 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* All Rights Reserved */
7/* Produced by Andrew Hanushevsky for Stanford University under contract */
8/* DE-AC02-76-SFO0515 with the Department of Energy */
9/* */
10/* This file is part of the XRootD software suite. */
11/* */
12/* XRootD is free software: you can redistribute it and/or modify it under */
13/* the terms of the GNU Lesser General Public License as published by the */
14/* Free Software Foundation, either version 3 of the License, or (at your */
15/* option) any later version. */
16/* */
17/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20/* License for more details. */
21/* */
22/* You should have received a copy of the GNU Lesser General Public License */
23/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25/* */
26/* The copyright holder's institutional names and contributor's names may not */
27/* be used to endorse or promote products derived from this software without */
28/* specific prior written permission of the institution or contributor. */
29/******************************************************************************/
30
31#include <cerrno>
32#include <fcntl.h>
33#include <cstdlib>
34#include <cstring>
35#include <strings.h>
36#include <unistd.h>
37#include <sys/param.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40
41#include "XrdOuc/XrdOucSxeq.hh"
43
44/******************************************************************************/
45/* C o n s t r u c t o r */
46/******************************************************************************/
47
48XrdOucSxeq::XrdOucSxeq(int sOpts, const char *path)
49{
50 static const int AMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
51 lokFN = strdup(path);
52 lokUL = 0;
53
54// Open the file, creating it
55//
56 if ((lokFD = open(lokFN, O_CREAT|O_RDWR, AMode)) < 0) lokRC = errno;
57 else {lokRC = 0;
58 if (sOpts) Serialize(sOpts);
59 }
60}
61
62/******************************************************************************/
63
64XrdOucSxeq::XrdOucSxeq(const char *sfx1, const char *sfx2, const char *Dir)
65{
66 static const int AMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
67 char pbuff[MAXPATHLEN+1], *pP;
68
69// Construct the lock file name
70//
71 strcpy(pbuff, Dir);
72 pP = pbuff + strlen(Dir);
73 if (*sfx1 != '/' && *(pP-1) != '/') *pP++ = '/';
74 strcpy(pP, sfx1);
75 if (sfx2) strcpy(pP+strlen(sfx1), sfx2);
76 lokFN = strdup(pbuff);
77 lokUL = 0;
78
79// Open the file, creating it
80//
81 if ((lokFD = open(lokFN, O_CREAT|O_RDWR, AMode)) < 0) lokRC = errno;
82 else lokRC = 0;
83}
84
85/******************************************************************************/
86/* D e s t r u c t o r */
87/******************************************************************************/
88
90{
91
92// Check if we should unlink this file we need to do so while it's locked)
93//
94 if (lokFD >= 0 && lokUL) unlink(lokFN);
95
96// Close the file and free th file name
97//
98 if (lokFD >= 0) close(lokFD);
99 free(lokFN);
100}
101
102/******************************************************************************/
103/* R e l e a s e */
104/******************************************************************************/
105
107{
108 FLOCK_t lock_args;
109 int rc;
110
111// If the file is not open, return failure
112//
113 if (lokFD < 0) return 0;
114
115// Establish locking options
116//
117 bzero(&lock_args, sizeof(lock_args));
118 lock_args.l_type = F_UNLCK;
119
120// Now perform the action
121//
122 do {rc = fcntl(lokFD, F_SETLKW, &lock_args);}
123 while(rc < 0 && errno == EINTR);
124
125// Determine result
126//
127 if (rc < 0) {lokRC = errno; return 0;}
128
129// We succeeded, unlink is not possible now
130//
131 lokUL = 0;
132 lokRC = 0;
133 return 1;
134}
135/******************************************************************************/
136
138{
139 FLOCK_t lock_args;
140 int rc;
141
142// If the file is not open, return failure
143//
144 if (fileD < 0) return EBADF;
145
146// Establish locking options
147//
148 bzero(&lock_args, sizeof(lock_args));
149 lock_args.l_type = F_UNLCK;
150
151// Now perform the action
152//
153 do {rc = fcntl(fileD, F_SETLKW, &lock_args);}
154 while(rc < 0 && errno == EINTR);
155
156// Return result
157//
158 return (rc ? errno : 0);
159}
160
161/******************************************************************************/
162/* S e r i a l i z e */
163/******************************************************************************/
164
166{
167 FLOCK_t lock_args;
168 int Act, rc;
169
170// If the file is not open, return failure
171//
172 if (lokFD < 0) return 0;
173
174// Establish lock flags
175//
176
177// Establish locking options
178//
179 bzero(&lock_args, sizeof(lock_args));
180 lock_args.l_type = (Opts & Share ? F_RDLCK : F_WRLCK);
181 Act = (Opts & noWait ? F_SETLK : F_SETLKW);
182
183// Now perform the action
184//
185 do {rc = fcntl(lokFD, Act, &lock_args);} while(rc < 0 && errno == EINTR);
186
187// Determine result
188//
189 if (rc < 0) {lokRC = errno; return 0;}
190
191// We succeeded check if an unlink is possible
192//
193 if (Opts & Unlink && !(Opts & Share)) lokUL = 1;
194 lokRC = 0;
195 return 1;
196}
197
198/******************************************************************************/
199
200int XrdOucSxeq::Serialize(int fileD, int opts)
201{
202 FLOCK_t lock_args;
203
204// Make sure we have a lock outstanding
205//
206 if (fileD < 0) return EBADF;
207
208// Establish locking options
209//
210 bzero(&lock_args, sizeof(lock_args));
211 if (opts & Share) lock_args.l_type = F_RDLCK;
212 else lock_args.l_type = F_WRLCK;
213
214// Perform action.
215//
216 if (fcntl(fileD, (opts & noWait ? F_SETLK : F_SETLKW), &lock_args))
217 return errno;
218 return 0;
219}
int fcntl(int fd, int cmd,...)
#define close(a)
Definition XrdPosix.hh:43
#define open
Definition XrdPosix.hh:71
#define unlink(a)
Definition XrdPosix.hh:108
XrdOucString Dir
struct myOpts opts
#define FLOCK_t
int Release()
static const int Unlink
Definition XrdOucSxeq.hh:39
static const int Share
Definition XrdOucSxeq.hh:38
int Serialize(int Opts=0)
XrdOucSxeq(int sOpts, const char *path)
Definition XrdOucSxeq.cc:48
static const int noWait
Definition XrdOucSxeq.hh:37