From 88fd88373d0524404a3c38703ae2c03dc075063c Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Tue, 16 May 2006 13:53:34 +0000 Subject: Added lock for log file (~/.weechat/weechat.log), only one WeeChat process can use this file (bug #16382) --- ChangeLog | 4 +++- src/common/weechat.c | 20 ++++++++++++++++---- weechat/ChangeLog | 4 +++- weechat/src/common/weechat.c | 20 ++++++++++++++++---- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7b1a0f208..6c229d829 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,9 +1,11 @@ WeeChat - Wee Enhanced Environment for Chat =========================================== -ChangeLog - 2006-05-14 +ChangeLog - 2006-05-16 Version 0.1.9 (under dev!): + * added lock for log file (~/.weechat/weechat.log), only one WeeChat + process can use this file (bug #16382) * fixed crash with malformed UTF-8 strings * fixed crash with ncurses color when too many colors defined in ncurses (bug #16556) diff --git a/src/common/weechat.c b/src/common/weechat.c index 6468bc6dc..8a69facdb 100644 --- a/src/common/weechat.c +++ b/src/common/weechat.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -846,10 +847,18 @@ weechat_init_log () filename = (char *) malloc (filename_length * sizeof (char)); snprintf (filename, filename_length, "%s/" WEECHAT_LOG_NAME, weechat_home); - if ((weechat_log_file = fopen (filename, "wt")) == NULL) + + weechat_log_file = fopen (filename, "wt"); + if (!weechat_log_file + || (flock (fileno (weechat_log_file), LOCK_EX | LOCK_NB) != 0)) + { fprintf (stderr, - _("%s unable to create/append to log file (%s/%s)"), - WEECHAT_WARNING, weechat_home, WEECHAT_LOG_NAME); + _("%s unable to create/append to log file (%s/%s)\n" + "If another WeeChat process is using this file, try to run WeeChat\n" + "with another home using \"--dir\" command line option.\n"), + WEECHAT_ERROR, weechat_home, WEECHAT_LOG_NAME); + exit (1); + } free (filename); } @@ -939,7 +948,10 @@ weechat_shutdown (int return_code, int crash) if (weechat_home) free (weechat_home); if (weechat_log_file) + { + flock (fileno (weechat_log_file), LOCK_UN); fclose (weechat_log_file); + } if (local_charset) free (local_charset); alias_free_all (); @@ -956,7 +968,7 @@ weechat_shutdown (int return_code, int crash) } /* - * weechat_dump writes dump to WeeChat log file + * weechat_dump: write dump to WeeChat log file */ void diff --git a/weechat/ChangeLog b/weechat/ChangeLog index 7b1a0f208..6c229d829 100644 --- a/weechat/ChangeLog +++ b/weechat/ChangeLog @@ -1,9 +1,11 @@ WeeChat - Wee Enhanced Environment for Chat =========================================== -ChangeLog - 2006-05-14 +ChangeLog - 2006-05-16 Version 0.1.9 (under dev!): + * added lock for log file (~/.weechat/weechat.log), only one WeeChat + process can use this file (bug #16382) * fixed crash with malformed UTF-8 strings * fixed crash with ncurses color when too many colors defined in ncurses (bug #16556) diff --git a/weechat/src/common/weechat.c b/weechat/src/common/weechat.c index 6468bc6dc..8a69facdb 100644 --- a/weechat/src/common/weechat.c +++ b/weechat/src/common/weechat.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -846,10 +847,18 @@ weechat_init_log () filename = (char *) malloc (filename_length * sizeof (char)); snprintf (filename, filename_length, "%s/" WEECHAT_LOG_NAME, weechat_home); - if ((weechat_log_file = fopen (filename, "wt")) == NULL) + + weechat_log_file = fopen (filename, "wt"); + if (!weechat_log_file + || (flock (fileno (weechat_log_file), LOCK_EX | LOCK_NB) != 0)) + { fprintf (stderr, - _("%s unable to create/append to log file (%s/%s)"), - WEECHAT_WARNING, weechat_home, WEECHAT_LOG_NAME); + _("%s unable to create/append to log file (%s/%s)\n" + "If another WeeChat process is using this file, try to run WeeChat\n" + "with another home using \"--dir\" command line option.\n"), + WEECHAT_ERROR, weechat_home, WEECHAT_LOG_NAME); + exit (1); + } free (filename); } @@ -939,7 +948,10 @@ weechat_shutdown (int return_code, int crash) if (weechat_home) free (weechat_home); if (weechat_log_file) + { + flock (fileno (weechat_log_file), LOCK_UN); fclose (weechat_log_file); + } if (local_charset) free (local_charset); alias_free_all (); @@ -956,7 +968,7 @@ weechat_shutdown (int return_code, int crash) } /* - * weechat_dump writes dump to WeeChat log file + * weechat_dump: write dump to WeeChat log file */ void -- cgit v1.2.3