1 /************************************************/
4 /* Jean-Marc Pigeon <jmp@safe.ca> 2009 */
6 /************************************************/
7 /* This program is free software; you can */
8 /* redistribute it and/or modify it under the */
9 /* terms of the GNU General Public License as */
10 /* published by the Free Software Foundation */
11 /* version 2 of the License */
13 /* This program is distributed in the hope that */
14 /* it will be useful, but WITHOUT ANY WARRANTY; */
15 /* without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR */
17 /* PURPOSE. See the GNU General Public License */
18 /* for more details. */
20 /* You should have received a copy of the GNU */
21 /* General Public License along with this */
22 /* program; if not, write to the Free Software */
23 /* Foundation, Inc., 51 Franklin Street, */
24 /* Fifth Floor, Boston, MA 02110-1301, USA. */
25 /************************************************/
28 /* Take Care of all process locking */
31 /************************************************/
43 /*Application locking directory */
44 #define LOCKEXT "-lock" /*lock file extension */
49 /************************************************/
51 /* procedure to set/unset a lock */
52 /* return true if successful, */
53 /* false otherwise. */
55 /************************************************/
56 int lck_locking(const char *ident,int lock,int tentative)
64 dlock=apl_appdir(d_lock);
65 (void) asprintf(&lockname,"%s%s%s",dlock,ident,LOCKEXT);
67 (void) apl_alert(9,"Request locking <%s>",lockname);
72 if ((handle=open(lockname,O_RDWR|O_EXCL|O_CREAT,0640))<0) {
75 if ((fichier=fopen(lockname,"r"))!=(FILE *)0) {
79 (void) fgets(strloc,sizeof(strloc)-1,fichier);
80 (void) fclose(fichier);
81 if (sscanf(strloc,"%d",&pid)==1) {
82 (void) apl_alert(1,"Locking, check %d process active",pid);
83 if (prc_checkprocess(pid)==false) {
84 (void) apl_alert(1,"Locking, remove unactive lock");
85 (void) unlink(lockname);
90 (void) apl_alert(2,"waiting 1 sec to be unlocked");
102 (void) snprintf(numid,sizeof(numid),"%06d\n",getpid());
103 (void) write(handle,numid,strlen(numid));
104 (void) close(handle);
110 (void) apl_alert(0,"Unable to set locking <%s>",lockname);
114 (void) apl_alert(9,"Request unlocking <%s>",lockname);
115 (void) unlink(lockname);
118 (void) free(lockname);