/************************************************/ /* */ /* Copyright: */ /* Jean-Marc Pigeon 2009 */ /* */ /************************************************/ /* This program is free software; you can */ /* redistribute it and/or modify it under the */ /* terms of the GNU General Public License as */ /* published by the Free Software Foundation */ /* version 2 of the License */ /* */ /* This program is distributed in the hope that */ /* it will be useful, but WITHOUT ANY WARRANTY; */ /* without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR */ /* PURPOSE. See the GNU General Public License */ /* for more details. */ /* */ /* You should have received a copy of the GNU */ /* General Public License along with this */ /* program; if not, write to the Free Software */ /* Foundation, Inc., 51 Franklin Street, */ /* Fifth Floor, Boston, MA 02110-1301, USA. */ /************************************************/ /* */ /* UNILCK: */ /* Take Care of all process locking */ /* function. */ /* */ /************************************************/ #include #include #include #include #include #include "lowtyp.h" #include "dbgmem.h" #include "utlprc.h" #include "subapl.h" #include "unilck.h" /*Application locking directory */ #define LOCKEXT "-lock" /*lock file extension */ /* */ /************************************************/ /* */ /* procedure to set/unset a lock */ /* return true if successful, */ /* false otherwise. */ /* */ /************************************************/ int lck_locking(const char *ident,int lock,int tentative) { int done; char *dlock; char *lockname; done=false; dlock=apl_appdir(d_lock); (void) asprintf(&lockname,"%s%s%s",dlock,ident,LOCKEXT); if (lock==LCK_LOCK) { (void) apl_alert(9,"Request locking <%s>",lockname); while (tentative>0) { int handle; tentative--; if ((handle=open(lockname,O_RDWR|O_EXCL|O_CREAT,0640))<0) { FILE *fichier; if ((fichier=fopen(lockname,"r"))!=(FILE *)0) { int pid; char strloc[80]; (void) fgets(strloc,sizeof(strloc)-1,fichier); (void) fclose(fichier); if (sscanf(strloc,"%d",&pid)==1) { (void) apl_alert(1,"Locking, check %d process active",pid); if (prc_checkprocess(pid)==false) { (void) apl_alert(1,"Locking, remove unactive lock"); (void) unlink(lockname); } else { if (tentative>0) { (void) apl_alert(2,"waiting 1 sec to be unlocked"); (void) sleep(1); } } } } continue; } else { char numid[30]; (void) snprintf(numid,sizeof(numid),"%06d\n",getpid()); (void) write(handle,numid,strlen(numid)); (void) close(handle); done=true; break; } } if (done==false) (void) apl_alert(0,"Unable to set locking <%s>",lockname); } else { (void) apl_alert(9,"Request unlocking <%s>",lockname); (void) unlink(lockname); done=true; } (void) free(lockname); (void) free(dlock); return done; }