Browse code

make cli_lockdb() errors non critical (bb#232)

git-svn: trunk@2630

Tomasz Kojm authored on 2007/01/16 05:01:35
Showing 3 changed files
... ...
@@ -1,3 +1,9 @@
1
+Mon Jan 15 20:58:54 CET 2007 (tk)
2
+---------------------------------
3
+  * libclamav: make cli_lockdb() errors non critical (bb#232)
4
+	       To take full advantage of locking clamscan/clamd must have
5
+	       write access to the database directory.
6
+
1 7
 Mon Jan 15 20:37:16 CET 2007 (tk)
2 8
 ---------------------------------
3 9
   * libclamav/lockdb.c: fix bounds errors (bb#237), patch from Edwin
... ...
@@ -44,6 +44,7 @@
44 44
 #include <unistd.h>
45 45
 #include <fcntl.h>
46 46
 #include <sys/stat.h>
47
+#include <errno.h>
47 48
 #else
48 49
 #include <windows.h>
49 50
 #endif
... ...
@@ -170,7 +171,7 @@ static int cli_lockdb(const char *dbdirpath, int wait, int writelock)
170 170
 	if(!lock) {
171 171
 	    cli_errmsg("cli_lockdb(): Can't allocate lock structure to lock Database Directory: %s\n", dbdirpath);
172 172
 	    pthread_mutex_unlock(&lock_mutex);
173
-	    return CL_ELOCKDB;
173
+	    return CL_EMEM;
174 174
 	}
175 175
 	lock->lock_link = dblocks;
176 176
 	strcpy(lock->lock_file, lock_file);
... ...
@@ -179,7 +180,7 @@ static int cli_lockdb(const char *dbdirpath, int wait, int writelock)
179 179
 	dblocks = lock;
180 180
     }
181 181
     if(lock->lock_type != -1) {
182
-	cli_errmsg("Database Directory: %s already %s locked\n", dbdirpath, (lock->lock_type? "write" : "read"));
182
+	cli_dbgmsg("Database Directory: %s already %s locked\n", dbdirpath, (lock->lock_type? "write" : "read"));
183 183
 	pthread_mutex_unlock(&lock_mutex);
184 184
 	return CL_ELOCKDB;
185 185
     }
... ...
@@ -189,10 +190,10 @@ static int cli_lockdb(const char *dbdirpath, int wait, int writelock)
189 189
 	if(-1 == (lock->lock_fd = open(lock->lock_file, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IROTH))) {
190 190
 	    if((writelock) ||
191 191
 	       (-1 == (lock->lock_fd = open(lock->lock_file, O_RDWR, 0)))) {
192
-		cli_errmsg("Can't %s Lock file for Database Directory: %s\n", (writelock ? "create" : "open"), dbdirpath);
192
+		cli_dbgmsg("Can't %s Lock file for Database Directory: %s\n", (writelock ? "create" : "open"), dbdirpath);
193 193
 		umask(old_mask);
194 194
 		pthread_mutex_unlock(&lock_mutex);
195
-		return CL_ELOCKDB;
195
+		return CL_EIO; /* or CL_EACCESS */
196 196
 	    }
197 197
 	}
198 198
 	umask(old_mask);
... ...
@@ -208,9 +209,9 @@ static int cli_lockdb(const char *dbdirpath, int wait, int writelock)
208 208
 	if(!(lock->lock_fd = CreateMutexA(&saAttr, TRUE, lock->lock_file))) {
209 209
 	    if((GetLastError() != ERROR_ACCESS_DENIED) || 
210 210
 	       (!(lock->lock_fd = OpenMutexA(MUTEX_MODIFY_STATE, FALSE, lock->lock_file)))) {
211
-		cli_errmsg("Can't Create Mutex Lock for Database Directory: %s\n", dbdirpath);
211
+		cli_dbgmsg("Can't Create Mutex Lock for Database Directory: %s\n", dbdirpath);
212 212
 		pthread_mutex_unlock(&lock_mutex);
213
-		return CL_ELOCKDB;
213
+		return CL_EIO;
214 214
 	    }
215 215
 	    LastError = ERROR_ALREADY_EXISTS;
216 216
 	}
... ...
@@ -226,8 +227,11 @@ static int cli_lockdb(const char *dbdirpath, int wait, int writelock)
226 226
     fl.l_type = (writelock ? F_WRLCK : F_RDLCK);
227 227
     if(fcntl(lock->lock_fd, ((wait) ? F_SETLKW : F_SETLK), &fl) == -1) {
228 228
 #ifndef C_WINDOWS
229
-	close(lock->lock_fd);
230
-	unlink(lock->lock_file);
229
+	if(errno != EACCES && errno != EAGAIN) {
230
+	    close(lock->lock_fd);
231
+	    unlink(lock->lock_file);
232
+	    return CL_EIO;
233
+	}
231 234
 #endif
232 235
 	return CL_ELOCKDB;
233 236
     }
... ...
@@ -1278,11 +1278,11 @@ static int cli_loaddbdir_l(const char *dirname, struct cl_engine **engine, unsig
1278 1278
 
1279 1279
 static int cli_loaddbdir(const char *dirname, struct cl_engine **engine, unsigned int *signo, unsigned int options)
1280 1280
 {
1281
-	int ret, try = 0;
1281
+	int ret, try = 0, lock;
1282 1282
 
1283 1283
 
1284 1284
     cli_dbgmsg("cli_loaddbdir: Acquiring dbdir lock\n");
1285
-    while(cli_readlockdb(dirname, 0) == CL_ELOCKDB) {
1285
+    while((lock = cli_readlockdb(dirname, 0)) == CL_ELOCKDB) {
1286 1286
 	sleep(5);
1287 1287
 	if(try++ > 24) {
1288 1288
 	    cli_errmsg("cl_load(): Unable to lock database directory: %s\n", dirname);
... ...
@@ -1291,7 +1291,9 @@ static int cli_loaddbdir(const char *dirname, struct cl_engine **engine, unsigne
1291 1291
     }
1292 1292
 
1293 1293
     ret = cli_loaddbdir_l(dirname, engine, signo, options);
1294
-    cli_unlockdb(dirname);
1294
+    if(lock == CL_SUCCESS)
1295
+	cli_unlockdb(dirname);
1296
+
1295 1297
     return ret;
1296 1298
 }
1297 1299