#include <sys/stat.h>
#include <sys/types.h>
#include <search.h>
+#include <fcntl.h>
#ifdef _POSIX
#include <sys/time.h>
#include <tchar.h>
#include <psapi.h>
#include <io.h>
-#include <fcntl.h>
typedef UINT8 uint8_t;
typedef UINT16 uint16_t;
char *logfile_generate_subid ();
void logfile_append (const char *fmt, ...);
+#if F_SETLKW
+void lock_file (FILE *fp);
+void unlock_file (FILE *fp);
+#else
+#define lock_file(dummy) {}
+#define unlock_file(dummy) {}
+#endif
+
#ifdef _WIN
void fsync (int fd);
#endif
if (pot_fp)
{
+ lock_file (pot_fp);
+
fprintf (pot_fp, "%s:", out_buf);
format_plain (pot_fp, plain_ptr, plain_len, 1);
fputc ('\n', pot_fp);
fflush (pot_fp);
+
+ unlock_file (pot_fp);
}
// outfile
out_fp = stdout;
}
+ lock_file (out_fp);
}
else
{
if ((fb_fp = fopen (loopback_file, "ab")) != NULL)
{
+ lock_file (fb_fp);
+
format_plain (fb_fp, plain_ptr, plain_len, 1);
fputc ('\n', fb_fp);
if (dictstat_fp)
{
+ lock_file (dictstat_fp);
+
fwrite (dictstat_base, sizeof (dictstat_t), dictstat_nmemb, dictstat_fp);
fclose (dictstat_fp);
* system
*/
+#if F_SETLKW
+void lock_file (FILE *fp)
+{
+ struct flock lock = { 0 };
+
+ lock.l_type = F_WRLCK;
+ while (fcntl(fileno(fp), F_SETLKW, &lock))
+ {
+ if (errno != EINTR)
+ {
+ log_error ("ERROR: failed acquiring write lock: %s", strerror (errno));
+
+ exit (-1);
+ }
+ }
+}
+
+void unlock_file (FILE *fp)
+{
+ struct flock lock = { 0 };
+
+ lock.l_type = F_UNLCK;
+ fcntl(fileno(fp), F_SETLK, &lock);
+}
+#endif /* F_SETLKW */
+
#ifdef _WIN
void fsync (int fd)
{
if (debug_file != NULL)
{
debug_fp = fopen (debug_file, "ab");
+
+ lock_file (debug_fp);
}
else
{
{
FILE *fp = fopen (dst, "wb");
+ lock_file (fp);
fwrite (binary, sizeof (u8), binary_size, fp);
fflush (fp);