Fixed occasional 'internal error: 31' when running multiple instances of YARA at the
same time
@@ -31,6 +31,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#if !defined(_WIN32) && !defined(__CYGWIN__)
#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
#endif
#if defined(__FreeBSD__)
@@ -103,12 +105,18 @@ int semaphore_init(
// from the name. More info at:
//
// http://stackoverflow.com/questions/1413785/sem-init-on-os-x
- *semaphore = sem_open("/semaphore", O_CREAT, S_IRUSR, value);
+ //
+ // Also create name for semaphore from PID because running multiple instances
+ // of YARA at the same time can cause that sem_open() was called in two processes
+ // simultaneously while neither of them had chance to call sem_unlink() yet.
+ char name[20];
+ snprintf(name, sizeof(name), "/yara.sem.%i", (int)getpid());
+ *semaphore = sem_open(name, O_CREAT, S_IRUSR, value);
if (*semaphore == SEM_FAILED)
return errno;
- if (sem_unlink("/semaphore") != 0)
+ if (sem_unlink(name) != 0)
return errno;
#endif
