1 /* lockfile handling
2 
3    Written by Matthias Hensler
4    Copyright WSPse 1999+2000
5    eMail: wsp@gmx.de
6 
7 Created: 1999/09/27
8 Updated: 2000/06/24
9 */
10 
11 /* Copying:
12    This program is free software; you can redistribute it and/or modify it under
13    the terms of the GNU Gerneral Public License as published by the Free Soft-
14    ware Foundation; either version 2 of License, or (at your option) any later
15    version.
16 
17    This program is distributed in the hope that it will be useful, but WITHOUT
18    ANY WARRANTY; without even the implied warranty of MERCHANTABILTY or
19    FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20    more details.
21 
22    You should have received a copy of the GNU General Public License along with
23    this program; if not, write to the Free Software Foundation, Inc., 675 Mass
24    Ave, Cambridge, MA 02139, USA.
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <stdio.h>
32 #include "mp3creat.h"
33 
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37 #ifdef HAVE_SYS_TYPES_H
38 #include <sys/types.h>
39 #endif
40 #ifdef HAVE_SIGNAL_H
41 #include <signal.h>
42 #endif
43 
44 extern void popup_error_win(char *tx);
45 extern void wuuush(int);
46 
lock_add_extension(char * lockfile)47 char *lock_add_extension(char *lockfile)
48 {
49   char *new;
50 
51   if(! lockfile) return NULL;
52 
53   new = (char *) malloc(sizeof(char) * strlen(lockfile) + 6);
54   if(! new) {
55     wuuush(1);
56   }
57 
58   sprintf(new, "%s.lock", lockfile);
59 
60   return new;
61 }
62 
63 /* return-codes:
64    0 - lockfile created
65    1 - error determining if process is running
66    2 - error writing lockfile
67    3 - locked process still running
68  */
lock_lock_file(char * lockfile,BOOL extension)69 int lock_lock_file(char *lockfile, BOOL extension)
70 {
71   FILE *lckdes;
72   pid_t lckpid;
73   char *file;
74 
75   if(! lockfile) return 1;
76 
77   if(extension) {
78     file = lock_add_extension(lockfile);
79   } else {
80     file = lockfile;
81   }
82 
83   lckdes = fopen(file, "r");
84   if(lckdes) {     /* lockfile exists */
85     if(fscanf(lckdes, "%d", &lckpid) == 1) {
86       if(kill(lckpid, 0) == -1 && errno == ESRCH) {        /* process is not longer existing */
87 	/* remove stale lockfile */
88 	fclose(lckdes);
89 	unlink(file);
90       } else {          /* process still running */
91 	fclose(lckdes);
92 	if(extension) {
93 	  free(file);
94 	  file = NULL;
95 	}
96 	return 3;
97       }
98     } else {
99       popup_error_win(_("error while reading pid from existing lockfile"));
100       fclose(lckdes);
101       return 1;
102     }
103   } else if(errno != ENOENT) {
104     popup_error_win(_("lockfile exists and is not readable"));
105     return 1;
106   }
107 
108   lckdes = fopen(file, "w");
109   if(! lckdes) {
110     if(extension) {
111       free(file);
112       file = NULL;
113     }
114     return 2;
115   }
116 
117   fprintf(lckdes, "%d", getpid());
118   fclose(lckdes);
119   return 0;
120 }
121 
lock_unlock_file(char * lockfile,BOOL extension)122 int lock_unlock_file(char *lockfile, BOOL extension)
123 {
124   char *file;
125 
126   if(! lockfile) return 0;
127 
128   if(extension) {
129     file = lock_add_extension(lockfile);
130   } else {
131     file = lockfile;
132   }
133 
134   if(unlink(file)) {
135     if(extension) {
136       free(file);
137       file = NULL;
138     }
139     return 1;
140   }
141 
142   if(extension) {
143     free(file);
144     file = NULL;
145   }
146   return 0;
147 }
148 
149