00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 const char *XrdOucSxeqCVSID = "$Id: XrdOucSxeq.cc 34000 2010-06-21 06:49:56Z ganis $";
00014
00015 #include <errno.h>
00016 #include <fcntl.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 #include <strings.h>
00020 #include <unistd.h>
00021 #include <sys/param.h>
00022 #include <sys/stat.h>
00023 #include <sys/types.h>
00024
00025 #include "XrdOuc/XrdOucSxeq.hh"
00026 #include "XrdSys/XrdSysPlatform.hh"
00027
00028
00029
00030
00031
00032 XrdOucSxeq::XrdOucSxeq(int sOpts, const char *path)
00033 {
00034 static const int AMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
00035 lokFN = strdup(path);
00036 lokUL = 0;
00037
00038
00039
00040 if ((lokFD = open(lokFN, O_CREAT|O_RDWR, AMode)) < 0) lokRC = errno;
00041 else {lokRC = 0;
00042 if (sOpts) Serialize(sOpts);
00043 }
00044 }
00045
00046
00047
00048 XrdOucSxeq::XrdOucSxeq(const char *sfx1, const char *sfx2, const char *Dir)
00049 {
00050 static const int AMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
00051 char pbuff[MAXPATHLEN+1], *pP;
00052
00053
00054
00055 strcpy(pbuff, Dir);
00056 pP = pbuff + strlen(Dir);
00057 if (*sfx1 != '/' && *(pP-1) != '/') *pP++ = '/';
00058 strcpy(pP, sfx1);
00059 if (sfx2) strcpy(pP+strlen(sfx1), sfx2);
00060 lokFN = strdup(pbuff);
00061 lokUL = 0;
00062
00063
00064
00065 if ((lokFD = open(lokFN, O_CREAT|O_RDWR, AMode)) < 0) lokRC = errno;
00066 else lokRC = 0;
00067 }
00068
00069
00070
00071
00072
00073 XrdOucSxeq::~XrdOucSxeq()
00074 {
00075
00076
00077
00078 if (lokFD >= 0 && lokUL) unlink(lokFN);
00079
00080
00081
00082 if (lokFD >= 0) close(lokFD);
00083 free(lokFN);
00084 }
00085
00086
00087
00088
00089
00090 int XrdOucSxeq::Release()
00091 {
00092 FLOCK_t lock_args;
00093 int rc;
00094
00095
00096
00097 if (lokFD < 0) return 0;
00098
00099
00100
00101 bzero(&lock_args, sizeof(lock_args));
00102 lock_args.l_type = F_UNLCK;
00103
00104
00105
00106 do {rc = fcntl(lokFD, F_SETLKW, &lock_args);}
00107 while(rc < 0 && errno == EINTR);
00108
00109
00110
00111 if (rc < 0) {lokRC = errno; return 0;}
00112
00113
00114
00115 lokUL = 0;
00116 lokRC = 0;
00117 return 1;
00118 }
00119
00120
00121
00122
00123
00124 int XrdOucSxeq::Serialize(int Opts)
00125 {
00126 FLOCK_t lock_args;
00127 int Act, rc;
00128
00129
00130
00131 if (lokFD < 0) return 0;
00132
00133
00134
00135
00136
00137
00138 bzero(&lock_args, sizeof(lock_args));
00139 lock_args.l_type = (Opts & Share ? F_RDLCK : F_WRLCK);
00140 Act = (Opts & noWait ? F_SETLK : F_SETLKW);
00141
00142
00143
00144 do {rc = fcntl(lokFD, Act, &lock_args);} while(rc < 0 && errno == EINTR);
00145
00146
00147
00148 if (rc < 0) {lokRC = errno; return 0;}
00149
00150
00151
00152 if (Opts & Unlink && !(Opts & Share)) lokUL = 1;
00153 lokRC = 0;
00154 return 1;
00155 }