00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "lockfile.h"
00023
00024 void create_lockfile(void)
00025 {
00026 int lockfile_d;
00027 unsigned int other_pid;
00028 FILE *lockfile_s;
00029 struct stat stats;
00030
00031 if (mkdir(directory, S_IRWXU) == -1)
00032 {
00033 if ((errno != EEXIST) || ((stat(directory, &stats) == 0) && ((stats.st_mode & S_IFMT) != S_IFDIR)))
00034 DIE("Cannot create directory for storing settings. Program will exit.")
00035 chmod(directory, S_IRWXU);
00036 }
00037 if ((lockfile_d = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) == -1)
00038 {
00039 if ((errno==EEXIST) && (stat(lockfile, &stats) == 0) && ((stats.st_mode & S_IFMT) == S_IFREG) && ((lockfile_s = fopen(lockfile, "r")) != NULL) && (fscanf(lockfile_s, "%u", &other_pid) == 1))
00040 {
00041 fprintf(stdout, "Probably another copy of RSS Background Reader runs with PID %u. Program will exit.\nIf you are sure, that RSSBGR is not running, please delete the file %s\n", other_pid, lockfile);
00042 fclose(lockfile_s);
00043 _exit(1);
00044 }
00045 else
00046 DIE("Cannot create lockfile")
00047 }
00048
00049
00050
00051
00052 lockfile_s = fdopen(lockfile_d, "w");
00053 fprintf(lockfile_s, "%u", (int) getpid());
00054 fflush(lockfile_s);
00055 close(lockfile_d);
00056 }
00057
00058
00059
00060 void check_lockfile(void)
00061 {
00062 time_t mtime, ctime;
00063 struct stat stats;
00064
00065 if (stat(lockfile, &stats) == -1) return;
00066 mtime = stats.st_mtime;
00067 ctime = stats.st_ctime;
00068
00069 while(1)
00070 {
00071 sleep(CHECK_LOCKFILE_FREQUENCY);
00072 if ((stat(lockfile, &stats) == -1) || (stats.st_mtime != mtime) || (stats.st_ctime != ctime))
00073 return;
00074 }
00075 }