XrdOucSxeq.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                         X r d O u c S x e q . c c                          */
00004 /*                                                                            */
00005 /* (c) 2009 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010   
00011 //         $Id: XrdOucSxeq.cc 34000 2010-06-21 06:49:56Z ganis $
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 /*                           C o n s t r u c t o r                            */
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 // Open the file, creating it
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 // Construct the lock file name
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 // Open the file, creating it
00064 //
00065    if ((lokFD = open(lokFN, O_CREAT|O_RDWR, AMode)) < 0) lokRC = errno;
00066       else lokRC = 0;
00067 }
00068 
00069 /******************************************************************************/
00070 /*                            D e s t r u c t o r                             */
00071 /******************************************************************************/
00072   
00073 XrdOucSxeq::~XrdOucSxeq()
00074 {
00075 
00076 // Check if we should unlink this file we need to do so while it's locked)
00077 //
00078    if (lokFD >= 0 && lokUL) unlink(lokFN);
00079 
00080 // Close the file and free th file name
00081 //
00082    if (lokFD >= 0) close(lokFD);
00083    free(lokFN);
00084 }
00085 
00086 /******************************************************************************/
00087 /*                               R e l e a s e                                */
00088 /******************************************************************************/
00089   
00090 int XrdOucSxeq::Release()
00091 {
00092    FLOCK_t lock_args;
00093    int rc;
00094 
00095 // If the file is not open, return failure
00096 //
00097    if (lokFD < 0) return 0;
00098 
00099 // Establish locking options
00100 //
00101    bzero(&lock_args, sizeof(lock_args));
00102    lock_args.l_type = F_UNLCK;
00103 
00104 // Now perform the action
00105 //
00106    do {rc = fcntl(lokFD, F_SETLKW, &lock_args);}
00107       while(rc < 0 && errno == EINTR);
00108 
00109 // Determine result
00110 //
00111    if (rc < 0) {lokRC = errno; return 0;}
00112 
00113 // We succeeded, unlink is not possible now
00114 //
00115    lokUL = 0;
00116    lokRC = 0;
00117    return 1;
00118 }
00119   
00120 /******************************************************************************/
00121 /*                             S e r i a l i z e                              */
00122 /******************************************************************************/
00123   
00124 int XrdOucSxeq::Serialize(int Opts)
00125 {
00126    FLOCK_t lock_args;
00127    int Act, rc;
00128 
00129 // If the file is not open, return failure
00130 //
00131    if (lokFD < 0) return 0;
00132 
00133 // Establish lock flags
00134 //
00135 
00136 // Establish locking options
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 // Now perform the action
00143 //
00144    do {rc = fcntl(lokFD, Act, &lock_args);} while(rc < 0 && errno == EINTR);
00145 
00146 // Determine result
00147 //
00148    if (rc < 0) {lokRC = errno; return 0;}
00149 
00150 // We succeeded check if an unlink is possible
00151 //
00152    if (Opts & Unlink && !(Opts & Share)) lokUL = 1;
00153    lokRC = 0;
00154    return 1;
00155 }

Generated on Tue Jul 5 14:46:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1