Release.
[safe/jmp/vzgot] / lib / unilck.c
1 /************************************************/
2 /*                                              */
3 /*      Copyright:                              */
4 /*       Jean-Marc Pigeon <jmp@safe.ca>  2009   */
5 /*                                              */
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                     */
12 /*                                              */
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.                            */
19 /*                                              */
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 /************************************************/
26 /*                                              */
27 /*      UNILCK:                                 */
28 /*      Take Care of all process locking        */
29 /*      function.                               */
30 /*                                              */
31 /************************************************/
32 #include        <string.h>
33 #include        <stdio.h>
34 #include        <syslog.h>
35 #include        <fcntl.h>
36 #include        <unistd.h>
37 #include        "lowtyp.h"
38 #include        "dbgmem.h"
39 #include        "utlprc.h"
40 #include        "subapl.h"
41 #include        "unilck.h"
42
43 /*Application locking directory                 */
44 #define LOCKEXT "-lock" /*lock file extension   */
45
46 /*
47 \f
48 */
49 /************************************************/
50 /*                                              */
51 /*      procedure to set/unset a lock           */
52 /*      return true if successful,              */
53 /*      false otherwise.                        */
54 /*                                              */
55 /************************************************/
56 int lck_locking(const char *ident,int lock,int tentative)
57
58 {
59 int done;
60 char *dlock;
61 char *lockname;
62
63 done=false;
64 dlock=apl_appdir(d_lock); 
65 (void) asprintf(&lockname,"%s%s%s",dlock,ident,LOCKEXT);
66 if (lock==LCK_LOCK) {
67   (void) apl_alert(9,"Request locking <%s>",lockname);
68   while (tentative>0) {
69     int handle;
70
71     tentative--;
72     if ((handle=open(lockname,O_RDWR|O_EXCL|O_CREAT,0640))<0) {
73       FILE *fichier;
74
75       if ((fichier=fopen(lockname,"r"))!=(FILE *)0) {
76         int pid;
77         char strloc[80];
78
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);
86             }
87           else {
88             if (tentative>0)
89               {
90               (void) apl_alert(2,"waiting 1 sec to be unlocked");
91               (void) sleep(1);
92               }
93             }
94           }
95         }
96       continue;
97       }
98     else
99       {
100       char numid[30];
101
102       (void) snprintf(numid,sizeof(numid),"%06d\n",getpid());
103       (void) write(handle,numid,strlen(numid));
104       (void) close(handle);
105       done=true;
106       break;
107       }
108     }
109   if (done==false)
110     (void) apl_alert(0,"Unable to set locking <%s>",lockname);
111   }
112 else
113   {
114   (void) apl_alert(9,"Request unlocking <%s>",lockname);
115   (void) unlink(lockname);
116   done=true;
117   }
118 (void) free(lockname);
119 (void) free(dlock);
120 return done;
121 }